Example usage for org.dom4j.io SAXReader setMergeAdjacentText

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

Introduction

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

Prototype

public void setMergeAdjacentText(boolean mergeAdjacentText) 

Source Link

Document

Sets whether or not adjacent text nodes should be merged together when parsing.

Usage

From source file:com.cladonia.xml.webservice.soap.SOAPDialog.java

License:Open Source License

private String format(Reader reader) throws SAXParseException, IOException {
    SAXReader sax = XMLUtilities.createReader(false);
    sax.setStripWhitespaceText(false);//from  ww  w.  j av a  2s  .c om
    sax.setMergeAdjacentText(false);

    XDocument doc = XMLUtilities.parse(sax, new BufferedReader(reader), null);

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    String indent = "";

    ExchangerOutputFormat format = new ExchangerOutputFormat();
    format.setEncoding(doc.getEncoding());

    XMLFormatter formatter = new XMLFormatter(out, format);

    EditorProperties properties = config.getEditorProperties();
    switch (properties.getFormatType()) {
    case EditorProperties.FORMAT_CUSTOM:
        if (properties.isCustomIndent()) {
            format.setIndent(TextPreferences.getTabString());
        } else {
            format.setIndent("");
        }

        format.setNewlines(properties.isCustomNewline());
        format.setPadText(properties.isCustomPadText());

        formatter.setTrimText(properties.isCustomStrip());
        formatter.setPreserveMixedContent(properties.isCustomPreserveMixedContent());
        break;
    case EditorProperties.FORMAT_COMPACT:
        format.setIndent("");
        format.setNewlines(false);
        format.setPadText(false);

        formatter.setTrimText(true);
        formatter.setPreserveMixedContent(false);
        break;
    case EditorProperties.FORMAT_STANDARD:
        format.setIndent(TextPreferences.getTabString());

        format.setNewlines(true);
        format.setPadText(false);

        formatter.setTrimText(true);
        formatter.setPreserveMixedContent(true);
        break;
    }

    formatter.write(doc);

    return out.toString(doc.getEncoding());
}

From source file:com.cladonia.xml.XMLUtilities.java

License:Open Source License

/**
 * Creates a new SAXReader./*ww w. jav  a2 s .c  om*/
 *
 * @param validate when true the reader validates the input.
 *
 * @return the reader.
 */
public static SAXReader createReader(boolean validate, boolean loadExternalDTD) {
    SAXReader reader = new SAXReader(XDocumentFactory.getInstance(), validate);

    reader.setStripWhitespaceText(false);
    reader.setMergeAdjacentText(true);
    //      reader.setMergeAdjacentText( true);

    if (!validate) {
        reader.setIncludeExternalDTDDeclarations(false);
        reader.setIncludeInternalDTDDeclarations(true);

        try {
            if (loadExternalDTD) {
                reader.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", true);
                //               System.out.println( "http://apache.org/xml/features/nonvalidating/load-external-dtd = "+reader.getXMLReader().getFeature( "http://apache.org/xml/features/nonvalidating/load-external-dtd"));
                reader.setEntityResolver(getCatalogResolver());
            } else {
                reader.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
                reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        try {
            reader.getXMLReader().setFeature("http://apache.org/xml/features/validation/schema", true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return reader;
}

From source file:com.cladonia.xml.XMLUtilities.java

License:Open Source License

/**
 * Creates a new SAXReader.//from   w w w.  j  a va  2 s . c  o m
 *
 * @param validate when true the reader validates the input.
 *
 * @return the reader.
 */
public static SAXReader createReader(boolean validate, boolean loadExternalDTD, boolean stripWhiteSpace) {
    SAXReader reader = new SAXReader(XDocumentFactory.getInstance(), validate);

    reader.setStripWhitespaceText(stripWhiteSpace);
    reader.setMergeAdjacentText(true);
    //      reader.setMergeAdjacentText( true);

    if (!validate) {
        reader.setIncludeExternalDTDDeclarations(false);
        reader.setIncludeInternalDTDDeclarations(true);

        try {
            if (loadExternalDTD) {
                reader.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", true);
                //               System.out.println( "http://apache.org/xml/features/nonvalidating/load-external-dtd = "+reader.getXMLReader().getFeature( "http://apache.org/xml/features/nonvalidating/load-external-dtd"));
                reader.setEntityResolver(getCatalogResolver());
            } else {
                reader.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
                reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        try {
            reader.getXMLReader().setFeature("http://apache.org/xml/features/validation/schema", true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return reader;
}

From source file:com.cladonia.xml.XMLUtilities.java

License:Open Source License

/**
 * Parses the document for this URL, does not substitute & 
 * and does resolve all XIncludes./*from  ww w .ja  v a2 s .  c  o m*/
 *
 * @param url the URL of the document.
 *
 * @return the Dom4J document.
 */
public static synchronized XDocument parseWithoutSubstitution(URL url) throws IOException, SAXParseException {
    if (DEBUG)
        System.out.println("XMLUtilities.parseWithoutSubstitution( " + url + ")");
    XDocument document = null;
    InputStream stream = URLUtilities.open(url);
    XMLReader reader = new XMLReader(stream);

    // create parser that resolves XIncludes.
    SAXParser parser = new SAXParser(new XIncludeParserConfiguration());
    SAXReader saxer = new SAXReader(parser);

    // create parser that does not resolves XIncludes.
    // SAXReader saxer = createReader( false);
    saxer.setStripWhitespaceText(true);
    saxer.setMergeAdjacentText(true);

    try {
        document = parse(saxer, reader, url.toString());
    } finally {
        stream.close();
        reader.close();
    }

    return document;
}

From source file:com.cladonia.xml.XMLUtilities.java

License:Open Source License

public static String format(String text, String systemId, String encoding, String indent, boolean newLines,
        boolean padText, int lineLength, boolean trim, boolean preserveMixed, ExchangerOutputFormat format)
        throws IOException, SAXParseException {

    ByteArrayInputStream stream = new ByteArrayInputStream(text.getBytes(mapXMLEncodingToJava(encoding)));
    InputStreamReader reader = new InputStreamReader(stream, mapXMLEncodingToJava(encoding));

    SAXReader sax = createReader(false, false);
    sax.setStripWhitespaceText(false);//from  w w w .  ja v  a2 s .  co m
    sax.setMergeAdjacentText(false);

    XDocument doc = XMLUtilities.parse(sax, new BufferedReader(reader), systemId);

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    //      OutputFormat format = new OutputFormat();
    //      format.setEncoding( doc.getEncoding());

    XMLFormatter formatter = new XMLFormatter(out, format);

    format.setIndent(indent);
    format.setNewlines(newLines);
    format.setPadText(padText);
    formatter.setMaxLineLength(lineLength);
    formatter.setTrimText(trim);
    formatter.setPreserveMixedContent(preserveMixed);

    formatter.write(doc);
    formatter.flush();

    text = out.toString(mapXMLEncodingToJava(encoding));

    stream = new ByteArrayInputStream(text.getBytes(mapXMLEncodingToJava(encoding)));
    reader = new InputStreamReader(stream, mapXMLEncodingToJava(encoding));

    sax = createReader(false, false);
    sax.setStripWhitespaceText(false);
    sax.setMergeAdjacentText(false);

    doc = XMLUtilities.parse(sax, new BufferedReader(reader), systemId);

    out = new ByteArrayOutputStream();
    ExchangerOutputFormat format2 = new ExchangerOutputFormat("", false, encoding);

    if (!format.isSuppressDeclaration()) {
        format2.setStandalone(format.getStandalone());
        format2.setOmitStandalone(format.isOmitStandalone());

        format2.setVersion(format.getVersion());
        format2.setOmitEncoding(format.isOmitEncoding());
        format2.setSuppressDeclaration(false);
    } else {
        format2.setSuppressDeclaration(true);
    }

    ExchangerXMLWriter writer = new ExchangerXMLWriter(out, format2);
    writer.write(doc);
    writer.flush();

    return out.toString(mapXMLEncodingToJava(encoding));
}

From source file:org.cgiar.ccafs.ap.util.ClientRepository.java

License:Open Source License

protected static Element getDocumentRoot(InputStreamReader stream) {
    try {//from  ww  w .java 2 s  .c o m
        SAXReader saxReader = new SAXReader();
        saxReader.setEntityResolver(new DTDEntityResolver());
        saxReader.setMergeAdjacentText(true);
        return saxReader.read(stream).getRootElement();
    } catch (DocumentException de) {
        throw new RuntimeException(de);
    }
}

From source file:org.cgiar.ccafs.marlo.utils.ClientRepository.java

License:Open Source License

protected static Element getDocumentRoot(InputStreamReader stream) {
    try {//from w w w  . ja  v  a  2  s .  c o  m
        SAXReader saxReader = new SAXReader();
        saxReader.setEntityResolver(new org.hibernate.internal.util.xml.DTDEntityResolver());
        saxReader.setMergeAdjacentText(true);
        return saxReader.read(stream).getRootElement();
    } catch (DocumentException de) {
        throw new RuntimeException(de);
    }
}

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

License:Open Source License

/**
 * Creates and configures an XML SAX reader.
 *///from  w w w  . jav a  2 s . com
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.craftercms.search.service.impl.SolrDocumentBuilder.java

License:Open Source License

protected SAXReader createSAXReader() {
    SAXReader reader = new SAXReader();
    reader.setEncoding(CharEncoding.UTF_8);
    reader.setMergeAdjacentText(true);

    return reader;
}

From source file:org.craftercms.search.service.impl.SolrDocumentBuilderImpl.java

License:Open Source License

protected SAXReader createSAXReader() {
    SAXReader reader = new SAXReader();
    reader.setEncoding(CharEncoding.UTF_8);
    reader.setMergeAdjacentText(true);
    try {/*from   w ww.  j a va  2s. com*/
        reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
        reader.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 reader;
}