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 6359c3e
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 15 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/fasterxml/aalto/AaltoInputProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.fasterxml.aalto;

/**
* Class that contains constant for property names used to configure
* cursor and event readers produced by Aalto implementation of
* {@link javax.xml.stream.XMLInputFactory}.
*/
public final class AaltoInputProperties {
public final static String EXPAND_GENERAL_ENTITIES = "com.fasterxml.aalto.expandGeneralEntities";
}
9 changes: 8 additions & 1 deletion src/main/java/com/fasterxml/aalto/in/ReaderConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javax.xml.stream.*;

import com.fasterxml.aalto.AaltoInputProperties;
import org.codehaus.stax2.XMLInputFactory2;

import com.fasterxml.aalto.impl.CommonConfig;
Expand Down Expand Up @@ -41,6 +42,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 +58,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 +101,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(AaltoInputProperties.EXPAND_GENERAL_ENTITIES, Integer.valueOf(F_EXPAND_GENERAL_ENTITIES));
}

/**
Expand Down Expand Up @@ -412,6 +416,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
11 changes: 5 additions & 6 deletions src/main/java/com/fasterxml/aalto/sax/SAXParserFactoryImpl.java
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.AaltoInputProperties;
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 @@ -73,6 +69,8 @@ public boolean getFeature(String name)
switch (stdFeat) {
case IS_STANDALONE: // read-only, but only during parsing
return true;
case EXTERNAL_GENERAL_ENTITIES:
return ((Boolean) mStaxFactory.getProperty(AaltoInputProperties.EXPAND_GENERAL_ENTITIES)).booleanValue();
default:
}
} else {
Expand Down Expand Up @@ -100,7 +98,8 @@ public void setFeature(String name, boolean enabled)

switch (stdFeat) {
case EXTERNAL_GENERAL_ENTITIES:
ok = !enabled;
mStaxFactory.setProperty(AaltoInputProperties.EXPAND_GENERAL_ENTITIES, enabled);
ok = true;
break;
case EXTERNAL_PARAMETER_ENTITIES:
ok = !enabled;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/fasterxml/aalto/sax/SAXParserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;

import com.fasterxml.aalto.AaltoInputProperties;
import org.xml.sax.*;
import org.xml.sax.ext.Attributes2;
import org.xml.sax.ext.DeclHandler;
Expand Down Expand Up @@ -262,6 +263,8 @@ public boolean getFeature(String name)
case IS_STANDALONE: // read-only, but only during parsing
// !!! TBI
return true;
case EXTERNAL_GENERAL_ENTITIES:
return ((Boolean) _staxFactory.getProperty(AaltoInputProperties.EXPAND_GENERAL_ENTITIES)).booleanValue();
default:
}
} else {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/fasterxml/aalto/sax/SAXUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ 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
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.fasterxml.aalto.sax;

import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;

public class TestSAXParserFactoryImpl extends base.BaseTestCase {

public void testSetGetFeatureExternalGeneralEntities() throws SAXNotRecognizedException, SAXNotSupportedException {
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"));

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

}

0 comments on commit 6359c3e

Please sign in to comment.