From 8658e9155e38afadd6d7c1314bbf93633566cce0 Mon Sep 17 00:00:00 2001 From: claudemamo <823038+claudemamo@users.noreply.github.com> Date: Thu, 18 Mar 2021 09:17:29 +0100 Subject: [PATCH] refactor: map SAX external-general-entities feature to new custom parameter meant for retaining entities instead of misusing the StAX XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES feature Refs: FasterXML/aalto-xml#65 --- .../java/com/fasterxml/aalto/impl/CommonConfig.java | 2 +- src/main/java/com/fasterxml/aalto/in/ReaderConfig.java | 10 +++++++++- .../java/com/fasterxml/aalto/in/ReaderScanner.java | 2 +- .../com/fasterxml/aalto/sax/SAXParserFactoryImpl.java | 9 +++------ .../com/fasterxml/aalto/sax/TestEntityResolver.java | 8 +++----- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/fasterxml/aalto/impl/CommonConfig.java b/src/main/java/com/fasterxml/aalto/impl/CommonConfig.java index 1f679fd..37a24a4 100644 --- a/src/main/java/com/fasterxml/aalto/impl/CommonConfig.java +++ b/src/main/java/com/fasterxml/aalto/impl/CommonConfig.java @@ -7,7 +7,7 @@ /** * Base class for reader and writer-side configuration/context objects */ -public abstract class CommonConfig +public abstract class CommonConfig implements XMLStreamProperties { /* /********************************************************************** diff --git a/src/main/java/com/fasterxml/aalto/in/ReaderConfig.java b/src/main/java/com/fasterxml/aalto/in/ReaderConfig.java index eddbc7f..e59b635 100644 --- a/src/main/java/com/fasterxml/aalto/in/ReaderConfig.java +++ b/src/main/java/com/fasterxml/aalto/in/ReaderConfig.java @@ -17,6 +17,8 @@ public final class ReaderConfig extends CommonConfig { + public final static String EXPAND_GENERAL_ENTITIES = "org.codehaus.stax2.expandGeneralEntities"; + public final static int DEFAULT_SMALL_BUFFER_LEN = 60; public final static int DEFAULT_CHAR_BUFFER_LEN = 4000; @@ -41,6 +43,7 @@ public final class ReaderConfig final static int F_AUTO_CLOSE_INPUT = 0x2000; // Custom flags: + final static int F_EXPAND_GENERAL_ENTITIES = 0x3000; /** * These are the default settigs for XMLInputFactory. @@ -56,6 +59,7 @@ public final class ReaderConfig // and will report CDATA as such (and not as CHARACTERS) | F_REPORT_CDATA | F_PRESERVE_LOCATION + | F_EXPAND_GENERAL_ENTITIES ; private final static HashMap sProperties; @@ -98,7 +102,8 @@ public final class ReaderConfig // !!! Not really implemented, but let's recognize it sProperties.put(XMLInputFactory2.P_DTD_OVERRIDE, null); - // Custom ones? + // Custom ones + sProperties.put(EXPAND_GENERAL_ENTITIES, Integer.valueOf(F_EXPAND_GENERAL_ENTITIES)); } /** @@ -412,6 +417,9 @@ public boolean willParseLazily() { public boolean hasInternNsURIsBeenEnabled() { return hasExplicitFlag(F_INTERN_NS_URIS); } + // // // Custom properties + + public boolean willExpandGeneralEntities() { return hasFlag(F_EXPAND_GENERAL_ENTITIES); } /* /********************************************************************** diff --git a/src/main/java/com/fasterxml/aalto/in/ReaderScanner.java b/src/main/java/com/fasterxml/aalto/in/ReaderScanner.java index b2e596e..bccf9ea 100644 --- a/src/main/java/com/fasterxml/aalto/in/ReaderScanner.java +++ b/src/main/java/com/fasterxml/aalto/in/ReaderScanner.java @@ -896,7 +896,7 @@ private final int collectValue(int attrPtr, char quoteChar, PName attrName) throwUnexpectedChar(c, "'<' not allowed in attribute value"); case XmlCharTypes.CT_AMP: { - if (_config.willExpandEntities()) { + if (_config.willExpandGeneralEntities()) { int d = handleEntityInText(false); if (d == 0) { // unexpanded general entity... not good reportUnexpandedEntityInAttr(attrName, false); diff --git a/src/main/java/com/fasterxml/aalto/sax/SAXParserFactoryImpl.java b/src/main/java/com/fasterxml/aalto/sax/SAXParserFactoryImpl.java index 5fc3380..71c769b 100644 --- a/src/main/java/com/fasterxml/aalto/sax/SAXParserFactoryImpl.java +++ b/src/main/java/com/fasterxml/aalto/sax/SAXParserFactoryImpl.java @@ -18,6 +18,7 @@ import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; +import com.fasterxml.aalto.in.ReaderConfig; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; @@ -36,11 +37,6 @@ public class SAXParserFactoryImpl extends SAXParserFactory { final InputFactoryImpl mStaxFactory; - - public SAXParserFactoryImpl(InputFactoryImpl inputFactory) - { - mStaxFactory = inputFactory; - } public SAXParserFactoryImpl() { @@ -100,7 +96,8 @@ public void setFeature(String name, boolean enabled) switch (stdFeat) { case EXTERNAL_GENERAL_ENTITIES: - ok = !enabled; + mStaxFactory.setProperty(ReaderConfig.EXPAND_GENERAL_ENTITIES, enabled); + ok = true; break; case EXTERNAL_PARAMETER_ENTITIES: ok = !enabled; diff --git a/src/test/java/com/fasterxml/aalto/sax/TestEntityResolver.java b/src/test/java/com/fasterxml/aalto/sax/TestEntityResolver.java index 18d9db2..7666a20 100644 --- a/src/test/java/com/fasterxml/aalto/sax/TestEntityResolver.java +++ b/src/test/java/com/fasterxml/aalto/sax/TestEntityResolver.java @@ -67,11 +67,9 @@ 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]"); } - - InputFactoryImpl inputFactory = new InputFactoryImpl(); - inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false); - SAXParserFactoryImpl spfKeepEntityReferences = new SAXParserFactoryImpl(inputFactory); - spfKeepEntityReferences.setNamespaceAware(true); + + SAXParserFactoryImpl spfKeepEntityReferences = new SAXParserFactoryImpl(); + spfKeepEntityReferences.setFeature("http://xml.org/sax/features/external-general-entities", false); SAXParser spKeepEntityReferences = spfKeepEntityReferences.newSAXParser(); final CountDownLatch countDownLatch = new CountDownLatch(1);