Example usage for javax.xml.parsers DocumentBuilderFactory setIgnoringComments

List of usage examples for javax.xml.parsers DocumentBuilderFactory setIgnoringComments

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilderFactory setIgnoringComments.

Prototype


public void setIgnoringComments(boolean ignoreComments) 

Source Link

Document

Specifies that the parser produced by this code will ignore comments.

Usage

From source file:org.fosstrak.epcis.repository.capture.CaptureOperationsModule.java

/**
 * Parses the input into a DOM. If a schema is given, the input is also
 * validated against this schema. The schema may be null.
 *///w  ww.jav  a 2  s.c  o  m
private Document parseInput(InputStream in, Schema schema)
        throws InternalBusinessException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setIgnoringComments(true);
    factory.setIgnoringElementContentWhitespace(true);
    factory.setSchema(schema);
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setErrorHandler(new ErrorHandler() {
            public void warning(SAXParseException e) throws SAXException {
                LOG.warn("warning while parsing XML input: " + e.getMessage());
            }

            public void fatalError(SAXParseException e) throws SAXException {
                LOG.error("non-recovarable error while parsing XML input: " + e.getMessage());
                throw e;
            }

            public void error(SAXParseException e) throws SAXException {
                LOG.error("error while parsing XML input: " + e.getMessage());
                throw e;
            }
        });
        Document document = builder.parse(in);
        LOG.debug("payload successfully parsed as XML document");
        if (LOG.isDebugEnabled()) {
            logDocument(document);
        }
        return document;
    } catch (ParserConfigurationException e) {
        throw new InternalBusinessException("unable to configure document builder to parse XML input", e);
    }
}

From source file:org.fusesource.cloudmix.agent.jbi.JBIInstallerAgent.java

private Document parse(String result) throws ParserConfigurationException, SAXException, IOException {

    LOGGER.debug("Parsing XML Document:\n" + result);

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//from  w w w .ja v a2 s. co m
    factory.setIgnoringElementContentWhitespace(true);
    factory.setIgnoringComments(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new InputSource(new StringReader(result)));
}

From source file:org.geoserver.security.xml.XMLRoleService.java

public XMLRoleService() throws IOException {
    super();/*w  ww.  ja  v  a 2  s  . c  o  m*/
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setIgnoringComments(true);
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new IOException(e);
    }
}

From source file:org.geoserver.security.xml.XMLUserGroupService.java

public XMLUserGroupService() throws IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//  w ww  .j  av  a 2 s. c o  m
    factory.setIgnoringComments(true);
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new IOException(e);
    }
}

From source file:org.gluu.oxtrust.ldap.service.Shibboleth2ConfService.java

/**
 * @param stream/*from  ww  w  . j a  v  a2  s . c o m*/
 * @throws IOException
 * @throws SAXException
 * @throws ParserConfigurationException
 */
public synchronized GluuErrorHandler validateMetadata(InputStream stream)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory newFactory = DocumentBuilderFactory.newInstance();
    newFactory.setCoalescing(false);
    newFactory.setExpandEntityReferences(true);
    newFactory.setIgnoringComments(false);

    newFactory.setIgnoringElementContentWhitespace(false);
    newFactory.setNamespaceAware(true);
    newFactory.setValidating(false);
    DocumentBuilder xmlParser = newFactory.newDocumentBuilder();
    Document xmlDoc = xmlParser.parse(stream);
    String schemaDir = System.getProperty("catalina.home") + File.separator + "conf" + File.separator
            + "shibboleth2" + File.separator + "idp" + File.separator + "schema" + File.separator;
    Schema schema = SchemaBuilder.buildSchema(SchemaLanguage.XML, schemaDir);
    Validator validator = schema.newValidator();
    GluuErrorHandler handler = new GluuErrorHandler();
    validator.setErrorHandler(handler);
    validator.validate(new DOMSource(xmlDoc));

    return handler;

}

From source file:org.jboss.dashboard.displayer.AbstractDataDisplayerXMLFormat.java

public DataDisplayer parse(String xml, ImportResults results) throws Exception {
    DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
    dFactory.setIgnoringComments(true);
    StringReader isr = new StringReader(xml);
    Document doc = dBuilder.parse(new InputSource(isr));
    isr.close();//www .  j  a va2 s  .c  o  m
    return parse(doc.getChildNodes(), results);
}

From source file:org.jboss.dashboard.export.ImportManagerImpl.java

protected DocumentBuilder createDocumentBuilder() throws Exception {
    DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
    dFactory.setIgnoringComments(true);

    // BZ-1211316: XXE/SSRF vulnerability
    dFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
    dFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    dFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);

    return dFactory.newDocumentBuilder();
}

From source file:org.jboss.loom.utils.as7.AS7ModuleUtils.java

private static Document createDoc(String namespace, String rootElmName) throws ParserConfigurationException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    // Need specifically Xerces as it treats namespaces better way.
    /*DocumentBuilderFactory domFactory;
    try {/* ww w.jav a2  s . co  m*/
    domFactory = (DocumentBuilderFactory) Class.forName("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl").newInstance();
    } catch( ClassNotFoundException | InstantiationException | IllegalAccessException ex ) {
    throw new IllegalStateException("JDK's DocumentBuilderFactoryImpl not found:\n    " + ex.getMessage(), ex );
    }*/
    //DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl", ClassLoader.getSystemClassLoader());

    domFactory.setIgnoringComments(true);
    domFactory.setNamespaceAware(false);
    domFactory.setValidating(false);
    DocumentBuilder builder = domFactory.newDocumentBuilder();

    Document doc = builder.getDOMImplementation().createDocument(namespace, rootElmName, null);// rootElmName
    return doc;
}

From source file:org.josso.tooling.gshell.install.installer.VFSInstaller.java

protected Document readContentAsDom(FileObject file, boolean nameSpaceAware) throws Exception {
    InputStream is = null;/*from   w w w  .j  a  va  2  s .  c om*/

    try {
        is = file.getContent().getInputStream();

        DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance();
        parserFactory.setValidating(false);
        parserFactory.setNamespaceAware(nameSpaceAware);
        parserFactory.setIgnoringElementContentWhitespace(false);
        parserFactory.setIgnoringComments(false);

        DocumentBuilder builder = parserFactory.newDocumentBuilder();

        boolean dtdNotFound = false;
        Document doc = null;
        try {
            doc = builder.parse(is);
        } catch (FileNotFoundException e) {
            dtdNotFound = true;
        }

        // if dtd doesn't exist parse the document again without trying to load dtd
        if (dtdNotFound) {
            is = file.getContent().getInputStream();
            // disable dtd loading
            parserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            builder = parserFactory.newDocumentBuilder();
            doc = builder.parse(is);
        }

        DocumentType docType = doc.getDoctype();

        if (log.isDebugEnabled() && docType != null) {
            log.debug("docType.getPublicId()=" + docType.getPublicId());
            log.debug("docType.getSystemId()=" + docType.getSystemId());
        }

        return doc;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw e;
    } finally {
        if (is != null)
            try {
                is.close();
            } catch (IOException e) {
                /**/}
    }

}

From source file:org.kapott.hbci.tools.TransactionsToXML.java

public Document createXMLDocument(List<UmsLine> transactions, String rawMT940) {
    // Empfangene Transaktionen als XML-Datei aufbereiten
    DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
    fac.setIgnoringComments(true);
    fac.setValidating(false);//from  w  w w  . j av  a2s  .c  om

    // create document
    DocumentBuilder builder;
    try {
        builder = fac.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
    Document doc = builder.newDocument();

    Element root = doc.createElement("account_transactions");
    doc.appendChild(root);

    // <transactions>
    if (transactions != null) {
        Element transElement = doc.createElement("transactions");
        root.appendChild(transElement);
        createTransactionElements(doc, transElement, transactions);
    }

    // <raw>
    if (rawMT940 != null) {
        Element rawElem = doc.createElement("raw");
        root.appendChild(rawElem);

        try {
            String mt940_encoded = Base64.encodeBase64String(rawMT940.getBytes("ISO-8859-1"));
            rawElem.appendChild(doc.createCDATASection(mt940_encoded));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    return doc;
}