Search in sources :

Example 81 with DocumentBuilder

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

the class DirectoryListingTest method directoryListing.

/**
     * Test directory listing
     * @throws java.lang.Exception if an error occurs while generating the
     *         list.
     */
@Test
public void directoryListing() throws Exception {
    StringWriter out = new StringWriter();
    out.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<start>\n");
    DirectoryListing instance = new DirectoryListing();
    instance.listTo("ctx", directory, out, directory.getPath(), Arrays.asList(directory.list()));
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    assertNotNull("DocumentBuilderFactory is null", factory);
    DocumentBuilder builder = factory.newDocumentBuilder();
    assertNotNull("DocumentBuilder is null", builder);
    out.append("</start>\n");
    String str = out.toString();
    System.out.println(str);
    Document document = builder.parse(new ByteArrayInputStream(str.getBytes()));
    NodeList nl = document.getElementsByTagName("tr");
    int len = nl.getLength();
    // add one extra for header and one for parent directory link
    assertEquals(entries.length + 2, len);
    // Skip the the header and parent link
    for (int i = 2; i < len; ++i) {
        validateEntry((Element) nl.item(i));
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 82 with DocumentBuilder

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

the class XmlUtils method getDocument.

/**
     * Convenience method to parse an input stream into a document
     * @param inputStream
     * @return
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
public static Document getDocument(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    return db.parse(inputStream);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Example 83 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder in project atlas by alibaba.

the class Configuration method readXmlConfig.

/**
     * read args from xml
     **/
void readXmlConfig(File xmlConfigFile) throws IOException, ParserConfigurationException, SAXException {
    if (!xmlConfigFile.exists()) {
        return;
    }
    System.out.printf("reading config file, %s\n", xmlConfigFile.getAbsolutePath());
    BufferedInputStream input = null;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        input = new BufferedInputStream(new FileInputStream(xmlConfigFile));
        InputSource source = new InputSource(input);
        factory.setNamespaceAware(false);
        factory.setValidating(false);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(source);
        NodeList issues = document.getElementsByTagName(TAG_ISSUE);
        for (int i = 0, count = issues.getLength(); i < count; i++) {
            Node node = issues.item(i);
            Element element = (Element) node;
            String id = element.getAttribute(ATTR_ID);
            if (id.length() == 0) {
                System.err.println("Invalid config file: Missing required issue id attribute");
                continue;
            }
            if (id.equals(PROPERTY_ISSUE)) {
                readPropertyFromXml(node);
            } else if (id.equals(DEX_ISSUE)) {
                readDexPatternsFromXml(node);
            } else if (id.equals(SO_ISSUE)) {
                readLibPatternsFromXml(node);
            } else if (id.equals(RES_ISSUE)) {
                readResPatternsFromXml(node);
            } else if (id.equals(PACKAGE_CONFIG_ISSUE)) {
                readPackageConfigFromXml(node);
            } else if (id.equals(SIGN_ISSUE)) {
                if (mUseSignAPk) {
                    readSignFromXml(node);
                }
            } else {
                System.err.println("unknown issue " + id);
            }
        }
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                System.exit(-1);
            }
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) BufferedInputStream(java.io.BufferedInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream)

Example 84 with DocumentBuilder

use of javax.xml.parsers.DocumentBuilder 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 85 with DocumentBuilder

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

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