Search in sources :

Example 56 with OutputFormat

use of org.dom4j.io.OutputFormat in project xwiki-platform by xwiki.

the class XMLWriterTest method testBase64.

@Test
public void testBase64() throws Exception {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final OutputFormat of = new OutputFormat(" ", true, "UTF-8");
    this.writer = new XMLWriter(baos, of);
    this.writer.startDocument();
    this.writer.writeOpen(new DOMElement("root"));
    this.writer.writeOpen(new DOMElement("doc"));
    this.writer.writeOpen(new DOMElement("obj"));
    this.writer.writeBase64(new DOMElement("prop"), new ByteArrayInputStream(BASE64_INPUT.getBytes("UTF-8")));
    this.writer.writeClose(new DOMElement("root"));
    this.writer.endDocument();
    Assert.assertEquals("Incorrect response from testBase64.", BASE64_TEST, new String(baos.toByteArray(), "UTF-8"));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) OutputFormat(org.dom4j.io.OutputFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DOMElement(org.dom4j.dom.DOMElement) Test(org.junit.Test)

Example 57 with OutputFormat

use of org.dom4j.io.OutputFormat in project xwiki-platform by xwiki.

the class XMLWriterTest method testWriteClose.

/**
 * Make sure writeClose closes internediet nodes.
 */
@Test
public void testWriteClose() throws Exception {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final OutputFormat of = new OutputFormat(" ", true, "UTF-8");
    this.writer = new XMLWriter(baos, of);
    this.writer.startDocument();
    this.writer.writeOpen(new DOMElement("root"));
    this.writer.writeOpen(new DOMElement("doc"));
    this.writer.writeOpen(new DOMElement("obj"));
    this.writer.writeOpen(new DOMElement("prop"));
    this.writer.writeClose(new DOMElement("root"));
    this.writer.endDocument();
    Assert.assertEquals("WriteClose didn't write the correct response.", TEST_CONTENT, new String(baos.toByteArray(), "UTF-8"));
}
Also used : OutputFormat(org.dom4j.io.OutputFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DOMElement(org.dom4j.dom.DOMElement) Test(org.junit.Test)

Example 58 with OutputFormat

use of org.dom4j.io.OutputFormat in project my_curd by qinyou.

the class ToolFormatXml method formatXML.

public static String formatXML(String inputXML) {
    String requestXML = null;
    Document document = null;
    try {
        SAXReader reader = new SAXReader();
        document = reader.read(new StringReader(inputXML));
    } catch (DocumentException e1) {
        e1.printStackTrace();
    }
    if (document != null) {
        XMLWriter writer = null;
        try {
            StringWriter stringWriter = new StringWriter();
            OutputFormat format = new OutputFormat("    ", true);
            writer = new XMLWriter(stringWriter, format);
            writer.write(document);
            writer.flush();
            requestXML = stringWriter.getBuffer().toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                }
            }
        }
    }
    return requestXML;
}
Also used : StringWriter(java.io.StringWriter) SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) StringReader(java.io.StringReader) OutputFormat(org.dom4j.io.OutputFormat) IOException(java.io.IOException) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter)

Example 59 with OutputFormat

use of org.dom4j.io.OutputFormat in project archiva by apache.

the class XMLWriter method write.

/**
 * Write the Document to the provided Writer, with an option to close the writer upon completion.
 *
 * @param doc the document to write.
 * @param writer the writer to write to.
 * @param close true to close the writer on completion.
 * @throws XMLException if there was a problem writing the xml to the writer.
 */
public static void write(Document doc, Writer writer, boolean close) throws XMLException {
    org.dom4j.io.XMLWriter xmlwriter = null;
    try {
        OutputFormat outputFormat = OutputFormat.createPrettyPrint();
        xmlwriter = new org.dom4j.io.XMLWriter(writer, outputFormat);
        xmlwriter.write(doc);
        xmlwriter.flush();
    } catch (IOException e) {
        throw new XMLException("Unable to write xml contents to writer: " + e.getMessage(), e);
    } finally {
        if (close && (xmlwriter != null)) {
            try {
                xmlwriter.close();
            } catch (IOException e) {
            /* quietly ignore */
            }
        }
    }
}
Also used : OutputFormat(org.dom4j.io.OutputFormat) IOException(java.io.IOException)

Example 60 with OutputFormat

use of org.dom4j.io.OutputFormat in project tdq-studio-se by Talend.

the class AliasManager method saveAliases.

/**
 * Saves all the Aliases to the users preferences
 */
public void saveAliases() throws ExplorerException {
    DefaultElement root = new DefaultElement(Alias.ALIASES);
    for (Alias alias : aliases.values()) {
        root.add(alias.describeAsXml());
    }
    try {
        FileWriter writer = new FileWriter(new File(ApplicationFiles.USER_ALIAS_FILE_NAME));
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter xmlWriter = new XMLWriter(writer, format);
        xmlWriter.write(root);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        throw new ExplorerException(e);
    }
}
Also used : ExplorerException(net.sourceforge.sqlexplorer.ExplorerException) DefaultElement(org.dom4j.tree.DefaultElement) FileWriter(java.io.FileWriter) OutputFormat(org.dom4j.io.OutputFormat) IOException(java.io.IOException) File(java.io.File) 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