1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
|
26
|
|
|
27
|
|
|
28
|
|
|
29
|
|
|
30
|
|
|
31
|
|
|
32
|
|
|
33
|
|
|
34
|
|
|
35
|
|
|
36
|
|
|
37
|
|
|
38
|
|
|
39
|
|
|
40
|
|
|
41
|
|
|
42
|
|
|
43
|
|
|
44
|
|
|
45
|
|
|
46
|
|
package org.codehaus.groovy.tools.xml;
|
47
|
|
|
48
|
|
import groovy.util.IndentPrinter;
|
49
|
|
|
50
|
|
import java.io.PrintWriter;
|
51
|
|
import java.util.HashMap;
|
52
|
|
import java.util.Map;
|
53
|
|
|
54
|
|
import org.w3c.dom.Attr;
|
55
|
|
import org.w3c.dom.Comment;
|
56
|
|
import org.w3c.dom.Document;
|
57
|
|
import org.w3c.dom.Element;
|
58
|
|
import org.w3c.dom.NamedNodeMap;
|
59
|
|
import org.w3c.dom.Node;
|
60
|
|
import org.w3c.dom.NodeList;
|
61
|
|
import org.w3c.dom.ProcessingInstruction;
|
62
|
|
import org.w3c.dom.Text;
|
63
|
|
|
64
|
|
|
65
|
|
|
66
|
|
|
67
|
|
|
68
|
|
|
69
|
|
|
70
|
|
public class DomToGroovy {
|
71
|
|
|
72
|
|
private IndentPrinter out;
|
73
|
|
|
74
|
0
|
public DomToGroovy(PrintWriter out) {
|
75
|
0
|
this(new IndentPrinter(out));
|
76
|
|
}
|
77
|
|
|
78
|
0
|
public DomToGroovy(IndentPrinter out) {
|
79
|
0
|
this.out = out;
|
80
|
|
}
|
81
|
|
|
82
|
|
|
83
|
0
|
public void print(Document document) {
|
84
|
0
|
printChildren(document, new HashMap());
|
85
|
|
}
|
86
|
|
|
87
|
|
|
88
|
|
|
89
|
0
|
protected void print(Node node, Map namespaces, boolean endWithComma) {
|
90
|
0
|
switch (node.getNodeType()) {
|
91
|
|
case Node.ELEMENT_NODE :
|
92
|
0
|
printElement((Element) node, namespaces, endWithComma);
|
93
|
0
|
break;
|
94
|
|
case Node.PROCESSING_INSTRUCTION_NODE :
|
95
|
0
|
printPI((ProcessingInstruction) node, endWithComma);
|
96
|
0
|
break;
|
97
|
|
case Node.TEXT_NODE :
|
98
|
0
|
printText((Text) node, endWithComma);
|
99
|
0
|
break;
|
100
|
|
case Node.COMMENT_NODE :
|
101
|
0
|
printComment((Comment) node, endWithComma);
|
102
|
0
|
break;
|
103
|
|
}
|
104
|
|
}
|
105
|
|
|
106
|
0
|
protected void printElement(Element element, Map namespaces, boolean endWithComma) {
|
107
|
0
|
namespaces = defineNamespaces(element, namespaces);
|
108
|
|
|
109
|
0
|
element.normalize();
|
110
|
0
|
printIndent();
|
111
|
|
|
112
|
0
|
String prefix = element.getPrefix();
|
113
|
0
|
if (prefix != null && prefix.length() > 0) {
|
114
|
0
|
print(prefix);
|
115
|
0
|
print(".");
|
116
|
|
}
|
117
|
0
|
print(getLocalName(element));
|
118
|
|
|
119
|
0
|
boolean hasAttributes = printAttributes(element);
|
120
|
|
|
121
|
0
|
NodeList list = element.getChildNodes();
|
122
|
0
|
int length = list.getLength();
|
123
|
0
|
if (length == 0) {
|
124
|
0
|
printEnd("", endWithComma);
|
125
|
|
}
|
126
|
|
else {
|
127
|
0
|
Node node = list.item(0);
|
128
|
0
|
if (length == 1 && node instanceof Text) {
|
129
|
0
|
Text textNode = (Text) node;
|
130
|
0
|
String text = getTextNodeData(textNode);
|
131
|
0
|
if (hasAttributes) {
|
132
|
0
|
print(" [\"");
|
133
|
0
|
print(text);
|
134
|
0
|
printEnd("\"]", endWithComma);
|
135
|
|
}
|
136
|
|
else {
|
137
|
0
|
print("(\"");
|
138
|
0
|
print(text);
|
139
|
0
|
printEnd("\")", endWithComma);
|
140
|
|
}
|
141
|
|
}
|
142
|
0
|
else if (mixedContent(list)) {
|
143
|
0
|
println(" [");
|
144
|
0
|
out.incrementIndent();
|
145
|
0
|
for (node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
|
146
|
0
|
boolean useComma = node.getNextSibling() != null;
|
147
|
0
|
print(node, namespaces, useComma);
|
148
|
|
}
|
149
|
0
|
out.decrementIndent();
|
150
|
0
|
printIndent();
|
151
|
0
|
printEnd("]", endWithComma);
|
152
|
|
}
|
153
|
|
else {
|
154
|
0
|
println(" {");
|
155
|
0
|
out.incrementIndent();
|
156
|
0
|
printChildren(element, namespaces);
|
157
|
0
|
out.decrementIndent();
|
158
|
0
|
printIndent();
|
159
|
0
|
printEnd("}", endWithComma);
|
160
|
|
}
|
161
|
|
}
|
162
|
|
}
|
163
|
|
|
164
|
0
|
protected void printPI(ProcessingInstruction instruction, boolean endWithComma) {
|
165
|
0
|
printIndent();
|
166
|
0
|
print("xml.pi('");
|
167
|
0
|
print(instruction.getTarget());
|
168
|
0
|
print("', '");
|
169
|
0
|
print(instruction.getData());
|
170
|
0
|
printEnd("');", endWithComma);
|
171
|
|
}
|
172
|
|
|
173
|
0
|
protected void printComment(Comment comment, boolean endWithComma) {
|
174
|
0
|
String text = comment.getData().trim();
|
175
|
0
|
if (text.length() >0) {
|
176
|
0
|
printIndent();
|
177
|
0
|
print("/* ");
|
178
|
0
|
print(text);
|
179
|
0
|
printEnd(" */", endWithComma);
|
180
|
|
}
|
181
|
|
}
|
182
|
|
|
183
|
0
|
protected void printText(Text node, boolean endWithComma) {
|
184
|
0
|
String text = getTextNodeData(node);
|
185
|
0
|
if (text.length() > 0) {
|
186
|
0
|
printIndent();
|
187
|
|
|
188
|
|
|
189
|
|
|
190
|
0
|
print("\"");
|
191
|
0
|
print(text);
|
192
|
0
|
printEnd("\"", endWithComma);
|
193
|
|
}
|
194
|
|
}
|
195
|
|
|
196
|
0
|
protected Map defineNamespaces(Element element, Map namespaces) {
|
197
|
0
|
Map answer = null;
|
198
|
0
|
String prefix = element.getPrefix();
|
199
|
0
|
if (prefix != null && prefix.length() > 0 && !namespaces.containsKey(prefix)) {
|
200
|
0
|
answer = new HashMap(namespaces);
|
201
|
0
|
defineNamespace(answer, prefix, element.getNamespaceURI());
|
202
|
|
}
|
203
|
0
|
NamedNodeMap attributes = element.getAttributes();
|
204
|
0
|
int length = attributes.getLength();
|
205
|
0
|
for (int i = 0; i < length; i++) {
|
206
|
0
|
Attr attribute = (Attr) attributes.item(i);
|
207
|
0
|
prefix = attribute.getPrefix();
|
208
|
0
|
if (prefix != null && prefix.length() > 0 && !namespaces.containsKey(prefix)) {
|
209
|
0
|
if (answer == null) {
|
210
|
0
|
answer = new HashMap(namespaces);
|
211
|
|
}
|
212
|
0
|
defineNamespace(answer, prefix, attribute.getNamespaceURI());
|
213
|
|
}
|
214
|
|
}
|
215
|
0
|
return (answer != null) ? answer : namespaces;
|
216
|
|
}
|
217
|
|
|
218
|
0
|
protected void defineNamespace(Map namespaces, String prefix, String uri) {
|
219
|
0
|
namespaces.put(prefix, uri);
|
220
|
0
|
if (!prefix.equals("xmlns") && !prefix.equals("xml")) {
|
221
|
0
|
printIndent();
|
222
|
0
|
print(prefix);
|
223
|
0
|
print(" = xmlns.namespace('");
|
224
|
0
|
print(uri);
|
225
|
0
|
println("')");
|
226
|
|
}
|
227
|
|
}
|
228
|
|
|
229
|
0
|
protected boolean printAttributes(Element element) {
|
230
|
0
|
boolean hasAttribute = false;
|
231
|
|
|
232
|
0
|
NamedNodeMap attributes = element.getAttributes();
|
233
|
0
|
int length = attributes.getLength();
|
234
|
0
|
if (length > 0) {
|
235
|
0
|
StringBuffer buffer = new StringBuffer();
|
236
|
0
|
for (int i = 0; i < length; i++) {
|
237
|
0
|
Attr attribute = (Attr) attributes.item(i);
|
238
|
0
|
String prefix = attribute.getPrefix();
|
239
|
0
|
if (prefix != null && prefix.length() > 0) {
|
240
|
0
|
if (buffer.length() > 0) {
|
241
|
0
|
buffer.append(", ");
|
242
|
|
}
|
243
|
0
|
buffer.append(prefix);
|
244
|
0
|
buffer.append(".");
|
245
|
0
|
buffer.append(getLocalName(attribute));
|
246
|
0
|
buffer.append(":'");
|
247
|
0
|
buffer.append(attribute.getValue());
|
248
|
0
|
buffer.append("'");
|
249
|
|
}
|
250
|
|
}
|
251
|
|
|
252
|
0
|
print("(");
|
253
|
0
|
for (int i = 0; i < length; i++) {
|
254
|
0
|
Attr attribute = (Attr) attributes.item(i);
|
255
|
0
|
String prefix = attribute.getPrefix();
|
256
|
0
|
if (prefix == null || prefix.length() == 0) {
|
257
|
0
|
if (!hasAttribute) {
|
258
|
0
|
hasAttribute = true;
|
259
|
|
}
|
260
|
|
else {
|
261
|
0
|
print(", ");
|
262
|
|
}
|
263
|
0
|
print(getLocalName(attribute));
|
264
|
0
|
print(":'");
|
265
|
0
|
print(attribute.getValue());
|
266
|
0
|
print("'");
|
267
|
|
}
|
268
|
|
}
|
269
|
0
|
if (buffer.length() > 0) {
|
270
|
0
|
if (hasAttribute) {
|
271
|
0
|
print(", ");
|
272
|
|
}
|
273
|
0
|
print("xmlns=[");
|
274
|
0
|
print(buffer.toString());
|
275
|
0
|
print("]");
|
276
|
0
|
hasAttribute = true;
|
277
|
|
}
|
278
|
0
|
print(")");
|
279
|
|
}
|
280
|
0
|
return hasAttribute;
|
281
|
|
}
|
282
|
|
|
283
|
0
|
protected String getTextNodeData(Text node) {
|
284
|
0
|
String text = node.getData().trim();
|
285
|
0
|
return text;
|
286
|
|
}
|
287
|
|
|
288
|
0
|
protected boolean mixedContent(NodeList list) {
|
289
|
0
|
boolean hasText = false;
|
290
|
0
|
boolean hasElement = false;
|
291
|
0
|
for (int i = 0, size = list.getLength(); i < size; i++) {
|
292
|
0
|
Node node = list.item(i);
|
293
|
0
|
if (node instanceof Element) {
|
294
|
0
|
hasElement = true;
|
295
|
|
}
|
296
|
0
|
else if (node instanceof Text) {
|
297
|
0
|
String text = getTextNodeData((Text) node);
|
298
|
0
|
if (text.length() > 0) {
|
299
|
0
|
hasText = true;
|
300
|
|
}
|
301
|
|
}
|
302
|
|
}
|
303
|
0
|
return hasText && hasElement;
|
304
|
|
}
|
305
|
|
|
306
|
0
|
protected void printChildren(Node parent, Map namespaces) {
|
307
|
0
|
for (Node node = parent.getFirstChild(); node != null; node = node.getNextSibling()) {
|
308
|
0
|
print(node, namespaces, false);
|
309
|
|
}
|
310
|
|
}
|
311
|
|
|
312
|
0
|
protected String getLocalName(Node node) {
|
313
|
0
|
String answer = node.getLocalName();
|
314
|
0
|
if (answer == null) {
|
315
|
0
|
answer = node.getNodeName();
|
316
|
|
}
|
317
|
0
|
return answer.trim();
|
318
|
|
}
|
319
|
|
|
320
|
0
|
protected void printEnd(String text, boolean endWithComma) {
|
321
|
0
|
if (endWithComma) {
|
322
|
0
|
print(text);
|
323
|
0
|
println(",");
|
324
|
|
}
|
325
|
|
else {
|
326
|
0
|
println(text);
|
327
|
|
}
|
328
|
|
}
|
329
|
|
|
330
|
0
|
protected void println(String text) {
|
331
|
0
|
out.println(text); }
|
332
|
|
|
333
|
0
|
protected void print(String text) {
|
334
|
0
|
out.print(text);
|
335
|
|
}
|
336
|
|
|
337
|
0
|
protected void printIndent() {
|
338
|
0
|
out.printIndent();
|
339
|
|
}
|
340
|
|
}
|
341
|
|
|