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.shared.utils.xml;
20  
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  import java.io.Writer;
24  
25  /**
26   * @author Brett Porter
27   */
28  public class Xpp3DomWriter {
29      /**
30       * @param writer {@link Writer}
31       * @param dom {@link Xpp3Dom}
32       * @throws IOException if writing fails.
33       */
34      public static void write(Writer writer, Xpp3Dom dom) throws IOException {
35          write(new PrettyPrintXMLWriter(writer), dom);
36      }
37  
38      /**
39       * @param writer {@link PrintWriter}
40       * @param dom {@link Xpp3Dom}
41       * @throws IOException if writing fails.
42       */
43      public static void write(PrintWriter writer, Xpp3Dom dom) throws IOException {
44          write(new PrettyPrintXMLWriter(writer), dom);
45      }
46  
47      /**
48       * @param xmlWriter {@link XMLWriter}
49       * @param dom {@link Xpp3Dom}
50       * @throws IOException if writing fails.
51       */
52      public static void write(XMLWriter xmlWriter, Xpp3Dom dom) throws IOException {
53          write(xmlWriter, dom, true);
54      }
55  
56      /**
57       * @param xmlWriter {@link XMLWriter}
58       * @param dom {@link Xpp3Dom}
59       * @param escape true/false.
60       * @throws IOException if writing fails.
61       */
62      public static void write(XMLWriter xmlWriter, Xpp3Dom dom, boolean escape) throws IOException {
63          xmlWriter.startElement(dom.getName());
64          String[] attributeNames = dom.getAttributeNames();
65          for (String attributeName : attributeNames) {
66              xmlWriter.addAttribute(attributeName, dom.getAttribute(attributeName));
67          }
68          Xpp3Dom[] children = dom.getChildren();
69          for (Xpp3Dom aChildren : children) {
70              write(xmlWriter, aChildren, escape);
71          }
72  
73          String value = dom.getValue();
74          if (value != null) {
75              if (escape) {
76                  xmlWriter.writeText(value);
77              } else {
78                  xmlWriter.writeMarkup(value);
79              }
80          }
81          xmlWriter.endElement();
82      }
83  }