nokogiri-fitzsimmons 1.5.5.3-java
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +26 -0
- data/.gemtest +0 -0
- data/CHANGELOG.ja.rdoc +666 -0
- data/CHANGELOG.rdoc +659 -0
- data/C_CODING_STYLE.rdoc +33 -0
- data/Manifest.txt +295 -0
- data/README.ja.rdoc +106 -0
- data/README.rdoc +178 -0
- data/ROADMAP.md +86 -0
- data/Rakefile +194 -0
- data/STANDARD_RESPONSES.md +47 -0
- data/Y_U_NO_GEMSPEC.md +155 -0
- data/bin/nokogiri +63 -0
- data/build_all +58 -0
- data/ext/java/nokogiri/EncodingHandler.java +124 -0
- data/ext/java/nokogiri/HtmlDocument.java +163 -0
- data/ext/java/nokogiri/HtmlElementDescription.java +145 -0
- data/ext/java/nokogiri/HtmlEntityLookup.java +79 -0
- data/ext/java/nokogiri/HtmlSaxParserContext.java +259 -0
- data/ext/java/nokogiri/NokogiriService.java +598 -0
- data/ext/java/nokogiri/XmlAttr.java +190 -0
- data/ext/java/nokogiri/XmlAttributeDecl.java +130 -0
- data/ext/java/nokogiri/XmlCdata.java +84 -0
- data/ext/java/nokogiri/XmlComment.java +95 -0
- data/ext/java/nokogiri/XmlDocument.java +580 -0
- data/ext/java/nokogiri/XmlDocumentFragment.java +234 -0
- data/ext/java/nokogiri/XmlDtd.java +469 -0
- data/ext/java/nokogiri/XmlElement.java +97 -0
- data/ext/java/nokogiri/XmlElementContent.java +382 -0
- data/ext/java/nokogiri/XmlElementDecl.java +152 -0
- data/ext/java/nokogiri/XmlEntityDecl.java +162 -0
- data/ext/java/nokogiri/XmlEntityReference.java +97 -0
- data/ext/java/nokogiri/XmlNamespace.java +215 -0
- data/ext/java/nokogiri/XmlNode.java +1534 -0
- data/ext/java/nokogiri/XmlNodeSet.java +270 -0
- data/ext/java/nokogiri/XmlProcessingInstruction.java +99 -0
- data/ext/java/nokogiri/XmlReader.java +456 -0
- data/ext/java/nokogiri/XmlRelaxng.java +144 -0
- data/ext/java/nokogiri/XmlSaxParserContext.java +356 -0
- data/ext/java/nokogiri/XmlSaxPushParser.java +215 -0
- data/ext/java/nokogiri/XmlSchema.java +324 -0
- data/ext/java/nokogiri/XmlSyntaxError.java +136 -0
- data/ext/java/nokogiri/XmlText.java +119 -0
- data/ext/java/nokogiri/XmlXpathContext.java +203 -0
- data/ext/java/nokogiri/XsltStylesheet.java +360 -0
- data/ext/java/nokogiri/internals/HtmlDomParserContext.java +243 -0
- data/ext/java/nokogiri/internals/NokogiriDocumentCache.java +73 -0
- data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +86 -0
- data/ext/java/nokogiri/internals/NokogiriHandler.java +333 -0
- data/ext/java/nokogiri/internals/NokogiriHelpers.java +800 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +163 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +130 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +74 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +121 -0
- data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +79 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +141 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +73 -0
- data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +67 -0
- data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +87 -0
- data/ext/java/nokogiri/internals/ParserContext.java +288 -0
- data/ext/java/nokogiri/internals/ReaderNode.java +531 -0
- data/ext/java/nokogiri/internals/SaveContextVisitor.java +775 -0
- data/ext/java/nokogiri/internals/SchemaErrorHandler.java +76 -0
- data/ext/java/nokogiri/internals/XmlDeclHandler.java +42 -0
- data/ext/java/nokogiri/internals/XmlDomParser.java +105 -0
- data/ext/java/nokogiri/internals/XmlDomParserContext.java +266 -0
- data/ext/java/nokogiri/internals/XmlSaxParser.java +65 -0
- data/ext/java/nokogiri/internals/XsltExtensionFunction.java +72 -0
- data/ext/nokogiri/depend +358 -0
- data/ext/nokogiri/extconf.rb +142 -0
- data/ext/nokogiri/html_document.c +170 -0
- data/ext/nokogiri/html_document.h +10 -0
- data/ext/nokogiri/html_element_description.c +276 -0
- data/ext/nokogiri/html_element_description.h +10 -0
- data/ext/nokogiri/html_entity_lookup.c +32 -0
- data/ext/nokogiri/html_entity_lookup.h +8 -0
- data/ext/nokogiri/html_sax_parser_context.c +116 -0
- data/ext/nokogiri/html_sax_parser_context.h +11 -0
- data/ext/nokogiri/html_sax_push_parser.c +87 -0
- data/ext/nokogiri/html_sax_push_parser.h +9 -0
- data/ext/nokogiri/nokogiri.c +133 -0
- data/ext/nokogiri/nokogiri.h +160 -0
- data/ext/nokogiri/xml_attr.c +94 -0
- data/ext/nokogiri/xml_attr.h +9 -0
- data/ext/nokogiri/xml_attribute_decl.c +70 -0
- data/ext/nokogiri/xml_attribute_decl.h +9 -0
- data/ext/nokogiri/xml_cdata.c +56 -0
- data/ext/nokogiri/xml_cdata.h +9 -0
- data/ext/nokogiri/xml_comment.c +54 -0
- data/ext/nokogiri/xml_comment.h +9 -0
- data/ext/nokogiri/xml_document.c +576 -0
- data/ext/nokogiri/xml_document.h +23 -0
- data/ext/nokogiri/xml_document_fragment.c +48 -0
- data/ext/nokogiri/xml_document_fragment.h +10 -0
- data/ext/nokogiri/xml_dtd.c +202 -0
- data/ext/nokogiri/xml_dtd.h +10 -0
- data/ext/nokogiri/xml_element_content.c +123 -0
- data/ext/nokogiri/xml_element_content.h +10 -0
- data/ext/nokogiri/xml_element_decl.c +69 -0
- data/ext/nokogiri/xml_element_decl.h +9 -0
- data/ext/nokogiri/xml_encoding_handler.c +79 -0
- data/ext/nokogiri/xml_encoding_handler.h +8 -0
- data/ext/nokogiri/xml_entity_decl.c +110 -0
- data/ext/nokogiri/xml_entity_decl.h +10 -0
- data/ext/nokogiri/xml_entity_reference.c +52 -0
- data/ext/nokogiri/xml_entity_reference.h +9 -0
- data/ext/nokogiri/xml_io.c +56 -0
- data/ext/nokogiri/xml_io.h +11 -0
- data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
- data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
- data/ext/nokogiri/xml_namespace.c +78 -0
- data/ext/nokogiri/xml_namespace.h +13 -0
- data/ext/nokogiri/xml_node.c +1480 -0
- data/ext/nokogiri/xml_node.h +13 -0
- data/ext/nokogiri/xml_node_set.c +467 -0
- data/ext/nokogiri/xml_node_set.h +14 -0
- data/ext/nokogiri/xml_processing_instruction.c +56 -0
- data/ext/nokogiri/xml_processing_instruction.h +9 -0
- data/ext/nokogiri/xml_reader.c +684 -0
- data/ext/nokogiri/xml_reader.h +10 -0
- data/ext/nokogiri/xml_relax_ng.c +161 -0
- data/ext/nokogiri/xml_relax_ng.h +9 -0
- data/ext/nokogiri/xml_sax_parser.c +293 -0
- data/ext/nokogiri/xml_sax_parser.h +39 -0
- data/ext/nokogiri/xml_sax_parser_context.c +222 -0
- data/ext/nokogiri/xml_sax_parser_context.h +10 -0
- data/ext/nokogiri/xml_sax_push_parser.c +115 -0
- data/ext/nokogiri/xml_sax_push_parser.h +9 -0
- data/ext/nokogiri/xml_schema.c +205 -0
- data/ext/nokogiri/xml_schema.h +9 -0
- data/ext/nokogiri/xml_syntax_error.c +58 -0
- data/ext/nokogiri/xml_syntax_error.h +13 -0
- data/ext/nokogiri/xml_text.c +52 -0
- data/ext/nokogiri/xml_text.h +9 -0
- data/ext/nokogiri/xml_xpath_context.c +319 -0
- data/ext/nokogiri/xml_xpath_context.h +10 -0
- data/ext/nokogiri/xslt_stylesheet.c +270 -0
- data/ext/nokogiri/xslt_stylesheet.h +14 -0
- data/lib/isorelax.jar +0 -0
- data/lib/jing.jar +0 -0
- data/lib/nekodtd.jar +0 -0
- data/lib/nekohtml.jar +0 -0
- data/lib/nokogiri.rb +127 -0
- data/lib/nokogiri/css.rb +27 -0
- data/lib/nokogiri/css/node.rb +102 -0
- data/lib/nokogiri/css/parser.rb +720 -0
- data/lib/nokogiri/css/parser.y +258 -0
- data/lib/nokogiri/css/parser_extras.rb +91 -0
- data/lib/nokogiri/css/syntax_error.rb +7 -0
- data/lib/nokogiri/css/tokenizer.rb +152 -0
- data/lib/nokogiri/css/tokenizer.rex +55 -0
- data/lib/nokogiri/css/xpath_visitor.rb +171 -0
- data/lib/nokogiri/decorators/slop.rb +35 -0
- data/lib/nokogiri/html.rb +37 -0
- data/lib/nokogiri/html/builder.rb +35 -0
- data/lib/nokogiri/html/document.rb +254 -0
- data/lib/nokogiri/html/document_fragment.rb +41 -0
- data/lib/nokogiri/html/element_description.rb +23 -0
- data/lib/nokogiri/html/element_description_defaults.rb +671 -0
- data/lib/nokogiri/html/entity_lookup.rb +13 -0
- data/lib/nokogiri/html/sax/parser.rb +52 -0
- data/lib/nokogiri/html/sax/parser_context.rb +16 -0
- data/lib/nokogiri/html/sax/push_parser.rb +16 -0
- data/lib/nokogiri/nokogiri.jar +0 -0
- data/lib/nokogiri/syntax_error.rb +4 -0
- data/lib/nokogiri/version.rb +88 -0
- data/lib/nokogiri/xml.rb +73 -0
- data/lib/nokogiri/xml/attr.rb +14 -0
- data/lib/nokogiri/xml/attribute_decl.rb +18 -0
- data/lib/nokogiri/xml/builder.rb +431 -0
- data/lib/nokogiri/xml/cdata.rb +11 -0
- data/lib/nokogiri/xml/character_data.rb +7 -0
- data/lib/nokogiri/xml/document.rb +267 -0
- data/lib/nokogiri/xml/document_fragment.rb +103 -0
- data/lib/nokogiri/xml/dtd.rb +22 -0
- data/lib/nokogiri/xml/element_content.rb +36 -0
- data/lib/nokogiri/xml/element_decl.rb +13 -0
- data/lib/nokogiri/xml/entity_decl.rb +19 -0
- data/lib/nokogiri/xml/namespace.rb +13 -0
- data/lib/nokogiri/xml/node.rb +946 -0
- data/lib/nokogiri/xml/node/save_options.rb +61 -0
- data/lib/nokogiri/xml/node_set.rb +357 -0
- data/lib/nokogiri/xml/notation.rb +6 -0
- data/lib/nokogiri/xml/parse_options.rb +98 -0
- data/lib/nokogiri/xml/pp.rb +2 -0
- data/lib/nokogiri/xml/pp/character_data.rb +18 -0
- data/lib/nokogiri/xml/pp/node.rb +56 -0
- data/lib/nokogiri/xml/processing_instruction.rb +8 -0
- data/lib/nokogiri/xml/reader.rb +112 -0
- data/lib/nokogiri/xml/relax_ng.rb +32 -0
- data/lib/nokogiri/xml/sax.rb +4 -0
- data/lib/nokogiri/xml/sax/document.rb +164 -0
- data/lib/nokogiri/xml/sax/parser.rb +115 -0
- data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
- data/lib/nokogiri/xml/schema.rb +63 -0
- data/lib/nokogiri/xml/syntax_error.rb +47 -0
- data/lib/nokogiri/xml/text.rb +9 -0
- data/lib/nokogiri/xml/xpath.rb +10 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +11 -0
- data/lib/nokogiri/xml/xpath_context.rb +16 -0
- data/lib/nokogiri/xslt.rb +56 -0
- data/lib/nokogiri/xslt/stylesheet.rb +25 -0
- data/lib/xercesImpl.jar +0 -0
- data/lib/xsd/xmlparser/nokogiri.rb +90 -0
- data/tasks/cross_compile.rb +153 -0
- data/tasks/nokogiri.org.rb +24 -0
- data/tasks/test.rb +95 -0
- data/test/css/test_nthiness.rb +159 -0
- data/test/css/test_parser.rb +341 -0
- data/test/css/test_tokenizer.rb +198 -0
- data/test/css/test_xpath_visitor.rb +91 -0
- data/test/decorators/test_slop.rb +16 -0
- data/test/files/2ch.html +108 -0
- data/test/files/address_book.rlx +12 -0
- data/test/files/address_book.xml +10 -0
- data/test/files/bar/bar.xsd +4 -0
- data/test/files/dont_hurt_em_why.xml +422 -0
- data/test/files/encoding.html +82 -0
- data/test/files/encoding.xhtml +84 -0
- data/test/files/exslt.xml +8 -0
- data/test/files/exslt.xslt +35 -0
- data/test/files/foo/foo.xsd +4 -0
- data/test/files/metacharset.html +10 -0
- data/test/files/noencoding.html +47 -0
- data/test/files/po.xml +32 -0
- data/test/files/po.xsd +66 -0
- data/test/files/shift_jis.html +10 -0
- data/test/files/shift_jis.xml +5 -0
- data/test/files/snuggles.xml +3 -0
- data/test/files/staff.dtd +10 -0
- data/test/files/staff.xml +59 -0
- data/test/files/staff.xslt +32 -0
- data/test/files/tlm.html +850 -0
- data/test/files/to_be_xincluded.xml +2 -0
- data/test/files/valid_bar.xml +2 -0
- data/test/files/xinclude.xml +4 -0
- data/test/helper.rb +147 -0
- data/test/html/sax/test_parser.rb +138 -0
- data/test/html/sax/test_parser_context.rb +46 -0
- data/test/html/test_builder.rb +164 -0
- data/test/html/test_document.rb +529 -0
- data/test/html/test_document_encoding.rb +138 -0
- data/test/html/test_document_fragment.rb +254 -0
- data/test/html/test_element_description.rb +100 -0
- data/test/html/test_named_characters.rb +14 -0
- data/test/html/test_node.rb +188 -0
- data/test/html/test_node_encoding.rb +27 -0
- data/test/test_convert_xpath.rb +135 -0
- data/test/test_css_cache.rb +45 -0
- data/test/test_encoding_handler.rb +46 -0
- data/test/test_memory_leak.rb +152 -0
- data/test/test_nokogiri.rb +132 -0
- data/test/test_reader.rb +488 -0
- data/test/test_soap4r_sax.rb +52 -0
- data/test/test_xslt_transforms.rb +254 -0
- data/test/xml/node/test_save_options.rb +28 -0
- data/test/xml/node/test_subclass.rb +44 -0
- data/test/xml/sax/test_parser.rb +338 -0
- data/test/xml/sax/test_parser_context.rb +106 -0
- data/test/xml/sax/test_push_parser.rb +157 -0
- data/test/xml/test_attr.rb +64 -0
- data/test/xml/test_attribute_decl.rb +86 -0
- data/test/xml/test_builder.rb +248 -0
- data/test/xml/test_c14n.rb +151 -0
- data/test/xml/test_cdata.rb +48 -0
- data/test/xml/test_comment.rb +29 -0
- data/test/xml/test_document.rb +742 -0
- data/test/xml/test_document_encoding.rb +28 -0
- data/test/xml/test_document_fragment.rb +216 -0
- data/test/xml/test_dtd.rb +103 -0
- data/test/xml/test_dtd_encoding.rb +33 -0
- data/test/xml/test_element_content.rb +56 -0
- data/test/xml/test_element_decl.rb +73 -0
- data/test/xml/test_entity_decl.rb +122 -0
- data/test/xml/test_entity_reference.rb +235 -0
- data/test/xml/test_namespace.rb +75 -0
- data/test/xml/test_node.rb +1029 -0
- data/test/xml/test_node_attributes.rb +53 -0
- data/test/xml/test_node_encoding.rb +107 -0
- data/test/xml/test_node_inheritance.rb +32 -0
- data/test/xml/test_node_reparenting.rb +374 -0
- data/test/xml/test_node_set.rb +755 -0
- data/test/xml/test_parse_options.rb +64 -0
- data/test/xml/test_processing_instruction.rb +30 -0
- data/test/xml/test_reader_encoding.rb +142 -0
- data/test/xml/test_relax_ng.rb +60 -0
- data/test/xml/test_schema.rb +94 -0
- data/test/xml/test_syntax_error.rb +12 -0
- data/test/xml/test_text.rb +45 -0
- data/test/xml/test_unparented_node.rb +413 -0
- data/test/xml/test_xinclude.rb +83 -0
- data/test/xml/test_xpath.rb +295 -0
- data/test/xslt/test_custom_functions.rb +129 -0
- data/test/xslt/test_exception_handling.rb +37 -0
- data/test_all +84 -0
- metadata +571 -0
@@ -0,0 +1,775 @@
|
|
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
|
+
package nokogiri.internals;
|
34
|
+
|
35
|
+
import static nokogiri.internals.NokogiriHelpers.canonicalizeWhitespce;
|
36
|
+
import static nokogiri.internals.NokogiriHelpers.encodeJavaString;
|
37
|
+
import static nokogiri.internals.NokogiriHelpers.isNamespace;
|
38
|
+
import static nokogiri.internals.NokogiriHelpers.isWhitespaceText;
|
39
|
+
|
40
|
+
import java.util.ArrayDeque;
|
41
|
+
import java.util.ArrayList;
|
42
|
+
import java.util.Arrays;
|
43
|
+
import java.util.Comparator;
|
44
|
+
import java.util.Deque;
|
45
|
+
import java.util.Iterator;
|
46
|
+
import java.util.List;
|
47
|
+
import java.util.Stack;
|
48
|
+
import java.util.regex.Matcher;
|
49
|
+
import java.util.regex.Pattern;
|
50
|
+
|
51
|
+
import org.cyberneko.html.HTMLElements;
|
52
|
+
import org.w3c.dom.Attr;
|
53
|
+
import org.w3c.dom.CDATASection;
|
54
|
+
import org.w3c.dom.Comment;
|
55
|
+
import org.w3c.dom.Document;
|
56
|
+
import org.w3c.dom.DocumentType;
|
57
|
+
import org.w3c.dom.Element;
|
58
|
+
import org.w3c.dom.Entity;
|
59
|
+
import org.w3c.dom.EntityReference;
|
60
|
+
import org.w3c.dom.NamedNodeMap;
|
61
|
+
import org.w3c.dom.Node;
|
62
|
+
import org.w3c.dom.Notation;
|
63
|
+
import org.w3c.dom.ProcessingInstruction;
|
64
|
+
import org.w3c.dom.Text;
|
65
|
+
|
66
|
+
/**
|
67
|
+
* A class for serializing a document.
|
68
|
+
*
|
69
|
+
* @author sergio
|
70
|
+
* @author Patrick Mahoney <pat@polycrystal.org>
|
71
|
+
* @author Yoko Harada <yokolet@gmail.com>
|
72
|
+
*/
|
73
|
+
public class SaveContextVisitor {
|
74
|
+
|
75
|
+
private StringBuffer buffer;
|
76
|
+
private Stack<String> indentation;
|
77
|
+
private String encoding, indentString;
|
78
|
+
private boolean format, noDecl, noEmpty, noXhtml, asXhtml, asXml, asHtml, asBuilder, htmlDoc, fragment;
|
79
|
+
private boolean canonical, incl_ns, with_comments, subsets, exclusive;
|
80
|
+
private List<Node> c14nNodeList;
|
81
|
+
private Deque<Attr[]> c14nNamespaceStack;
|
82
|
+
private Deque<Attr[]> c14nAttrStack;
|
83
|
+
private List<String> c14nExclusiveInclusivePrefixes = null;
|
84
|
+
/*
|
85
|
+
* U can't touch this.
|
86
|
+
* http://www.youtube.com/watch?v=WJ2ZFVx6A4Q
|
87
|
+
*
|
88
|
+
* Taken from libxml save options.
|
89
|
+
*/
|
90
|
+
|
91
|
+
public static final int FORMAT = 1;
|
92
|
+
public static final int NO_DECL = 2;
|
93
|
+
public static final int NO_EMPTY = 4;
|
94
|
+
public static final int NO_XHTML = 8;
|
95
|
+
public static final int AS_XHTML = 16;
|
96
|
+
public static final int AS_XML = 32;
|
97
|
+
public static final int AS_HTML = 64;
|
98
|
+
public static final int AS_BUILDER = 128;
|
99
|
+
|
100
|
+
public static final int CANONICAL = 1;
|
101
|
+
public static final int INCL_NS = 2;
|
102
|
+
public static final int WITH_COMMENTS = 4;
|
103
|
+
public static final int SUBSETS = 8;
|
104
|
+
public static final int EXCLUSIVE = 16;
|
105
|
+
|
106
|
+
public SaveContextVisitor(int options, String indent, String encoding, boolean htmlDoc, boolean fragment, int canonicalOpts) {
|
107
|
+
buffer = new StringBuffer();
|
108
|
+
this.encoding = encoding;
|
109
|
+
indentation = new Stack<String>(); indentation.push("");
|
110
|
+
this.htmlDoc = htmlDoc;
|
111
|
+
this.fragment = fragment;
|
112
|
+
c14nNodeList = new ArrayList<Node>();
|
113
|
+
c14nNamespaceStack = new ArrayDeque<Attr[]>();
|
114
|
+
c14nAttrStack = new ArrayDeque<Attr[]>();
|
115
|
+
format = (options & FORMAT) == FORMAT;
|
116
|
+
|
117
|
+
noDecl = (options & NO_DECL) == NO_DECL;
|
118
|
+
noEmpty = (options & NO_EMPTY) == NO_EMPTY;
|
119
|
+
noXhtml = (options & NO_XHTML) == NO_XHTML;
|
120
|
+
asXhtml = (options & AS_XHTML) == AS_XHTML;
|
121
|
+
asXml = (options & AS_XML) == AS_XML;
|
122
|
+
asHtml = (options & AS_HTML) == AS_HTML;
|
123
|
+
asBuilder = (options & AS_BUILDER) == AS_BUILDER;
|
124
|
+
|
125
|
+
canonical = (canonicalOpts & CANONICAL) == CANONICAL;
|
126
|
+
incl_ns = (canonicalOpts & INCL_NS) == INCL_NS;
|
127
|
+
with_comments = (canonicalOpts & WITH_COMMENTS) == WITH_COMMENTS;
|
128
|
+
subsets = (canonicalOpts & SUBSETS) == SUBSETS;
|
129
|
+
|
130
|
+
if ((format && indent == null) || (format && indent.length() == 0)) indent = " "; // default, two spaces
|
131
|
+
if ((!format && indent != null) && indent.length() > 0) format = true;
|
132
|
+
if ((asBuilder && indent == null) || (asBuilder && indent.length() == 0)) indent = " "; // default, two spaces
|
133
|
+
indentString = indent;
|
134
|
+
if (!asXml && !asHtml && !asXhtml && !asBuilder) asXml = true;
|
135
|
+
}
|
136
|
+
|
137
|
+
@Override
|
138
|
+
public String toString() {
|
139
|
+
return (new String(buffer));
|
140
|
+
}
|
141
|
+
|
142
|
+
public void setHtmlDoc(boolean htmlDoc) {
|
143
|
+
this.htmlDoc = htmlDoc;
|
144
|
+
}
|
145
|
+
|
146
|
+
public void setEncoding(String encoding) {
|
147
|
+
this.encoding = encoding;
|
148
|
+
}
|
149
|
+
|
150
|
+
public List<Node> getC14nNodeList() {
|
151
|
+
return c14nNodeList;
|
152
|
+
}
|
153
|
+
|
154
|
+
public void setC14nExclusiveInclusivePrefixes(List<String> prefixes) {
|
155
|
+
c14nExclusiveInclusivePrefixes = prefixes;
|
156
|
+
}
|
157
|
+
|
158
|
+
public boolean enter(Node node) {
|
159
|
+
if (node instanceof Document) {
|
160
|
+
return enter((Document)node);
|
161
|
+
}
|
162
|
+
if (node instanceof Element) {
|
163
|
+
return enter((Element)node);
|
164
|
+
}
|
165
|
+
if (node instanceof Attr) {
|
166
|
+
return enter((Attr)node);
|
167
|
+
}
|
168
|
+
if (node instanceof Text) {
|
169
|
+
return enter((Text)node);
|
170
|
+
}
|
171
|
+
if (node instanceof CDATASection) {
|
172
|
+
return enter((CDATASection)node);
|
173
|
+
}
|
174
|
+
if (node instanceof Comment) {
|
175
|
+
return enter((Comment)node);
|
176
|
+
}
|
177
|
+
if (node instanceof DocumentType) {
|
178
|
+
return enter((DocumentType)node);
|
179
|
+
}
|
180
|
+
if (node instanceof Entity) {
|
181
|
+
return enter((Entity)node);
|
182
|
+
}
|
183
|
+
if (node instanceof EntityReference) {
|
184
|
+
return enter((EntityReference)node);
|
185
|
+
}
|
186
|
+
if (node instanceof Notation) {
|
187
|
+
return enter((Notation)node);
|
188
|
+
}
|
189
|
+
if (node instanceof ProcessingInstruction) {
|
190
|
+
return enter((ProcessingInstruction)node);
|
191
|
+
}
|
192
|
+
return false;
|
193
|
+
}
|
194
|
+
|
195
|
+
public void leave(Node node) {
|
196
|
+
if (node instanceof Document) {
|
197
|
+
leave((Document)node);
|
198
|
+
return;
|
199
|
+
}
|
200
|
+
if (node instanceof Element) {
|
201
|
+
leave((Element)node);
|
202
|
+
return;
|
203
|
+
}
|
204
|
+
if (node instanceof Attr) {
|
205
|
+
leave((Attr)node);
|
206
|
+
return;
|
207
|
+
}
|
208
|
+
if (node instanceof Text) {
|
209
|
+
leave((Text)node);
|
210
|
+
return;
|
211
|
+
}
|
212
|
+
if (node instanceof CDATASection) {
|
213
|
+
leave((CDATASection)node);
|
214
|
+
return;
|
215
|
+
}
|
216
|
+
if (node instanceof Comment) {
|
217
|
+
leave((Comment)node);
|
218
|
+
return;
|
219
|
+
}
|
220
|
+
if (node instanceof DocumentType) {
|
221
|
+
leave((DocumentType)node);
|
222
|
+
return;
|
223
|
+
}
|
224
|
+
if (node instanceof Entity) {
|
225
|
+
leave((Entity)node);
|
226
|
+
return;
|
227
|
+
}
|
228
|
+
if (node instanceof EntityReference) {
|
229
|
+
leave((EntityReference)node);
|
230
|
+
return;
|
231
|
+
}
|
232
|
+
if (node instanceof Notation) {
|
233
|
+
leave((Notation)node);
|
234
|
+
return;
|
235
|
+
}
|
236
|
+
if (node instanceof ProcessingInstruction) {
|
237
|
+
leave((ProcessingInstruction)node);
|
238
|
+
return;
|
239
|
+
}
|
240
|
+
}
|
241
|
+
|
242
|
+
public boolean enter(String string) {
|
243
|
+
buffer.append(string);
|
244
|
+
return true;
|
245
|
+
}
|
246
|
+
|
247
|
+
public void leave(String string) {
|
248
|
+
// no-op
|
249
|
+
}
|
250
|
+
|
251
|
+
public boolean enter(Attr attr) {
|
252
|
+
String name = attr.getName();
|
253
|
+
buffer.append(name);
|
254
|
+
if (!asHtml || !isHtmlBooleanAttr(name)) {
|
255
|
+
buffer.append("=");
|
256
|
+
buffer.append("\"");
|
257
|
+
String value = replaceCharsetIfNecessary(attr);
|
258
|
+
buffer.append(serializeAttrTextContent(value, htmlDoc));
|
259
|
+
buffer.append("\"");
|
260
|
+
}
|
261
|
+
return true;
|
262
|
+
}
|
263
|
+
|
264
|
+
private static Pattern p =
|
265
|
+
Pattern.compile("charset(()|\\s+)=(()|\\s+)(\\w|\\_|\\.|\\-)+", Pattern.CASE_INSENSITIVE);
|
266
|
+
|
267
|
+
private String replaceCharsetIfNecessary(Attr attr) {
|
268
|
+
String value = attr.getValue();
|
269
|
+
if (encoding == null) return value; // unable to replace in any case
|
270
|
+
if (!"content".equals(attr.getName().toLowerCase())) return value; // must be content attr
|
271
|
+
if (!"meta".equals(attr.getOwnerElement().getNodeName().toLowerCase())) return value;
|
272
|
+
Matcher m = p.matcher(value);
|
273
|
+
if (!m.find()) return value;
|
274
|
+
if (value.contains(encoding)) return value; // no need to replace
|
275
|
+
return value.replace(m.group(), "charset=" + encoding);
|
276
|
+
}
|
277
|
+
|
278
|
+
public static final String[] HTML_BOOLEAN_ATTRS = {
|
279
|
+
"checked", "compact", "declare", "defer", "disabled", "ismap",
|
280
|
+
"multiple", "nohref", "noresize", "noshade", "nowrap", "readonly",
|
281
|
+
"selected"
|
282
|
+
};
|
283
|
+
|
284
|
+
private boolean isHtmlBooleanAttr(String name) {
|
285
|
+
for (String s : HTML_BOOLEAN_ATTRS) {
|
286
|
+
if (s.equals(name)) return true;
|
287
|
+
}
|
288
|
+
return false;
|
289
|
+
}
|
290
|
+
|
291
|
+
private String serializeAttrTextContent(String s, boolean htmlDoc) {
|
292
|
+
if (s == null) return "";
|
293
|
+
|
294
|
+
char[] c = s.toCharArray();
|
295
|
+
StringBuffer buffer = new StringBuffer(c.length);
|
296
|
+
|
297
|
+
for(int i = 0; i < c.length; i++) {
|
298
|
+
switch(c[i]){
|
299
|
+
case '\n': buffer.append(" "); break;
|
300
|
+
case '\r': buffer.append(" "); break;
|
301
|
+
case '\t': buffer.append("	"); break;
|
302
|
+
case '"': if (htmlDoc) buffer.append("%22");
|
303
|
+
else buffer.append(""");
|
304
|
+
break;
|
305
|
+
case '<': buffer.append("<"); break;
|
306
|
+
case '>': buffer.append(">"); break;
|
307
|
+
case '&': buffer.append("&"); break;
|
308
|
+
default: buffer.append(c[i]);
|
309
|
+
}
|
310
|
+
}
|
311
|
+
|
312
|
+
return buffer.toString();
|
313
|
+
}
|
314
|
+
|
315
|
+
public void leave(Attr attr) {
|
316
|
+
// no-op
|
317
|
+
}
|
318
|
+
|
319
|
+
public boolean enter(CDATASection cdata) {
|
320
|
+
buffer.append("<![CDATA[");
|
321
|
+
buffer.append(cdata.getData());
|
322
|
+
buffer.append("]]>");
|
323
|
+
return true;
|
324
|
+
}
|
325
|
+
|
326
|
+
public void leave(CDATASection cdata) {
|
327
|
+
// no-op
|
328
|
+
}
|
329
|
+
|
330
|
+
public boolean enter(Comment comment) {
|
331
|
+
if (canonical) {
|
332
|
+
c14nNodeList.add(comment);
|
333
|
+
if (!with_comments) return true;
|
334
|
+
}
|
335
|
+
buffer.append("<!--");
|
336
|
+
buffer.append(comment.getData());
|
337
|
+
buffer.append("-->");
|
338
|
+
return true;
|
339
|
+
}
|
340
|
+
|
341
|
+
public void leave(Comment comment) {
|
342
|
+
// no-op
|
343
|
+
}
|
344
|
+
|
345
|
+
public boolean enter(Document document) {
|
346
|
+
if (!noDecl) {
|
347
|
+
buffer.append("<?xml version=\"");
|
348
|
+
buffer.append(document.getXmlVersion());
|
349
|
+
buffer.append("\"");
|
350
|
+
|
351
|
+
if (encoding != null) {
|
352
|
+
buffer.append(" encoding=\"");
|
353
|
+
buffer.append(encoding);
|
354
|
+
buffer.append("\"");
|
355
|
+
}
|
356
|
+
buffer.append("?>\n");
|
357
|
+
}
|
358
|
+
return true;
|
359
|
+
}
|
360
|
+
|
361
|
+
public void leave(Document document) {
|
362
|
+
// no-op
|
363
|
+
}
|
364
|
+
|
365
|
+
public boolean enter(DocumentType docType) {
|
366
|
+
if (canonical) {
|
367
|
+
c14nNodeList.add(docType);
|
368
|
+
return true;
|
369
|
+
}
|
370
|
+
String name = docType.getName();
|
371
|
+
String pubId = docType.getPublicId();
|
372
|
+
String sysId = docType.getSystemId();
|
373
|
+
String internalSubset = docType.getInternalSubset();
|
374
|
+
if (docType.getPreviousSibling() != null) {
|
375
|
+
buffer.append("\n");
|
376
|
+
}
|
377
|
+
buffer.append("<!DOCTYPE " + name + " ");
|
378
|
+
if (pubId != null) {
|
379
|
+
buffer.append("PUBLIC \"" + pubId + "\"");
|
380
|
+
if (sysId != null) buffer.append(" \"" + sysId + "\"");
|
381
|
+
} else if (sysId != null) {
|
382
|
+
buffer.append("SYSTEM \"" + sysId + "\"");
|
383
|
+
}
|
384
|
+
if (internalSubset != null) {
|
385
|
+
buffer.append(" [");
|
386
|
+
buffer.append(internalSubset);
|
387
|
+
buffer.append("]");
|
388
|
+
}
|
389
|
+
buffer.append(">\n");
|
390
|
+
return true;
|
391
|
+
}
|
392
|
+
|
393
|
+
public void leave(DocumentType docType) {
|
394
|
+
// no-op
|
395
|
+
}
|
396
|
+
|
397
|
+
public boolean enter(Element element) {
|
398
|
+
if (canonical) {
|
399
|
+
c14nNodeList.add(element);
|
400
|
+
if (element == element.getOwnerDocument().getDocumentElement()) {
|
401
|
+
c14nNodeList.add(element.getOwnerDocument());
|
402
|
+
}
|
403
|
+
}
|
404
|
+
String current = indentation.peek();
|
405
|
+
buffer.append(current);
|
406
|
+
if (needIndent()) {
|
407
|
+
indentation.push(current + indentString);
|
408
|
+
}
|
409
|
+
String name = element.getTagName();
|
410
|
+
buffer.append("<" + name);
|
411
|
+
Attr[] attrs = getAttrsAndNamespaces(element);
|
412
|
+
for (Attr attr : attrs) {
|
413
|
+
if (attr.getSpecified()) {
|
414
|
+
buffer.append(" ");
|
415
|
+
enter(attr);
|
416
|
+
leave(attr);
|
417
|
+
}
|
418
|
+
}
|
419
|
+
if (element.hasChildNodes()) {
|
420
|
+
buffer.append(">");
|
421
|
+
if (needBreakInOpening(element)) buffer.append("\n");
|
422
|
+
return true;
|
423
|
+
}
|
424
|
+
// no child
|
425
|
+
if (asHtml || asXhtml) {
|
426
|
+
buffer.append(">");
|
427
|
+
} else if (asXml && noEmpty) {
|
428
|
+
buffer.append(">");
|
429
|
+
} else {
|
430
|
+
buffer.append("/>");
|
431
|
+
}
|
432
|
+
if (needBreakInOpening(element)) {
|
433
|
+
buffer.append("\n");
|
434
|
+
}
|
435
|
+
return true;
|
436
|
+
}
|
437
|
+
|
438
|
+
private boolean needIndent() {
|
439
|
+
if (fragment) return false; // a given option might be fragment and format. fragment matters
|
440
|
+
if (format || asBuilder) return true;
|
441
|
+
return false;
|
442
|
+
}
|
443
|
+
|
444
|
+
private boolean needBreakInOpening(Element element) {
|
445
|
+
if (fragment) return false;
|
446
|
+
if (format) return true;
|
447
|
+
if (asBuilder && element.getFirstChild() != null && element.getFirstChild().getNodeType() == Node.ELEMENT_NODE) return true;
|
448
|
+
if (format && element.getNextSibling() == null && element.hasChildNodes()) return true;
|
449
|
+
return false;
|
450
|
+
}
|
451
|
+
|
452
|
+
private boolean isEmpty(String name) {
|
453
|
+
HTMLElements.Element element = HTMLElements.getElement(name);
|
454
|
+
return element.isEmpty();
|
455
|
+
}
|
456
|
+
|
457
|
+
private Attr[] getAttrsAndNamespaces(Element element) {
|
458
|
+
NamedNodeMap attrs = element.getAttributes();
|
459
|
+
if (!canonical) {
|
460
|
+
if (attrs == null || attrs.getLength() == 0) return new Attr[0];
|
461
|
+
Attr[] attrsAndNamespaces = new Attr[attrs.getLength()];
|
462
|
+
for (int i=0; i<attrs.getLength(); i++) {
|
463
|
+
attrsAndNamespaces[i] = (Attr) attrs.item(i);
|
464
|
+
}
|
465
|
+
return attrsAndNamespaces;
|
466
|
+
} else {
|
467
|
+
List<Attr> namespaces = new ArrayList<Attr>();
|
468
|
+
List<Attr> attributes = new ArrayList<Attr>();
|
469
|
+
if (subsets) {
|
470
|
+
getAttrsOfAncestors(element.getParentNode(), namespaces, attributes);
|
471
|
+
Attr[] namespaceOfAncestors = getSortedArray(namespaces);
|
472
|
+
Attr[] attributeOfAncestors = getSortedArray(attributes);
|
473
|
+
c14nNamespaceStack.push(namespaceOfAncestors);
|
474
|
+
c14nAttrStack.push(attributeOfAncestors);
|
475
|
+
subsets = false; // namespace propagation should be done only once on top level node.
|
476
|
+
}
|
477
|
+
|
478
|
+
getNamespacesAndAttrs(element, namespaces, attributes);
|
479
|
+
|
480
|
+
Attr[] namespaceArray = getSortedArray(namespaces);
|
481
|
+
Attr[] attributeArray = getSortedArray(attributes);
|
482
|
+
Attr[] allAttrs = new Attr[namespaceArray.length + attributeArray.length];
|
483
|
+
for (int i=0; i<allAttrs.length; i++) {
|
484
|
+
if (i < namespaceArray.length) {
|
485
|
+
allAttrs[i] = namespaceArray[i];
|
486
|
+
} else {
|
487
|
+
allAttrs[i] = attributeArray[i-namespaceArray.length];
|
488
|
+
}
|
489
|
+
}
|
490
|
+
c14nNamespaceStack.push(namespaceArray);
|
491
|
+
c14nAttrStack.push(attributeArray);
|
492
|
+
return allAttrs;
|
493
|
+
}
|
494
|
+
|
495
|
+
}
|
496
|
+
|
497
|
+
private void getAttrsOfAncestors(Node parent, List<Attr> namespaces, List<Attr> attributes) {
|
498
|
+
if (parent == null) return;
|
499
|
+
NamedNodeMap attrs = parent.getAttributes();
|
500
|
+
if (attrs == null || attrs.getLength() == 0) return;
|
501
|
+
for (int i=0; i < attrs.getLength(); i++) {
|
502
|
+
Attr attr = (Attr)attrs.item(i);
|
503
|
+
if (isNamespace(attr.getNodeName())) namespaces.add(attr);
|
504
|
+
else attributes.add(attr);
|
505
|
+
}
|
506
|
+
getAttrsOfAncestors(parent.getParentNode(), namespaces, attributes);
|
507
|
+
}
|
508
|
+
|
509
|
+
private void getNamespacesAndAttrs(Node current, List<Attr> namespaces, List<Attr> attributes) {
|
510
|
+
NamedNodeMap attrs = current.getAttributes();
|
511
|
+
for (int i=0; i<attrs.getLength(); i++) {
|
512
|
+
Attr attr = (Attr)attrs.item(i);
|
513
|
+
if (isNamespace(attr.getNodeName())) {
|
514
|
+
getNamespacesWithPropagated(namespaces, attr);
|
515
|
+
} else {
|
516
|
+
getAttributesWithPropagated(attributes, attr);
|
517
|
+
}
|
518
|
+
if (exclusive) {
|
519
|
+
verifyXmlSpace(attributes, attrs);
|
520
|
+
}
|
521
|
+
}
|
522
|
+
}
|
523
|
+
|
524
|
+
private void getNamespacesWithPropagated(List<Attr> namespaces, Attr attr) {
|
525
|
+
boolean newNamespace = true;
|
526
|
+
Iterator<Attr[]> iter = c14nNamespaceStack.iterator();
|
527
|
+
while (iter.hasNext()) {
|
528
|
+
Attr[] parentNamespaces = iter.next();
|
529
|
+
for (int n=0; n < parentNamespaces.length; n++) {
|
530
|
+
if (parentNamespaces[n].getNodeName().equals(attr.getNodeName())) {
|
531
|
+
if (parentNamespaces[n].getNodeValue().equals(attr.getNodeValue())) {
|
532
|
+
// exactly the same namespace should not be added
|
533
|
+
newNamespace = false;
|
534
|
+
} else {
|
535
|
+
// in case of namespace url change, propagated namespace will be override
|
536
|
+
namespaces.remove(parentNamespaces[n]);
|
537
|
+
}
|
538
|
+
}
|
539
|
+
}
|
540
|
+
if (newNamespace && !namespaces.contains(attr)) namespaces.add(attr);
|
541
|
+
}
|
542
|
+
}
|
543
|
+
|
544
|
+
private void getAttributesWithPropagated(List<Attr> attributes, Attr attr) {
|
545
|
+
boolean newAttribute = true;
|
546
|
+
Iterator<Attr[]> iter = c14nAttrStack.iterator();
|
547
|
+
while (iter.hasNext()) {
|
548
|
+
Attr[] parentAttr = iter.next();
|
549
|
+
for (int n=0; n < parentAttr.length; n++) {
|
550
|
+
if (!parentAttr[n].getNodeName().startsWith("xml:")) continue;
|
551
|
+
if (parentAttr[n].getNodeName().equals(attr.getNodeName())) {
|
552
|
+
if (parentAttr[n].getNodeValue().equals(attr.getNodeValue())) {
|
553
|
+
// exactly the same attribute should not be added
|
554
|
+
newAttribute = false;
|
555
|
+
} else {
|
556
|
+
// in case of attribute value change, propagated attribute will be override
|
557
|
+
attributes.remove(parentAttr[n]);
|
558
|
+
}
|
559
|
+
}
|
560
|
+
}
|
561
|
+
if (newAttribute) attributes.add(attr);
|
562
|
+
}
|
563
|
+
}
|
564
|
+
|
565
|
+
private void verifyXmlSpace(List<Attr> attributes, NamedNodeMap attrs) {
|
566
|
+
Attr attr = (Attr) attrs.getNamedItem("xml:space");
|
567
|
+
if (attr == null) {
|
568
|
+
for (int i=0; i < attributes.size(); i++) {
|
569
|
+
if (attributes.get(i).getNodeName().equals("xml:space")) {
|
570
|
+
attributes.remove(i);
|
571
|
+
break;
|
572
|
+
}
|
573
|
+
}
|
574
|
+
}
|
575
|
+
}
|
576
|
+
|
577
|
+
private Attr[] getSortedArray(List<Attr> attrList) {
|
578
|
+
Attr[] attrArray = attrList.toArray(new Attr[0]);
|
579
|
+
Arrays.sort(attrArray, new Comparator<Attr>() {
|
580
|
+
@Override
|
581
|
+
public int compare(Attr attr0, Attr attr1) {
|
582
|
+
return attr0.getNodeName().compareTo(attr1.getNodeName());
|
583
|
+
}
|
584
|
+
});
|
585
|
+
return attrArray;
|
586
|
+
}
|
587
|
+
|
588
|
+
public void leave(Element element) {
|
589
|
+
if (canonical) {
|
590
|
+
c14nNamespaceStack.poll();
|
591
|
+
c14nAttrStack.poll();
|
592
|
+
}
|
593
|
+
String name = element.getTagName();
|
594
|
+
if (element.hasChildNodes()) {
|
595
|
+
if (needIndentInClosing(element)) {
|
596
|
+
indentation.pop();
|
597
|
+
buffer.append(indentation.peek());
|
598
|
+
} else if (asBuilder) {
|
599
|
+
indentation.pop();
|
600
|
+
}
|
601
|
+
buffer.append("</" + name + ">");
|
602
|
+
if (needBreakInClosing()) {
|
603
|
+
buffer.append("\n");
|
604
|
+
}
|
605
|
+
return;
|
606
|
+
}
|
607
|
+
// no child, but HTML might need a closing tag.
|
608
|
+
if (asHtml || noEmpty) {
|
609
|
+
if (!isEmpty(name) && noEmpty) {
|
610
|
+
buffer.append("</" + name + ">");
|
611
|
+
}
|
612
|
+
}
|
613
|
+
if (needBreakInClosing()) {
|
614
|
+
indentation.pop();
|
615
|
+
buffer.append("\n");
|
616
|
+
}
|
617
|
+
}
|
618
|
+
|
619
|
+
private boolean needIndentInClosing(Element element) {
|
620
|
+
if (fragment) return false; // a given option might be fragment and format. fragment matters
|
621
|
+
if (format) return true;
|
622
|
+
if (asBuilder && element.getFirstChild() != null && element.getFirstChild().getNodeType() == Node.ELEMENT_NODE) return true;
|
623
|
+
return false;
|
624
|
+
}
|
625
|
+
|
626
|
+
private boolean needBreakInClosing() {
|
627
|
+
if (fragment) return false;
|
628
|
+
if (format || asBuilder) return true;
|
629
|
+
return false;
|
630
|
+
}
|
631
|
+
|
632
|
+
public boolean enter(Entity entity) {
|
633
|
+
String name = entity.getNodeName();
|
634
|
+
String pubId = entity.getPublicId();
|
635
|
+
String sysId = entity.getSystemId();
|
636
|
+
String notation = entity.getNotationName();
|
637
|
+
buffer.append("<!ENTITY ");
|
638
|
+
buffer.append(name);
|
639
|
+
if (pubId != null) {
|
640
|
+
buffer.append(" PUBLIC \"");
|
641
|
+
buffer.append(pubId);
|
642
|
+
buffer.append("\"");
|
643
|
+
}
|
644
|
+
if (sysId != null) {
|
645
|
+
buffer.append(" SYSTEM \"");
|
646
|
+
buffer.append(sysId);
|
647
|
+
buffer.append("\"");
|
648
|
+
}
|
649
|
+
if (notation != null) {
|
650
|
+
buffer.append(" NDATA ");
|
651
|
+
buffer.append(notation);
|
652
|
+
}
|
653
|
+
buffer.append(">");
|
654
|
+
return true;
|
655
|
+
}
|
656
|
+
|
657
|
+
public void leave(Entity entity) {
|
658
|
+
// no-op
|
659
|
+
}
|
660
|
+
|
661
|
+
public boolean enter(EntityReference entityRef) {
|
662
|
+
// no-op?
|
663
|
+
return true;
|
664
|
+
}
|
665
|
+
|
666
|
+
public void leave(EntityReference entityRef) {
|
667
|
+
// no-op
|
668
|
+
}
|
669
|
+
|
670
|
+
public boolean enter(Notation notation) {
|
671
|
+
String name = notation.getNodeName();
|
672
|
+
String pubId = notation.getPublicId();
|
673
|
+
String sysId = notation.getSystemId();
|
674
|
+
buffer.append("<!NOTATION ");
|
675
|
+
buffer.append(name);
|
676
|
+
if (pubId != null) {
|
677
|
+
buffer.append(" PUBLIC \"");
|
678
|
+
buffer.append(pubId);
|
679
|
+
buffer.append("\"");
|
680
|
+
if (sysId != null) {
|
681
|
+
buffer.append(" \"");
|
682
|
+
buffer.append(sysId);
|
683
|
+
buffer.append("\"");
|
684
|
+
}
|
685
|
+
} else if (sysId != null) {
|
686
|
+
buffer.append(" SYSTEM \"");
|
687
|
+
buffer.append(sysId);
|
688
|
+
buffer.append("\"");
|
689
|
+
}
|
690
|
+
buffer.append(">");
|
691
|
+
return true;
|
692
|
+
}
|
693
|
+
|
694
|
+
public void leave(Notation notation) {
|
695
|
+
// no-op
|
696
|
+
}
|
697
|
+
|
698
|
+
public boolean enter(ProcessingInstruction pi) {
|
699
|
+
buffer.append("<?");
|
700
|
+
buffer.append(pi.getTarget());
|
701
|
+
buffer.append(" ");
|
702
|
+
buffer.append(pi.getData());
|
703
|
+
if (asHtml) buffer.append(">");
|
704
|
+
else buffer.append("?>");
|
705
|
+
buffer.append("\n");
|
706
|
+
if (canonical) c14nNodeList.add(pi);
|
707
|
+
return true;
|
708
|
+
}
|
709
|
+
|
710
|
+
public void leave(ProcessingInstruction pi) {
|
711
|
+
// no-op
|
712
|
+
}
|
713
|
+
|
714
|
+
private static char lineSeparator = '\n'; // System.getProperty("line.separator"); ?
|
715
|
+
public boolean enter(Text text) {
|
716
|
+
String textContent = text.getNodeValue();
|
717
|
+
if (canonical) {
|
718
|
+
c14nNodeList.add(text);
|
719
|
+
if (isWhitespaceText(textContent)) {
|
720
|
+
buffer.append(canonicalizeWhitespce(textContent));
|
721
|
+
return true;
|
722
|
+
}
|
723
|
+
}
|
724
|
+
if (needIndentText() && "".equals(textContent.trim())) return true;
|
725
|
+
if (needIndentText()) {
|
726
|
+
String current = indentation.peek();
|
727
|
+
buffer.append(current);
|
728
|
+
indentation.push(current + indentString);
|
729
|
+
if (textContent.charAt(0) == lineSeparator) textContent = textContent.substring(1);
|
730
|
+
}
|
731
|
+
if (text.getUserData(NokogiriHelpers.ENCODED_STRING) == null || !((Boolean)text.getUserData(NokogiriHelpers.ENCODED_STRING))) {
|
732
|
+
textContent = encodeJavaString(textContent);
|
733
|
+
}
|
734
|
+
|
735
|
+
if (getEncoding(text) == null) {
|
736
|
+
textContent = encodeStringToHtmlEntity(textContent);
|
737
|
+
}
|
738
|
+
buffer.append(textContent);
|
739
|
+
return true;
|
740
|
+
}
|
741
|
+
|
742
|
+
private boolean needIndentText() {
|
743
|
+
if (fragment) return false;
|
744
|
+
if (format) return true;
|
745
|
+
return false;
|
746
|
+
}
|
747
|
+
|
748
|
+
public void leave(Text text) {
|
749
|
+
String textContent = text.getNodeValue();
|
750
|
+
if (needIndentText() && !"".equals(textContent.trim())) {
|
751
|
+
indentation.pop();
|
752
|
+
if (textContent.charAt(textContent.length()-1) != lineSeparator) {
|
753
|
+
buffer.append("\n");
|
754
|
+
}
|
755
|
+
}
|
756
|
+
}
|
757
|
+
|
758
|
+
private String getEncoding(Text text) {
|
759
|
+
if (encoding != null) return encoding;
|
760
|
+
encoding = text.getOwnerDocument().getInputEncoding();
|
761
|
+
return encoding;
|
762
|
+
}
|
763
|
+
|
764
|
+
private String encodeStringToHtmlEntity(String text) {
|
765
|
+
int last = 126; // = U+007E. No need to encode under U+007E.
|
766
|
+
StringBuffer sb = new StringBuffer();
|
767
|
+
for (int i=0; i<text.length(); i++) {
|
768
|
+
int codePoint = text.codePointAt(i);
|
769
|
+
if (codePoint > last) sb.append("&#x" + Integer.toHexString(codePoint) + ";");
|
770
|
+
else sb.append(text.charAt(i));
|
771
|
+
}
|
772
|
+
return new String(sb);
|
773
|
+
}
|
774
|
+
|
775
|
+
}
|