1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 }