Skip to content
This repository has been archived by the owner on Mar 26, 2019. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
…atures configurable
  • Loading branch information
dejanb committed Aug 26, 2014
1 parent e29cfd5 commit e564755
Showing 1 changed file with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;


public class XalanXPathEvaluator implements XPathExpression.XPathEvaluator {

public static final String DOCUMENT_BUILDER_FACTORY_FEATURE = "org.apache.activemq.apollo.documentBuilderFactory.feature";
private final String xpath;

public XalanXPathEvaluator(String xpath) {
Expand All @@ -51,7 +56,13 @@ protected boolean evaluate(String text) {
protected boolean evaluate(InputSource inputSource) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
setupFeatures(factory);
factory.setNamespaceAware(true);
factory.setIgnoringElementContentWhitespace(true);
factory.setIgnoringComments(true);
DocumentBuilder dbuilder = factory.newDocumentBuilder();
Document doc = dbuilder.parse(inputSource);

Expand All @@ -73,4 +84,33 @@ protected boolean evaluate(InputSource inputSource) {
return false;
}
}

protected void setupFeatures(DocumentBuilderFactory factory) {
Properties properties = System.getProperties();
List<String> features = new ArrayList<String>();
for (Map.Entry<Object, Object> prop : properties.entrySet()) {
String key = (String) prop.getKey();
if (key.startsWith(DOCUMENT_BUILDER_FACTORY_FEATURE)) {
String uri = key.split(DOCUMENT_BUILDER_FACTORY_FEATURE + ":")[1];
Boolean value = Boolean.valueOf((String)prop.getValue());
try {
factory.setFeature(uri, value);
features.add("feature " + uri + " value " + value);
} catch (ParserConfigurationException e) {
throw new RuntimeException("DocumentBuilderFactory doesn't support the feature " + uri + " with value " + value + ", due to " + e);
}
}
}
if (features.size() > 0) {
StringBuffer featureString = new StringBuffer();
// just log the configured feature
for (String feature : features) {
if (featureString.length() != 0) {
featureString.append(", ");
}
featureString.append(feature);
}
}

}
}

0 comments on commit e564755

Please sign in to comment.