use of javax.xml.parsers.DocumentBuilder in project tomcat by apache.
the class WebdavStatus method getDocumentBuilder.
// ------------------------------------------------------ Protected Methods
/**
* Return JAXP document builder instance.
* @return the document builder
* @throws ServletException document builder creation failed
* (wrapped <code>ParserConfigurationException</code> exception)
*/
protected DocumentBuilder getDocumentBuilder() throws ServletException {
DocumentBuilder documentBuilder = null;
DocumentBuilderFactory documentBuilderFactory = null;
try {
documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilderFactory.setExpandEntityReferences(false);
documentBuilder = documentBuilderFactory.newDocumentBuilder();
documentBuilder.setEntityResolver(new WebdavResolver(this.getServletContext()));
} catch (ParserConfigurationException e) {
throw new ServletException(sm.getString("webdavservlet.jaxpfailed"));
}
return documentBuilder;
}
use of javax.xml.parsers.DocumentBuilder in project checkstyle by checkstyle.
the class CheckUtil method getCheckStyleModulesReferencedInConfig.
/**
* Gets a set of names of checkstyle's checks which are referenced in checkstyle_checks.xml.
*
* @param configFilePath
* file path of checkstyle_checks.xml.
* @return names of checkstyle's checks which are referenced in checkstyle_checks.xml.
*/
private static Set<String> getCheckStyleModulesReferencedInConfig(String configFilePath) {
try {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Validations of XML file make parsing too slow, that is why we
// disable all validations.
factory.setNamespaceAware(false);
factory.setValidating(false);
factory.setFeature("http://xml.org/sax/features/namespaces", false);
factory.setFeature("http://xml.org/sax/features/validation", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document document = builder.parse(new File(configFilePath));
// optional, but recommended
// FYI:
// http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-
// how-does-it-work
document.getDocumentElement().normalize();
final NodeList nodeList = document.getElementsByTagName("module");
final Set<String> checksReferencedInCheckstyleChecksXml = new HashSet<>();
for (int i = 0; i < nodeList.getLength(); i++) {
final Node currentNode = nodeList.item(i);
if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
final Element module = (Element) currentNode;
final String checkName = module.getAttribute("name");
checksReferencedInCheckstyleChecksXml.add(checkName);
}
}
return checksReferencedInCheckstyleChecksXml;
} catch (Exception exception) {
throw new IllegalStateException(exception);
}
}
use of javax.xml.parsers.DocumentBuilder in project checkstyle by checkstyle.
the class XmlUtil method getRawXml.
public static Document getRawXml(String fileName, String code, String unserializedSource) throws ParserConfigurationException {
try {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);
final DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(code)));
} catch (IOException | SAXException ex) {
Assert.fail(fileName + " has invalid xml (" + ex.getMessage() + "): " + unserializedSource);
}
return null;
}
use of javax.xml.parsers.DocumentBuilder in project lucida by claritylab.
the class RuleBasedQuestionClassifier method loadRulesFile.
private void loadRulesFile(String fileName) {
// PARSE XML RULES FILE
log.debug("Parsing xml rules file");
Document rulesDocument;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(true);
factory.setNamespaceAware(true);
DocumentBuilder db = factory.newDocumentBuilder();
rulesDocument = db.parse(fileName);
} catch (Exception e) {
throw new RuntimeException("Failed to parse XML patterns file", e);
}
// EXTRACT RULE DATA.
log.debug("Loading rules");
NodeList ruleList = rulesDocument.getElementsByTagName("RULE");
for (int i = 0; i < ruleList.getLength(); i++) {
Node rule = ruleList.item(i);
if (!rule.getNodeName().equals("RULE") || rule.getNodeType() != Node.ELEMENT_NODE)
continue;
rules.add(new Rule((Element) rule));
}
}
use of javax.xml.parsers.DocumentBuilder in project lucida by claritylab.
the class RuleElement method main.
/**
* Tests RuleElement creation, compilation and matching.
*
* @param args
*/
public static void main(String[] args) throws Exception {
String test = "<RULE_ELEMENT feature_name=\"TEST_FEATURE123\">" + "<FEATURE_VALUE>value1</FEATURE_VALUE>" + "<FEATURE_VALUE>value2</FEATURE_VALUE>" + "<FEATURE_VALUE>value3</FEATURE_VALUE>" + "</RULE_ELEMENT>";
Document ruleElementDocument;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(true);
factory.setNamespaceAware(true);
DocumentBuilder db = factory.newDocumentBuilder();
ruleElementDocument = db.parse(new InputSource(new StringReader(test)));
RuleElement re = new RuleElement(ruleElementDocument.getDocumentElement());
System.out.println("Test input: " + test);
System.out.println(re.toString());
} catch (Exception e) {
throw new RuntimeException("Failed to parse XML string", e);
}
}
Aggregations