View Javadoc
1   package org.codehaus.plexus.util.xml;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.PrintWriter;
20  import java.io.Writer;
21  
22  /**
23   *
24   */
25  public class Xpp3DomWriter
26  {
27      public static void write( Writer writer, Xpp3Dom dom )
28      {
29          write( new PrettyPrintXMLWriter( writer ), dom );
30      }
31  
32      public static void write( PrintWriter writer, Xpp3Dom dom )
33      {
34          write( new PrettyPrintXMLWriter( writer ), dom );
35      }
36  
37      public static void write( XMLWriter xmlWriter, Xpp3Dom dom )
38      {
39          write( xmlWriter, dom, true );
40      }
41  
42      public static void write( XMLWriter xmlWriter, Xpp3Dom dom, boolean escape )
43      {
44          // TODO: move to XMLWriter?
45          xmlWriter.startElement( dom.getName() );
46          String[] attributeNames = dom.getAttributeNames();
47          for ( String attributeName : attributeNames )
48          {
49              xmlWriter.addAttribute( attributeName, dom.getAttribute( attributeName ) );
50          }
51          Xpp3Dom[] children = dom.getChildren();
52          for ( Xpp3Dom aChildren : children )
53          {
54              write( xmlWriter, aChildren, escape );
55          }
56  
57          String value = dom.getValue();
58          if ( value != null )
59          {
60              if ( escape )
61              {
62                  xmlWriter.writeText( value );
63              }
64              else
65              {
66                  xmlWriter.writeMarkup( value );
67              }
68          }
69  
70          xmlWriter.endElement();
71      }
72  
73  }