diff --git a/src/main/java/com/fasterxml/aalto/sax/SAXParserFactoryImpl.java b/src/main/java/com/fasterxml/aalto/sax/SAXParserFactoryImpl.java index 2494b1f..6a55c3d 100644 --- a/src/main/java/com/fasterxml/aalto/sax/SAXParserFactoryImpl.java +++ b/src/main/java/com/fasterxml/aalto/sax/SAXParserFactoryImpl.java @@ -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(); } @@ -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: @@ -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; @@ -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: @@ -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); } diff --git a/src/main/java/com/fasterxml/aalto/sax/SAXUtil.java b/src/main/java/com/fasterxml/aalto/sax/SAXUtil.java index be9db8f..57a77aa 100644 --- a/src/main/java/com/fasterxml/aalto/sax/SAXUtil.java +++ b/src/main/java/com/fasterxml/aalto/sax/SAXUtil.java @@ -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 diff --git a/src/test/java/com/fasterxml/aalto/sax/TestEntityResolver.java b/src/test/java/com/fasterxml/aalto/sax/TestEntityResolver.java index 7666a20..a027420 100644 --- a/src/test/java/com/fasterxml/aalto/sax/TestEntityResolver.java +++ b/src/test/java/com/fasterxml/aalto/sax/TestEntityResolver.java @@ -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. @@ -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) { @@ -50,6 +49,7 @@ public void testWithDummyExtSubset() } } + // [aalto-xml#65]: allow retaining GEs in attribute values public void testRetainAttributeEntityReference() throws Exception { @@ -58,6 +58,9 @@ public void testRetainAttributeEntityReference() +""; 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(); @@ -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); diff --git a/src/test/java/com/fasterxml/aalto/sax/TestSAXParserFactoryImpl.java b/src/test/java/com/fasterxml/aalto/sax/TestSAXParserFactoryImpl.java index 2c3c438..75b03c8 100644 --- a/src/test/java/com/fasterxml/aalto/sax/TestSAXParserFactoryImpl.java +++ b/src/test/java/com/fasterxml/aalto/sax/TestSAXParserFactoryImpl.java @@ -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)); } - + }