Search in sources :

Example 11 with OutputFormat

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

the class XmlUtil method write.

public static void write(Document document, String filePath, String printMode, String encoding) throws IOException {
    OutputFormat format = null;
    if (printMode.toLowerCase().equals("pretty")) {
        // $NON-NLS-1$
        // Pretty print the document
        format = OutputFormat.createPrettyPrint();
    } else if (printMode.toLowerCase().equals("compact")) {
        // $NON-NLS-1$
        // Compact format
        format = OutputFormat.createCompactFormat();
    }
    format.setEncoding(encoding);
    // lets write to a file
    XMLWriter writer = new XMLWriter(new FileOutputStream(filePath), format);
    // XMLWriter logger = new XMLWriter( System.out, format );
    writer.write(document);
    logger.info(Messages.bind(Messages.XmlUtil_Loginfo1, filePath));
    // logger.write( document );
    // logger.close();
    writer.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputFormat(org.dom4j.io.OutputFormat) XMLWriter(org.dom4j.io.XMLWriter)

Example 12 with OutputFormat

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

the class AbstractXMLSerializer method serialize.

@Override
public InputStream serialize(final R object) throws IOException {
    // This puts everything on the heap for now.
    // if size becomes a major problem, the alternitive is to fork over another thread
    // and use a PipedInputStream.
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final XMLWriter writer;
    try {
        // This should always be UTF-8 because it is a property of the serializer.
        final OutputFormat of = new OutputFormat(" ", true, "UTF-8");
        writer = new XMLWriter(baos, of);
        writer.startDocument();
    } catch (SAXException e) {
        throw new IOException("Could not open the XML writer.");
    }
    this.serialize(object, writer);
    try {
        writer.endDocument();
    } catch (SAXException e) {
        throw new IOException("Could not close the XML writer.");
    }
    return new ByteArrayInputStream(baos.toByteArray());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) OutputFormat(org.dom4j.io.OutputFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 13 with OutputFormat

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

the class XMLWriterTest method testWriteStream.

@Test
public void testWriteStream() 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.write(new DOMElement("prop"), new StringReader("Hello World!"));
    this.writer.writeClose(new DOMElement("root"));
    this.writer.endDocument();
    Assert.assertEquals("Incorrect response from testWriteStream.", WRITE_STREAM_TEST, new String(baos.toByteArray(), "UTF-8"));
}
Also used : OutputFormat(org.dom4j.io.OutputFormat) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DOMElement(org.dom4j.dom.DOMElement) Test(org.junit.Test)

Example 14 with OutputFormat

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

the class Package method toXML.

/**
 * Write the package.xml file to an OutputStream
 *
 * @param out the OutputStream to write to
 * @param context curent XWikiContext
 * @throws IOException when an error occurs during streaming operation
 * @since 2.3M2
 */
public void toXML(OutputStream out, XWikiContext context) throws IOException {
    XMLWriter wr = new XMLWriter(out, new OutputFormat("", true, context.getWiki().getEncoding()));
    Document doc = new DOMDocument();
    wr.writeDocumentStart(doc);
    toXML(wr);
    wr.writeDocumentEnd(doc);
}
Also used : OutputFormat(org.dom4j.io.OutputFormat) DOMDocument(org.dom4j.dom.DOMDocument) DOMDocument(org.dom4j.dom.DOMDocument) Document(org.dom4j.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XMLWriter(com.xpn.xwiki.internal.xml.XMLWriter)

Example 15 with OutputFormat

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

the class PdfExportImpl method applyCSS.

/**
 * Apply a CSS style sheet to an XHTML document and return the document with the resulting style properties inlined
 * in <tt>style</tt> attributes.
 *
 * @param html the valid XHTML document to style
 * @param css the style sheet to apply
 * @param context the current request context
 * @return the document with inlined style
 */
String applyCSS(String html, String css, XWikiContext context) {
    LOGGER.debug("Applying the following CSS [{}] to HTML [{}]", css, html);
    try {
        // System.setProperty("org.w3c.css.sac.parser", "org.apache.batik.css.parser.Parser");
        // Prepare the input
        Reader re = new StringReader(html);
        InputSource source = new InputSource(re);
        SAXReader reader = new SAXReader(XHTMLDocumentFactory.getInstance());
        reader.setEntityResolver(new DefaultEntityResolver());
        XHTMLDocument document = (XHTMLDocument) reader.read(source);
        // Set the base URL so that CSS4J can resolve URLs in CSS. Use the current document in the XWiki Context
        document.setBaseURL(new URL(context.getDoc().getExternalURL("view", context)));
        // Apply the style sheet
        document.addStyleSheet(new org.w3c.css.sac.InputSource(new StringReader(css)));
        applyInlineStyle(document.getRootElement());
        OutputFormat outputFormat = new OutputFormat("", false);
        if ((context == null) || (context.getWiki() == null)) {
            outputFormat.setEncoding("UTF-8");
        } else {
            outputFormat.setEncoding(context.getWiki().getEncoding());
        }
        StringWriter out = new StringWriter();
        XMLWriter writer = new XMLWriter(out, outputFormat);
        writer.write(document);
        String result = out.toString();
        // Debug output
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("HTML with CSS applied [{}]", result);
        }
        return result;
    } catch (Exception e) {
        LOGGER.warn("Failed to apply CSS [{}] to HTML [{}]", css, html, e);
        return html;
    }
}
Also used : InputSource(org.xml.sax.InputSource) SAXReader(org.dom4j.io.SAXReader) OutputFormat(org.dom4j.io.OutputFormat) SAXReader(org.dom4j.io.SAXReader) Reader(java.io.Reader) XMLReader(org.xml.sax.XMLReader) StringReader(java.io.StringReader) XMLWriter(org.dom4j.io.XMLWriter) URL(java.net.URL) XWikiException(com.xpn.xwiki.XWikiException) XWikiVelocityException(org.xwiki.velocity.XWikiVelocityException) IOException(java.io.IOException) DefaultEntityResolver(io.sf.carte.doc.xml.dtd.DefaultEntityResolver) XHTMLDocument(io.sf.carte.doc.dom4j.XHTMLDocument) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader)

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