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      public static void write( Writer writer, Xpp3Dom dom )
31      {
32          write( new PrettyPrintXMLWriter( writer ), dom );
33      }
34  
35      public static void write( PrintWriter writer, Xpp3Dom dom )
36      {
37          write( new PrettyPrintXMLWriter( writer ), dom );
38      }
39  
40      public static void write( XMLWriter xmlWriter, Xpp3Dom dom )
41      {
42          write( xmlWriter, dom, true );
43      }
44  
45      public static void write( XMLWriter xmlWriter, Xpp3Dom dom, boolean escape )
46      {
47          xmlWriter.startElement( dom.getName() );
48          String[] attributeNames = dom.getAttributeNames();
49          for ( String attributeName : attributeNames )
50          {
51              xmlWriter.addAttribute( attributeName, dom.getAttribute( attributeName ) );
52          }
53          Xpp3Dom[] children = dom.getChildren();
54          for ( Xpp3Dom aChildren : children )
55          {
56              write( xmlWriter, aChildren, escape );
57          }
58  
59          String value = dom.getValue();
60          if ( value != null )
61          {
62              if ( escape )
63              {
64                  xmlWriter.writeText( value );
65              }
66              else
67              {
68                  xmlWriter.writeMarkup( value );
69              }
70          }
71          xmlWriter.endElement();
72      }
73  
74  }