Search in sources :

Example 31 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project hbase by apache.

the class TestConfServlet method testWriteXml.

@Test
public void testWriteXml() throws Exception {
    StringWriter sw = new StringWriter();
    ConfServlet.writeResponse(getTestConf(), sw, "xml");
    String xml = sw.toString();
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(xml)));
    NodeList nameNodes = doc.getElementsByTagName("name");
    boolean foundSetting = false;
    for (int i = 0; i < nameNodes.getLength(); i++) {
        Node nameNode = nameNodes.item(i);
        String key = nameNode.getTextContent();
        System.err.println("xml key: " + key);
        if (TEST_KEY.equals(key)) {
            foundSetting = true;
            Element propertyElem = (Element) nameNode.getParentNode();
            String val = propertyElem.getElementsByTagName("value").item(0).getTextContent();
            assertEquals(TEST_VAL, val);
        }
    }
    assertTrue(foundSetting);
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 32 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory in project hive by apache.

the class GenHiveTemplate method generateTemplate.

private Document generateTemplate() throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = dbf.newDocumentBuilder();
    Document doc = docBuilder.newDocument();
    doc.appendChild(doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"configuration.xsl\""));
    doc.appendChild(doc.createComment("\n" + "   Licensed to the Apache Software Foundation (ASF) under one or more\n" + "   contributor license agreements.  See the NOTICE file distributed with\n" + "   this work for additional information regarding copyright ownership.\n" + "   The ASF licenses this file to You under the Apache License, Version 2.0\n" + "   (the \"License\"); you may not use this file except in compliance with\n" + "   the License.  You may obtain a copy of the License at\n" + "\n" + "       http://www.apache.org/licenses/LICENSE-2.0\n" + "\n" + "   Unless required by applicable law or agreed to in writing, software\n" + "   distributed under the License is distributed on an \"AS IS\" BASIS,\n" + "   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + "   See the License for the specific language governing permissions and\n" + "   limitations under the License.\n"));
    Element root = doc.createElement("configuration");
    doc.appendChild(root);
    root.appendChild(doc.createComment(" WARNING!!! This file is auto generated for documentation purposes ONLY! "));
    root.appendChild(doc.createComment(" WARNING!!! Any changes you make to this file will be ignored by Hive.   "));
    root.appendChild(doc.createComment(" WARNING!!! You must make your changes in hive-site.xml instead.         "));
    root.appendChild(doc.createComment(" Hive Execution Parameters "));
    Thread.currentThread().setContextClassLoader(ShimLoader.class.getClassLoader());
    for (HiveConf.ConfVars confVars : HiveConf.ConfVars.values()) {
        if (confVars.isExcluded()) {
            // thought of creating template for each shims, but I couldn't generate proper mvn script
            continue;
        }
        Element property = appendElement(root, "property", null);
        appendElement(property, "name", confVars.varname);
        appendElement(property, "value", confVars.getDefaultExpr());
        appendElement(property, "description", normalize(confVars.getDescription()));
    // wish to add new line here.
    }
    return doc;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ShimLoader(org.apache.hadoop.hive.shims.ShimLoader) Element(org.w3c.dom.Element) HiveConf(org.apache.hadoop.hive.conf.HiveConf) Document(org.w3c.dom.Document)

Example 33 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory 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 34 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory 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 35 with DocumentBuilderFactory

use of javax.xml.parsers.DocumentBuilderFactory 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)

Aggregations

DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)759 DocumentBuilder (javax.xml.parsers.DocumentBuilder)622 Document (org.w3c.dom.Document)526 Element (org.w3c.dom.Element)244 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)232 NodeList (org.w3c.dom.NodeList)209 IOException (java.io.IOException)197 InputSource (org.xml.sax.InputSource)193 SAXException (org.xml.sax.SAXException)173 Node (org.w3c.dom.Node)145 StringReader (java.io.StringReader)142 Test (org.junit.Test)117 File (java.io.File)86 DOMSource (javax.xml.transform.dom.DOMSource)77 ByteArrayInputStream (java.io.ByteArrayInputStream)72 InputStream (java.io.InputStream)63 StreamResult (javax.xml.transform.stream.StreamResult)50 ArrayList (java.util.ArrayList)47 SAXParseException (org.xml.sax.SAXParseException)46 Transformer (javax.xml.transform.Transformer)44