Search in sources :

Example 86 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project Openfire by igniterealtime.

the class MonitoringDWR method configure.

@Override
public void configure(ServletConfig servletConfig, Configuration configuration) throws ServletException {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dbf.newDocumentBuilder();
        document = builder.newDocument();
        Element root = document.createElement("dwr");
        document.appendChild(root);
        Element allowElement = document.createElement("allow");
        // Build stats bean
        Element createElement = buildCreator("Stats", org.jivesoftware.openfire.reporting.stats.StatsAction.class.getName());
        Element convertConversationElement = document.createElement("convert");
        convertConversationElement.setAttribute("converter", "bean");
        convertConversationElement.setAttribute("match", org.jivesoftware.openfire.archive.ConversationInfo.class.getName());
        // Build conversation Element.
        Element conversationElement = buildCreator("conversations", org.jivesoftware.openfire.archive.ConversationUtils.class.getName());
        allowElement.appendChild(createElement);
        allowElement.appendChild(convertConversationElement);
        allowElement.appendChild(conversationElement);
        root.appendChild(allowElement);
    } catch (ParserConfigurationException e) {
        Log.error("error creating DWR configuration: " + e);
    }
    configuration.addConfig(document);
    // Specify the path for the Stat.js file 
    Object bean = container.getBean("interface");
    if (bean instanceof DefaultInterfaceProcessor) {
        DefaultInterfaceProcessor processor = (DefaultInterfaceProcessor) bean;
        processor.setOverridePath("/plugins/" + MonitoringConstants.NAME + "/dwr");
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) DefaultInterfaceProcessor(uk.ltd.getahead.dwr.impl.DefaultInterfaceProcessor) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 87 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project jOOQ by jOOQ.

the class XMLasDOMBinding method builder.

/**
     * Get a namespace-aware document builder
     */
public static DocumentBuilder builder() {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // [#4592] FIX START: Prevent OWASP attack vectors
        try {
            factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        } catch (ParserConfigurationException ignore) {
        }
        try {
            factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        } catch (ParserConfigurationException ignore) {
        }
        try {
            factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        } catch (ParserConfigurationException ignore) {
        }
        factory.setXIncludeAware(false);
        factory.setExpandEntityReferences(false);
        // [#4592] FIX END
        // -----------------------------------------------------------------
        // [#9] [#107] In order to take advantage of namespace-related DOM
        // features, the internal builder should be namespace-aware
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 88 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project jersey by jersey.

the class ExtendedWadlWebappOsgiTest method checkWadl.

private void checkWadl(String wadl, URI baseUri) throws Exception {
    DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
    bf.setNamespaceAware(true);
    bf.setValidating(false);
    DocumentBuilder b = bf.newDocumentBuilder();
    Document document = b.parse(new ByteArrayInputStream(wadl.getBytes(Charset.forName("UTF-8"))));
    XPath xp = XPathFactory.newInstance().newXPath();
    xp.setNamespaceContext(new SimpleNamespaceResolver("wadl", "http://wadl.dev.java.net/2009/02"));
    String val = (String) xp.evaluate("/wadl:application/wadl:resources/@base", document, XPathConstants.STRING);
    assertEquals(baseUri.toString(), val.endsWith("/") ? val.substring(0, val.length() - 1) : val);
    val = (String) xp.evaluate("count(//wadl:resource)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements.", val, "4");
    val = (String) xp.evaluate("count(//wadl:resource[@path='items'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with 'items' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='{id}'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with '{id}' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='try-hard'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with 'try-hard' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='value/{value}'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with 'value/{value}' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='{id}']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with '{id}' path.", "2", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='{id}']/wadl:method[@id='getItem']" + "/wadl:doc[contains(., 'Typically returns the item if it exists.')])", document, XPathConstants.STRING);
    assertEquals("Unexpected documentation of getItem resource method at '{id}' path", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='try-hard']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with 'try-hard' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='try-hard']/wadl:method[@id='getItem']" + "/wadl:doc[contains(., 'Tries hard to return the item if it exists.')])", document, XPathConstants.STRING);
    assertEquals("Unexpected documentation of getItem resource method at 'try-hard' path", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='items']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with 'items' path.", "4", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='value/{value}']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with 'value/{value}' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:application/wadl:doc)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of doc elements in application element.", "3", val);
}
Also used : XPath(javax.xml.xpath.XPath) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleNamespaceResolver(org.glassfish.jersey.internal.util.SimpleNamespaceResolver) Document(org.w3c.dom.Document)

Example 89 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project jersey by jersey.

the class ExtendedWadlWebappTest method checkWadl.

private void checkWadl(String wadl, URI baseUri) throws Exception {
    DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
    bf.setNamespaceAware(true);
    bf.setValidating(false);
    //        if (!SaxHelper.isXdkDocumentBuilderFactory(bf)) {
    //            bf.setXIncludeAware(false);
    //        }
    DocumentBuilder b = bf.newDocumentBuilder();
    Document document = b.parse(new ByteArrayInputStream(wadl.getBytes(Charset.forName("UTF-8"))));
    XPath xp = XPathFactory.newInstance().newXPath();
    xp.setNamespaceContext(new SimpleNamespaceResolver("wadl", "http://wadl.dev.java.net/2009/02"));
    String val = (String) xp.evaluate("/wadl:application/wadl:resources/@base", document, XPathConstants.STRING);
    assertEquals(baseUri.toString(), val.endsWith("/") ? val.substring(0, val.length() - 1) : val);
    val = (String) xp.evaluate("count(//wadl:resource)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements.", val, "4");
    val = (String) xp.evaluate("count(//wadl:resource[@path='items'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with 'items' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='{id}'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with '{id}' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='try-hard'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with 'try-hard' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='value/{value}'])", document, XPathConstants.STRING);
    assertEquals("Unexpected number of resource elements with 'value/{value}' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='{id}']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with '{id}' path.", "2", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='{id}']/wadl:method[@id='getItem']" + "/wadl:doc[contains(., 'Typically returns the item if it exists.')])", document, XPathConstants.STRING);
    assertEquals("Unexpected documentation of getItem resource method at '{id}' path", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='try-hard']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with 'try-hard' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='try-hard']/wadl:method[@id='getItem']" + "/wadl:doc[contains(., 'Tries hard to return the item if it exists.')])", document, XPathConstants.STRING);
    assertEquals("Unexpected documentation of getItem resource method at 'try-hard' path", "1", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='items']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with 'items' path.", "4", val);
    val = (String) xp.evaluate("count(//wadl:resource[@path='value/{value}']/wadl:method)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of methods in resource element with 'value/{value}' path.", "1", val);
    val = (String) xp.evaluate("count(//wadl:application/wadl:doc)", document, XPathConstants.STRING);
    assertEquals("Unexpected number of doc elements in application element.", "3", val);
}
Also used : XPath(javax.xml.xpath.XPath) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleNamespaceResolver(org.glassfish.jersey.internal.util.SimpleNamespaceResolver) Document(org.w3c.dom.Document)

Example 90 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project quickstarts by jboss-switchyard.

the class TypeTransformationTest method loadXML.

private Document loadXML(String path) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    return db.parse(getClass().getClassLoader().getResourceAsStream(path));
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

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