Example usage for org.dom4j.io SAXReader setStripWhitespaceText

List of usage examples for org.dom4j.io SAXReader setStripWhitespaceText

Introduction

In this page you can find the example usage for org.dom4j.io SAXReader setStripWhitespaceText.

Prototype

public void setStripWhitespaceText(boolean stripWhitespaceText) 

Source Link

Document

Sets whether whitespace between element start and end tags should be ignored.

Usage

From source file:edu.ku.brc.specify.toycode.L18NStringResApp.java

License:Open Source License

/**
 * Reads a DOM from a stream// w  w w . j ava 2s.  c  om
 * @param fileinputStream the stream to be read
 * @return the root element of the DOM
 */
public static org.dom4j.Document readFileToDOM4J(final FileInputStream fileinputStream) throws Exception {
    SAXReader saxReader = new SAXReader();

    saxReader.setValidation(false);
    saxReader.setStripWhitespaceText(true);

    saxReader.setFeature("http://xml.org/sax/features/namespaces", false);
    saxReader.getXMLReader().setFeature("http://xml.org/sax/features/namespaces", false);

    //saxReader.setFeature("http://apache.org/xml/features/validation/schema", false);
    //saxReader.setFeature("http://xml.org/sax/features/validation", false);
    //saxReader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
    //                   (FormViewFactory.class.getResource("../form.xsd")).getPath());

    return saxReader.read(fileinputStream);
}

From source file:org.craftercms.core.store.impl.AbstractFileBasedContentStoreAdapter.java

License:Open Source License

/**
 * Creates and configures an XML SAX reader.
 *//*w  w w .  j a va2  s.  c o m*/
protected SAXReader createXmlReader() {
    SAXReader xmlReader = new SAXReader();
    xmlReader.setMergeAdjacentText(true);
    xmlReader.setStripWhitespaceText(true);
    xmlReader.setIgnoreComments(true);

    try {
        xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
        xmlReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    } catch (SAXException ex) {
        LOGGER.error("Unable to turn off external entity loading, This could be a security risk.", ex);
    }

    return xmlReader;
}

From source file:org.opencms.util.ant.CmsXmlUtils.java

License:Open Source License

/**
 * Helper to unmarshal (read) xml contents from an input source into a document.<p>
 * /*from  w  ww  .j  av  a 2s. c o m*/
 * Using this method ensures that the OpenCms XML entity resolver is used.<p>
 * 
 * Important: The encoding provided will NOT be used during unmarshalling,
 * the XML parser will do this on the base of the information in the source String.
 * The encoding is used for initializing the created instance of the document,
 * which means it will be used when marshalling the document again later.<p>
 *  
 * @param source the XML input source to use
 * @param resolver the XML entity resolver to use
 * @param validate if the reader should try to validate the xml code
 * 
 * @return the unmarshalled XML document
 * 
 * @throws Exception if something goes wrong
 */
public static Document unmarshalHelper(InputSource source, EntityResolver resolver, boolean validate)
        throws Exception {

    SAXReader reader = new SAXReader();
    if (resolver != null) {
        reader.setEntityResolver(resolver);
    }
    reader.setMergeAdjacentText(true);
    reader.setStripWhitespaceText(true);
    if (!validate) {
        reader.setValidation(false);
        reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    }
    return reader.read(source);
}

From source file:org.opencms.xml.CmsXmlUtils.java

License:Open Source License

/**
 * Helper to unmarshal (read) xml contents from an input source into a document.<p>
 * /*  w w w  . ja va2  s.co  m*/
 * Using this method ensures that the OpenCms XML entity resolver is used.<p>
 * 
 * Important: The encoding provided will NOT be used during unmarshalling,
 * the XML parser will do this on the base of the information in the source String.
 * The encoding is used for initializing the created instance of the document,
 * which means it will be used when marshalling the document again later.<p>
 *  
 * @param source the XML input source to use
 * @param resolver the XML entity resolver to use
 * @param validate if the reader should try to validate the xml code
 * 
 * @return the unmarshalled XML document
 * 
 * @throws CmsXmlException if something goes wrong
 */
public static Document unmarshalHelper(InputSource source, EntityResolver resolver, boolean validate)
        throws CmsXmlException {

    try {
        SAXReader reader = new SAXReader();
        if (resolver != null) {
            reader.setEntityResolver(resolver);
        }
        reader.setMergeAdjacentText(true);
        reader.setStripWhitespaceText(true);
        if (!validate) {
            reader.setValidation(false);
            reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        }
        return reader.read(source);
    } catch (DocumentException e) {
        throw new CmsXmlException(Messages.get().container(Messages.ERR_UNMARSHALLING_XML_DOC_0), e);
    } catch (SAXException e) {
        throw new CmsXmlException(Messages.get().container(Messages.ERR_UNMARSHALLING_XML_DOC_0), e);
    }
}

From source file:org.sapia.util.xml.confix.Dom4jProcessor.java

License:Open Source License

/**
 * This method takes an XML stream as input and returns an object
 * representation of the passed-in XML.//w  w w .  jav  a 2 s . c  o m
 *
 * @param is an XML stream
 * @return an object representation of the XML stream.
 * @exception ProcessingException
 */
public Object process(InputStream is) throws ProcessingException {
    try {
        // Build the document from the input stream
        SAXReader builder = new SAXReader();
        builder.setStripWhitespaceText(false);
        builder.setMergeAdjacentText(false);
        Document doc = builder.read(is);

        // Process the document
        Object aResult = process(null, doc.getRootElement());

        return aResult;
    } catch (DocumentException de) {
        String aMessage = "Error parsing the XML of the input stream.";
        throw new ProcessingException(aMessage, de);
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException ioe) {
            String aMessage = "Error closing the input stream to process.";

            throw new ProcessingException(aMessage, ioe);
        }
    }
}

From source file:org.talend.camel.designer.ui.wizards.export.RouteJavaScriptOSGIForESBManager.java

License:Open Source License

private void handleSpringXml(String targetFilePath, ProcessItem processItem, InputStream springInput,
        ExportFileResource osgiResource, boolean convertToBP, boolean convertImports) {

    File targetFile = new File(getTmpFolder() + PATH_SEPARATOR + targetFilePath);
    try {/*from ww  w . j  a v  a2s . c  o m*/
        SAXReader saxReader = new SAXReader();
        saxReader.setStripWhitespaceText(false);
        Document document = saxReader.read(springInput);
        Element root = document.getRootElement();

        if (convertToBP) {
            if ("blueprint".equals(root.getName())) {
                formatSchemaLocation(root, false, false);
                InputStream inputStream = new ByteArrayInputStream(root.asXML().getBytes());
                FilesUtils.copyFile(inputStream, targetFile);
                osgiResource.addResource(FileConstants.BLUEPRINT_FOLDER_NAME, targetFile.toURI().toURL());
                return;
            }

            String bpPrefix = "bp";
            int cnt = 0;
            while (root.getNamespaceForPrefix(bpPrefix) != null) {
                bpPrefix = "bp" + (++cnt);
            }
            root.setQName(QName.get("blueprint", bpPrefix, BLUEPRINT_NSURI));
        }

        Namespace springCamelNsp = Namespace.get("camel", CAMEL_SPRING_NSURI);
        boolean addCamel = springCamelNsp.equals(root.getNamespaceForPrefix("camel"));
        formatSchemaLocation(root, convertToBP, addCamel);

        for (Iterator<?> i = root.elementIterator("import"); i.hasNext();) {
            Element ip = (Element) i.next();
            Attribute resource = ip.attribute("resource");
            URL path = dummyURL(resource.getValue());
            for (ResourceDependencyModel resourceModel : RouteResourceUtil
                    .getResourceDependencies(processItem)) {
                if (matches(path, resourceModel)) {
                    IFile resourceFile = RouteResourceUtil.getSourceFile(resourceModel.getItem());
                    String cpUrl = adaptedClassPathUrl(resourceModel, convertImports);
                    handleSpringXml(cpUrl, processItem, resourceFile.getContents(), osgiResource,
                            convertImports, convertImports);
                    resource.setValue(IMPORT_RESOURCE_PREFIX + cpUrl);
                }
            }
            if (convertImports) {
                i.remove();
            }
        }

        if (CONVERT_CAMEL_CONTEXT) {
            if (CONVERT_CAMEL_CONTEXT_ALL) {
                moveNamespace(root, CAMEL_SPRING_NSURI, CAMEL_BLUEPRINT_NSURI);
            } else {
                Namespace blueprintCamelNsp = Namespace.get("camel", CAMEL_BLUEPRINT_NSURI);
                moveNamespace(root, springCamelNsp, blueprintCamelNsp);
                if (springCamelNsp.equals(root.getNamespaceForPrefix("camel"))) {
                    root.remove(springCamelNsp);
                    root.add(blueprintCamelNsp);
                }
                Namespace springCamelDefNsp = Namespace.get(CAMEL_SPRING_NSURI);
                Namespace blueprintCamelDefNsp = Namespace.get(CAMEL_BLUEPRINT_NSURI);
                for (Iterator<?> i = root.elementIterator("camelContext"); i.hasNext();) {
                    Element cc = (Element) i.next();
                    if (springCamelDefNsp.equals(cc.getNamespace())) {
                        moveNamespace(cc, springCamelDefNsp, blueprintCamelDefNsp);
                    }
                }
            }
        }

        InputStream inputStream = new ByteArrayInputStream(root.asXML().getBytes());
        FilesUtils.copyFile(inputStream, targetFile);
        osgiResource.addResource(adaptedResourceFolderName(targetFilePath, convertToBP),
                targetFile.toURI().toURL());
    } catch (Exception e) {
        Logger.getAnonymousLogger().log(Level.WARNING, "Custom Spring to OSGi conversion failed. ", e);
    } finally {
        try {
            springInput.close();
        } catch (IOException e) {
            Logger.getAnonymousLogger().log(Level.WARNING, "Unexpected File closing failure. ", e);
        }
    }
}

From source file:org.xwiki.store.serialization.xml.internal.AbstractXMLSerializer.java

License:Open Source License

/**
 * {@inheritDoc}/*from  w w w  .  ja va 2  s  .  com*/
 *
 * @see org.xwiki.store.filesystem.internal.XMLSerializer#parse(InputStream)
 */
public P parse(final InputStream stream) throws IOException {
    final SAXReader reader = new SAXReader();

    // Remove nodes generated by indentation.
    reader.setStripWhitespaceText(true);
    reader.setMergeAdjacentText(true);

    final Document domdoc;
    try {
        domdoc = reader.read(stream);
    } catch (DocumentException e) {
        throw new IOException("Failed to parse XML, probably malformed input.");
    }
    return this.parse(domdoc.getRootElement());
}