This project has retired. For details please refer to its Attic page.
XMLWriter xref
View Javadoc
1   package org.apache.archiva.xml;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.w3c.dom.Document;
23  
24  import javax.xml.transform.OutputKeys;
25  import javax.xml.transform.Transformer;
26  import javax.xml.transform.TransformerException;
27  import javax.xml.transform.TransformerFactory;
28  import javax.xml.transform.dom.DOMSource;
29  import javax.xml.transform.stream.StreamResult;
30  import java.io.IOException;
31  import java.io.Writer;
32  
33  /**
34   * XMLWriter - Making writing XML files easier.
35   */
36  public class XMLWriter {
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       */
44      public static void write(Document doc, Writer writer)
45              throws XMLException {
46          write(doc, writer, false);
47      }
48  
49      /**
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       */
57      public static void write(Document doc, Writer writer, boolean close)
58              throws XMLException {
59  
60          try {
61              TransformerFactory transformerFactory = TransformerFactory.newInstance();
62              Transformer transformer = transformerFactory.newTransformer();
63              transformer.setOutputProperty(OutputKeys.INDENT, "yes");
64              try {
65                  transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
66              } catch (IllegalArgumentException ex) {
67                  // Indent not supported
68              }
69              // Writing the XML declaration, because the JDK implementation does not create a newline
70              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);
75  
76  
77          } catch (TransformerException e) {
78              throw new XMLException("Could not create the xml transformer: " + e.getMessage(), e);
79          } catch (IOException e) {
80              throw new XMLException("Could not write to xml output: " + e.getMessage(), e);
81          } finally {
82              if (writer!=null) {
83                  if (close) {
84                      try {
85                          writer.flush();
86                      } catch (IOException e) {
87                          /* quietly ignore */
88                      }
89                      try {
90                          writer.close();
91                      } catch (IOException e) {
92                          /* quietly ignore */
93                      }
94                  }
95              }
96          }
97      }
98  }