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