1package org.apache.archiva.xml;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122import org.w3c.dom.Document;
2324import javax.xml.transform.OutputKeys;
25import javax.xml.transform.Transformer;
26import javax.xml.transform.TransformerException;
27import javax.xml.transform.TransformerFactory;
28import javax.xml.transform.dom.DOMSource;
29import javax.xml.transform.stream.StreamResult;
30import java.io.IOException;
31import java.io.Writer;
3233/**34 * XMLWriter - Making writing XML files easier.35 */36publicclassXMLWriter {
37/**38 * Write the Document to the provided Writer, leaving the Writer open.39 *40 * @param doc the document to write.41 * @param writer the writer to write to.42 * @throws XMLException if there was a problem writing the xml to the writer.43 */44publicstaticvoid write(Document doc, Writer writer)
45throwsXMLException {
46 write(doc, writer, false);
47 }
4849/**50 * Write the Document to the provided Writer, with an option to close the writer upon completion.51 *52 * @param doc the document to write.53 * @param writer the writer to write to.54 * @param close true to close the writer on completion.55 * @throws XMLException if there was a problem writing the xml to the writer.56 */57publicstaticvoid write(Document doc, Writer writer, boolean close)
58throwsXMLException {
5960try {
61 TransformerFactory transformerFactory = TransformerFactory.newInstance();
62 Transformer transformer = transformerFactory.newTransformer();
63 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
64try {
65 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
66 } catch (IllegalArgumentException ex) {
67// Indent not supported68 }
69// Writing the XML declaration, because the JDK implementation does not create a newline70 writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
71 StreamResult result = new StreamResult(writer);
72 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
73 DOMSource source = new DOMSource(doc);
74 transformer.transform(source, result);
757677 } catch (TransformerException e) {
78thrownewXMLException("Could not create the xml transformer: " + e.getMessage(), e);
79 } catch (IOException e) {
80thrownewXMLException("Could not write to xml output: " + e.getMessage(), e);
81 } finally {
82if (writer!=null) {
83if (close) {
84try {
85 writer.flush();
86 } catch (IOException e) {
87/* quietly ignore */88 }
89try {
90 writer.close();
91 } catch (IOException e) {
92/* quietly ignore */93 }
94 }
95 }
96 }
97 }
98 }