SimpleXml.java
10.3 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* Decompiled with CFR 0_118.
*/
package com.day.cq.commons;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Stack;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
public class SimpleXml {
private boolean indentSet;
private boolean encodingSet;
private boolean inited;
private PrintWriter writer;
private TransformerHandler handler;
private Transformer transformer;
private StreamResult result;
private Element currentElement;
private Stack<Element> openElements;
public SimpleXml(PrintWriter w) throws IOException {
try {
this.writer = w;
this.handler = ((SAXTransformerFactory)SAXTransformerFactory.newInstance()).newTransformerHandler();
this.result = new StreamResult(this.writer);
this.openElements = new Stack();
}
catch (TransformerException te) {
throw new IOException(te.getMessage());
}
}
public PrintWriter getWriter() {
return this.writer;
}
public SimpleXml setIndent(boolean indent) {
this.getTransformer().setOutputProperty("indent", indent ? "yes" : "no");
this.indentSet = true;
return this;
}
public SimpleXml setEncoding(String encoding) {
this.getTransformer().setOutputProperty("encoding", encoding);
this.encodingSet = true;
return this;
}
public SimpleXml omitXmlDeclaration(boolean omit) {
this.getTransformer().setOutputProperty("omit-xml-declaration", omit ? "yes" : "no");
return this;
}
public SimpleXml setDocumentLocator(Locator locator) {
this.handler.setDocumentLocator(locator);
return this;
}
public SimpleXml open() throws IOException {
return this.openDocument();
}
public SimpleXml openDocument() throws IOException {
if (!"yes".equals(this.getTransformer().getOutputProperty("omit-xml-declaration"))) {
this.init();
try {
this.handler.startDocument();
}
catch (SAXException se) {
throw new IOException(se.getMessage());
}
}
return this;
}
public Element open(String name) throws IOException {
return this.open("", "", name);
}
public Element open(String localName, String name) throws IOException {
return this.open("", localName, name);
}
public Element open(String uri, String localName, String name) throws IOException {
this.startElement(this.currentElement);
this.currentElement = this.openElements.push(new Element(uri, localName, name));
return this.currentElement;
}
public Element open(String name, String content, boolean cdata) throws IOException {
Element element = this.open("", "", name);
element.setText(content, cdata);
return element;
}
public void closeDocument() throws IOException {
this.tidyUp();
this.init();
if (!"yes".equals(this.getTransformer().getOutputProperty("omit-xml-declaration"))) {
try {
this.handler.endDocument();
}
catch (SAXException se) {
throw new IOException(se.getMessage());
}
}
}
public SimpleXml close() throws IOException {
this.endElement(this.openElements.pop());
return this;
}
public SimpleXml tidyUp() throws IOException {
while (!this.openElements.empty()) {
Element element = this.openElements.pop();
this.endElement(element);
}
return this;
}
private void init() {
if (!this.inited) {
this.handler.setResult(this.result);
if (!this.indentSet) {
this.setIndent(true);
}
if (!this.encodingSet) {
this.setEncoding("utf-8");
}
this.inited = true;
}
}
private Transformer getTransformer() {
if (this.transformer == null) {
this.transformer = this.handler.getTransformer();
}
return this.transformer;
}
private void startElement(Element element) throws IOException {
if (element == null) {
return;
}
if (!element.isOpened()) {
this.init();
try {
String uri = element.getUri();
String localName = element.getLocalName();
String name = element.getName();
AttributesImpl atts = element.getAttributes();
this.handler.startElement(uri, localName, name, atts);
}
catch (SAXException se) {
throw new IOException(se.getMessage());
}
element.setOpened(true);
}
}
private void endElement(Element element) throws IOException {
if (element == null) {
return;
}
if (!element.isClosed()) {
this.startElement(element);
this.init();
try {
String uri = element.getUri();
String localName = element.getLocalName();
String name = element.getName();
String content = element.getText();
if (content != null) {
if (element.hasCDATA()) {
this.handler.startCDATA();
}
this.handler.characters(content.toCharArray(), 0, content.length());
if (element.hasCDATA()) {
this.handler.endCDATA();
}
}
this.handler.endElement(uri, localName, name);
}
catch (SAXException se) {
throw new IOException(se.getMessage());
}
element.setClosed(true);
this.currentElement = null;
}
this.openElements.remove(element);
}
public class Element {
private boolean opened;
private boolean closed;
private boolean cdata;
private AttributesImpl atts;
private String uri;
private String localName;
private String name;
private String text;
protected Element(String uri, String localName, String name) {
this.uri = uri;
this.localName = localName;
this.name = name;
this.atts = new AttributesImpl();
}
public Element attr(String name, String value) {
return this.attr("", "", name, value, "CDATA");
}
public Element attr(String localName, String name, String value) {
return this.attr("", localName, name, value, "CDATA");
}
public Element attr(String uri, String localName, String name, String value, String type) {
return this.addAttribute(uri, localName, name, value, type);
}
public /* varargs */ Element attrs(String[] ... atts) {
for (int i = 0; i < atts.length; ++i) {
this.attr(atts[i][0], atts[i][1]);
}
return this;
}
public Element addAttribute(String uri, String localName, String name, String value, String type) {
if (!this.opened) {
this.atts.addAttribute(uri, localName, name, type, value);
}
return this;
}
public Element text(String text) {
return this.text(text, false);
}
public Element text(String text, boolean cdata) {
return this.setText(text, cdata);
}
public Element setText(String text, boolean cdata) {
if (!this.closed) {
this.text = text;
this.cdata = cdata;
}
return this;
}
public Element setCDATA(boolean cdata) {
if (!this.closed) {
this.cdata = cdata;
}
return this;
}
public SimpleXml open() throws IOException {
if (!this.opened) {
SimpleXml.this.startElement(this);
}
return this.getWriter();
}
public Element open(String name) throws IOException {
this.open();
return this.getWriter().open(name);
}
public Element open(String localName, String name) throws IOException {
this.open();
return this.getWriter().open(localName, name);
}
public Element open(String uri, String localName, String name) throws IOException {
this.open();
return this.getWriter().open(uri, localName, name);
}
public Element open(String name, String content, boolean cdata) throws IOException {
this.open();
return this.getWriter().open(name, content, cdata);
}
public SimpleXml close() throws IOException {
if (!this.closed) {
SimpleXml.this.endElement(this);
}
return this.getWriter();
}
public boolean hasCDATA() {
return this.cdata;
}
protected SimpleXml getWriter() {
return SimpleXml.this;
}
protected boolean isOpened() {
return this.opened;
}
protected void setOpened(boolean opened) {
this.opened = opened;
}
protected boolean isClosed() {
return this.closed;
}
protected void setClosed(boolean closed) {
this.closed = closed;
}
protected AttributesImpl getAttributes() {
return this.atts;
}
protected String getUri() {
return this.uri;
}
protected String getLocalName() {
return this.localName;
}
protected String getName() {
return this.name;
}
protected String getText() {
return this.text;
}
}
}