Example usage for org.dom4j.io OutputFormat OutputFormat

List of usage examples for org.dom4j.io OutputFormat OutputFormat

Introduction

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

Prototype

public OutputFormat(String indent, boolean newlines, String encoding) 

Source Link

Document

Creates an OutputFormat with the given indent added with optional newlines between the Elements and the given encoding format.

Usage

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

License:Open Source License

/**
 * Writes the document to the location specified by the URL,
 * using the default XML writer!/*from   w w  w . jav  a  2  s  .  c om*/
 *
 * @param document the dom4j document.
 * @param url the URL of the document.
 */
public static synchronized void write(XDocument document, URL url) throws IOException {
    if (DEBUG)
        System.out.println("XMLUtilities.write( " + document + ", " + url + ")");

    File documentFile = null;

    documentFile = new File(url.getFile());

    if (documentFile != null) {
        FileOutputStream out = new FileOutputStream(documentFile);

        //      XMLWriter writer = new XMLWriter( out, format);
        XMLWriter writer = new XMLWriter(out,
                new OutputFormat(TextPreferences.getTabString(), true, document.getEncoding()));
        writer.write(document);
        writer.flush();

        out.flush();
        out.close();

    } else {
        throw new IOException(url.toExternalForm() + " (The system cannot find the path specified)");
    }
}

From source file:com.eurelis.tools.xml.transformation.local.LocalDocumentSource.java

License:Open Source License

/**
 * Write a dom4j document to the location specified by a File object
 *
 * @param file the location where to write the document
 * @param document the dom4j document to write
 *///from  ww w .  java2  s  .com
public static void writeDocument(File file, Document document) {
    XMLWriter xmlWriter = null;

    OutputStream os = null;
    OutputStreamWriter osw = null;

    try {

        os = new FileOutputStream(file);
        osw = new OutputStreamWriter(os, "UTF-8");

        xmlWriter = new XMLWriter(osw, new OutputFormat("  ", true, "UTF-8"));
        xmlWriter.write(document);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.flush();
                xmlWriter.close();

                osw.close();
                os.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

From source file:com.googlecode.fascinator.common.sax.SafeSAXReader.java

License:Open Source License

/**
 * Convert node to string// w ww .  j  ava  2 s  . co  m
 * 
 * @param outDoc Node to be converted
 * @return String of the converted node
 * @throws IOException if the conversion fail
 */
public String docToString(Node outDoc) throws IOException {
    Writer osw = new StringWriter();
    OutputFormat opf = new OutputFormat("", false, "UTF-8");
    opf.setSuppressDeclaration(true);
    opf.setExpandEmptyElements(true);
    XMLWriter writer = new XMLWriter(osw, opf);
    writer.setEscapeText(false);
    writer.write(outDoc);
    writer.close();

    return osw.toString();
}

From source file:com.googlecode.fascinator.common.sax.SafeSAXReader.java

License:Open Source License

/**
 * Convert node to stream//from ww  w .  j  a v  a 2  s  .  c o m
 * 
 * @param outDoc Node to be converted
 * @param outStream output stream of the converted node
 * @throws IOException if the conversion fail
 */
public void docToStream(Node outDoc, OutputStream outStream) throws IOException {
    OutputFormat opf = new OutputFormat("", false, "UTF-8");
    opf.setSuppressDeclaration(true);
    opf.setExpandEmptyElements(true);
    XMLWriter writer = new XMLWriter(outStream, opf);
    writer.setEscapeText(false);
    writer.write(outDoc);
    writer.close();
}

From source file:com.iisigroup.cap.utils.CapXmlUtil.java

License:Open Source License

/**
 * xml document ? string// w w  w . j  a v  a2s.c  o  m
 * 
 * @param doc
 *            document
 * @param format
 *            format
 * @return String
 */
public static String convertDocumentToString(Document doc, boolean format) {
    StringWriter out = new StringWriter();
    try {
        new XMLWriter(out, new OutputFormat(Constants.EMPTY_STRING, format, CharEncoding.UTF_8)).write(doc);
        return out.toString();
    } catch (IOException e) {
        throw new CapMessageException(e, CapXmlUtil.class);
    }
}

From source file:com.xpn.xwiki.doc.XWikiAttachment.java

License:Open Source License

/**
 * Retrieve an attachment as an XML string. You should prefer
 * {@link #toXML(com.xpn.xwiki.internal.xml.XMLWriter, boolean, boolean, com.xpn.xwiki.XWikiContext)
 * to avoid memory loads when appropriate.
 *
 * @param bWithAttachmentContent if true, binary content of the attachment is included (base64 encoded)
 * @param bWithVersions if true, all archived versions are also included
 * @param context current XWikiContext//from w  ww  . jav  a2  s .c  om
 * @return a string containing an XML representation of the attachment
 * @throws XWikiException when an error occurs during wiki operations
 */
public String toStringXML(boolean bWithAttachmentContent, boolean bWithVersions, XWikiContext context)
        throws XWikiException {
    // This is very bad. baos holds the entire attachment on the heap, then it makes a copy when toByteArray
    // is called, then String forces us to make a copy when we construct a new String.
    // Unfortunately this can't be fixed because jrcs demands the content as a String.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        XMLWriter wr = new XMLWriter(baos, new OutputFormat("", true, context.getWiki().getEncoding()));
        Document doc = new DOMDocument();
        wr.writeDocumentStart(doc);
        toXML(wr, bWithAttachmentContent, bWithVersions, context);
        wr.writeDocumentEnd(doc);
        byte[] array = baos.toByteArray();
        baos = null;
        return new String(array, context.getWiki().getEncoding());
    } catch (IOException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:com.xpn.xwiki.doc.XWikiAttachment.java

License:Open Source License

/**
 * Retrieve XML representation of attachment's metadata into an {@link Element}. You should prefer
 * {@link #toXML(com.xpn.xwiki.internal.xml.XMLWriter, boolean, boolean, com.xpn.xwiki.XWikiContext)
 * to avoid memory loads when appropriate.
 *
 * @param bWithAttachmentContent if true, binary content of the attachment is included (base64 encoded)
 * @param bWithVersions if true, all archived versions are also included
 * @param context current XWikiContext//from   w  w  w.j  ava  2 s .co m
 * @return an {@link Element} containing an XML representation of the attachment
 * @throws XWikiException when an error occurs during wiki operations
 * @since 2.3M2
 */
public Element toXML(boolean bWithAttachmentContent, boolean bWithVersions, XWikiContext context)
        throws XWikiException {
    Document doc = new DOMDocument();
    DOMXMLWriter wr = new DOMXMLWriter(doc, new OutputFormat("", true, context.getWiki().getEncoding()));

    try {
        toXML(wr, bWithAttachmentContent, bWithVersions, context);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return doc.getRootElement();
}

From source file:com.xpn.xwiki.doc.XWikiDocument.java

License:Open Source License

/**
 * Convert a {@link Document} into an XML string. You should prefer
 * {@link #toXML(OutputStream, boolean, boolean, boolean, boolean, XWikiContext)} or
 * {@link #toXML(com.xpn.xwiki.internal.xml.XMLWriter, boolean, boolean, boolean, boolean, XWikiContext)} when
 * possible to avoid memory load./*from  w ww  .  j a v  a2  s .  com*/
 * 
 * @param doc the {@link Document} to convert to a String
 * @param context current XWikiContext
 * @return an XML representation of the {@link Document}
 * @deprecated this method has nothing to do here and is apparently unused
 */
@Deprecated
public String toXML(Document doc, XWikiContext context) {
    String encoding = context.getWiki().getEncoding();

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
        XMLWriter wr = new XMLWriter(os, new OutputFormat("", true, encoding));
        wr.write(doc);
        return os.toString(encoding);
    } catch (IOException e) {
        LOGGER.error("Exception while doc.toXML", e);
        return "";
    }
}

From source file:com.xpn.xwiki.doc.XWikiDocument.java

License:Open Source License

/**
 * Serialize the document to an XML {@link DOMDocument}. You should prefer
 * {@link #toXML(OutputStream, boolean, boolean, boolean, boolean, XWikiContext)} or
 * {@link #toXML(com.xpn.xwiki.internal.xml.XMLWriter, boolean, boolean, boolean, boolean, XWikiContext)} when
 * possible to reduce memory load./*from  w ww.  jav a  2  s  . com*/
 * 
 * @param bWithObjects include XObjects
 * @param bWithRendering include the rendered content
 * @param bWithAttachmentContent include attachments content
 * @param bWithVersions include archived versions
 * @param context current XWikiContext
 * @return a {@link DOMDocument} containing the serialized document.
 * @throws XWikiException when an errors occurs during wiki operations
 */
public Document toXMLDocument(boolean bWithObjects, boolean bWithRendering, boolean bWithAttachmentContent,
        boolean bWithVersions, XWikiContext context) throws XWikiException {
    Document doc = new DOMDocument();
    DOMXMLWriter wr = new DOMXMLWriter(doc, new OutputFormat("", true, context.getWiki().getEncoding()));

    try {
        toXML(wr, bWithObjects, bWithRendering, bWithAttachmentContent, bWithVersions, context);
        return doc;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.xpn.xwiki.doc.XWikiDocument.java

License:Open Source License

/**
 * Serialize the document to an OutputStream.
 * /*from   w  w w .ja v a2 s.co  m*/
 * @param bWithObjects include XObjects
 * @param bWithRendering include the rendered content
 * @param bWithAttachmentContent include attachments content
 * @param bWithVersions include archived versions
 * @param context current XWikiContext
 * @throws XWikiException when an errors occurs during wiki operations
 * @throws IOException when an errors occurs during streaming operations
 * @since 2.3M2
 */
public void toXML(OutputStream out, boolean bWithObjects, boolean bWithRendering,
        boolean bWithAttachmentContent, boolean bWithVersions, XWikiContext context)
        throws XWikiException, IOException {
    XMLWriter wr = new XMLWriter(out, new OutputFormat("", true, context.getWiki().getEncoding()));

    Document doc = new DOMDocument();
    wr.writeDocumentStart(doc);
    toXML(wr, bWithObjects, bWithRendering, bWithAttachmentContent, bWithVersions, context);
    wr.writeDocumentEnd(doc);
}