Search in sources :

Example 81 with OutputFormat

use of org.dom4j.io.OutputFormat in project core by craftercms.

the class CrafterXStreamMarshaller method marshalWriter.

/**
 * Just as super(), but instead of a {@link com.thoughtworks.xstream.io.xml.CompactWriter} creates a {@link
 * EscapingCompactWriter}.
 * Also if the object graph is a Dom4j document, the document is written directly instead of using XStream.
 */
@Override
public void marshalWriter(Object graph, Writer writer, DataHolder dataHolder) throws XmlMappingException, IOException {
    if (graph instanceof Document) {
        OutputFormat outputFormat = OutputFormat.createCompactFormat();
        outputFormat.setSuppressDeclaration(suppressXmlDeclaration);
        XMLWriter xmlWriter = new XMLWriter(writer, outputFormat);
        try {
            xmlWriter.write((Document) graph);
        } finally {
            try {
                xmlWriter.flush();
            } catch (Exception ex) {
                logger.debug("Could not flush XMLWriter", ex);
            }
        }
    } else {
        if (!suppressXmlDeclaration) {
            writer.write(XML_DECLARATION);
        }
        HierarchicalStreamWriter streamWriter = new EscapingCompactWriter(writer);
        try {
            getXStream().marshal(graph, streamWriter, dataHolder);
        } catch (Exception ex) {
            throw convertXStreamException(ex, true);
        } finally {
            try {
                streamWriter.flush();
            } catch (Exception ex) {
                logger.debug("Could not flush HierarchicalStreamWriter", ex);
            }
        }
    }
}
Also used : HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) OutputFormat(org.dom4j.io.OutputFormat) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter) IOException(java.io.IOException) XmlMappingException(org.springframework.oxm.XmlMappingException)

Example 82 with OutputFormat

use of org.dom4j.io.OutputFormat in project atlas by alibaba.

the class XmlHelper method saveDocument.

public static void saveDocument(Document document, File file) throws IOException {
    file.getParentFile().mkdirs();
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");
    saveFile(document, format, file);
}
Also used : OutputFormat(org.dom4j.io.OutputFormat)

Example 83 with OutputFormat

use of org.dom4j.io.OutputFormat in project zm-mailbox by Zimbra.

the class DavResource method getPropertiesAsText.

protected String getPropertiesAsText(DavContext ctxt) throws IOException {
    Element e = org.dom4j.DocumentHelper.createElement(DavElements.E_PROP);
    for (ResourceProperty rp : mProps.values()) rp.toElement(ctxt, e, false);
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setTrimText(false);
    format.setOmitEncoding(false);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLWriter writer = new XMLWriter(baos, format);
    writer.write(e);
    return new String(baos.toByteArray());
}
Also used : ResourceProperty(com.zimbra.cs.dav.property.ResourceProperty) Element(org.dom4j.Element) OutputFormat(org.dom4j.io.OutputFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XMLWriter(org.dom4j.io.XMLWriter)

Example 84 with OutputFormat

use of org.dom4j.io.OutputFormat in project zm-mailbox by Zimbra.

the class DomUtil method writeDocumentToStream.

public static void writeDocumentToStream(Document doc, OutputStream out) throws IOException {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setTrimText(false);
    format.setOmitEncoding(false);
    XMLWriter writer = new XMLWriter(out, format);
    writer.write(doc);
}
Also used : OutputFormat(org.dom4j.io.OutputFormat) XMLWriter(org.dom4j.io.XMLWriter)

Example 85 with OutputFormat

use of org.dom4j.io.OutputFormat in project zm-mailbox by Zimbra.

the class DomUtil method toString.

/**
 * Convert an Element to a String.
 */
public static String toString(Element env, boolean prettyPrint) {
    if (prettyPrint) {
        StringWriter buff = new StringWriter();
        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer = new XMLWriter(buff, format);
            writer.write(env);
            writer.close();
        } catch (IOException e) {
        // ignore, since StringWriter doesn't throw IOExceptions
        }
        return buff.toString();
    } else {
        return env.asXML();
    }
}
Also used : StringWriter(java.io.StringWriter) OutputFormat(org.dom4j.io.OutputFormat) IOException(java.io.IOException) XMLWriter(org.dom4j.io.XMLWriter)

Aggregations

OutputFormat (org.dom4j.io.OutputFormat)89 XMLWriter (org.dom4j.io.XMLWriter)75 IOException (java.io.IOException)39 Document (org.dom4j.Document)31 Element (org.dom4j.Element)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)18 StringWriter (java.io.StringWriter)18 SAXReader (org.dom4j.io.SAXReader)16 File (java.io.File)15 FileOutputStream (java.io.FileOutputStream)14 FileWriter (java.io.FileWriter)10 OutputStreamWriter (java.io.OutputStreamWriter)8 StringReader (java.io.StringReader)6 DocumentException (org.dom4j.DocumentException)6 OutputStream (java.io.OutputStream)5 Test (org.junit.Test)5 BufferedOutputStream (java.io.BufferedOutputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 MalformedURLException (java.net.MalformedURLException)4 ArrayList (java.util.ArrayList)4