Skip to content

Commit 4e88dfb

Browse files
committed
Simplify XML FSP
1 parent b5052c9 commit 4e88dfb

File tree

3 files changed

+8
-34
lines changed

3 files changed

+8
-34
lines changed

src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,15 +1718,14 @@ public StringLookup xmlStringLookup(final Map<String, Boolean> factoryFeatures)
17181718
* <li>{@code "com/domain/document.xml:/path/to/node"}</li>
17191719
* </ul>
17201720
* <p>
1721-
* Secure processing is enabled by default and can be overridden with the system property {@code "XmlStringLookup.secure"} set to {@code false}. The secure
1722-
* boolean String parsing follows the syntax defined by {@link Boolean#parseBoolean(String)}.
1721+
* Secure processing is enabled by default and can be overridden with this constructor.
17231722
* </p>
17241723
* <p>
17251724
* Using a {@link StringLookup} from the {@link StringLookupFactory} fenced by the current directory ({@code Paths.get("")}):
17261725
* </p>
17271726
*
17281727
* <pre>
1729-
* StringLookupFactory.INSTANCE.xmlStringLookup(map, Pathe.get("")).lookup("com/domain/document.xml:/path/to/node");
1728+
* StringLookupFactory.INSTANCE.xmlStringLookup(map, Path.get("")).lookup("com/domain/document.xml:/path/to/node");
17301729
* </pre>
17311730
* <p>
17321731
* To use a {@link StringLookup} fenced by the current directory, use:

src/main/java/org/apache/commons/text/lookup/XmlStringLookup.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import javax.xml.xpath.XPathFactory;
3131

3232
import org.apache.commons.lang3.StringUtils;
33-
import org.apache.commons.lang3.SystemProperties;
3433
import org.w3c.dom.Document;
3534

3635
/**
@@ -42,8 +41,7 @@
4241
* <li>{@code "com/domain/document.xml:/path/to/node"}</li>
4342
* </ul>
4443
* <p>
45-
* Secure processing is enabled by default and can be overridden with the system property {@code "XmlStringLookup.secure"} set to {@code false}. The secure
46-
* boolean String parsing follows the syntax defined by {@link Boolean#parseBoolean(String)}.
44+
* Secure processing is enabled by default and can be overridden with {@link StringLookupFactory#xmlStringLookup(Map, Path...)}.
4745
* </p>
4846
*
4947
* @since 1.5
@@ -72,14 +70,13 @@ final class XmlStringLookup extends AbstractPathFencedLookup {
7270
}
7371

7472
/**
75-
* Defines the singleton for this class with secure processing enabled.
73+
* Defines the singleton for this class with secure processing enabled by default.
74+
* <p>
75+
* Secure processing is enabled by default and can be overridden with {@link StringLookupFactory#xmlStringLookup(Map, Path...)}.
76+
* </p>
7677
*/
7778
static final XmlStringLookup INSTANCE = new XmlStringLookup(DEFAULT_XML_FEATURES, DEFAULT_XPATH_FEATURES, (Path[]) null);
7879

79-
private static boolean isSecure() {
80-
return SystemProperties.getBoolean(XmlStringLookup.class, "secure", () -> true);
81-
}
82-
8380
/**
8481
* Defines XPath factory features.
8582
*/
@@ -113,8 +110,7 @@ private static boolean isSecure() {
113110
* <li>{@code "com/domain/document.xml:/path/to/node"}</li>
114111
* </ul>
115112
* <p>
116-
* Secure processing is enabled by default. The secure boolean String parsing follows the syntax defined by {@link Boolean#parseBoolean(String)}. The secure
117-
* value in the key overrides instance settings given in the constructor.
113+
* Secure processing is enabled by default and can be overridden with {@link StringLookupFactory#xmlStringLookup(Map, Path...)}.
118114
* </p>
119115
*
120116
* @param key the key to be looked up, may be null.
@@ -130,22 +126,19 @@ public String lookup(final String key) {
130126
if (keyLen != KEY_PARTS_LEN) {
131127
throw IllegalArgumentExceptions.format("Bad XML key format '%s'; the expected format is 'DocumentPath:XPath'.", key);
132128
}
133-
final boolean secure = isSecure();
134129
final String documentPath = keys[0];
135130
final String xpath = StringUtils.substringAfterLast(key, SPLIT_CH);
136131
final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
137132
try {
138133
for (final Entry<String, Boolean> p : xmlFactoryFeatures.entrySet()) {
139134
dbFactory.setFeature(p.getKey(), p.getValue());
140135
}
141-
dbFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, secure);
142136
try (InputStream inputStream = Files.newInputStream(getPath(documentPath))) {
143137
final Document doc = dbFactory.newDocumentBuilder().parse(inputStream);
144138
final XPathFactory xpFactory = XPathFactory.newInstance();
145139
for (final Entry<String, Boolean> p : xPathFactoryFeatures.entrySet()) {
146140
xpFactory.setFeature(p.getKey(), p.getValue());
147141
}
148-
xpFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, secure);
149142
return xpFactory.newXPath().evaluate(xpath, doc);
150143
}
151144
} catch (final Exception e) {

src/test/java/org/apache/commons/text/lookup/XmlStringLookupTest.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.apache.commons.lang3.StringUtils;
3636
import org.apache.commons.text.StringSubstitutor;
3737
import org.junit.jupiter.api.Test;
38-
import org.junitpioneer.jupiter.SetSystemProperty;
3938

4039
/**
4140
* Tests {@link XmlStringLookup}.
@@ -69,7 +68,6 @@ void testExternalEntityOff() {
6968
}
7069

7170
@Test
72-
@SetSystemProperty(key = "XmlStringLookup.secure", value = "false")
7371
void testExternalEntityOn() {
7472
final String key = DOC_DIR + "document-entity-ref.xml:/document/content";
7573
assertEquals(DATA, new XmlStringLookup(EMPTY_MAP, EMPTY_MAP).apply(key).trim());
@@ -83,35 +81,19 @@ void testInterpolatorExternalDtdOff() {
8381
+ "document-external-dtd.xml:/document/content}"));
8482
}
8583

86-
@Test
87-
@SetSystemProperty(key = "XmlStringLookup.secure", value = "false")
88-
void testInterpolatorExternalDtdOn() {
89-
final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
90-
assertEquals("This is an external entity.",
91-
stringSubstitutor.replace("${xml:" + DOC_DIR + "document-external-dtd.xml:/document/content}").trim());
92-
}
93-
9484
@Test
9585
void testInterpolatorExternalEntityOff() {
9686
final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
9787
assertThrows(IllegalArgumentException.class, () -> stringSubstitutor.replace("${xml:" + DOC_DIR + "document-entity-ref.xml:/document/content}"));
9888
}
9989

100-
@Test
101-
@SetSystemProperty(key = "XmlStringLookup.secure", value = "false")
102-
void testInterpolatorExternalEntityOffOverride() {
103-
final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
104-
assertEquals(DATA, stringSubstitutor.replace("${xml:" + DOC_DIR + "document-entity-ref.xml:/document/content}").trim());
105-
}
106-
10790
@Test
10891
void testInterpolatorExternalEntityOn() {
10992
final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
11093
assertThrows(IllegalArgumentException.class, () -> stringSubstitutor.replace("${xml:" + DOC_DIR + "document-entity-ref.xml:/document/content}"));
11194
}
11295

11396
@Test
114-
@SetSystemProperty(key = "XmlStringLookup.secure", value = "true")
11597
void testInterpolatorExternalEntityOnOverride() {
11698
final StringSubstitutor stringSubstitutor = StringSubstitutor.createInterpolator();
11799
assertThrows(IllegalArgumentException.class,

0 commit comments

Comments
 (0)