Skip to content

Commit

Permalink
Changes post #65: make AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERA…
Browse files Browse the repository at this point in the history
…L_ENTITIES be the SAX feature key too.
  • Loading branch information
cowtowncoder committed Apr 26, 2021
1 parent 5deb3f4 commit 51d8bfa
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 30 deletions.
26 changes: 12 additions & 14 deletions src/main/java/com/fasterxml/aalto/sax/SAXParserFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ public SAXParserFactoryImpl()
mStaxFactory = new InputFactoryImpl();
}

// As per [Issue#4], let's re-define this method
/**
* @since 0.9.8
*/
public static SAXParserFactory newInstance() {
return new SAXParserFactoryImpl();
}
Expand All @@ -69,12 +65,12 @@ public boolean getFeature(String name)
switch (stdFeat) {
case IS_STANDALONE: // read-only, but only during parsing
return true;
case EXTERNAL_GENERAL_ENTITIES:
return Boolean.FALSE.equals(mStaxFactory.getProperty(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES));
default:
}
} else {
// any non-standard one we may support?
if (name.equals(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES)) {
return Boolean.TRUE.equals(mStaxFactory.getProperty(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES));
}
}

// nope, not recognized:
Expand All @@ -98,8 +94,7 @@ public void setFeature(String name, boolean enabled)

switch (stdFeat) {
case EXTERNAL_GENERAL_ENTITIES:
mStaxFactory.setProperty(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES, !enabled);
ok = true;
ok = !enabled;
break;
case EXTERNAL_PARAMETER_ENTITIES:
ok = !enabled;
Expand All @@ -120,9 +115,8 @@ public void setFeature(String name, boolean enabled)
ok = true;
break;
case STRING_INTERNING:
/* Can not disable; however, doesn't harm if they try to
* do it, so let's not care
*/
// Can not disable; however, doesn't harm if they try to
// do it, so let's not care
ok = true;
break;
case UNICODE_NORMALIZATION_CHECKING:
Expand Down Expand Up @@ -154,10 +148,14 @@ public void setFeature(String name, boolean enabled)
throw new SAXNotSupportedException("Setting std feature "+stdFeat+" to "+enabled+" not supported");
}
return;
} else {
// [aalto-xml#65]: allow retaining GEs in attribute values
if (name.equals(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES)) {
mStaxFactory.setProperty(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES, enabled);
return;
}
}

// any non-standard one we may support?

// nope, not recognized:
SAXUtil.reportUnknownFeature(name);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/fasterxml/aalto/sax/SAXUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public static SAXProperty findStdProperty(String featURI)
public static Boolean getFixedStdFeatureValue(SAXFeature stdFeat)
{
switch (stdFeat) {
case EXTERNAL_GENERAL_ENTITIES: // not yet implemented
return Boolean.FALSE;
case EXTERNAL_PARAMETER_ENTITIES: // not yet implemented
return Boolean.FALSE;
case IS_STANDALONE: // read-only, but only during parsing
Expand Down
20 changes: 13 additions & 7 deletions src/test/java/com/fasterxml/aalto/sax/TestEntityResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import java.util.concurrent.CountDownLatch;

import javax.xml.parsers.SAXParser;
import javax.xml.stream.XMLInputFactory;

import com.fasterxml.aalto.stax.InputFactoryImpl;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

import com.fasterxml.aalto.AaltoInputProperties;

/**
* Simple unit tests to verify that most fundamental parsing functionality
* works via Woodstox SAX implementation.
Expand All @@ -30,9 +30,8 @@ public void testWithDummyExtSubset()
SAXParser sp = spf.newSAXParser();
DefaultHandler h = new DefaultHandler();

/* First: let's verify that we get an exception for
* unresolved reference...
*/
// First: let's verify that we get an exception for
// unresolved reference...
try {
sp.parse(new InputSource(new StringReader(XML)), h);
} catch (SAXException e) {
Expand All @@ -50,6 +49,7 @@ public void testWithDummyExtSubset()
}
}

// [aalto-xml#65]: allow retaining GEs in attribute values
public void testRetainAttributeEntityReference()
throws Exception
{
Expand All @@ -58,6 +58,9 @@ public void testRetainAttributeEntityReference()
+"<root b=\"&replace-me;\" />";

SAXParserFactoryImpl spf = new SAXParserFactoryImpl();
// should be disabled by default:
assertEquals(false,
spf.getFeature(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES));
SAXParser sp = spf.newSAXParser();
DefaultHandler h = new DefaultHandler();

Expand All @@ -67,9 +70,12 @@ public void testRetainAttributeEntityReference()
} catch (SAXException e) {
verifyException(e, "General entity reference (&replace-me;) encountered in entity expanding mode: operation not (yet) implemented\n at [row,col {unknown-source}]: [2,22]");
}

SAXParserFactoryImpl spfKeepEntityReferences = new SAXParserFactoryImpl();
spfKeepEntityReferences.setFeature("http://xml.org/sax/features/external-general-entities", false);
spfKeepEntityReferences.setFeature(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES, true);
assertEquals(true,
spfKeepEntityReferences.getFeature(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES));

SAXParser spKeepEntityReferences = spfKeepEntityReferences.newSAXParser();

final CountDownLatch countDownLatch = new CountDownLatch(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.fasterxml.aalto.sax;

import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import com.fasterxml.aalto.AaltoInputProperties;

public class TestSAXParserFactoryImpl extends base.BaseTestCase {

public void testSetGetFeatureExternalGeneralEntities() throws SAXNotRecognizedException, SAXNotSupportedException {
public class TestSAXParserFactoryImpl extends base.BaseTestCase
{
// [aalto-xml#65]
public void testSetGetFeatureExternalGeneralEntities() throws Exception
{
SAXParserFactoryImpl saxParserFactory = new SAXParserFactoryImpl();
saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
assertFalse(saxParserFactory.getFeature("http://xml.org/sax/features/external-general-entities"));
assertFalse(saxParserFactory.getFeature(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES));

saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", true);
assertTrue(saxParserFactory.getFeature("http://xml.org/sax/features/external-general-entities"));
saxParserFactory.setFeature(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES, true);
assertTrue(saxParserFactory.getFeature(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES));
}

}

0 comments on commit 51d8bfa

Please sign in to comment.