Search in sources :

Example 41 with DocumentBuilder

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;
}
Also used : ServletException(javax.servlet.ServletException) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 42 with 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);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) IOException(java.io.IOException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File) HashSet(java.util.HashSet)

Example 43 with DocumentBuilder

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;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 44 with DocumentBuilder

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));
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 45 with DocumentBuilder

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);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Aggregations

DocumentBuilder (javax.xml.parsers.DocumentBuilder)883 Document (org.w3c.dom.Document)694 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)622 Element (org.w3c.dom.Element)339 IOException (java.io.IOException)276 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)276 NodeList (org.w3c.dom.NodeList)267 InputSource (org.xml.sax.InputSource)238 SAXException (org.xml.sax.SAXException)235 Node (org.w3c.dom.Node)199 StringReader (java.io.StringReader)167 Test (org.junit.Test)127 DOMSource (javax.xml.transform.dom.DOMSource)102 File (java.io.File)99 ByteArrayInputStream (java.io.ByteArrayInputStream)86 InputStream (java.io.InputStream)73 ArrayList (java.util.ArrayList)72 StreamResult (javax.xml.transform.stream.StreamResult)65 Transformer (javax.xml.transform.Transformer)59 SAXParseException (org.xml.sax.SAXParseException)56