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