OutputFormat.java
9.69 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
352
/*
* Decompiled with CFR 0_118.
*/
package com.day.jcr.vault.util.xml.serialize;
import com.day.jcr.vault.util.xml.serialize.EncodingInfo;
import com.day.jcr.vault.util.xml.serialize.Encodings;
import java.io.UnsupportedEncodingException;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Node;
import org.w3c.dom.html.HTMLDocument;
public class OutputFormat {
private String _method;
private String _version;
private int _indent = 0;
private boolean _breakEachAttribute = false;
private String _encoding = "UTF-8";
private EncodingInfo _encodingInfo = null;
private boolean _allowJavaNames = false;
private String _mediaType;
private String _doctypeSystem;
private String _doctypePublic;
private boolean _omitXmlDeclaration = false;
private boolean _omitDoctype = false;
private boolean _omitComments = false;
private boolean _stripComments = false;
private boolean _standalone = false;
private String[] _cdataElements;
private String[] _nonEscapingElements;
private String _lineSeparator = "\n";
private int _lineWidth = 72;
private boolean _preserve = false;
private boolean _preserveEmptyAttributes = false;
public OutputFormat() {
}
public OutputFormat(String method, String encoding, boolean indenting) {
this.setMethod(method);
this.setEncoding(encoding);
this.setIndenting(indenting);
}
public OutputFormat(Document doc) {
this.setMethod(OutputFormat.whichMethod(doc));
this.setDoctype(OutputFormat.whichDoctypePublic(doc), OutputFormat.whichDoctypeSystem(doc));
this.setMediaType(OutputFormat.whichMediaType(this.getMethod()));
}
public OutputFormat(Document doc, String encoding, boolean indenting) {
this(doc);
this.setEncoding(encoding);
this.setIndenting(indenting);
}
public String getMethod() {
return this._method;
}
public void setMethod(String method) {
this._method = method;
}
public String getVersion() {
return this._version;
}
public void setVersion(String version) {
this._version = version;
}
public int getIndent() {
return this._indent;
}
public boolean getIndenting() {
return this._indent > 0;
}
public void setIndent(int indent) {
this._indent = indent < 0 ? 0 : indent;
}
public void setIndenting(boolean on) {
if (on) {
this._indent = 4;
this._lineWidth = 72;
} else {
this._indent = 0;
this._lineWidth = 0;
}
}
public String getEncoding() {
return this._encoding;
}
public void setEncoding(String encoding) {
this._encoding = encoding;
this._encodingInfo = null;
}
public void setEncoding(EncodingInfo encInfo) {
this._encoding = encInfo.getIANAName();
this._encodingInfo = encInfo;
}
public EncodingInfo getEncodingInfo() throws UnsupportedEncodingException {
if (this._encodingInfo == null) {
this._encodingInfo = Encodings.getEncodingInfo(this._encoding, this._allowJavaNames);
}
return this._encodingInfo;
}
public void setAllowJavaNames(boolean allow) {
this._allowJavaNames = allow;
}
public boolean setAllowJavaNames() {
return this._allowJavaNames;
}
public String getMediaType() {
return this._mediaType;
}
public void setMediaType(String mediaType) {
this._mediaType = mediaType;
}
public void setDoctype(String publicId, String systemId) {
this._doctypePublic = publicId;
this._doctypeSystem = systemId;
}
public String getDoctypePublic() {
return this._doctypePublic;
}
public String getDoctypeSystem() {
return this._doctypeSystem;
}
public boolean getOmitComments() {
return this._omitComments;
}
public void setOmitComments(boolean omit) {
this._omitComments = omit;
}
public boolean getOmitDocumentType() {
return this._omitDoctype;
}
public void setOmitDocumentType(boolean omit) {
this._omitDoctype = omit;
}
public boolean getOmitXMLDeclaration() {
return this._omitXmlDeclaration;
}
public void setOmitXMLDeclaration(boolean omit) {
this._omitXmlDeclaration = omit;
}
public boolean getStandalone() {
return this._standalone;
}
public void setStandalone(boolean standalone) {
this._standalone = standalone;
}
public String[] getCDataElements() {
return this._cdataElements;
}
public boolean isCDataElement(String tagName) {
if (this._cdataElements == null) {
return false;
}
for (int i = 0; i < this._cdataElements.length; ++i) {
if (!this._cdataElements[i].equals(tagName)) continue;
return true;
}
return false;
}
public void setCDataElements(String[] cdataElements) {
this._cdataElements = cdataElements;
}
public String[] getNonEscapingElements() {
return this._nonEscapingElements;
}
public boolean isNonEscapingElement(String tagName) {
if (this._nonEscapingElements == null) {
return false;
}
for (int i = 0; i < this._nonEscapingElements.length; ++i) {
if (!this._nonEscapingElements[i].equals(tagName)) continue;
return true;
}
return false;
}
public void setNonEscapingElements(String[] nonEscapingElements) {
this._nonEscapingElements = nonEscapingElements;
}
public String getLineSeparator() {
return this._lineSeparator;
}
public void setLineSeparator(String lineSeparator) {
this._lineSeparator = lineSeparator == null ? "\n" : lineSeparator;
}
public boolean getPreserveSpace() {
return this._preserve;
}
public void setPreserveSpace(boolean preserve) {
this._preserve = preserve;
}
public int getLineWidth() {
return this._lineWidth;
}
public void setLineWidth(int lineWidth) {
this._lineWidth = lineWidth <= 0 ? 0 : lineWidth;
}
public boolean getBreakEachAttribute() {
return this._breakEachAttribute;
}
public void setBreakEachAttribute(boolean on) {
this._breakEachAttribute = on;
}
public boolean getPreserveEmptyAttributes() {
return this._preserveEmptyAttributes;
}
public void setPreserveEmptyAttributes(boolean preserve) {
this._preserveEmptyAttributes = preserve;
}
public char getLastPrintable() {
if (this.getEncoding() != null && this.getEncoding().equalsIgnoreCase("ASCII")) {
return '\u00ff';
}
return '\uffff';
}
public static String whichMethod(Document doc) {
if (doc instanceof HTMLDocument) {
return "html";
}
for (Node node = doc.getFirstChild(); node != null; node = node.getNextSibling()) {
if (node.getNodeType() == 1) {
if (node.getNodeName().equalsIgnoreCase("html")) {
return "html";
}
if (node.getNodeName().equalsIgnoreCase("root")) {
return "fop";
}
return "xml";
}
if (node.getNodeType() != 3) continue;
String value = node.getNodeValue();
for (int i = 0; i < value.length(); ++i) {
if (value.charAt(i) == ' ' || value.charAt(i) == '\n' || value.charAt(i) == '\t' || value.charAt(i) == '\r') continue;
return "xml";
}
}
return "xml";
}
public static String whichDoctypePublic(Document doc) {
DocumentType doctype = doc.getDoctype();
if (doctype != null) {
try {
return doctype.getPublicId();
}
catch (Error except) {
// empty catch block
}
}
if (doc instanceof HTMLDocument) {
return "-//W3C//DTD XHTML 1.0 Strict//EN";
}
return null;
}
public static String whichDoctypeSystem(Document doc) {
DocumentType doctype = doc.getDoctype();
if (doctype != null) {
try {
return doctype.getSystemId();
}
catch (Error except) {
// empty catch block
}
}
if (doc instanceof HTMLDocument) {
return "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
}
return null;
}
public static String whichMediaType(String method) {
if (method.equalsIgnoreCase("xml")) {
return "text/xml";
}
if (method.equalsIgnoreCase("html")) {
return "text/html";
}
if (method.equalsIgnoreCase("xhtml")) {
return "text/html";
}
if (method.equalsIgnoreCase("text")) {
return "text/plain";
}
if (method.equalsIgnoreCase("fop")) {
return "application/pdf";
}
return null;
}
public static class Defaults {
public static final int Indent = 4;
public static final String Encoding = "UTF-8";
public static final int LineWidth = 72;
}
public static class DTD {
public static final String HTMLPublicId = "-//W3C//DTD HTML 4.01//EN";
public static final String HTMLSystemId = "http://www.w3.org/TR/html4/strict.dtd";
public static final String XHTMLPublicId = "-//W3C//DTD XHTML 1.0 Strict//EN";
public static final String XHTMLSystemId = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
}
}