Example usage for org.dom4j.io XMLWriter close

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the underlying Writer

Usage

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

License:Open Source License

/**
 * Marshals (writes) an XML document into an output stream using XML pretty-print formatting.<p>
 * /* w ww .  j  a  v a  2s . c  o  m*/
 * @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 Exception if something goes wrong
 */
public static OutputStream marshal(Document document, OutputStream out, String encoding) throws Exception {

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

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

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

    return out;
}

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>
 * //from ww w . jav a2  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 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>
 * //from   www  .  ja  v  a2s  . co m
 * @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>
 * //from  w w w . jav a  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.opencms.xml.CmsXmlUtils.java

License:Open Source License

/**
 * Validates the structure of a XML document contained in a byte array 
 * with the DTD or XML schema used by the document.<p>
 * //from   w ww .jav  a  2s.com
 * @param xmlStream a source providing a XML document that should be validated
 * @param resolver the XML entity resolver to use
 * 
 * @throws CmsXmlException if the validation fails
 */
public static void validateXmlStructure(InputStream xmlStream, EntityResolver resolver) throws CmsXmlException {

    XMLReader reader;
    try {
        reader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
    } catch (SAXException e) {
        // xerces parser not available - no schema validation possible
        if (LOG.isWarnEnabled()) {
            LOG.warn(Messages.get().getBundle().key(Messages.LOG_VALIDATION_INIT_XERXES_SAX_READER_FAILED_0),
                    e);
        }
        // no validation of the content is possible
        return;
    }
    // turn on validation
    try {
        reader.setFeature("http://xml.org/sax/features/validation", true);
        // turn on schema validation
        reader.setFeature("http://apache.org/xml/features/validation/schema", true);
        // configure namespace support
        reader.setFeature("http://xml.org/sax/features/namespaces", true);
        reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
    } catch (SAXNotRecognizedException e) {
        // should not happen as Xerces 2 support this feature
        if (LOG.isWarnEnabled()) {
            LOG.warn(Messages.get().getBundle().key(Messages.LOG_SAX_READER_FEATURE_NOT_RECOGNIZED_0), e);
        }
        // no validation of the content is possible
        return;
    } catch (SAXNotSupportedException e) {
        // should not happen as Xerces 2 support this feature
        if (LOG.isWarnEnabled()) {
            LOG.warn(Messages.get().getBundle().key(Messages.LOG_SAX_READER_FEATURE_NOT_SUPPORTED_0), e);
        }
        // no validation of the content is possible
        return;
    }

    // add an error handler which turns any errors into XML
    CmsXmlValidationErrorHandler errorHandler = new CmsXmlValidationErrorHandler();
    reader.setErrorHandler(errorHandler);

    if (resolver != null) {
        // set the resolver for the "opencms://" URIs
        reader.setEntityResolver(resolver);
    }

    try {
        reader.parse(new InputSource(xmlStream));
    } catch (IOException e) {
        // should not happen since we read form a byte array
        if (LOG.isErrorEnabled()) {
            LOG.error(Messages.get().getBundle().key(Messages.LOG_READ_XML_FROM_BYTE_ARR_FAILED_0), e);
        }
        return;
    } catch (SAXException e) {
        // should not happen since all errors are handled in the XML error handler
        if (LOG.isErrorEnabled()) {
            LOG.error(Messages.get().getBundle().key(Messages.LOG_PARSE_SAX_EXC_0), e);
        }
        return;
    }

    if (errorHandler.getErrors().elements().size() > 0) {
        // there was at last one validation error, so throw an exception
        StringWriter out = new StringWriter(256);
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(out, format);
        try {
            writer.write(errorHandler.getErrors());
            writer.write(errorHandler.getWarnings());
            writer.close();
        } catch (IOException e) {
            // should not happen since we write to a StringWriter
            if (LOG.isErrorEnabled()) {
                LOG.error(Messages.get().getBundle().key(Messages.LOG_STRINGWRITER_IO_EXC_0), e);
            }
        }
        // generate String from XML for display of document in error message
        throw new CmsXmlException(Messages.get().container(Messages.ERR_XML_VALIDATION_1, out.toString()));
    }
}

From source file:org.opensha.commons.geo.RegionUtils.java

License:Apache License

public static void regionToKML(Region region, String filename, Color c) {
    String kmlFileName = filename + ".kml";
    Document doc = DocumentHelper.createDocument();
    Element root = new DefaultElement("kml", new Namespace("", "http://www.opengis.net/kml/2.2"));
    doc.add(root);//from  ww w.  ja  va2 s.  c  o m

    Element e_doc = root.addElement("Document");
    Element e_doc_name = e_doc.addElement("name");
    e_doc_name.addText(kmlFileName);

    addBorderStyle(e_doc, c);
    addBorderVertexStyle(e_doc);
    addGridNodeStyle(e_doc, c);

    Element e_folder = e_doc.addElement("Folder");
    Element e_folder_name = e_folder.addElement("name");
    e_folder_name.addText("region");
    Element e_open = e_folder.addElement("open");
    e_open.addText("1");

    addBorder(e_folder, region);
    addPoints(e_folder, "Border Nodes", region.getBorder(), Style.BORDER_VERTEX);
    if (region.getInteriors() != null) {
        for (LocationList interior : region.getInteriors()) {
            addPoints(e_folder, "Interior Nodes", interior, Style.BORDER_VERTEX);
        }
    }

    if (region instanceof GriddedRegion) {
        addPoints(e_folder, "Grid Nodes", ((GriddedRegion) region).getNodeList(), Style.GRID_NODE);
    }

    // TODO absolutely need to create seom platform specific output directory
    // that is not in project space (e.g. desktop, Decs and Settings);

    String outDirName = "sha_kml/";
    File outDir = new File(outDirName);
    outDir.mkdirs();
    String tmpFile = outDirName + kmlFileName;

    try {
        //XMLUtils.writeDocumentToFile(tmpFile, doc);
        XMLWriter writer;
        OutputFormat format = new OutputFormat("\t", true);
        writer = new XMLWriter(new FileWriter(tmpFile), format);
        writer.write(doc);
        writer.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    //Element e = new Elem
}

From source file:org.opensha.commons.geo.RegionUtils.java

License:Apache License

public static void locListToKML(LocationList locs, String filename, Color c) {
    String kmlFileName = filename + ".kml";
    Document doc = DocumentHelper.createDocument();
    Element root = new DefaultElement("kml", new Namespace("", "http://www.opengis.net/kml/2.2"));
    doc.add(root);//from   w  w w. j av  a2  s . c  om

    Element e_doc = root.addElement("Document");
    Element e_doc_name = e_doc.addElement("name");
    e_doc_name.addText(kmlFileName);

    addBorderStyle(e_doc, c);
    addBorderVertexStyle(e_doc);
    addGridNodeStyle(e_doc, c);

    Element e_folder = e_doc.addElement("Folder");
    Element e_folder_name = e_folder.addElement("name");
    e_folder_name.addText("region");
    Element e_open = e_folder.addElement("open");
    e_open.addText("1");

    //      addLocationPoly(e_folder, locs);
    addLocationLine(e_folder, locs);
    addPoints(e_folder, "Border Nodes", locs, Style.BORDER_VERTEX);

    // TODO absolutely need to create seom platform specific output directory
    // that is not in project space (e.g. desktop, Decs and Settings);

    String outDirName = "sha_kml/";
    File outDir = new File(outDirName);
    outDir.mkdirs();
    String tmpFile = outDirName + kmlFileName;

    try {
        //XMLUtils.writeDocumentToFile(tmpFile, doc);
        XMLWriter writer;
        OutputFormat format = new OutputFormat("\t", true);
        writer = new XMLWriter(new FileWriter(tmpFile), format);
        writer.write(doc);
        writer.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    //Element e = new Elem
}

From source file:org.opensha.commons.util.XMLUtils.java

License:Apache License

/**
 * Writes an XML document to a file//ww w  .  j a  v a  2s.c  om
 * 
 * @param fileName
 * @param document
 * @throws IOException
 */
public static void writeDocumentToFile(File file, Document document) throws IOException {

    XMLWriter writer;

    writer = new XMLWriter(new FileWriter(file), format);
    writer.write(document);
    writer.close();
}

From source file:org.opensha.commons.util.XMLUtils.java

License:Apache License

public static String getDocumentAsString(Document document) throws IOException {
    StringWriter swrite = new StringWriter();

    XMLWriter writer;

    writer = new XMLWriter(swrite, format);
    writer.write(document);//from ww w.j  av a  2s . com
    writer.close();

    return swrite.getBuffer().toString();
}

From source file:org.orbeon.oxf.xml.dom4j.Dom4jUtils.java

License:Open Source License

private static String domToString(final Node node, final OutputFormat format) {
    try {/* ww w .  j a  v  a2s .  c o  m*/
        final StringBuilderWriter writer = new StringBuilderWriter();
        // Ugh, XMLWriter doesn't accept null formatter _and_ default formatter is protected.
        final XMLWriter xmlWriter = format == null ? new XMLWriter(writer) : new XMLWriter(writer, format);
        xmlWriter.write(node);
        xmlWriter.close();
        return writer.toString();
    } catch (final IOException e) {
        throw new OXFException(e);
    }
}