archiva-modules/archiva-base/archiva-xml-tools/src/main/java/org/apache/archiva/xml/XMLWriter.java
/**
     * Write the Document to the provided Writer, with an option to close the writer upon completion.
     *
     * @param doc    the document to write.
     * @param writer the writer to write to.
     * @param close  true to close the writer on completion.
     * @throws XMLException if there was a problem writing the xml to the writer.
     */
Write the Document to the provided Writer, with an option to close the writer upon completion.
@param doc    the document to write.
@param writer the writer to write to.
@param close  true to close the writer on completion.
@throws XMLException if there was a problem writing the xml to the writer.
Write the Document to the provided Writer with an option to close the writer upon completion . @param doc the document to write . @param writer the writer to write to . @param close true to close the writer on completion . @throws XMLException if there was a problem writing the xml to the writer .
public static void write(Document doc, Writer writer, boolean close)
            throws XMLException {
        try {
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            try {
                transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            } catch (IllegalArgumentException ex) {
            }
            writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            StreamResult result = new StreamResult(writer);
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            DOMSource source = new DOMSource(doc);
            transformer.transform(source, result);
        } catch (TransformerException e) {
            throw new XMLException("Could not create the xml transformer: " + e.getMessage(), e);
        } catch (IOException e) {
            throw new XMLException("Could not write to xml output: " + e.getMessage(), e);
        } finally {
            if (writer!=null) {
                if (close) {
                    try {
                        writer.flush();
                    } catch (IOException e) {
                        /* quietly ignore */
                    }
                    try {
                        writer.close();
                    } catch (IOException e) {
                        /* quietly ignore */
                    }
                }
            }
        }
    }
public static void write ( Document doc , Writer writer , boolean close ) throws XMLException { try { TransformerFactory transformerFactory = TransformerFactory . newInstance ( ) ; Transformer transformer = transformerFactory . newTransformer ( ) ; transformer . setOutputProperty ( OutputKeys . INDENT , "yes" ) ; try { transformer . setOutputProperty ( "{http://xml.apache.org/xslt}indent-amount" , "2" ) ; } catch ( IllegalArgumentException ex ) { } writer . write ( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ) ; StreamResult result = new StreamResult ( writer ) ; transformer . setOutputProperty ( OutputKeys . OMIT_XML_DECLARATION , "yes" ) ; DOMSource source = new DOMSource ( doc ) ; transformer . transform ( source , result ) ; } catch ( TransformerException e ) { throw new XMLException ( "Could not create the xml transformer: " + e . getMessage ( ) , e ) ; } catch ( IOException e ) { throw new XMLException ( "Could not write to xml output: " + e . getMessage ( ) , e ) ; } finally { if ( writer != null ) { if ( close ) { try { writer . flush ( ) ; } catch ( IOException e ) { /* quietly ignore */ } try { writer . close ( ) ; } catch ( IOException e ) { /* quietly ignore */ } } } } }
Write the Document to the provided Writer, with an option to close the writer upon completion.
Write the Document to the provided Writer with an option to close the writer upon completion .
// Indent not supported // Writing the XML declaration, because the JDK implementation does not create a newline