Search in sources :

Example 66 with DocumentBuilderFactory

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

the class UserSchemaValidator method validateAndParse.

/**
   * Perform a validate and a parse of the specified source document.
   * Validating is done with the specified schemafiles.
   * 
   * @return A Document when the validation s successful.
   */
Document validateAndParse() {
    ValidatorErrorHandler handler = new ValidatorErrorHandler();
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        documentBuilderFactory.setXIncludeAware(true);
        documentBuilderFactory.setValidating(false);
        // We don't want xml:base and xml:lang attributes in the output.
        documentBuilderFactory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
        documentBuilderFactory.setFeature("http://apache.org/xml/features/xinclude/fixup-language", false);
        if (schemaSources.length > 0) {
            Log.info("Checking Schema's");
            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            schemaFactory.setErrorHandler(handler);
            Schema schema = schemaFactory.newSchema(schemaSources);
            documentBuilderFactory.setSchema(schema);
            Log.info("Start validating document");
        } else {
            Log.info("Loading document");
        }
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        handler.reset();
        // Communicate some info about the Xincludes in the imported file. These imports should be resolvable by the server. 
        documentBuilder.setEntityResolver(new EntityResolver() {

            @Override
            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                Log.info(String.format("resolved Entity:%s %s", (publicId != null ? publicId : ""), systemId));
                return null;
            }
        });
        documentBuilder.setErrorHandler(handler);
        Document result = documentBuilder.parse(source);
        if (result != null && handler.isValid()) {
            return result;
        } else {
            Log.warn(String.format("document is invalid. %1$d errors found.", handler.getNrOfErrors()));
            return null;
        }
    } catch (Exception e) {
        Log.warn(String.format("document validation failed. %1$d errors found.", handler.getNrOfErrors()));
        Log.debug("", e);
        return null;
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Schema(javax.xml.validation.Schema) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.w3c.dom.Document) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Example 67 with DocumentBuilderFactory

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

the class Launcher method launchBrowser.

private synchronized void launchBrowser() {
    try {
        // Note, we use standard DOM to read in the XML. This is necessary so that
        // Launcher has fewer dependencies.
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        Document document = factory.newDocumentBuilder().parse(configFile);
        Element rootElement = document.getDocumentElement();
        Element adminElement = (Element) rootElement.getElementsByTagName("adminConsole").item(0);
        String port = "-1";
        String securePort = "-1";
        Element portElement = (Element) adminElement.getElementsByTagName("port").item(0);
        if (portElement != null) {
            port = portElement.getTextContent();
        }
        Element securePortElement = (Element) adminElement.getElementsByTagName("securePort").item(0);
        if (securePortElement != null) {
            securePort = securePortElement.getTextContent();
        }
        if ("-1".equals(port)) {
            Desktop.getDesktop().browse(URI.create("https://127.0.0.1:" + securePort + "/index.html"));
        } else {
            Desktop.getDesktop().browse(URI.create("http://127.0.0.1:" + port + "/index.html"));
        }
    } catch (Exception e) {
        // Make sure to print the exception
        e.printStackTrace(System.out);
        JOptionPane.showMessageDialog(new JFrame(), configFile + " " + e.getMessage());
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) JFrame(javax.swing.JFrame) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) BadLocationException(javax.swing.text.BadLocationException) FileNotFoundException(java.io.FileNotFoundException) AWTException(java.awt.AWTException) IOException(java.io.IOException)

Example 68 with DocumentBuilderFactory

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

the class GatewayDWR 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");
        allowElement.appendChild(buildCreator("ConfigManager", "net.sf.kraken.web.ConfigManager"));
        allowElement.appendChild(buildCreator("ConnectionTester", "net.sf.kraken.web.ConnectionTester"));
        root.appendChild(allowElement);
    } catch (ParserConfigurationException e) {
        Log.error("Error configuring DWR for gateway plugin: ", e);
    }
    configuration.addConfig(document);
    // Specify the path for the js files 
    Object bean = container.getBean("interface");
    if (bean instanceof DefaultInterfaceProcessor) {
        DefaultInterfaceProcessor processor = (DefaultInterfaceProcessor) bean;
        processor.setOverridePath("/plugins/kraken/dwr");
    }
}
Also used : DefaultInterfaceProcessor(uk.ltd.getahead.dwr.impl.DefaultInterfaceProcessor) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 69 with DocumentBuilderFactory

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

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

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