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