Skip to content

Commit 51d8bfa

Browse files
committed
Changes post #65: make AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES be the SAX feature key too.
1 parent 5deb3f4 commit 51d8bfa

File tree

4 files changed

+37
-30
lines changed

4 files changed

+37
-30
lines changed

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

+12-14
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ public SAXParserFactoryImpl()
4545
mStaxFactory = new InputFactoryImpl();
4646
}
4747

48-
// As per [Issue#4], let's re-define this method
49-
/**
50-
* @since 0.9.8
51-
*/
5248
public static SAXParserFactory newInstance() {
5349
return new SAXParserFactoryImpl();
5450
}
@@ -69,12 +65,12 @@ public boolean getFeature(String name)
6965
switch (stdFeat) {
7066
case IS_STANDALONE: // read-only, but only during parsing
7167
return true;
72-
case EXTERNAL_GENERAL_ENTITIES:
73-
return Boolean.FALSE.equals(mStaxFactory.getProperty(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES));
7468
default:
7569
}
7670
} else {
77-
// any non-standard one we may support?
71+
if (name.equals(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES)) {
72+
return Boolean.TRUE.equals(mStaxFactory.getProperty(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES));
73+
}
7874
}
7975

8076
// nope, not recognized:
@@ -98,8 +94,7 @@ public void setFeature(String name, boolean enabled)
9894

9995
switch (stdFeat) {
10096
case EXTERNAL_GENERAL_ENTITIES:
101-
mStaxFactory.setProperty(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES, !enabled);
102-
ok = true;
97+
ok = !enabled;
10398
break;
10499
case EXTERNAL_PARAMETER_ENTITIES:
105100
ok = !enabled;
@@ -120,9 +115,8 @@ public void setFeature(String name, boolean enabled)
120115
ok = true;
121116
break;
122117
case STRING_INTERNING:
123-
/* Can not disable; however, doesn't harm if they try to
124-
* do it, so let's not care
125-
*/
118+
// Can not disable; however, doesn't harm if they try to
119+
// do it, so let's not care
126120
ok = true;
127121
break;
128122
case UNICODE_NORMALIZATION_CHECKING:
@@ -154,10 +148,14 @@ public void setFeature(String name, boolean enabled)
154148
throw new SAXNotSupportedException("Setting std feature "+stdFeat+" to "+enabled+" not supported");
155149
}
156150
return;
151+
} else {
152+
// [aalto-xml#65]: allow retaining GEs in attribute values
153+
if (name.equals(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES)) {
154+
mStaxFactory.setProperty(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES, enabled);
155+
return;
156+
}
157157
}
158158

159-
// any non-standard one we may support?
160-
161159
// nope, not recognized:
162160
SAXUtil.reportUnknownFeature(name);
163161
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ 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;
7476
case EXTERNAL_PARAMETER_ENTITIES: // not yet implemented
7577
return Boolean.FALSE;
7678
case IS_STANDALONE: // read-only, but only during parsing

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

+13-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import java.util.concurrent.CountDownLatch;
55

66
import javax.xml.parsers.SAXParser;
7-
import javax.xml.stream.XMLInputFactory;
87

9-
import com.fasterxml.aalto.stax.InputFactoryImpl;
108
import org.xml.sax.*;
119
import org.xml.sax.helpers.DefaultHandler;
1210

11+
import com.fasterxml.aalto.AaltoInputProperties;
12+
1313
/**
1414
* Simple unit tests to verify that most fundamental parsing functionality
1515
* works via Woodstox SAX implementation.
@@ -30,9 +30,8 @@ public void testWithDummyExtSubset()
3030
SAXParser sp = spf.newSAXParser();
3131
DefaultHandler h = new DefaultHandler();
3232

33-
/* First: let's verify that we get an exception for
34-
* unresolved reference...
35-
*/
33+
// First: let's verify that we get an exception for
34+
// unresolved reference...
3635
try {
3736
sp.parse(new InputSource(new StringReader(XML)), h);
3837
} catch (SAXException e) {
@@ -50,6 +49,7 @@ public void testWithDummyExtSubset()
5049
}
5150
}
5251

52+
// [aalto-xml#65]: allow retaining GEs in attribute values
5353
public void testRetainAttributeEntityReference()
5454
throws Exception
5555
{
@@ -58,6 +58,9 @@ public void testRetainAttributeEntityReference()
5858
+"<root b=\"&replace-me;\" />";
5959

6060
SAXParserFactoryImpl spf = new SAXParserFactoryImpl();
61+
// should be disabled by default:
62+
assertEquals(false,
63+
spf.getFeature(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES));
6164
SAXParser sp = spf.newSAXParser();
6265
DefaultHandler h = new DefaultHandler();
6366

@@ -67,9 +70,12 @@ public void testRetainAttributeEntityReference()
6770
} catch (SAXException e) {
6871
verifyException(e, "General entity reference (&replace-me;) encountered in entity expanding mode: operation not (yet) implemented\n at [row,col {unknown-source}]: [2,22]");
6972
}
70-
73+
7174
SAXParserFactoryImpl spfKeepEntityReferences = new SAXParserFactoryImpl();
72-
spfKeepEntityReferences.setFeature("http://xml.org/sax/features/external-general-entities", false);
75+
spfKeepEntityReferences.setFeature(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES, true);
76+
assertEquals(true,
77+
spfKeepEntityReferences.getFeature(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES));
78+
7379
SAXParser spKeepEntityReferences = spfKeepEntityReferences.newSAXParser();
7480

7581
final CountDownLatch countDownLatch = new CountDownLatch(1);
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package com.fasterxml.aalto.sax;
22

3-
import org.xml.sax.SAXNotRecognizedException;
4-
import org.xml.sax.SAXNotSupportedException;
3+
import com.fasterxml.aalto.AaltoInputProperties;
54

6-
public class TestSAXParserFactoryImpl extends base.BaseTestCase {
7-
8-
public void testSetGetFeatureExternalGeneralEntities() throws SAXNotRecognizedException, SAXNotSupportedException {
5+
public class TestSAXParserFactoryImpl extends base.BaseTestCase
6+
{
7+
// [aalto-xml#65]
8+
public void testSetGetFeatureExternalGeneralEntities() throws Exception
9+
{
910
SAXParserFactoryImpl saxParserFactory = new SAXParserFactoryImpl();
1011
saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
11-
assertFalse(saxParserFactory.getFeature("http://xml.org/sax/features/external-general-entities"));
12+
assertFalse(saxParserFactory.getFeature(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES));
1213

13-
saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", true);
14-
assertTrue(saxParserFactory.getFeature("http://xml.org/sax/features/external-general-entities"));
14+
saxParserFactory.setFeature(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES, true);
15+
assertTrue(saxParserFactory.getFeature(AaltoInputProperties.P_RETAIN_ATTRIBUTE_GENERAL_ENTITIES));
1516
}
16-
17+
1718
}

0 commit comments

Comments
 (0)