nokogiri 1.11.0.rc4-java → 1.11.5-java
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of nokogiri might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/Gemfile +3 -0
- data/LICENSE-DEPENDENCIES.md +12 -12
- data/LICENSE.md +1 -1
- data/README.md +168 -91
- data/dependencies.yml +12 -12
- data/ext/java/nokogiri/EncodingHandler.java +76 -89
- data/ext/java/nokogiri/HtmlDocument.java +135 -144
- data/ext/java/nokogiri/HtmlElementDescription.java +102 -117
- data/ext/java/nokogiri/HtmlEntityLookup.java +33 -60
- data/ext/java/nokogiri/HtmlSaxParserContext.java +218 -222
- data/ext/java/nokogiri/HtmlSaxPushParser.java +162 -169
- data/ext/java/nokogiri/NokogiriService.java +595 -556
- data/ext/java/nokogiri/XmlAttr.java +118 -126
- data/ext/java/nokogiri/XmlAttributeDecl.java +95 -106
- data/ext/java/nokogiri/XmlCdata.java +35 -58
- data/ext/java/nokogiri/XmlComment.java +46 -67
- data/ext/java/nokogiri/XmlDocument.java +645 -572
- data/ext/java/nokogiri/XmlDocumentFragment.java +125 -137
- data/ext/java/nokogiri/XmlDtd.java +448 -414
- data/ext/java/nokogiri/XmlElement.java +23 -48
- data/ext/java/nokogiri/XmlElementContent.java +343 -316
- data/ext/java/nokogiri/XmlElementDecl.java +124 -125
- data/ext/java/nokogiri/XmlEntityDecl.java +119 -127
- data/ext/java/nokogiri/XmlEntityReference.java +49 -72
- data/ext/java/nokogiri/XmlNamespace.java +175 -175
- data/ext/java/nokogiri/XmlNode.java +1843 -1620
- data/ext/java/nokogiri/XmlNodeSet.java +361 -331
- data/ext/java/nokogiri/XmlProcessingInstruction.java +47 -69
- data/ext/java/nokogiri/XmlReader.java +513 -450
- data/ext/java/nokogiri/XmlRelaxng.java +85 -104
- data/ext/java/nokogiri/XmlSaxParserContext.java +328 -315
- data/ext/java/nokogiri/XmlSaxPushParser.java +227 -220
- data/ext/java/nokogiri/XmlSchema.java +328 -295
- data/ext/java/nokogiri/XmlSyntaxError.java +113 -115
- data/ext/java/nokogiri/XmlText.java +55 -76
- data/ext/java/nokogiri/XmlXpathContext.java +240 -238
- data/ext/java/nokogiri/XsltStylesheet.java +280 -269
- data/ext/java/nokogiri/internals/ClosedStreamException.java +5 -2
- data/ext/java/nokogiri/internals/HtmlDomParserContext.java +201 -202
- data/ext/java/nokogiri/internals/IgnoreSchemaErrorsErrorHandler.java +17 -10
- data/ext/java/nokogiri/internals/NokogiriBlockingQueueInputStream.java +43 -16
- data/ext/java/nokogiri/internals/NokogiriDomParser.java +63 -80
- data/ext/java/nokogiri/internals/NokogiriEntityResolver.java +107 -88
- data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +27 -52
- data/ext/java/nokogiri/internals/NokogiriHandler.java +316 -286
- data/ext/java/nokogiri/internals/NokogiriHelpers.java +736 -652
- data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +184 -173
- data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +81 -98
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +64 -79
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +84 -99
- data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +48 -65
- data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +116 -131
- data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +34 -56
- data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +23 -46
- data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +55 -72
- data/ext/java/nokogiri/internals/ParserContext.java +206 -211
- data/ext/java/nokogiri/internals/ReaderNode.java +478 -403
- data/ext/java/nokogiri/internals/SaveContextVisitor.java +822 -739
- data/ext/java/nokogiri/internals/SchemaErrorHandler.java +31 -54
- data/ext/java/nokogiri/internals/XalanDTMManagerPatch.java +129 -123
- data/ext/java/nokogiri/internals/XmlDeclHandler.java +3 -34
- data/ext/java/nokogiri/internals/XmlDomParserContext.java +206 -207
- data/ext/java/nokogiri/internals/XmlSaxParser.java +22 -47
- data/ext/java/nokogiri/internals/c14n/AttrCompare.java +71 -68
- data/ext/java/nokogiri/internals/c14n/C14nHelper.java +137 -118
- data/ext/java/nokogiri/internals/c14n/CanonicalFilter.java +27 -21
- data/ext/java/nokogiri/internals/c14n/CanonicalizationException.java +74 -61
- data/ext/java/nokogiri/internals/c14n/Canonicalizer.java +230 -205
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11.java +572 -547
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_OmitComments.java +17 -10
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_WithComments.java +17 -10
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315.java +323 -302
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315Excl.java +232 -219
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclOmitComments.java +22 -15
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclWithComments.java +23 -16
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315OmitComments.java +23 -16
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315WithComments.java +22 -15
- data/ext/java/nokogiri/internals/c14n/CanonicalizerBase.java +575 -545
- data/ext/java/nokogiri/internals/c14n/CanonicalizerPhysical.java +141 -120
- data/ext/java/nokogiri/internals/c14n/CanonicalizerSpi.java +39 -38
- data/ext/java/nokogiri/internals/c14n/Constants.java +13 -10
- data/ext/java/nokogiri/internals/c14n/ElementProxy.java +279 -247
- data/ext/java/nokogiri/internals/c14n/HelperNodeList.java +66 -53
- data/ext/java/nokogiri/internals/c14n/IgnoreAllErrorHandler.java +44 -37
- data/ext/java/nokogiri/internals/c14n/InclusiveNamespaces.java +135 -120
- data/ext/java/nokogiri/internals/c14n/InvalidCanonicalizerException.java +59 -48
- data/ext/java/nokogiri/internals/c14n/NameSpaceSymbTable.java +384 -334
- data/ext/java/nokogiri/internals/c14n/NodeFilter.java +25 -24
- data/ext/java/nokogiri/internals/c14n/UtfHelpper.java +151 -140
- data/ext/java/nokogiri/internals/c14n/XMLUtils.java +456 -423
- data/ext/java/nokogiri/internals/dom2dtm/DOM2DTM.java +1466 -1500
- data/ext/java/nokogiri/internals/dom2dtm/DOM2DTMdefaultNamespaceDeclarationNode.java +626 -574
- data/ext/nokogiri/depend +34 -474
- data/ext/nokogiri/extconf.rb +270 -183
- data/ext/nokogiri/html_document.c +10 -15
- data/ext/nokogiri/html_element_description.c +84 -71
- data/ext/nokogiri/html_entity_lookup.c +21 -16
- data/ext/nokogiri/html_sax_parser_context.c +67 -64
- data/ext/nokogiri/html_sax_push_parser.c +42 -34
- data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
- data/ext/nokogiri/nokogiri.c +190 -60
- data/ext/nokogiri/test_global_handlers.c +40 -0
- data/ext/nokogiri/xml_attr.c +15 -15
- data/ext/nokogiri/xml_attribute_decl.c +18 -18
- data/ext/nokogiri/xml_cdata.c +13 -18
- data/ext/nokogiri/xml_comment.c +19 -26
- data/ext/nokogiri/xml_document.c +246 -188
- data/ext/nokogiri/xml_document_fragment.c +13 -15
- data/ext/nokogiri/xml_dtd.c +54 -48
- data/ext/nokogiri/xml_element_content.c +30 -27
- data/ext/nokogiri/xml_element_decl.c +22 -22
- data/ext/nokogiri/xml_encoding_handler.c +17 -11
- data/ext/nokogiri/xml_entity_decl.c +32 -30
- data/ext/nokogiri/xml_entity_reference.c +16 -18
- data/ext/nokogiri/xml_namespace.c +56 -49
- data/ext/nokogiri/xml_node.c +371 -320
- data/ext/nokogiri/xml_node_set.c +168 -156
- data/ext/nokogiri/xml_processing_instruction.c +17 -19
- data/ext/nokogiri/xml_reader.c +191 -157
- data/ext/nokogiri/xml_relax_ng.c +29 -23
- data/ext/nokogiri/xml_sax_parser.c +117 -112
- data/ext/nokogiri/xml_sax_parser_context.c +101 -84
- data/ext/nokogiri/xml_sax_push_parser.c +36 -27
- data/ext/nokogiri/xml_schema.c +48 -42
- data/ext/nokogiri/xml_syntax_error.c +42 -21
- data/ext/nokogiri/xml_text.c +13 -17
- data/ext/nokogiri/xml_xpath_context.c +134 -127
- data/ext/nokogiri/xslt_stylesheet.c +157 -157
- data/lib/nokogiri.rb +2 -6
- data/lib/nokogiri/css/parser.rb +1 -1
- data/lib/nokogiri/extension.rb +26 -0
- data/lib/nokogiri/html/document_fragment.rb +15 -15
- data/lib/nokogiri/nokogiri.jar +0 -0
- data/lib/nokogiri/version/constant.rb +1 -1
- data/lib/nokogiri/version/info.rb +32 -8
- data/lib/nokogiri/xml/document.rb +74 -28
- data/lib/nokogiri/xml/node.rb +39 -42
- data/lib/nokogiri/xml/reader.rb +2 -9
- data/lib/nokogiri/xml/xpath.rb +1 -3
- data/lib/nokogiri/xml/xpath/syntax_error.rb +1 -1
- metadata +62 -127
- data/ext/nokogiri/xml_io.c +0 -63
- data/ext/nokogiri/xml_libxml2_hacks.c +0 -112
@@ -1,35 +1,3 @@
|
|
1
|
-
/**
|
2
|
-
* (The MIT License)
|
3
|
-
*
|
4
|
-
* Copyright (c) 2008 - 2012:
|
5
|
-
*
|
6
|
-
* * {Aaron Patterson}[http://tenderlovemaking.com]
|
7
|
-
* * {Mike Dalessio}[http://mike.daless.io]
|
8
|
-
* * {Charles Nutter}[http://blog.headius.com]
|
9
|
-
* * {Sergio Arbeo}[http://www.serabe.com]
|
10
|
-
* * {Patrick Mahoney}[http://polycrystal.org]
|
11
|
-
* * {Yoko Harada}[http://yokolet.blogspot.com]
|
12
|
-
*
|
13
|
-
* Permission is hereby granted, free of charge, to any person obtaining
|
14
|
-
* a copy of this software and associated documentation files (the
|
15
|
-
* 'Software'), to deal in the Software without restriction, including
|
16
|
-
* without limitation the rights to use, copy, modify, merge, publish,
|
17
|
-
* distribute, sublicense, and/or sell copies of the Software, and to
|
18
|
-
* permit persons to whom the Software is furnished to do so, subject to
|
19
|
-
* the following conditions:
|
20
|
-
*
|
21
|
-
* The above copyright notice and this permission notice shall be
|
22
|
-
* included in all copies or substantial portions of the Software.
|
23
|
-
*
|
24
|
-
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
25
|
-
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
26
|
-
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
27
|
-
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
28
|
-
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
29
|
-
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
30
|
-
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
31
|
-
*/
|
32
|
-
|
33
1
|
package nokogiri.internals;
|
34
2
|
|
35
3
|
import static nokogiri.internals.NokogiriHelpers.canonicalizeWhitespace;
|
@@ -66,713 +34,828 @@ import org.w3c.dom.Text;
|
|
66
34
|
* @author Patrick Mahoney <pat@polycrystal.org>
|
67
35
|
* @author Yoko Harada <yokolet@gmail.com>
|
68
36
|
*/
|
69
|
-
public class SaveContextVisitor
|
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
|
-
buffer.append(
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
buffer.append(
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
37
|
+
public class SaveContextVisitor
|
38
|
+
{
|
39
|
+
|
40
|
+
private final StringBuilder buffer;
|
41
|
+
private final Stack<String> indentation;
|
42
|
+
private String encoding;
|
43
|
+
private final CharSequence indentString;
|
44
|
+
private boolean format;
|
45
|
+
private final boolean noDecl;
|
46
|
+
private final boolean noEmpty;
|
47
|
+
private final boolean noXhtml;
|
48
|
+
private final boolean asXhtml;
|
49
|
+
private boolean asXml;
|
50
|
+
private final boolean asHtml;
|
51
|
+
private final boolean asBuilder;
|
52
|
+
private boolean htmlDoc;
|
53
|
+
private final boolean fragment;
|
54
|
+
private final boolean canonical, incl_ns, with_comments;
|
55
|
+
private boolean subsets;
|
56
|
+
private boolean exclusive;
|
57
|
+
private final List<Node> c14nNodeList;
|
58
|
+
private final Deque<Attr[]> c14nNamespaceStack;
|
59
|
+
private final Deque<Attr[]> c14nAttrStack;
|
60
|
+
//private List<String> c14nExclusiveInclusivePrefixes = null;
|
61
|
+
|
62
|
+
/*
|
63
|
+
* U can't touch this.
|
64
|
+
* http://www.youtube.com/watch?v=WJ2ZFVx6A4Q
|
65
|
+
*
|
66
|
+
* Taken from libxml save options.
|
67
|
+
*/
|
68
|
+
|
69
|
+
public static final int FORMAT = 1;
|
70
|
+
public static final int NO_DECL = 2;
|
71
|
+
public static final int NO_EMPTY = 4;
|
72
|
+
public static final int NO_XHTML = 8;
|
73
|
+
public static final int AS_XHTML = 16;
|
74
|
+
public static final int AS_XML = 32;
|
75
|
+
public static final int AS_HTML = 64;
|
76
|
+
public static final int AS_BUILDER = 128;
|
77
|
+
|
78
|
+
public static final int CANONICAL = 1;
|
79
|
+
public static final int INCL_NS = 2;
|
80
|
+
public static final int WITH_COMMENTS = 4;
|
81
|
+
public static final int SUBSETS = 8;
|
82
|
+
public static final int EXCLUSIVE = 16;
|
83
|
+
|
84
|
+
public
|
85
|
+
SaveContextVisitor(int options, CharSequence indent, String encoding, boolean htmlDoc, boolean fragment,
|
86
|
+
int canonicalOpts)
|
87
|
+
{
|
88
|
+
buffer = new StringBuilder();
|
89
|
+
this.encoding = encoding;
|
90
|
+
indentation = new Stack<String>();
|
91
|
+
indentation.push("");
|
92
|
+
this.htmlDoc = htmlDoc;
|
93
|
+
this.fragment = fragment;
|
94
|
+
c14nNodeList = new ArrayList<Node>();
|
95
|
+
c14nNamespaceStack = new ArrayDeque<Attr[]>();
|
96
|
+
c14nAttrStack = new ArrayDeque<Attr[]>();
|
97
|
+
format = (options & FORMAT) == FORMAT;
|
98
|
+
|
99
|
+
noDecl = (options & NO_DECL) == NO_DECL;
|
100
|
+
noEmpty = (options & NO_EMPTY) == NO_EMPTY;
|
101
|
+
noXhtml = (options & NO_XHTML) == NO_XHTML;
|
102
|
+
asXhtml = (options & AS_XHTML) == AS_XHTML;
|
103
|
+
asXml = (options & AS_XML) == AS_XML;
|
104
|
+
asHtml = (options & AS_HTML) == AS_HTML;
|
105
|
+
asBuilder = (options & AS_BUILDER) == AS_BUILDER;
|
106
|
+
|
107
|
+
canonical = (canonicalOpts & CANONICAL) == CANONICAL;
|
108
|
+
incl_ns = (canonicalOpts & INCL_NS) == INCL_NS;
|
109
|
+
with_comments = (canonicalOpts & WITH_COMMENTS) == WITH_COMMENTS;
|
110
|
+
subsets = (canonicalOpts & SUBSETS) == SUBSETS;
|
111
|
+
|
112
|
+
if ((format && indent == null) || (format && indent.length() == 0)) { indent = " "; } // default, two spaces
|
113
|
+
if ((!format && indent != null) && indent.length() > 0) { format = true; }
|
114
|
+
if ((asBuilder && indent == null) || (asBuilder && indent.length() == 0)) { indent = " "; } // default, two spaces
|
115
|
+
indentString = indent;
|
116
|
+
if (!asXml && !asHtml && !asXhtml && !asBuilder) { asXml = true; }
|
117
|
+
}
|
118
|
+
|
119
|
+
@Override
|
120
|
+
public String
|
121
|
+
toString()
|
122
|
+
{
|
123
|
+
return buffer.toString();
|
124
|
+
}
|
125
|
+
|
126
|
+
public StringBuilder
|
127
|
+
getInternalBuffer() { return buffer; }
|
128
|
+
|
129
|
+
public void
|
130
|
+
setHtmlDoc(boolean htmlDoc)
|
131
|
+
{
|
132
|
+
this.htmlDoc = htmlDoc;
|
133
|
+
}
|
134
|
+
|
135
|
+
public void
|
136
|
+
setEncoding(String encoding)
|
137
|
+
{
|
138
|
+
this.encoding = encoding;
|
139
|
+
}
|
140
|
+
|
141
|
+
public boolean
|
142
|
+
enter(Node node)
|
143
|
+
{
|
144
|
+
if (node instanceof Document) {
|
145
|
+
return enter((Document)node);
|
146
|
+
}
|
147
|
+
if (node instanceof Element) {
|
148
|
+
return enter((Element)node);
|
149
|
+
}
|
150
|
+
if (node instanceof Attr) {
|
151
|
+
return enter((Attr)node);
|
152
|
+
}
|
153
|
+
if (node instanceof Text) {
|
154
|
+
return enter((Text)node);
|
155
|
+
}
|
156
|
+
if (node instanceof CDATASection) {
|
157
|
+
return enter((CDATASection)node);
|
158
|
+
}
|
159
|
+
if (node instanceof Comment) {
|
160
|
+
return enter((Comment)node);
|
161
|
+
}
|
162
|
+
if (node instanceof DocumentType) {
|
163
|
+
return enter((DocumentType)node);
|
164
|
+
}
|
165
|
+
if (node instanceof Entity) {
|
166
|
+
return enter((Entity)node);
|
167
|
+
}
|
168
|
+
if (node instanceof EntityReference) {
|
169
|
+
return enter((EntityReference) node);
|
170
|
+
}
|
171
|
+
if (node instanceof Notation) {
|
172
|
+
return enter((Notation)node);
|
173
|
+
}
|
174
|
+
if (node instanceof ProcessingInstruction) {
|
175
|
+
return enter((ProcessingInstruction)node);
|
176
|
+
}
|
177
|
+
return false;
|
178
|
+
}
|
179
|
+
|
180
|
+
public void
|
181
|
+
leave(Node node)
|
182
|
+
{
|
183
|
+
if (node instanceof Document) {
|
184
|
+
leave((Document)node);
|
185
|
+
return;
|
186
|
+
}
|
187
|
+
if (node instanceof Element) {
|
188
|
+
leave((Element)node);
|
189
|
+
return;
|
190
|
+
}
|
191
|
+
if (node instanceof Attr) {
|
192
|
+
leave((Attr)node);
|
193
|
+
return;
|
194
|
+
}
|
195
|
+
if (node instanceof Text) {
|
196
|
+
return;
|
197
|
+
}
|
198
|
+
if (node instanceof CDATASection) {
|
199
|
+
leave((CDATASection)node);
|
200
|
+
return;
|
201
|
+
}
|
202
|
+
if (node instanceof Comment) {
|
203
|
+
leave((Comment)node);
|
204
|
+
return;
|
205
|
+
}
|
206
|
+
if (node instanceof DocumentType) {
|
207
|
+
leave((DocumentType)node);
|
208
|
+
return;
|
209
|
+
}
|
210
|
+
if (node instanceof Entity) {
|
211
|
+
leave((Entity)node);
|
212
|
+
return;
|
213
|
+
}
|
214
|
+
if (node instanceof EntityReference) {
|
215
|
+
leave((EntityReference) node);
|
216
|
+
return;
|
217
|
+
}
|
218
|
+
if (node instanceof Notation) {
|
219
|
+
leave((Notation)node);
|
220
|
+
return;
|
221
|
+
}
|
222
|
+
if (node instanceof ProcessingInstruction) {
|
223
|
+
leave((ProcessingInstruction)node);
|
224
|
+
return;
|
225
|
+
}
|
226
|
+
}
|
227
|
+
|
228
|
+
public boolean
|
229
|
+
enter(String string)
|
230
|
+
{
|
231
|
+
buffer.append(string);
|
232
|
+
return true;
|
233
|
+
}
|
234
|
+
|
235
|
+
public void
|
236
|
+
leave(String string)
|
237
|
+
{
|
238
|
+
// no-op
|
239
|
+
}
|
240
|
+
|
241
|
+
public boolean
|
242
|
+
enter(Attr attr)
|
243
|
+
{
|
244
|
+
String name = attr.getName();
|
245
|
+
buffer.append(name);
|
246
|
+
if (!asHtml || !isHtmlBooleanAttr(name)) {
|
247
|
+
buffer.append('=');
|
248
|
+
buffer.append('"');
|
249
|
+
String value = replaceCharsetIfNecessary(attr);
|
250
|
+
buffer.append(serializeAttrTextContent(value, htmlDoc));
|
251
|
+
buffer.append('"');
|
252
|
+
}
|
253
|
+
return true;
|
254
|
+
}
|
255
|
+
|
256
|
+
private static final Pattern CHARSET =
|
257
|
+
Pattern.compile("charset(()|\\s+)=(()|\\s+)(\\w|\\_|\\.|\\-)+", Pattern.CASE_INSENSITIVE);
|
258
|
+
|
259
|
+
private String
|
260
|
+
replaceCharsetIfNecessary(Attr attr)
|
261
|
+
{
|
262
|
+
String value = attr.getValue();
|
263
|
+
if (encoding == null) { return value; } // unable to replace in any case
|
264
|
+
if (!"content".equals(attr.getName().toLowerCase())) { return value; } // must be content attr
|
265
|
+
if (!"meta".equals(attr.getOwnerElement().getNodeName().toLowerCase())) { return value; }
|
266
|
+
Matcher m = CHARSET.matcher(value);
|
267
|
+
if (!m.find()) { return value; }
|
268
|
+
if (value.contains(encoding)) { return value; } // no need to replace
|
269
|
+
return value.replace(m.group(), "charset=" + encoding);
|
270
|
+
}
|
271
|
+
|
272
|
+
static final Set<String> HTML_BOOLEAN_ATTRS;
|
273
|
+
static
|
274
|
+
{
|
275
|
+
final String[] _HTML_BOOLEAN_ATTRS = {
|
276
|
+
"checked", "compact", "declare", "defer", "disabled", "ismap",
|
277
|
+
"multiple", "nohref", "noresize", "noshade", "nowrap", "readonly",
|
278
|
+
"selected"
|
279
|
+
};
|
280
|
+
HTML_BOOLEAN_ATTRS = new HashSet<String>(Arrays.asList(_HTML_BOOLEAN_ATTRS));
|
281
|
+
}
|
282
|
+
|
283
|
+
private static boolean
|
284
|
+
isHtmlBooleanAttr(String name)
|
285
|
+
{
|
286
|
+
return HTML_BOOLEAN_ATTRS.contains(name);
|
287
|
+
}
|
288
|
+
|
289
|
+
private static CharSequence
|
290
|
+
serializeAttrTextContent(String str, boolean htmlDoc)
|
291
|
+
{
|
292
|
+
if (str == null || str.length() == 0) { return ""; }
|
293
|
+
|
294
|
+
StringBuilder buffer = new StringBuilder(str.length() + 16);
|
295
|
+
|
296
|
+
for (int i = 0; i < str.length(); i++) {
|
297
|
+
char c;
|
298
|
+
switch (c = str.charAt(i)) {
|
299
|
+
case '\n':
|
300
|
+
buffer.append(" ");
|
301
|
+
break;
|
302
|
+
case '\r':
|
303
|
+
buffer.append(" ");
|
304
|
+
break;
|
305
|
+
case '\t':
|
306
|
+
buffer.append("	");
|
307
|
+
break;
|
308
|
+
case '"':
|
309
|
+
if (htmlDoc) { buffer.append("%22"); }
|
310
|
+
else { buffer.append("""); }
|
311
|
+
break;
|
312
|
+
case '<':
|
313
|
+
buffer.append("<");
|
314
|
+
break;
|
315
|
+
case '>':
|
316
|
+
buffer.append(">");
|
317
|
+
break;
|
318
|
+
case '&':
|
319
|
+
buffer.append("&");
|
320
|
+
break;
|
321
|
+
default:
|
322
|
+
buffer.append(c);
|
323
|
+
}
|
324
|
+
}
|
325
|
+
|
326
|
+
return buffer;
|
327
|
+
}
|
328
|
+
|
329
|
+
public void
|
330
|
+
leave(Attr attr)
|
331
|
+
{
|
332
|
+
// no-op
|
333
|
+
}
|
334
|
+
|
335
|
+
public boolean
|
336
|
+
enter(CDATASection cdata)
|
337
|
+
{
|
338
|
+
buffer.append("<![CDATA[");
|
339
|
+
buffer.append(cdata.getData());
|
340
|
+
buffer.append("]]>");
|
341
|
+
return true;
|
342
|
+
}
|
343
|
+
|
344
|
+
public void
|
345
|
+
leave(CDATASection cdata)
|
346
|
+
{
|
347
|
+
// no-op
|
348
|
+
}
|
349
|
+
|
350
|
+
public boolean
|
351
|
+
enter(Comment comment)
|
352
|
+
{
|
353
|
+
if (canonical) {
|
354
|
+
c14nNodeList.add(comment);
|
355
|
+
if (!with_comments) { return true; }
|
356
|
+
}
|
357
|
+
buffer.append("<!--");
|
358
|
+
buffer.append(comment.getData());
|
359
|
+
buffer.append("-->");
|
360
|
+
return true;
|
361
|
+
}
|
362
|
+
|
363
|
+
public void
|
364
|
+
leave(Comment comment)
|
365
|
+
{
|
366
|
+
// no-op
|
367
|
+
}
|
368
|
+
|
369
|
+
public boolean
|
370
|
+
enter(Document document)
|
371
|
+
{
|
372
|
+
if (!noDecl) {
|
373
|
+
buffer.append("<?xml version=\"");
|
374
|
+
buffer.append(document.getXmlVersion());
|
375
|
+
buffer.append("\"");
|
376
|
+
|
377
|
+
if (encoding != null) {
|
378
|
+
buffer.append(" encoding=\"");
|
379
|
+
buffer.append(encoding);
|
380
|
+
buffer.append("\"");
|
381
|
+
}
|
382
|
+
buffer.append("?>\n");
|
383
|
+
}
|
384
|
+
return true;
|
385
|
+
}
|
386
|
+
|
387
|
+
public void
|
388
|
+
leave(Document document)
|
389
|
+
{
|
390
|
+
// no-op
|
391
|
+
}
|
392
|
+
|
393
|
+
public boolean
|
394
|
+
enter(DocumentType docType)
|
395
|
+
{
|
396
|
+
if (canonical) {
|
397
|
+
c14nNodeList.add(docType);
|
398
|
+
return true;
|
399
|
+
}
|
400
|
+
String name = docType.getName();
|
401
|
+
String pubId = docType.getPublicId();
|
402
|
+
String sysId = docType.getSystemId();
|
403
|
+
String internalSubset = docType.getInternalSubset();
|
404
|
+
if (docType.getPreviousSibling() != null) {
|
405
|
+
buffer.append('\n');
|
406
|
+
}
|
407
|
+
buffer.append("<!DOCTYPE ").append(name).append(' ');
|
408
|
+
if (pubId != null) {
|
409
|
+
buffer.append("PUBLIC \"").append(pubId).append('"');
|
410
|
+
if (sysId != null) { buffer.append(" \"").append(sysId).append('"'); }
|
411
|
+
} else if (sysId != null) {
|
412
|
+
buffer.append("SYSTEM \"").append(sysId).append('"');
|
413
|
+
}
|
414
|
+
if (internalSubset != null) {
|
415
|
+
buffer.append(' ').append('[');
|
416
|
+
buffer.append(internalSubset);
|
417
|
+
buffer.append(']');
|
418
|
+
}
|
419
|
+
buffer.append(">\n");
|
420
|
+
return true;
|
421
|
+
}
|
422
|
+
|
423
|
+
public void
|
424
|
+
leave(DocumentType docType)
|
425
|
+
{
|
426
|
+
// no-op
|
427
|
+
}
|
428
|
+
|
429
|
+
public boolean
|
430
|
+
enter(Element element)
|
431
|
+
{
|
432
|
+
if (canonical) {
|
433
|
+
c14nNodeList.add(element);
|
434
|
+
if (element == element.getOwnerDocument().getDocumentElement()) {
|
435
|
+
c14nNodeList.add(element.getOwnerDocument());
|
436
|
+
}
|
437
|
+
}
|
438
|
+
String current = indentation.peek();
|
439
|
+
buffer.append(current);
|
440
|
+
if (needIndent(element)) {
|
441
|
+
indentation.push(current + indentString);
|
442
|
+
}
|
443
|
+
String name = element.getTagName();
|
444
|
+
buffer.append('<').append(name);
|
445
|
+
Attr[] attrs = getAttrsAndNamespaces(element);
|
446
|
+
for (Attr attr : attrs) {
|
447
|
+
if (attr.getSpecified()) {
|
448
|
+
buffer.append(' ');
|
449
|
+
enter(attr);
|
450
|
+
leave(attr);
|
451
|
+
}
|
452
|
+
}
|
453
|
+
if (element.hasChildNodes()) {
|
454
|
+
buffer.append('>');
|
455
|
+
if (needBreakInOpening(element)) { buffer.append('\n'); }
|
456
|
+
return true;
|
457
|
+
}
|
458
|
+
// no child
|
459
|
+
if (asHtml) {
|
460
|
+
buffer.append('>');
|
461
|
+
} else if (asXml && noEmpty) {
|
462
|
+
buffer.append('>');
|
463
|
+
} else if (asXhtml) {
|
464
|
+
if (isEmpty(name)) {
|
465
|
+
buffer.append(" />"); // see http://www.w3.org/TR/xhtml1/#C_2
|
466
|
+
} else {
|
467
|
+
buffer.append('>');
|
468
|
+
}
|
469
|
+
} else {
|
470
|
+
buffer.append("/>");
|
471
|
+
}
|
472
|
+
if (needBreakInOpening(element)) {
|
473
|
+
buffer.append('\n');
|
474
|
+
}
|
475
|
+
return true;
|
476
|
+
}
|
477
|
+
|
478
|
+
private boolean
|
479
|
+
needIndent(Element element)
|
480
|
+
{
|
481
|
+
if (containsText(element)) { return false; }
|
482
|
+
if (fragment) { return false; } // a given option might be fragment and format. fragment matters
|
483
|
+
if (format || asBuilder) { return true; }
|
484
|
+
return false;
|
485
|
+
}
|
486
|
+
|
487
|
+
private boolean
|
488
|
+
needBreakInOpening(Element element)
|
489
|
+
{
|
490
|
+
if (containsText(element)) { return false; }
|
491
|
+
if (fragment) { return false; }
|
492
|
+
if (format) { return true; }
|
493
|
+
if (asBuilder && element.getFirstChild() != null && element.getFirstChild().getNodeType() == Node.ELEMENT_NODE) { return true; }
|
494
|
+
if (format && element.getNextSibling() == null && element.hasChildNodes()) { return true; }
|
495
|
+
return false;
|
496
|
+
}
|
497
|
+
|
498
|
+
private boolean
|
499
|
+
isEmpty(String name)
|
500
|
+
{
|
501
|
+
HTMLElements.Element element = HTMLElements.getElement(name);
|
502
|
+
return element.isEmpty();
|
503
|
+
}
|
504
|
+
|
505
|
+
private Attr[]
|
506
|
+
getAttrsAndNamespaces(Element element)
|
507
|
+
{
|
508
|
+
NamedNodeMap attrs = element.getAttributes();
|
509
|
+
if (!canonical) {
|
510
|
+
if (attrs == null || attrs.getLength() == 0) { return new Attr[0]; }
|
511
|
+
Attr[] attrsAndNamespaces = new Attr[attrs.getLength()];
|
512
|
+
for (int i = 0; i < attrs.getLength(); i++) {
|
513
|
+
attrsAndNamespaces[i] = (Attr) attrs.item(i);
|
514
|
+
}
|
515
|
+
return attrsAndNamespaces;
|
516
|
+
} else {
|
517
|
+
List<Attr> namespaces = new ArrayList<Attr>();
|
518
|
+
List<Attr> attributes = new ArrayList<Attr>();
|
519
|
+
if (subsets) {
|
520
|
+
getAttrsOfAncestors(element.getParentNode(), namespaces, attributes);
|
521
|
+
Attr[] namespaceOfAncestors = getSortedArray(namespaces);
|
522
|
+
Attr[] attributeOfAncestors = getSortedArray(attributes);
|
523
|
+
c14nNamespaceStack.push(namespaceOfAncestors);
|
524
|
+
c14nAttrStack.push(attributeOfAncestors);
|
525
|
+
subsets = false; // namespace propagation should be done only once on top level node.
|
526
|
+
}
|
527
|
+
|
528
|
+
getNamespacesAndAttrs(element, namespaces, attributes);
|
529
|
+
|
530
|
+
Attr[] namespaceArray = getSortedArray(namespaces);
|
531
|
+
Attr[] attributeArray = getSortedArray(attributes);
|
532
|
+
Attr[] allAttrs = new Attr[namespaceArray.length + attributeArray.length];
|
533
|
+
for (int i = 0; i < allAttrs.length; i++) {
|
534
|
+
if (i < namespaceArray.length) {
|
535
|
+
allAttrs[i] = namespaceArray[i];
|
476
536
|
} else {
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
}
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
}
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
537
|
+
allAttrs[i] = attributeArray[i - namespaceArray.length];
|
538
|
+
}
|
539
|
+
}
|
540
|
+
c14nNamespaceStack.push(namespaceArray);
|
541
|
+
c14nAttrStack.push(attributeArray);
|
542
|
+
return allAttrs;
|
543
|
+
}
|
544
|
+
|
545
|
+
}
|
546
|
+
|
547
|
+
private void
|
548
|
+
getAttrsOfAncestors(Node parent, List<Attr> namespaces, List<Attr> attributes)
|
549
|
+
{
|
550
|
+
if (parent == null) { return; }
|
551
|
+
NamedNodeMap attrs = parent.getAttributes();
|
552
|
+
if (attrs == null || attrs.getLength() == 0) { return; }
|
553
|
+
for (int i = 0; i < attrs.getLength(); i++) {
|
554
|
+
Attr attr = (Attr)attrs.item(i);
|
555
|
+
if (isNamespace(attr.getNodeName())) { namespaces.add(attr); }
|
556
|
+
else { attributes.add(attr); }
|
557
|
+
}
|
558
|
+
getAttrsOfAncestors(parent.getParentNode(), namespaces, attributes);
|
559
|
+
}
|
560
|
+
|
561
|
+
private void
|
562
|
+
getNamespacesAndAttrs(Node current, List<Attr> namespaces, List<Attr> attributes)
|
563
|
+
{
|
564
|
+
NamedNodeMap attrs = current.getAttributes();
|
565
|
+
for (int i = 0; i < attrs.getLength(); i++) {
|
566
|
+
Attr attr = (Attr)attrs.item(i);
|
567
|
+
if (isNamespace(attr.getNodeName())) {
|
568
|
+
getNamespacesWithPropagated(namespaces, attr);
|
569
|
+
} else {
|
570
|
+
getAttributesWithPropagated(attributes, attr);
|
571
|
+
}
|
572
|
+
if (exclusive) {
|
573
|
+
verifyXmlSpace(attributes, attrs);
|
574
|
+
}
|
575
|
+
}
|
576
|
+
}
|
577
|
+
|
578
|
+
private void
|
579
|
+
getNamespacesWithPropagated(List<Attr> namespaces, Attr attr)
|
580
|
+
{
|
581
|
+
boolean newNamespace = true;
|
582
|
+
Iterator<Attr[]> iter = c14nNamespaceStack.iterator();
|
583
|
+
while (iter.hasNext()) {
|
584
|
+
Attr[] parentNamespaces = iter.next();
|
585
|
+
for (int n = 0; n < parentNamespaces.length; n++) {
|
586
|
+
if (parentNamespaces[n].getNodeName().equals(attr.getNodeName())) {
|
587
|
+
if (parentNamespaces[n].getNodeValue().equals(attr.getNodeValue())) {
|
588
|
+
// exactly the same namespace should not be added
|
589
|
+
newNamespace = false;
|
590
|
+
} else {
|
591
|
+
// in case of namespace url change, propagated namespace will be override
|
592
|
+
namespaces.remove(parentNamespaces[n]);
|
593
|
+
}
|
594
|
+
}
|
595
|
+
}
|
596
|
+
if (newNamespace && !namespaces.contains(attr)) { namespaces.add(attr); }
|
597
|
+
}
|
598
|
+
}
|
599
|
+
|
600
|
+
private void
|
601
|
+
getAttributesWithPropagated(List<Attr> attributes, Attr attr)
|
602
|
+
{
|
603
|
+
boolean newAttribute = true;
|
604
|
+
Iterator<Attr[]> iter = c14nAttrStack.iterator();
|
605
|
+
while (iter.hasNext()) {
|
606
|
+
Attr[] parentAttr = iter.next();
|
607
|
+
for (int n = 0; n < parentAttr.length; n++) {
|
608
|
+
if (!parentAttr[n].getNodeName().startsWith("xml:")) { continue; }
|
609
|
+
if (parentAttr[n].getNodeName().equals(attr.getNodeName())) {
|
610
|
+
if (parentAttr[n].getNodeValue().equals(attr.getNodeValue())) {
|
611
|
+
// exactly the same attribute should not be added
|
612
|
+
newAttribute = false;
|
613
|
+
} else {
|
614
|
+
// in case of attribute value change, propagated attribute will be override
|
615
|
+
attributes.remove(parentAttr[n]);
|
616
|
+
}
|
617
|
+
}
|
618
|
+
}
|
619
|
+
if (newAttribute) { attributes.add(attr); }
|
620
|
+
}
|
621
|
+
}
|
622
|
+
|
623
|
+
private void
|
624
|
+
verifyXmlSpace(List<Attr> attributes, NamedNodeMap attrs)
|
625
|
+
{
|
626
|
+
Attr attr = (Attr) attrs.getNamedItem("xml:space");
|
627
|
+
if (attr == null) {
|
628
|
+
for (int i = 0; i < attributes.size(); i++) {
|
629
|
+
if (attributes.get(i).getNodeName().equals("xml:space")) {
|
630
|
+
attributes.remove(i);
|
631
|
+
break;
|
632
|
+
}
|
633
|
+
}
|
634
|
+
}
|
635
|
+
}
|
636
|
+
|
637
|
+
private Attr[]
|
638
|
+
getSortedArray(List<Attr> attrList)
|
639
|
+
{
|
640
|
+
Attr[] attrArray = attrList.toArray(new Attr[0]);
|
641
|
+
Arrays.sort(attrArray, new Comparator<Attr>() {
|
642
|
+
@Override
|
643
|
+
public int compare(Attr attr0, Attr attr1) {
|
644
|
+
return attr0.getNodeName().compareTo(attr1.getNodeName());
|
645
|
+
}
|
646
|
+
});
|
647
|
+
return attrArray;
|
648
|
+
}
|
649
|
+
|
650
|
+
public void
|
651
|
+
leave(Element element)
|
652
|
+
{
|
653
|
+
if (canonical) {
|
654
|
+
c14nNamespaceStack.poll();
|
655
|
+
c14nAttrStack.poll();
|
656
|
+
}
|
657
|
+
String name = element.getTagName();
|
658
|
+
if (element.hasChildNodes()) {
|
659
|
+
if (needIndentInClosing(element)) {
|
660
|
+
indentation.pop();
|
661
|
+
buffer.append(indentation.peek());
|
662
|
+
} else if (asBuilder) {
|
663
|
+
if (!containsText(element)) { indentation.pop(); }
|
664
|
+
}
|
665
|
+
buffer.append("</").append(name).append('>');
|
666
|
+
if (needBreakInClosing(element)) {
|
667
|
+
buffer.append('\n');
|
668
|
+
}
|
669
|
+
return;
|
670
|
+
}
|
671
|
+
// no child, but HTML might need a closing tag.
|
672
|
+
if (asHtml || noEmpty) {
|
673
|
+
if (!isEmpty(name) && noEmpty) {
|
674
|
+
buffer.append("</").append(name).append('>');
|
675
|
+
}
|
676
|
+
}
|
677
|
+
if (needBreakInClosing(element)) {
|
678
|
+
if (!containsText(element)) { indentation.pop(); }
|
679
|
+
buffer.append('\n');
|
680
|
+
}
|
681
|
+
}
|
682
|
+
|
683
|
+
private boolean
|
684
|
+
needIndentInClosing(Element element)
|
685
|
+
{
|
686
|
+
if (containsText(element)) { return false; }
|
687
|
+
|
688
|
+
if (fragment) { return false; } // a given option might be fragment and format. fragment matters
|
689
|
+
if (format) { return true; }
|
690
|
+
if (asBuilder && element.getFirstChild() != null && element.getFirstChild().getNodeType() == Node.ELEMENT_NODE) { return true; }
|
691
|
+
return false;
|
692
|
+
}
|
693
|
+
|
694
|
+
private boolean
|
695
|
+
needBreakInClosing(Element element)
|
696
|
+
{
|
697
|
+
if (fragment) { return false; }
|
698
|
+
if (format || asBuilder) { return true; }
|
699
|
+
return false;
|
700
|
+
}
|
701
|
+
|
702
|
+
private boolean
|
703
|
+
containsText(Element element)
|
704
|
+
{
|
705
|
+
return (element.getFirstChild() != null && element.getFirstChild().getNodeType() == Node.TEXT_NODE);
|
706
|
+
}
|
707
|
+
|
708
|
+
public boolean
|
709
|
+
enter(Entity entity)
|
710
|
+
{
|
711
|
+
String name = entity.getNodeName();
|
712
|
+
String pubId = entity.getPublicId();
|
713
|
+
String sysId = entity.getSystemId();
|
714
|
+
String notation = entity.getNotationName();
|
715
|
+
buffer.append("<!ENTITY ");
|
716
|
+
buffer.append(name);
|
717
|
+
if (pubId != null) {
|
718
|
+
buffer.append(" PUBLIC \"");
|
719
|
+
buffer.append(pubId);
|
720
|
+
buffer.append("\"");
|
721
|
+
}
|
722
|
+
if (sysId != null) {
|
723
|
+
buffer.append(" SYSTEM \"");
|
724
|
+
buffer.append(sysId);
|
725
|
+
buffer.append("\"");
|
726
|
+
}
|
727
|
+
if (notation != null) {
|
728
|
+
buffer.append(" NDATA ");
|
729
|
+
buffer.append(notation);
|
730
|
+
}
|
731
|
+
buffer.append(">");
|
732
|
+
return true;
|
733
|
+
}
|
734
|
+
|
735
|
+
public void
|
736
|
+
leave(Entity entity)
|
737
|
+
{
|
738
|
+
// no-op
|
739
|
+
}
|
740
|
+
|
741
|
+
public boolean
|
742
|
+
enter(EntityReference entityRef)
|
743
|
+
{
|
744
|
+
buffer.append('&').append(entityRef.getNodeName()).append(';');
|
745
|
+
return true;
|
746
|
+
}
|
747
|
+
public void
|
748
|
+
leave(EntityReference entityRef)
|
749
|
+
{
|
750
|
+
// no-op
|
751
|
+
}
|
752
|
+
|
753
|
+
public boolean
|
754
|
+
enter(Notation notation)
|
755
|
+
{
|
756
|
+
String name = notation.getNodeName();
|
757
|
+
String pubId = notation.getPublicId();
|
758
|
+
String sysId = notation.getSystemId();
|
759
|
+
buffer.append("<!NOTATION ");
|
760
|
+
buffer.append(name);
|
761
|
+
if (pubId != null) {
|
762
|
+
buffer.append(" PUBLIC \"");
|
763
|
+
buffer.append(pubId);
|
764
|
+
buffer.append("\"");
|
765
|
+
if (sysId != null) {
|
766
|
+
buffer.append(" \"");
|
767
|
+
buffer.append(sysId);
|
768
|
+
buffer.append("\"");
|
769
|
+
}
|
770
|
+
} else if (sysId != null) {
|
771
|
+
buffer.append(" SYSTEM \"");
|
772
|
+
buffer.append(sysId);
|
773
|
+
buffer.append("\"");
|
774
|
+
}
|
775
|
+
buffer.append(">");
|
776
|
+
return true;
|
777
|
+
}
|
778
|
+
|
779
|
+
public void
|
780
|
+
leave(Notation notation)
|
781
|
+
{
|
782
|
+
// no-op
|
783
|
+
}
|
784
|
+
|
785
|
+
public boolean
|
786
|
+
enter(ProcessingInstruction pi)
|
787
|
+
{
|
788
|
+
buffer.append("<?");
|
789
|
+
buffer.append(pi.getTarget());
|
790
|
+
buffer.append(" ");
|
791
|
+
buffer.append(pi.getData());
|
792
|
+
if (asHtml) { buffer.append(">"); }
|
793
|
+
else { buffer.append("?>"); }
|
794
|
+
buffer.append("\n");
|
795
|
+
if (canonical) { c14nNodeList.add(pi); }
|
796
|
+
return true;
|
797
|
+
}
|
798
|
+
|
799
|
+
public void
|
800
|
+
leave(ProcessingInstruction pi)
|
801
|
+
{
|
802
|
+
// no-op
|
803
|
+
}
|
804
|
+
|
805
|
+
private boolean
|
806
|
+
isHtmlScript(Text text)
|
807
|
+
{
|
808
|
+
return htmlDoc && text.getParentNode().getNodeName().equals("script");
|
809
|
+
}
|
810
|
+
|
811
|
+
private boolean
|
812
|
+
isHtmlStyle(Text text)
|
813
|
+
{
|
814
|
+
return htmlDoc && text.getParentNode().getNodeName().equals("style");
|
815
|
+
}
|
816
|
+
|
817
|
+
public boolean
|
818
|
+
enter(Text text)
|
819
|
+
{
|
820
|
+
CharSequence textContent = text.getNodeValue();
|
821
|
+
if (canonical) {
|
822
|
+
c14nNodeList.add(text);
|
823
|
+
if (isBlank(textContent)) {
|
824
|
+
buffer.append(canonicalizeWhitespace(textContent));
|
706
825
|
return true;
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
buffer.append(canonicalizeWhitespace(textContent));
|
743
|
-
return true;
|
744
|
-
}
|
745
|
-
}
|
746
|
-
|
747
|
-
if (shouldEncode(text) && !isHtmlScript(text) && !isHtmlStyle(text)) {
|
748
|
-
textContent = encodeJavaString(textContent);
|
749
|
-
}
|
750
|
-
|
751
|
-
textContent = encodeStringToHtmlEntity(textContent);
|
752
|
-
buffer.append(textContent);
|
753
|
-
return true;
|
754
|
-
}
|
755
|
-
|
756
|
-
private CharSequence encodeStringToHtmlEntity(CharSequence text) {
|
757
|
-
if (encoding == null) return text;
|
758
|
-
|
759
|
-
CharsetEncoder encoder = Charset.forName(encoding).newEncoder();
|
760
|
-
StringBuilder sb = new StringBuilder(text.length() + 16);
|
761
|
-
// make sure we can handle code points that are higher than 2 bytes
|
762
|
-
for ( int i = 0; i < text.length(); ) {
|
763
|
-
int code = Character.codePointAt(text, i);
|
764
|
-
// TODO not sure about bigger offset then 2 ?!
|
765
|
-
int offset = code > 65535 ? 2 : 1;
|
766
|
-
CharSequence substr = text.subSequence(i, i + offset);
|
767
|
-
boolean canEncode = encoder.canEncode(substr);
|
768
|
-
if (canEncode) {
|
769
|
-
sb.append(substr);
|
770
|
-
}
|
771
|
-
else {
|
772
|
-
sb.append("&#x").append(Integer.toHexString(code)).append(';');
|
773
|
-
}
|
774
|
-
i += offset;
|
775
|
-
}
|
776
|
-
return sb;
|
777
|
-
}
|
826
|
+
}
|
827
|
+
}
|
828
|
+
|
829
|
+
if (shouldEncode(text) && !isHtmlScript(text) && !isHtmlStyle(text)) {
|
830
|
+
textContent = encodeJavaString(textContent);
|
831
|
+
}
|
832
|
+
|
833
|
+
textContent = encodeStringToHtmlEntity(textContent);
|
834
|
+
buffer.append(textContent);
|
835
|
+
return true;
|
836
|
+
}
|
837
|
+
|
838
|
+
private CharSequence
|
839
|
+
encodeStringToHtmlEntity(CharSequence text)
|
840
|
+
{
|
841
|
+
if (encoding == null) { return text; }
|
842
|
+
|
843
|
+
CharsetEncoder encoder = Charset.forName(encoding).newEncoder();
|
844
|
+
StringBuilder sb = new StringBuilder(text.length() + 16);
|
845
|
+
// make sure we can handle code points that are higher than 2 bytes
|
846
|
+
for (int i = 0; i < text.length();) {
|
847
|
+
int code = Character.codePointAt(text, i);
|
848
|
+
// TODO not sure about bigger offset then 2 ?!
|
849
|
+
int offset = code > 65535 ? 2 : 1;
|
850
|
+
CharSequence substr = text.subSequence(i, i + offset);
|
851
|
+
boolean canEncode = encoder.canEncode(substr);
|
852
|
+
if (canEncode) {
|
853
|
+
sb.append(substr);
|
854
|
+
} else {
|
855
|
+
sb.append("&#x").append(Integer.toHexString(code)).append(';');
|
856
|
+
}
|
857
|
+
i += offset;
|
858
|
+
}
|
859
|
+
return sb;
|
860
|
+
}
|
778
861
|
}
|