1 | |
package org.kuali.rice.core.util.xml; |
2 | |
|
3 | |
import org.jdom.output.Format; |
4 | |
import org.jdom.output.XMLOutputter; |
5 | |
|
6 | |
import javax.xml.transform.*; |
7 | |
import javax.xml.transform.dom.DOMSource; |
8 | |
import javax.xml.transform.stream.StreamResult; |
9 | |
import java.io.IOException; |
10 | |
import java.io.StringWriter; |
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
public final class XmlJotter { |
17 | |
|
18 | 0 | private XmlJotter() { |
19 | 0 | throw new UnsupportedOperationException("do not call"); |
20 | |
} |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public static String jotDocument(org.jdom.Document document) { |
29 | 0 | return jotDocument(document, true); |
30 | |
} |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public static String jotDocument(org.jdom.Document document, boolean indent) { |
40 | 0 | XMLOutputter outputer = new XMLOutputter(getJdomFormat(indent)); |
41 | 0 | StringWriter writer = new StringWriter(); |
42 | |
try { |
43 | 0 | outputer.output(document, writer); |
44 | 0 | } catch (IOException e) { |
45 | 0 | throw new XmlException("Could not write XML data export.", e); |
46 | 0 | } |
47 | 0 | return writer.toString(); |
48 | |
} |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public static String jotDocument(org.w3c.dom.Document document) { |
57 | 0 | return jotNode(document.getDocumentElement()); |
58 | |
} |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public static String jotDocument(org.w3c.dom.Document document, boolean indent) { |
68 | 0 | return jotNode(document.getDocumentElement(), indent); |
69 | |
} |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public static String jotNode(org.jdom.Element node) { |
78 | 0 | return jotNode(node, true); |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public static String jotNode(org.jdom.Element node, boolean indent) { |
89 | 0 | XMLOutputter outputer = new XMLOutputter(getJdomFormat(indent)); |
90 | 0 | StringWriter writer = new StringWriter(); |
91 | |
try { |
92 | 0 | outputer.output(node, writer); |
93 | 0 | } catch (IOException e) { |
94 | 0 | throw new XmlException("Could not write XML data export.", e); |
95 | 0 | } |
96 | 0 | return writer.toString(); |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public static String jotNode(org.w3c.dom.Node node) { |
106 | 0 | return jotNode(node, true); |
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
public static String jotNode(org.w3c.dom.Node node, boolean indent) { |
117 | |
try { |
118 | 0 | return nodeToString(node, indent); |
119 | 0 | } catch (TransformerException te) { |
120 | 0 | throw new XmlException(te); |
121 | |
} |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
private static String nodeToString(org.w3c.dom.Node node, boolean indent) throws TransformerException { |
133 | 0 | Source source = new DOMSource(node); |
134 | 0 | StringWriter writer = new StringWriter(); |
135 | 0 | Result result = new StreamResult(writer); |
136 | 0 | Transformer transformer = TransformerFactory.newInstance().newTransformer(); |
137 | 0 | transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
138 | 0 | if (indent) { |
139 | 0 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
140 | |
} |
141 | 0 | transformer.transform(source, result); |
142 | 0 | return writer.toString(); |
143 | |
} |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
private static Format getJdomFormat(boolean indent) { |
152 | 0 | return indent ? Format.getPrettyFormat() : Format.getRawFormat(); |
153 | |
} |
154 | |
} |