Example usage for org.dom4j.io XMLWriter flush

List of usage examples for org.dom4j.io XMLWriter flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the underlying Writer

Usage

From source file:org.apache.openmeetings.util.XmlExport.java

License:Apache License

public static void toXml(Writer out, Document doc) throws Exception {
    OutputFormat outformat = OutputFormat.createPrettyPrint();
    outformat.setEncoding(StandardCharsets.UTF_8.name());
    XMLWriter writer = new XMLWriter(out, outformat);
    writer.write(doc);/*from  w w w  .j a v a 2s  .  c  om*/
    writer.flush();
    out.flush();
    out.close();
}

From source file:org.apache.taglibs.xtags.xpath.JspCopyOfAction.java

License:Apache License

public void run(Node node) throws Exception {
    if (node != null) {
        XMLWriter writer = getXMLWriter();
        writer.setWriter(pageContext.getOut());
        writer.write(node);/*from ww w.j ava2 s.  co  m*/
        writer.flush();
    }
}

From source file:org.codehaus.cargo.container.weblogic.WebLogic8xConfigXmlInstalledLocalDeployer.java

License:Apache License

/**
 * write the domain's config.xml to disk.
 * //from  www.  j av a2s  .  c o  m
 * @param configXml document to write to disk
 */
public void writeConfigXml(Document configXml) {
    OutputFormat outformat = OutputFormat.createPrettyPrint();
    XMLWriter writer = null;
    try {
        writer = new XMLWriter(
                getFileHandler().getOutputStream(getFileHandler().append(getDomainHome(), "config.xml")),
                outformat);
        writer.write(configXml);
        writer.flush();
    } catch (UnsupportedEncodingException e) {
        throw new ContainerException("Error encoding config.xml for " + this.getServerName(), e);
    } catch (IOException e) {
        throw new ContainerException("Error writing config.xml for " + this.getServerName(), e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException ignored) {
                // Ignored
            }
            writer = null;
            System.gc();
        }
    }
}

From source file:org.codehaus.cargo.util.Dom4JUtil.java

License:Apache License

/**
 * write the xml document to disk, closing the destination on completion.
 * /*  ww w  .j  a va2  s  .c  o m*/
 * @param document document to write to disk
 * @param destination where to write the document
 * @throws IOException when the document cannot be written to the destination
 */
private void writeXmlToOutputStream(Document document, OutputStream destination) throws IOException {
    try {
        OutputFormat outformat = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(destination, outformat);
        writer.write(document);
        writer.flush();
        writer.close();
    } finally {
        destination.close();
    }
}

From source file:org.codehaus.mojo.dita.DitaEclipseMojo.java

License:Apache License

private void writeOutEclipseProject(Document doc, File file) throws MojoExecutionException {
    FileWriter fileWriter = null;

    try {//from  w ww . j  a  v  a 2s.  co m
        OutputFormat outformat = OutputFormat.createPrettyPrint();
        fileWriter = new FileWriter(file);
        XMLWriter writer = new XMLWriter(fileWriter, outformat);
        writer.write(doc);
        writer.flush();
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } finally {
        IOUtil.close(fileWriter);
    }

}

From source file:org.craftercms.core.util.xml.marshalling.xstream.CrafterXStreamMarshaller.java

License:Open Source License

/**
 * Just as super(), but instead of a {@link com.thoughtworks.xstream.io.xml.CompactWriter} creates a {@link
 * EscapingCompactWriter}./*  w  w w  .ja v a 2s.c o m*/
 * Also if the object graph is a Dom4j document, the document is written directly instead of using XStream.
 */
@Override
public void marshalWriter(Object graph, Writer writer, DataHolder dataHolder)
        throws XmlMappingException, IOException {
    if (graph instanceof Document) {
        OutputFormat outputFormat = OutputFormat.createCompactFormat();
        outputFormat.setSuppressDeclaration(suppressXmlDeclaration);

        XMLWriter xmlWriter = new XMLWriter(writer, outputFormat);
        try {
            xmlWriter.write((Document) graph);
        } finally {
            try {
                xmlWriter.flush();
            } catch (Exception ex) {
                logger.debug("Could not flush XMLWriter", ex);
            }
        }
    } else {
        if (!suppressXmlDeclaration) {
            writer.write(XML_DECLARATION);
        }

        HierarchicalStreamWriter streamWriter = new EscapingCompactWriter(writer);
        try {
            getXStream().marshal(graph, streamWriter, dataHolder);
        } catch (Exception ex) {
            throw convertXStreamException(ex, true);
        } finally {
            try {
                streamWriter.flush();
            } catch (Exception ex) {
                logger.debug("Could not flush HierarchicalStreamWriter", ex);
            }
        }
    }
}

From source file:org.craftercms.cstudio.alfresco.util.XmlUtils.java

License:Open Source License

/**
 * convert document to string// w ww .j  av  a  2s . c o  m
 * 
 * @param document
 * @return XML as String
 * @throws IOException
 */
public static String convertDocumentToString(Document document) throws IOException {
    StringWriter sw = new StringWriter();
    XMLWriter writer = new XMLWriter(sw);
    try {
        writer.write(document);
        writer.flush();
        return sw.toString();
    } finally {
        sw.close();
        writer.close();
    }
}

From source file:org.craftercms.cstudio.loadtesting.actions.WriteContent.java

License:Open Source License

/**
 * convert InputStream to string/* ww w  .  java  2  s.  c o  m*/
 * @param internalName 
 * 
 * @param is
 * @return string
 */
public String getFileContent(String baseFileName, String fileName, String internalName) throws Exception {
    InputStream is = null;
    InputStreamReader isReader = null;
    StringWriter sw = null;
    XMLWriter writer = null;
    try {
        is = this.getClass().getResourceAsStream("/" + baseFileName);
        isReader = new InputStreamReader(is, "UTF-8");
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(isReader);
        Element root = document.getRootElement();
        Node node = root.selectSingleNode("file-name");
        node.setText(fileName);
        Node node2 = root.selectSingleNode("internal-name");
        node2.setText(internalName);
        sw = new StringWriter();
        writer = new XMLWriter(sw);
        writer.write(document);
        writer.flush();
        return sw.toString();
    } finally {
        if (is != null) {
            is.close();
        }
        if (isReader != null) {
            isReader.close();
        }
        if (sw != null) {
            sw.close();
        }
        if (writer != null) {
            writer.close();
        }
    }
}

From source file:org.dentaku.gentaku.tools.cgen.plugin.GenGenPlugin.java

License:Apache License

private void prettyPrint(Document document, Writer writer) throws IOException {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding(encoding);//from w ww. j ava  2s  .  com
    format.setSuppressDeclaration(false);
    format.setExpandEmptyElements(false);

    XMLWriter xmlWriter = new XMLWriter(writer, format);
    xmlWriter.write(document);
    xmlWriter.flush();
}

From source file:org.dentaku.gentaku.tools.cgen.visitor.JellyTemplateGeneratingVisitor.java

License:Apache License

public void handleElement(Element element) throws VisitorException {
    if (element.getParent().getName().equals("schema")) {
        QName rootName = DocumentFactory.getInstance().createQName("jelly", "j", "jelly:core");
        Branch parent = DocumentHelper.createDocument().addElement(rootName).addNamespace("x", "jelly:xml");

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding(getEncoding());
        format.setSuppressDeclaration(false);
        format.setExpandEmptyElements(false);

        pushParent(parent.addElement(element.getName()));
        for (Iterator it = element.elementIterator(); it.hasNext();) {
            visit((Element) it.next());
        }//from  ww w  . j  a v a2s .c  o m
        popParent();

        try {
            String filename = element.getName() + ".jelly";

            Writer out = new FileWriter(new File(getRootDir(), filename));
            final XMLWriter xmlWriter = new XMLWriter(out, format);
            xmlWriter.setEscapeText(false);

            xmlWriter.write(parent);
            xmlWriter.flush();
            xmlWriter.close();
        } catch (Exception e) {
            throw new VisitorException("Exception occurred when running Jelly", e);
        }

        topLevelElements.put(element.getName(), element);

    } else {
        String refName = element.attributeValue("ref");
        if (refName != null) {
            Branch parent = getCurrentParent();
            // create an include
            QName qName = DocumentFactory.getInstance().createQName("import", "j", "jelly:core");
            parent.addElement(qName).addAttribute("uri", refName + ".jelly").addAttribute("inherit", "true");
        }
    }
}