View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.internal.xml;
20  
21  import java.io.PrintWriter;
22  import java.io.Writer;
23  import java.util.Map;
24  import org.apache.maven.api.xml.Dom;
25  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
26  import org.codehaus.plexus.util.xml.XMLWriter;
27  
28  /**
29   *
30   */
31  public class Xpp3DomWriter {
32      public static void write(Writer writer, Dom dom) {
33          write(new PrettyPrintXMLWriter(writer), dom);
34      }
35  
36      public static void write(PrintWriter writer, Dom dom) {
37          write(new PrettyPrintXMLWriter(writer), dom);
38      }
39  
40      public static void write(XMLWriter xmlWriter, Dom dom) {
41          write(xmlWriter, dom, true);
42      }
43  
44      public static void write(XMLWriter xmlWriter, Dom dom, boolean escape) {
45          // TODO: move to XMLWriter?
46          xmlWriter.startElement(dom.getName());
47          for (Map.Entry<String, String> attr : dom.getAttributes().entrySet()) {
48              xmlWriter.addAttribute(attr.getKey(), attr.getValue());
49          }
50          for (Dom aChildren : dom.getChildren()) {
51              write(xmlWriter, aChildren, escape);
52          }
53  
54          String value = dom.getValue();
55          if (value != null) {
56              if (escape) {
57                  xmlWriter.writeText(value);
58              } else {
59                  xmlWriter.writeMarkup(value);
60              }
61          }
62  
63          xmlWriter.endElement();
64      }
65  }