View Javadoc
1   package org.apache.maven.internal.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  import java.util.Map;
25  
26  import org.apache.maven.api.xml.Dom;
27  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
28  import org.codehaus.plexus.util.xml.XMLWriter;
29  
30  /**
31   *
32   */
33  public class Xpp3DomWriter
34  {
35      public static void write( Writer writer, Dom dom )
36      {
37          write( new PrettyPrintXMLWriter( writer ), dom );
38      }
39  
40      public static void write( PrintWriter writer, Dom dom )
41      {
42          write( new PrettyPrintXMLWriter( writer ), dom );
43      }
44  
45      public static void write( XMLWriter xmlWriter, Dom dom )
46      {
47          write( xmlWriter, dom, true );
48      }
49  
50      public static void write( XMLWriter xmlWriter, Dom dom, boolean escape )
51      {
52          // TODO: move to XMLWriter?
53          xmlWriter.startElement( dom.getName() );
54          for ( Map.Entry<String, String> attr : dom.getAttributes().entrySet() )
55          {
56              xmlWriter.addAttribute( attr.getKey(), attr.getValue() );
57          }
58          for ( Dom aChildren : dom.getChildren() )
59          {
60              write( xmlWriter, aChildren, escape );
61          }
62  
63          String value = dom.getValue();
64          if ( value != null )
65          {
66              if ( escape )
67              {
68                  xmlWriter.writeText( value );
69              }
70              else
71              {
72                  xmlWriter.writeMarkup( value );
73              }
74          }
75  
76          xmlWriter.endElement();
77      }
78  
79  }