Skip to content

Commit

Permalink
refactor: map SAX external-general-entities feature to new custom par…
Browse files Browse the repository at this point in the history
…ameter meant for retaining entities instead of misusing the StAX XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES feature

Refs: FasterXML#65
  • Loading branch information
cjmamo committed Mar 18, 2021
1 parent 8aa77a7 commit 8658e91
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/fasterxml/aalto/impl/CommonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Base class for reader and writer-side configuration/context objects
*/
public abstract class CommonConfig
public abstract class CommonConfig implements XMLStreamProperties
{
/*
/**********************************************************************
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/fasterxml/aalto/in/ReaderConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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<String, Object> sProperties;
Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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); }

/*
/**********************************************************************
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/fasterxml/aalto/in/ReaderScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -36,11 +37,6 @@ public class SAXParserFactoryImpl
extends SAXParserFactory
{
final InputFactoryImpl mStaxFactory;

public SAXParserFactoryImpl(InputFactoryImpl inputFactory)
{
mStaxFactory = inputFactory;
}

public SAXParserFactoryImpl()
{
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 3 additions & 5 deletions src/test/java/com/fasterxml/aalto/sax/TestEntityResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8658e91

Please sign in to comment.