View Javadoc
1   package org.apache.maven.shared.utils.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 java.io.IOException;
23  import java.io.PrintWriter;
24  import java.io.Writer;
25  
26  /**
27   * @author Brett Porter
28   */
29  public class Xpp3DomWriter
30  {
31      /**
32       * @param writer {@link Writer}
33       * @param dom {@link Xpp3Dom}
34       * @throws IOException if writing fails.
35       */
36      public static void write( Writer writer, Xpp3Dom dom ) throws IOException
37      {
38          write( new PrettyPrintXMLWriter( writer ), dom );
39      }
40  
41      /**
42       * @param writer {@link PrintWriter}
43       * @param dom {@link Xpp3Dom}
44       * @throws IOException if writing fails.
45       */
46      public static void write( PrintWriter writer, Xpp3Dom dom ) throws IOException
47      {
48          write( new PrettyPrintXMLWriter( writer ), dom );
49      }
50  
51      /**
52       * @param xmlWriter {@link XMLWriter}
53       * @param dom {@link Xpp3Dom}
54       * @throws IOException if writing fails.
55       */
56      public static void write( XMLWriter xmlWriter, Xpp3Dom dom ) throws IOException
57      {
58          write( xmlWriter, dom, true );
59      }
60  
61      /**
62       * @param xmlWriter {@link XMLWriter}
63       * @param dom {@link Xpp3Dom}
64       * @param escape true/false.
65       * @throws IOException if writing fails.
66       */
67      public static void write( XMLWriter xmlWriter, Xpp3Dom dom, boolean escape ) throws IOException
68      {
69          xmlWriter.startElement( dom.getName() );
70          String[] attributeNames = dom.getAttributeNames();
71          for ( String attributeName : attributeNames )
72          {
73              xmlWriter.addAttribute( attributeName, dom.getAttribute( attributeName ) );
74          }
75          Xpp3Dom[] children = dom.getChildren();
76          for ( Xpp3Dom aChildren : children )
77          {
78              write( xmlWriter, aChildren, escape );
79          }
80  
81          String value = dom.getValue();
82          if ( value != null )
83          {
84              if ( escape )
85              {
86                  xmlWriter.writeText( value );
87              }
88              else
89              {
90                  xmlWriter.writeMarkup( value );
91              }
92          }
93          xmlWriter.endElement();
94      }
95  
96  }