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.dom4j.Document;
23  import org.dom4j.io.OutputFormat;
24  
25  import java.io.IOException;
26  import java.io.Writer;
27  
28  /**
29   * XMLWriter - Making writing XML files easier. 
30   *
31   *
32   */
33  public class XMLWriter
34  {
35      /**
36       * Write the Document to the provided Writer, leaving the Writer open.
37       * 
38       * @param doc the document to write.
39       * @param writer the writer to write to.
40       * @throws XMLException if there was a problem writing the xml to the writer.
41       */
42      public static void write( Document doc, Writer writer )
43          throws XMLException
44      {
45          write( doc, writer, false );
46      }
47  
48      /**
49       * Write the Document to the provided Writer, with an option to close the writer upon completion.
50       * 
51       * @param doc the document to write.
52       * @param writer the writer to write to.
53       * @param close true to close the writer on completion.
54       * @throws XMLException if there was a problem writing the xml to the writer.
55       */
56      public static void write( Document doc, Writer writer, boolean close )
57          throws XMLException
58      {
59          org.dom4j.io.XMLWriter xmlwriter = null;
60  
61          try
62          {
63              OutputFormat outputFormat = OutputFormat.createPrettyPrint();
64              xmlwriter = new org.dom4j.io.XMLWriter( writer, outputFormat );
65              xmlwriter.write( doc );
66              xmlwriter.flush();
67          }
68          catch ( IOException e )
69          {
70              throw new XMLException( "Unable to write xml contents to writer: " + e.getMessage(), e );
71          }
72          finally
73          {
74              if ( close && ( xmlwriter != null ) )
75              {
76                  try
77                  {
78                      xmlwriter.close();
79                  }
80                  catch ( IOException e )
81                  {
82                      /* quietly ignore */
83                  }
84              }
85          }
86      }
87  }