Search in sources :

Example 71 with OutputFormat

use of org.dom4j.io.OutputFormat in project plugin-compat-tester by jenkinsci.

the class MavenPom method writeDocument.

private void writeDocument(final File target, final Document doc) throws IOException {
    FileWriter w = new FileWriter(target);
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(w, format);
    try {
        writer.write(doc);
    } finally {
        writer.close();
        w.close();
    }
}
Also used : FileWriter(java.io.FileWriter) OutputFormat(org.dom4j.io.OutputFormat) XMLWriter(org.dom4j.io.XMLWriter)

Example 72 with OutputFormat

use of org.dom4j.io.OutputFormat in project xresloader by xresloader.

the class DataDstXml method dumpConst.

/**
 * 转储常量数据
 * @return 常量数据,不支持的时候返回空
 */
public final byte[] dumpConst(HashMap<String, Object> data) {
    // pretty print
    OutputFormat of = null;
    if (ProgramOptions.getInstance().prettyIndent <= 0) {
        of = OutputFormat.createCompactFormat();
    } else {
        of = OutputFormat.createPrettyPrint();
        of.setIndentSize(ProgramOptions.getInstance().prettyIndent);
    }
    // build xml tree
    Document doc = DocumentHelper.createDocument();
    String encoding = SchemeConf.getInstance().getKey().getEncoding();
    if (null != encoding && false == encoding.isEmpty()) {
        doc.setXMLEncoding(encoding);
        of.setEncoding(encoding);
    }
    doc.setRootElement(DocumentHelper.createElement(ProgramOptions.getInstance().xmlRootName));
    doc.getRootElement().addComment("this file is generated by xresloader, please don't edit it.");
    writeData(doc.getRootElement(), data, "");
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        XMLWriter writer = new XMLWriter(bos, of);
        writer.write(doc);
        return bos.toByteArray();
    } catch (Exception e) {
        ProgramOptions.getLoger().error("write xml failed, %s", e.getMessage());
        return null;
    }
}
Also used : OutputFormat(org.dom4j.io.OutputFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter) ConvException(com.owent.xresloader.data.err.ConvException)

Example 73 with OutputFormat

use of org.dom4j.io.OutputFormat in project syndesis by syndesisio.

the class XmlSchemaHelper method serialize.

public static String serialize(final Document document) {
    try (StringWriter out = new StringWriter()) {
        final OutputFormat format = new OutputFormat(null, false, "UTF-8");
        format.setExpandEmptyElements(false);
        format.setIndent(false);
        final XMLWriter writer = new XMLWriter(out, format);
        writer.write(document);
        writer.flush();
        return out.toString();
    } catch (final IOException e) {
        throw new IllegalStateException("Unable to serialize given document to XML", e);
    }
}
Also used : StringWriter(java.io.StringWriter) OutputFormat(org.dom4j.io.OutputFormat) IOException(java.io.IOException) XMLWriter(org.dom4j.io.XMLWriter)

Example 74 with OutputFormat

use of org.dom4j.io.OutputFormat in project ma-core-public by infiniteautomation.

the class DOM4JConverter method convertOutbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
     */
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    try {
        // Using XSLT to convert to a stream. Setup the source
        if (!(data instanceof Node)) {
            throw new MarshallException(data.getClass());
        }
        Node node = (Node) data;
        OutputFormat outformat = OutputFormat.createCompactFormat();
        outformat.setEncoding("UTF-8");
        // Setup the destination
        StringWriter xml = new StringWriter();
        XMLWriter writer = new XMLWriter(xml, outformat);
        writer.write(node);
        writer.flush();
        xml.flush();
        String script = EnginePrivate.xmlStringToJavascriptDom(xml.toString());
        OutboundVariable ov = new SimpleOutboundVariable(script, outctx, false);
        outctx.put(data, ov);
        return ov;
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(data.getClass(), ex);
    }
}
Also used : StringWriter(java.io.StringWriter) OutboundVariable(org.directwebremoting.extend.OutboundVariable) SimpleOutboundVariable(org.directwebremoting.dwrp.SimpleOutboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) Node(org.dom4j.Node) OutputFormat(org.dom4j.io.OutputFormat) XMLWriter(org.dom4j.io.XMLWriter) SimpleOutboundVariable(org.directwebremoting.dwrp.SimpleOutboundVariable) MarshallException(org.directwebremoting.extend.MarshallException)

Example 75 with OutputFormat

use of org.dom4j.io.OutputFormat in project azure-tools-for-java by Microsoft.

the class XmlUtils method prettyPrintElementNoNamespace.

public static String prettyPrintElementNoNamespace(Element node) {
    removeAllNamespaces(node);
    try {
        final StringWriter out = new StringWriter();
        final OutputFormat format = OutputFormat.createPrettyPrint();
        format.setSuppressDeclaration(true);
        // 4 spaces
        format.setIndent("    ");
        format.setPadText(false);
        final XMLWriter writer = new XMLWriter(out, format);
        writer.write(node);
        writer.flush();
        return StringUtils.stripStart(out.toString(), null);
    } catch (IOException e) {
        throw new RuntimeException("IOException while generating " + "textual representation: " + e.getMessage());
    }
}
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