Skip to content

Commit cd371f8

Browse files
committed
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#65
1 parent 8aa77a7 commit cd371f8

File tree

9 files changed

+48
-16
lines changed

9 files changed

+48
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.fasterxml.aalto;
2+
3+
/**
4+
* Class that contains constant for property names used to configure
5+
* cursor and event readers produced by Aalto implementation of
6+
* {@link javax.xml.stream.XMLInputFactory}.
7+
*/
8+
public final class AaltoInputProperties {
9+
public final static String EXPAND_GENERAL_ENTITIES = "com.fasterxml.aalto.expandGeneralEntities";
10+
}

src/main/java/com/fasterxml/aalto/impl/CommonConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Base class for reader and writer-side configuration/context objects
99
*/
10-
public abstract class CommonConfig
10+
public abstract class CommonConfig implements XMLStreamProperties
1111
{
1212
/*
1313
/**********************************************************************

src/main/java/com/fasterxml/aalto/in/ReaderConfig.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import javax.xml.stream.*;
77

8+
import com.fasterxml.aalto.AaltoInputProperties;
89
import org.codehaus.stax2.XMLInputFactory2;
910

1011
import com.fasterxml.aalto.impl.CommonConfig;
@@ -41,6 +42,7 @@ public final class ReaderConfig
4142
final static int F_AUTO_CLOSE_INPUT = 0x2000;
4243

4344
// Custom flags:
45+
final static int F_EXPAND_GENERAL_ENTITIES = 0x3000;
4446

4547
/**
4648
* These are the default settigs for XMLInputFactory.
@@ -56,6 +58,7 @@ public final class ReaderConfig
5658
// and will report CDATA as such (and not as CHARACTERS)
5759
| F_REPORT_CDATA
5860
| F_PRESERVE_LOCATION
61+
| F_EXPAND_GENERAL_ENTITIES
5962
;
6063

6164
private final static HashMap<String, Object> sProperties;
@@ -98,7 +101,8 @@ public final class ReaderConfig
98101
// !!! Not really implemented, but let's recognize it
99102
sProperties.put(XMLInputFactory2.P_DTD_OVERRIDE, null);
100103

101-
// Custom ones?
104+
// Custom ones
105+
sProperties.put(AaltoInputProperties.EXPAND_GENERAL_ENTITIES, Integer.valueOf(F_EXPAND_GENERAL_ENTITIES));
102106
}
103107

104108
/**
@@ -412,6 +416,9 @@ public boolean willParseLazily() {
412416

413417
public boolean hasInternNsURIsBeenEnabled() { return hasExplicitFlag(F_INTERN_NS_URIS); }
414418

419+
// // // Custom properties
420+
421+
public boolean willExpandGeneralEntities() { return hasFlag(F_EXPAND_GENERAL_ENTITIES); }
415422

416423
/*
417424
/**********************************************************************

src/main/java/com/fasterxml/aalto/in/ReaderScanner.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ private final int collectValue(int attrPtr, char quoteChar, PName attrName)
896896
throwUnexpectedChar(c, "'<' not allowed in attribute value");
897897
case XmlCharTypes.CT_AMP:
898898
{
899-
if (_config.willExpandEntities()) {
899+
if (_config.willExpandGeneralEntities()) {
900900
int d = handleEntityInText(false);
901901
if (d == 0) { // unexpanded general entity... not good
902902
reportUnexpandedEntityInAttr(attrName, false);

src/main/java/com/fasterxml/aalto/sax/SAXParserFactoryImpl.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import javax.xml.parsers.SAXParser;
1919
import javax.xml.parsers.SAXParserFactory;
2020

21+
import com.fasterxml.aalto.AaltoInputProperties;
2122
import org.xml.sax.SAXNotRecognizedException;
2223
import org.xml.sax.SAXNotSupportedException;
2324

@@ -36,11 +37,6 @@ public class SAXParserFactoryImpl
3637
extends SAXParserFactory
3738
{
3839
final InputFactoryImpl mStaxFactory;
39-
40-
public SAXParserFactoryImpl(InputFactoryImpl inputFactory)
41-
{
42-
mStaxFactory = inputFactory;
43-
}
4440

4541
public SAXParserFactoryImpl()
4642
{
@@ -73,6 +69,8 @@ public boolean getFeature(String name)
7369
switch (stdFeat) {
7470
case IS_STANDALONE: // read-only, but only during parsing
7571
return true;
72+
case EXTERNAL_GENERAL_ENTITIES:
73+
return ((Boolean) mStaxFactory.getProperty(AaltoInputProperties.EXPAND_GENERAL_ENTITIES)).booleanValue();
7674
default:
7775
}
7876
} else {
@@ -100,7 +98,8 @@ public void setFeature(String name, boolean enabled)
10098

10199
switch (stdFeat) {
102100
case EXTERNAL_GENERAL_ENTITIES:
103-
ok = !enabled;
101+
mStaxFactory.setProperty(AaltoInputProperties.EXPAND_GENERAL_ENTITIES, enabled);
102+
ok = true;
104103
break;
105104
case EXTERNAL_PARAMETER_ENTITIES:
106105
ok = !enabled;

src/main/java/com/fasterxml/aalto/sax/SAXParserImpl.java

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.xml.stream.XMLStreamConstants;
2323
import javax.xml.stream.XMLStreamException;
2424

25+
import com.fasterxml.aalto.AaltoInputProperties;
2526
import org.xml.sax.*;
2627
import org.xml.sax.ext.Attributes2;
2728
import org.xml.sax.ext.DeclHandler;
@@ -262,6 +263,8 @@ public boolean getFeature(String name)
262263
case IS_STANDALONE: // read-only, but only during parsing
263264
// !!! TBI
264265
return true;
266+
case EXTERNAL_GENERAL_ENTITIES:
267+
return ((Boolean) _staxFactory.getProperty(AaltoInputProperties.EXPAND_GENERAL_ENTITIES)).booleanValue();
265268
default:
266269
}
267270
} else {

src/main/java/com/fasterxml/aalto/sax/SAXUtil.java

-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ public static SAXProperty findStdProperty(String featURI)
7171
public static Boolean getFixedStdFeatureValue(SAXFeature stdFeat)
7272
{
7373
switch (stdFeat) {
74-
case EXTERNAL_GENERAL_ENTITIES: // not yet implemented
75-
return Boolean.FALSE;
7674
case EXTERNAL_PARAMETER_ENTITIES: // not yet implemented
7775
return Boolean.FALSE;
7876
case IS_STANDALONE: // read-only, but only during parsing

src/test/java/com/fasterxml/aalto/sax/TestEntityResolver.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,9 @@ public void testRetainAttributeEntityReference()
6767
} catch (SAXException e) {
6868
verifyException(e, "General entity reference (&replace-me;) encountered in entity expanding mode: operation not (yet) implemented\n at [row,col {unknown-source}]: [2,22]");
6969
}
70-
71-
InputFactoryImpl inputFactory = new InputFactoryImpl();
72-
inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
73-
SAXParserFactoryImpl spfKeepEntityReferences = new SAXParserFactoryImpl(inputFactory);
74-
spfKeepEntityReferences.setNamespaceAware(true);
70+
71+
SAXParserFactoryImpl spfKeepEntityReferences = new SAXParserFactoryImpl();
72+
spfKeepEntityReferences.setFeature("http://xml.org/sax/features/external-general-entities", false);
7573
SAXParser spKeepEntityReferences = spfKeepEntityReferences.newSAXParser();
7674

7775
final CountDownLatch countDownLatch = new CountDownLatch(1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.fasterxml.aalto.sax;
2+
3+
import org.xml.sax.SAXNotRecognizedException;
4+
import org.xml.sax.SAXNotSupportedException;
5+
6+
public class TestSAXParserFactoryImpl extends base.BaseTestCase {
7+
8+
public void testSetGetFeatureExternalGeneralEntities() throws SAXNotRecognizedException, SAXNotSupportedException {
9+
SAXParserFactoryImpl saxParserFactory = new SAXParserFactoryImpl();
10+
saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
11+
assertFalse(saxParserFactory.getFeature("http://xml.org/sax/features/external-general-entities"));
12+
13+
saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", true);
14+
assertTrue(saxParserFactory.getFeature("http://xml.org/sax/features/external-general-entities"));
15+
}
16+
17+
}

0 commit comments

Comments
 (0)