Example usage for org.dom4j.io XMLWriter write

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

Introduction

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

Prototype

public void write(Object object) throws IOException 

Source Link

Document

Writes the given object which should be a String, a Node or a List of Nodes.

Usage

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 ww  w .  java2s . com*/
        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.codehaus.mojo.hibernate2.MappingsAggregatorMojo.java

License:Apache License

public void execute() throws MojoExecutionException {
    try {/*from   w  ww. j a v  a2 s  . com*/
        String version = null;

        if (getBasedir() == null) {
            throw new MojoExecutionException("Required configuration missing: basedir");
        }

        File files[] = getIncludeFiles();
        if (files == null || files.length <= 0) {
            return;
        }
        File f = new File(getOutputFile());
        if (!f.exists()) {
            f.getParentFile().mkdirs();
            f.createNewFile();
        }
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(new FileWriter(f), format);
        writer.setEntityResolver(new HibernateEntityResolver());
        //writer.setResolveEntityRefs(false);
        Document finalDoc = DocumentHelper.createDocument();
        Element rootHM = null;
        for (int i = 0; i < files.length; i++) {
            print("Parsing: " + files[i].getAbsolutePath());
            SAXReader reader = new SAXReader(false);
            reader.setEntityResolver(new HibernateEntityResolver());
            //reader.setIncludeExternalDTDDeclarations(false);
            //reader.setIncludeExternalDTDDeclarations(false);
            Document current = reader.read(files[i]);
            String currentVersion = getVersion(current);
            if (version == null) {
                version = currentVersion;
                finalDoc.setProcessingInstructions(current.processingInstructions());
                finalDoc.setDocType(current.getDocType());
                rootHM = finalDoc.addElement("hibernate-mapping");
            } else if (!version.equals(currentVersion)) {
                //LOG.warn("Mapping in " + files[i].getName() + " is not of the same mapping version as " + files[0].getName() + " mapping, so merge is impossible. Skipping");
                continue;
            }
            for (Iterator iter = current.selectSingleNode("hibernate-mapping").selectNodes("class")
                    .iterator(); iter.hasNext(); rootHM.add((Element) ((Element) iter.next()).clone())) {
            }
        }

        print("Writing aggregate file: " + f.getAbsolutePath());
        writer.write(finalDoc);
        writer.close();
    } catch (Exception ex) {
        throw new MojoExecutionException("Error in executing MappingsAgrregatorBean", ex);
    }
}

From source file:org.compass.core.xml.dom4j.converter.AbstractXmlWriterXmlContentConverter.java

License:Apache License

/**
 * Converts the {@link XmlObject} (assumes it is a {@link org.compass.core.xml.dom4j.Dom4jXmlObject}) into
 * an xml string. Uses dom4j <code>XmlWriter</code> and <code>OutputFormat</code>
 * (in a compact mode) to perform it.//from w  w w.ja  v a2 s .  c  o m
 *
 * @param xmlObject The xml object to convert into an xml string (must be a {@link org.compass.core.xml.dom4j.Dom4jXmlObject} implementation).
 * @return An xml string representation of the xml object
 * @throws ConversionException Should not really happne...
 */
public String toXml(XmlObject xmlObject) throws ConversionException {
    Dom4jXmlObject dom4jXmlObject = (Dom4jXmlObject) xmlObject;
    StringBuilderWriter stringWriter = StringBuilderWriter.Cached.cached();
    OutputFormat outputFormat = null;
    if (compact) {
        outputFormat = OutputFormat.createCompactFormat();
    }
    XMLWriter xmlWriter;
    if (outputFormat != null) {
        xmlWriter = new XMLWriter(stringWriter, outputFormat);
    } else {
        xmlWriter = new XMLWriter(stringWriter);
    }
    try {
        xmlWriter.write(dom4jXmlObject.getNode());
        xmlWriter.close();
    } catch (IOException e) {
        throw new ConversionException("This should not happen", e);
    }
    return stringWriter.toString();
}

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}./*from  w  w  w.ja  va 2  s .  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.core.util.XmlUtils.java

License:Open Source License

/**
 * Returns the given document as a XML string in a "pretty" format.
 *
 * @param document/*from  www .  ja va 2 s. c om*/
 * @return the document as an XML string
 */
public static String documentToPrettyString(Document document) {
    StringWriter stringWriter = new StringWriter();
    OutputFormat prettyPrintFormat = OutputFormat.createPrettyPrint();
    XMLWriter xmlWriter = new XMLWriter(stringWriter, prettyPrintFormat);

    try {
        xmlWriter.write(document);
    } catch (IOException e) {
        // Ignore, shouldn't happen.
    }

    return stringWriter.toString();
}

From source file:org.craftercms.cstudio.alfresco.transform.TaxonomyRendition.java

License:Open Source License

/**
 * Write renditionResult along with the flattened XML file to WCM
 * // w w w.j  a  v  a2  s. c o m
 * @param articleRef
 */
public InputStream generateOutputFile(NodeRef articleRef) throws ServiceException {

    InputStream retStream = null;

    try {

        Document document = retreiveXml(articleRef);

        String encoding = document.getXMLEncoding();
        if (encoding == null) {
            encoding = "UTF-16";
        }

        Writer stringOutputWriter = new StringWriter();
        OutputFormat opf = OutputFormat.createCompactFormat();
        opf.setSuppressDeclaration(true);
        XMLWriter writer = new XMLWriter(stringOutputWriter, opf);
        writer.write(document);
        writer.close();

        retStream = new ByteArrayInputStream(stringOutputWriter.toString().getBytes(encoding));

    } catch (ServiceException e) {
        if (LOGGER.isErrorEnabled()) {
            LOGGER.error("Cannot create output XML Document: " + e.getMessage(), e);
        }
        throw new ServiceException("Cannot create output XML Document: " + e.getMessage(), e);

    } catch (IOException e) {
        if (LOGGER.isErrorEnabled()) {
            LOGGER.error("Cannot create output XML Document: " + e.getMessage(), e);
        }
        throw new ServiceException("Cannot create output XML Document: " + e.getMessage(), e);
    }

    return retStream;
}

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

License:Open Source License

/**
 * convert document to string// ww w  . ja  va 2 s .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/*from  w ww  . ja  v  a 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.craftercms.search.batch.impl.XmlFileBatchIndexer.java

License:Open Source License

protected String documentToString(Document document) {
    StringWriter stringWriter = new StringWriter();
    OutputFormat format = OutputFormat.createCompactFormat();
    XMLWriter xmlWriter = new XMLWriter(stringWriter, format);

    try {/*from   w  w  w  .j  a  v  a 2s. c o  m*/
        xmlWriter.write(document);
    } catch (IOException e) {
        // Ignore, shouldn't happen.
    }

    return stringWriter.toString();
}