Example usage for org.dom4j.io OutputFormat setEncoding

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

Introduction

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

Prototype

public void setEncoding(String encoding) 

Source Link

Document

DOCUMENT ME!

Usage

From source file:org.opencms.util.ant.CmsXmlUtils.java

License:Open Source License

/**
 * Marshals (writes) an XML node into an output stream using XML pretty-print formatting.<p>
 * /* ww w.j av  a 2 s  .  c  om*/
 * @param node the XML node to marshal
 * @param encoding the encoding to use
 * 
 * @return the string with the xml content
 * 
 * @throws Exception if something goes wrong
 */
public static String marshal(Node node, String encoding) throws Exception {

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding(encoding);
    format.setSuppressDeclaration(true);

    XMLWriter writer = new XMLWriter(out, format);
    writer.setEscapeText(false);

    writer.write(node);
    writer.close();
    return new String(out.toByteArray());
}

From source file:org.opencms.xml.CmsXmlUtils.java

License:Open Source License

/**
 * Marshals (writes) an XML document into an output stream using XML pretty-print formatting.<p>
 * // w  ww .  ja  v  a 2  s.  com
 * @param document the XML document to marshal
 * @param out the output stream to write to
 * @param encoding the encoding to use
 * @return the output stream with the xml content
 * @throws CmsXmlException if something goes wrong
 */
public static OutputStream marshal(Document document, OutputStream out, String encoding)
        throws CmsXmlException {

    try {
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding(encoding);

        XMLWriter writer = new XMLWriter(out, format);
        writer.setEscapeText(false);

        writer.write(document);
        writer.close();

    } catch (Exception e) {
        throw new CmsXmlException(Messages.get().container(Messages.ERR_MARSHALLING_XML_DOC_0), e);
    }

    return out;
}

From source file:org.opencms.xml.CmsXmlUtils.java

License:Open Source License

/**
 * Marshals (writes) an XML node into an output stream using XML pretty-print formatting.<p>
 * //w w w  .j  a va  2 s  .c o  m
 * @param node the XML node to marshal
 * @param encoding the encoding to use
 * 
 * @return the string with the xml content
 * 
 * @throws CmsXmlException if something goes wrong
 */
public static String marshal(Node node, String encoding) throws CmsXmlException {

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding(encoding);
        format.setSuppressDeclaration(true);

        XMLWriter writer = new XMLWriter(out, format);
        writer.setEscapeText(false);

        writer.write(node);
        writer.close();
    } catch (Exception e) {
        throw new CmsXmlException(Messages.get().container(Messages.ERR_MARSHALLING_XML_DOC_0), e);
    }
    return new String(out.toByteArray());
}

From source file:org.orbeon.oxf.processor.tamino.dom4j.TDOM4JAdapter.java

License:Open Source License

/**
 * Initializes the io output helpers. Here only output helpers are needed.
 * ATTENTION: org.dom4j.XMLWriter is used, if you have no IOHelper in your
 * specific object model, you have to create a
 *///from  www  .j a va 2 s  .  c o m
private void initializeIOHelpers() {
    try {
        OutputFormat outformat = new OutputFormat();
        // Obtain the active encoding from the central preference instance.
        this.encoding = TPreference.getInstance().getEncoding();
        // Set the encoding on the outputter.
        outformat.setEncoding(this.encoding);
        // For serialization encoding attribute shall be included.
        outformat.setOmitEncoding(false);
        this.outputter = new XMLWriter(outformat);
    } catch (UnsupportedEncodingException uee) {
        System.err.println("WARNING: " + uee.getMessage());
    }
}

From source file:org.pdfsam.guiclient.business.environment.Environment.java

License:Open Source License

/**
 * saves and environment to the output file
 * //w w  w . java  2  s .c o m
 * @param outFile
 * @param savePasswords
 *            true save passwords informations
 */
public void saveEnvironment(File outFile, boolean savePasswords) {
    try {
        if (outFile != null) {
            synchronized (Environment.class) {
                Document document = DocumentHelper.createDocument();
                Element root = document.addElement("pdfsam_saved_jobs");
                root.addAttribute("version", GuiClient.getVersion());
                root.addAttribute("savedate", new SimpleDateFormat("dd-MMM-yyyy").format(new Date()));
                String selection = treePanel.getSelectedPlugin();
                if (selection != null && selection.length() > 0) {
                    root.addAttribute("selection", selection);
                }
                for (AbstractPlugablePanel plugablePanel : plugins.values()) {
                    Element node = (Element) root.addElement("plugin");
                    node.addAttribute("class", plugablePanel.getClass().getName());
                    node.addAttribute("name", plugablePanel.getPluginName());
                    plugablePanel.getJobNode(node, savePasswords);
                    LOG.info(GettextResource.gettext(i18nMessages,
                            plugablePanel.getPluginName() + " node environment loaded."));
                }
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFile));
                OutputFormat format = OutputFormat.createPrettyPrint();
                format.setEncoding("UTF-8");
                XMLWriter xmlWriter = new XMLWriter(bos, format);
                xmlWriter.write(document);
                xmlWriter.flush();
                xmlWriter.close();
            }
            LOG.info(GettextResource.gettext(i18nMessages, "Environment saved."));
        } else {
            LOG.error(GettextResource.gettext(i18nMessages, "Error saving environment, output file is null."));
        }
    } catch (Exception ex) {
        LOG.error(GettextResource.gettext(i18nMessages, "Error saving environment."), ex);
    }
}

From source file:org.pdfsam.guiclient.utils.xml.XMLParser.java

License:Open Source License

/**
 * Write the DOM to the xml file//from  www .  ja  v a2 s.  c  o m
 * 
 * @param domDoc Document to write
 * @param outFile xml File to write
 * @throws Exception
 */
public static void writeXmlFile(Document domDoc, File outFile) throws Exception {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFile));
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");
    XMLWriter xmlFileWriter = new XMLWriter(bos, format);
    xmlFileWriter.write(domDoc);
    xmlFileWriter.flush();
    xmlFileWriter.close();
}

From source file:org.pdfsam.guiclient.utils.XmlUtility.java

License:Open Source License

/**
 * Write the DOM to the xml file//w ww .  j  ava2s.c  o  m
 * 
 * @param domDoc
 *            Document to write
 * @param outFile
 *            xml File to write
 * @throws IOException
 */
public static void writeXmlFile(Document domDoc, File outFile) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFile));
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");
    XMLWriter xmlFileWriter = new XMLWriter(bos, format);
    xmlFileWriter.write(domDoc);
    xmlFileWriter.flush();
    xmlFileWriter.close();
}

From source file:org.pdfsam.plugin.merge.actions.SaveListAsXmlAction.java

License:Open Source License

/**
 * Save the xml file// w w w.j  a  v a2 s. co  m
 * 
 * @param rows
 * @param selectedFile
 * @throws Exception
 */
public void writeXmlFile(PdfSelectionTableItem[] rows, File selectedFile) throws Exception {
    if (selectedFile != null && rows != null) {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("filelist");
        for (int i = 0; i < rows.length; i++) {
            PdfSelectionTableItem row = rows[i];
            Element node = (Element) root.addElement("file");
            node.addAttribute("value", row.getInputFile().getAbsolutePath());
            String pwd = row.getPassword();
            if (pwd != null && pwd.length() > 0) {
                node.addAttribute("password", pwd);
            }
        }
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(selectedFile));
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        XMLWriter xmlWriter = new XMLWriter(bos, format);
        xmlWriter.write(document);
        xmlWriter.flush();
        xmlWriter.close();
        LOG.info(GettextResource.gettext(Configuration.getInstance().getI18nResourceBundle(),
                "File xml saved."));
    } else {
        LOG.error(GettextResource.gettext(Configuration.getInstance().getI18nResourceBundle(),
                "Error saving xml file, output file is null."));
    }
}

From source file:org.pdfsam.plugin.merge.components.JSaveListAsXmlMenuItem.java

License:Open Source License

/**
 * Save the xml file/*from  w  ww  . ja v a 2 s .c  om*/
 * @param rows
 * @param selectedFile
 * @throws Exception
 */
public void writeXmlFile(PdfSelectionTableItem[] rows, File selectedFile) throws Exception {
    if (selectedFile != null && rows != null) {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("filelist");
        for (int i = 0; i < rows.length; i++) {
            PdfSelectionTableItem row = rows[i];
            Element node = (Element) root.addElement("file");
            node.addAttribute("value", row.getInputFile().getAbsolutePath());
            String pwd = row.getPassword();
            if (pwd != null && pwd.length() > 0) {
                node.addAttribute("password", pwd);
            }
        }
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(selectedFile));
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        XMLWriter xmlWriter = new XMLWriter(bos, format);
        xmlWriter.write(document);
        xmlWriter.flush();
        xmlWriter.close();
        log.info(GettextResource.gettext(Configuration.getInstance().getI18nResourceBundle(),
                "File xml saved."));
    } else {
        log.error(GettextResource.gettext(Configuration.getInstance().getI18nResourceBundle(),
                "Error saving xml file, output file is null."));
    }
}

From source file:org.pentaho.actionsequence.dom.actions.MQLAction.java

License:Open Source License

private Document prettyPrint(Document document) {
    try {//  w w w . j a v a 2  s . c o  m
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding(document.getXMLEncoding());
        StringWriter stringWriter = new StringWriter();
        XMLWriter writer = new XMLWriter(stringWriter, format);
        // XMLWriter has a bug that is avoided if we reparse the document
        // prior to calling XMLWriter.write()
        writer.write(DocumentHelper.parseText(document.asXML()));
        writer.close();
        document = DocumentHelper.parseText(stringWriter.toString());
    } catch (Exception e) {
        e.printStackTrace();
        return (null);
    }
    return (document);
}