Example usage for javax.xml.parsers DocumentBuilderFactory setIgnoringElementContentWhitespace

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

Introduction

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

Prototype


public void setIgnoringElementContentWhitespace(boolean whitespace) 

Source Link

Document

Specifies that the parsers created by this factory must eliminate whitespace in element content (sometimes known loosely as 'ignorable whitespace') when parsing XML documents (see XML Rec 2.10).

Usage

From source file:org.apache.jk.config.WebXml2Jk.java

public static Document readXml(File xmlF) throws SAXException, IOException, ParserConfigurationException {
    if (!xmlF.exists()) {
        log.error("No xml file " + xmlF);
        return null;
    }//from   ww  w. j a v a  2s  . c  o m
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    //dbf.setCoalescing(true);
    //dbf.setExpandEntityReferences(true);

    DocumentBuilder db = null;
    db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());

    // db.setErrorHandler( new MyErrorHandler());

    Document doc = db.parse(xmlF);
    return doc;
}

From source file:org.apache.jmeter.functions.ComponentReferenceFunctionTest.java

private Element getBodyFromXMLDocument(InputStream stream)
        throws ParserConfigurationException, FileNotFoundException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setIgnoringComments(true);//from  w w  w  . ja va  2s  .co  m
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new InputSource(stream));
    org.w3c.dom.Element root = doc.getDocumentElement();
    org.w3c.dom.Element body = (org.w3c.dom.Element) root.getElementsByTagName("body").item(0);
    return body;
}

From source file:org.apache.jmeter.junit.JMeterTest.java

/**
 * @return/* w  w w.j a v  a  2 s .  c  o  m*/
 * @throws ParserConfigurationException
 * @throws IOException 
 * @throws SAXException 
 * @throws FileNotFoundException 
 */
private Element getBodyFromXMLDocument(InputStream stream)
        throws ParserConfigurationException, FileNotFoundException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setIgnoringComments(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new InputSource(stream));
    org.w3c.dom.Element root = doc.getDocumentElement();
    org.w3c.dom.Element body = (org.w3c.dom.Element) root.getElementsByTagName("body").item(0);
    return body;
}

From source file:org.apache.myfaces.shared_impl.webapp.webxml.WebXmlParser.java

public WebXml parse() {
    _webXml = new WebXml();

    try {/*from w  w  w  .j  a va  2  s .  c om*/
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setIgnoringElementContentWhitespace(true);
        dbf.setIgnoringComments(true);
        dbf.setNamespaceAware(true);
        dbf.setValidating(false);
        //            dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);

        DocumentBuilder db = dbf.newDocumentBuilder();
        db.setEntityResolver(new _EntityResolver());
        db.setErrorHandler(new MyFacesErrorHandler(log));

        InputSource is = createContextInputSource(null, WEB_XML_PATH);

        if (is == null) {
            URL url = _context.getResource(WEB_XML_PATH);
            log.debug("No web-xml found at : " + (url == null ? " null " : url.toString()));
            return _webXml;
        }

        Document document = db.parse(is);

        Element webAppElem = document.getDocumentElement();
        if (webAppElem == null || !webAppElem.getNodeName().equals("web-app")) {
            throw new FacesException("No valid web-app root element found!");
        }

        readWebApp(webAppElem);

        return _webXml;
    } catch (Exception e) {
        log.fatal("Unable to parse web.xml", e);
        throw new FacesException(e);
    }
}

From source file:org.apache.rahas.impl.util.SAMLUtilsTest.java

private static boolean equals(String element1, String element2)
        throws ParserConfigurationException, IOException, SAXException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//from w  w  w .ja  v  a2  s.co  m
    dbf.setCoalescing(true);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setIgnoringComments(true);
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document doc1 = db.parse(new ByteArrayInputStream(element1.getBytes("UTF-8")));
    doc1.normalizeDocument();

    Document doc2 = db.parse(new ByteArrayInputStream(element1.getBytes("UTF-8")));
    doc2.normalizeDocument();

    return doc1.isEqualNode(doc2);
}

From source file:org.apache.servicemix.jbi.deployer.utils.ManagementSupport.java

private static Document parse(String result) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//from   www  .  j  a va  2s .  com
    factory.setIgnoringElementContentWhitespace(true);
    factory.setIgnoringComments(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new InputSource(new StringReader(result)));
}

From source file:org.apache.sling.urlrewriter.internal.SlingUrlRewriteFilter.java

private Document createDocument(final String rules) {
    if (StringUtils.isBlank(rules)) {
        logger.warn("given rules are blank");
        return null;
    }/*  w w  w  .j av a  2  s .co  m*/

    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    factory.setNamespaceAware(true);
    factory.setIgnoringComments(true);
    factory.setIgnoringElementContentWhitespace(true);

    try {
        final String systemId = "";
        final ConfHandler confHandler = new ConfHandler(systemId);
        final DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        documentBuilder.setErrorHandler(confHandler);
        documentBuilder.setEntityResolver(confHandler);

        final InputStream inputStream = new ByteArrayInputStream(rules.getBytes("UTF-8"));
        final Document document = documentBuilder.parse(inputStream); // , systemId);
        IOUtils.closeQuietly(inputStream);
        return document;
    } catch (Exception e) {
        logger.error("error creating document from rules property", e);
        return null;
    }
}

From source file:org.codelibs.robot.transformer.impl.XmlTransformer.java

@Override
public ResultData transform(final ResponseData responseData) {
    if (responseData == null || responseData.getResponseBody() == null) {
        throw new RobotCrawlAccessException("No response body.");
    }/*from  ww  w  .j  a  va  2  s  .  c o  m*/

    final File tempFile = ResponseDataUtil.createResponseBodyFile(responseData);

    FileInputStream fis = null;

    try {
        fis = new FileInputStream(tempFile);
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        for (final Map.Entry<String, Object> entry : attributeMap.entrySet()) {
            factory.setAttribute(entry.getKey(), entry.getValue());
        }

        for (final Map.Entry<String, String> entry : featureMap.entrySet()) {
            factory.setFeature(entry.getKey(), "true".equalsIgnoreCase(entry.getValue()));
        }

        factory.setCoalescing(coalescing);
        factory.setExpandEntityReferences(expandEntityRef);
        factory.setIgnoringComments(ignoringComments);
        factory.setIgnoringElementContentWhitespace(ignoringElementContentWhitespace);
        factory.setNamespaceAware(namespaceAware);
        factory.setValidating(validating);
        factory.setXIncludeAware(includeAware);

        final DocumentBuilder builder = factory.newDocumentBuilder();

        final Document doc = builder.parse(fis);

        final StringBuilder buf = new StringBuilder(1000);
        buf.append(getResultDataHeader());
        for (final Map.Entry<String, String> entry : fieldRuleMap.entrySet()) {
            final List<String> nodeStrList = new ArrayList<String>();
            try {
                final NodeList nodeList = getNodeList(doc, entry.getValue());
                for (int i = 0; i < nodeList.getLength(); i++) {
                    final Node node = nodeList.item(i);
                    nodeStrList.add(node.getTextContent());
                }
            } catch (final TransformerException e) {
                logger.warn("Could not parse a value of " + entry.getKey() + ":" + entry.getValue(), e);
            }
            if (nodeStrList.size() == 1) {
                buf.append(getResultDataBody(entry.getKey(), nodeStrList.get(0)));
            } else if (nodeStrList.size() > 1) {
                buf.append(getResultDataBody(entry.getKey(), nodeStrList));
            }
        }
        buf.append(getAdditionalData(responseData, doc));
        buf.append(getResultDataFooter());

        final ResultData resultData = new ResultData();
        resultData.setTransformerName(getName());

        try {
            resultData.setData(buf.toString().getBytes(charsetName));
        } catch (final UnsupportedEncodingException e) {
            if (logger.isInfoEnabled()) {
                logger.info("Invalid charsetName: " + charsetName + ". Changed to " + Constants.UTF_8, e);
            }
            charsetName = Constants.UTF_8_CHARSET.name();
            resultData.setData(buf.toString().getBytes(Constants.UTF_8_CHARSET));
        }
        resultData.setEncoding(charsetName);

        return resultData;
    } catch (final RobotSystemException e) {
        throw e;
    } catch (final Exception e) {
        throw new RobotSystemException("Could not store data.", e);
    } finally {
        IOUtils.closeQuietly(fis);
        // clean up
        if (!tempFile.delete()) {
            logger.warn("Could not delete a temp file: " + tempFile);
        }
    }
}

From source file:org.codice.ddf.cxf.SecureCxfClientFactoryTest.java

private Element getAssertionElement() throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);/*  ww  w  .  j a  v a2  s .c om*/
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);

    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setEntityResolver(new DOMUtils.NullResolver());

    return db.parse(SecureCxfClientFactoryTest.class.getResourceAsStream("/SAMLAssertion.xml"))
            .getDocumentElement();
}

From source file:org.codice.ddf.security.common.jaxrs.RestSecurityTest.java

public static Document readXml(InputStream is) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);//from   ww w .  j a v  a2s.  co m
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);
    // dbf.setCoalescing(true);
    // dbf.setExpandEntityReferences(true);

    DocumentBuilder db = null;
    db = dbf.newDocumentBuilder();
    db.setEntityResolver(new DOMUtils.NullResolver());

    // db.setErrorHandler( new MyErrorHandler());

    return db.parse(is);
}