nokogiri-backport 1.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/LICENSE-DEPENDENCIES.md +1682 -0
- data/LICENSE.md +9 -0
- data/README.md +272 -0
- data/bin/nokogiri +118 -0
- data/dependencies.yml +74 -0
- data/ext/java/nokogiri/EncodingHandler.java +124 -0
- data/ext/java/nokogiri/HtmlDocument.java +178 -0
- data/ext/java/nokogiri/HtmlElementDescription.java +148 -0
- data/ext/java/nokogiri/HtmlEntityLookup.java +79 -0
- data/ext/java/nokogiri/HtmlSaxParserContext.java +282 -0
- data/ext/java/nokogiri/HtmlSaxPushParser.java +222 -0
- data/ext/java/nokogiri/NokogiriService.java +597 -0
- data/ext/java/nokogiri/XmlAttr.java +162 -0
- data/ext/java/nokogiri/XmlAttributeDecl.java +129 -0
- data/ext/java/nokogiri/XmlCdata.java +82 -0
- data/ext/java/nokogiri/XmlComment.java +97 -0
- data/ext/java/nokogiri/XmlDocument.java +633 -0
- data/ext/java/nokogiri/XmlDocumentFragment.java +185 -0
- data/ext/java/nokogiri/XmlDtd.java +481 -0
- data/ext/java/nokogiri/XmlElement.java +68 -0
- data/ext/java/nokogiri/XmlElementContent.java +382 -0
- data/ext/java/nokogiri/XmlElementDecl.java +147 -0
- data/ext/java/nokogiri/XmlEntityDecl.java +157 -0
- data/ext/java/nokogiri/XmlEntityReference.java +101 -0
- data/ext/java/nokogiri/XmlNamespace.java +199 -0
- data/ext/java/nokogiri/XmlNode.java +1684 -0
- data/ext/java/nokogiri/XmlNodeSet.java +434 -0
- data/ext/java/nokogiri/XmlProcessingInstruction.java +100 -0
- data/ext/java/nokogiri/XmlReader.java +531 -0
- data/ext/java/nokogiri/XmlRelaxng.java +151 -0
- data/ext/java/nokogiri/XmlSaxParserContext.java +374 -0
- data/ext/java/nokogiri/XmlSaxPushParser.java +286 -0
- data/ext/java/nokogiri/XmlSchema.java +388 -0
- data/ext/java/nokogiri/XmlSyntaxError.java +138 -0
- data/ext/java/nokogiri/XmlText.java +110 -0
- data/ext/java/nokogiri/XmlXpathContext.java +301 -0
- data/ext/java/nokogiri/XsltStylesheet.java +347 -0
- data/ext/java/nokogiri/internals/ClosedStreamException.java +10 -0
- data/ext/java/nokogiri/internals/HtmlDomParserContext.java +252 -0
- data/ext/java/nokogiri/internals/IgnoreSchemaErrorsErrorHandler.java +20 -0
- data/ext/java/nokogiri/internals/NokogiriBlockingQueueInputStream.java +151 -0
- data/ext/java/nokogiri/internals/NokogiriDomParser.java +116 -0
- data/ext/java/nokogiri/internals/NokogiriEntityResolver.java +121 -0
- data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +69 -0
- data/ext/java/nokogiri/internals/NokogiriHandler.java +327 -0
- data/ext/java/nokogiri/internals/NokogiriHelpers.java +734 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +217 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +127 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +100 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +121 -0
- data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +78 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +180 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +72 -0
- data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +60 -0
- data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +87 -0
- data/ext/java/nokogiri/internals/ParserContext.java +259 -0
- data/ext/java/nokogiri/internals/ReaderNode.java +488 -0
- data/ext/java/nokogiri/internals/SaveContextVisitor.java +778 -0
- data/ext/java/nokogiri/internals/SchemaErrorHandler.java +73 -0
- data/ext/java/nokogiri/internals/XalanDTMManagerPatch.java +168 -0
- data/ext/java/nokogiri/internals/XmlDeclHandler.java +42 -0
- data/ext/java/nokogiri/internals/XmlDomParserContext.java +274 -0
- data/ext/java/nokogiri/internals/XmlSaxParser.java +65 -0
- data/ext/java/nokogiri/internals/c14n/AttrCompare.java +119 -0
- data/ext/java/nokogiri/internals/c14n/C14nHelper.java +159 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalFilter.java +37 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizationException.java +93 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer.java +252 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11.java +639 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_OmitComments.java +38 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_WithComments.java +38 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315.java +367 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315Excl.java +295 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclOmitComments.java +40 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclWithComments.java +44 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315OmitComments.java +44 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315WithComments.java +43 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizerBase.java +630 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizerPhysical.java +173 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizerSpi.java +76 -0
- data/ext/java/nokogiri/internals/c14n/Constants.java +42 -0
- data/ext/java/nokogiri/internals/c14n/ElementProxy.java +293 -0
- data/ext/java/nokogiri/internals/c14n/HelperNodeList.java +93 -0
- data/ext/java/nokogiri/internals/c14n/IgnoreAllErrorHandler.java +79 -0
- data/ext/java/nokogiri/internals/c14n/InclusiveNamespaces.java +166 -0
- data/ext/java/nokogiri/internals/c14n/InvalidCanonicalizerException.java +76 -0
- data/ext/java/nokogiri/internals/c14n/NameSpaceSymbTable.java +402 -0
- data/ext/java/nokogiri/internals/c14n/NodeFilter.java +51 -0
- data/ext/java/nokogiri/internals/c14n/UtfHelpper.java +179 -0
- data/ext/java/nokogiri/internals/c14n/XMLUtils.java +507 -0
- data/ext/java/nokogiri/internals/dom2dtm/DOM2DTM.java +1745 -0
- data/ext/java/nokogiri/internals/dom2dtm/DOM2DTMdefaultNamespaceDeclarationNode.java +685 -0
- data/ext/nokogiri/depend +477 -0
- data/ext/nokogiri/extconf.rb +836 -0
- data/ext/nokogiri/html_document.c +171 -0
- data/ext/nokogiri/html_document.h +10 -0
- data/ext/nokogiri/html_element_description.c +279 -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 +135 -0
- data/ext/nokogiri/nokogiri.h +130 -0
- data/ext/nokogiri/xml_attr.c +103 -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 +62 -0
- data/ext/nokogiri/xml_cdata.h +9 -0
- data/ext/nokogiri/xml_comment.c +69 -0
- data/ext/nokogiri/xml_comment.h +9 -0
- data/ext/nokogiri/xml_document.c +622 -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 +63 -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 +111 -0
- data/ext/nokogiri/xml_namespace.h +14 -0
- data/ext/nokogiri/xml_node.c +1773 -0
- data/ext/nokogiri/xml_node.h +13 -0
- data/ext/nokogiri/xml_node_set.c +486 -0
- data/ext/nokogiri/xml_node_set.h +12 -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 +657 -0
- data/ext/nokogiri/xml_reader.h +10 -0
- data/ext/nokogiri/xml_relax_ng.c +179 -0
- data/ext/nokogiri/xml_relax_ng.h +9 -0
- data/ext/nokogiri/xml_sax_parser.c +305 -0
- data/ext/nokogiri/xml_sax_parser.h +39 -0
- data/ext/nokogiri/xml_sax_parser_context.c +262 -0
- data/ext/nokogiri/xml_sax_parser_context.h +10 -0
- data/ext/nokogiri/xml_sax_push_parser.c +159 -0
- data/ext/nokogiri/xml_sax_push_parser.h +9 -0
- data/ext/nokogiri/xml_schema.c +276 -0
- data/ext/nokogiri/xml_schema.h +9 -0
- data/ext/nokogiri/xml_syntax_error.c +64 -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 +374 -0
- data/ext/nokogiri/xml_xpath_context.h +10 -0
- data/ext/nokogiri/xslt_stylesheet.c +263 -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/css/node.rb +53 -0
- data/lib/nokogiri/css/parser.rb +751 -0
- data/lib/nokogiri/css/parser.y +272 -0
- data/lib/nokogiri/css/parser_extras.rb +94 -0
- data/lib/nokogiri/css/syntax_error.rb +8 -0
- data/lib/nokogiri/css/tokenizer.rb +154 -0
- data/lib/nokogiri/css/tokenizer.rex +55 -0
- data/lib/nokogiri/css/xpath_visitor.rb +260 -0
- data/lib/nokogiri/css.rb +28 -0
- data/lib/nokogiri/decorators/slop.rb +43 -0
- data/lib/nokogiri/html/builder.rb +36 -0
- data/lib/nokogiri/html/document.rb +322 -0
- data/lib/nokogiri/html/document_fragment.rb +50 -0
- data/lib/nokogiri/html/element_description.rb +24 -0
- data/lib/nokogiri/html/element_description_defaults.rb +672 -0
- data/lib/nokogiri/html/entity_lookup.rb +14 -0
- data/lib/nokogiri/html/sax/parser.rb +63 -0
- data/lib/nokogiri/html/sax/parser_context.rb +17 -0
- data/lib/nokogiri/html/sax/push_parser.rb +37 -0
- data/lib/nokogiri/html.rb +38 -0
- data/lib/nokogiri/jruby/dependencies.rb +20 -0
- data/lib/nokogiri/syntax_error.rb +5 -0
- data/lib/nokogiri/version/constant.rb +5 -0
- data/lib/nokogiri/version/info.rb +182 -0
- data/lib/nokogiri/version.rb +3 -0
- data/lib/nokogiri/xml/attr.rb +15 -0
- data/lib/nokogiri/xml/attribute_decl.rb +19 -0
- data/lib/nokogiri/xml/builder.rb +447 -0
- data/lib/nokogiri/xml/cdata.rb +12 -0
- data/lib/nokogiri/xml/character_data.rb +8 -0
- data/lib/nokogiri/xml/document.rb +290 -0
- data/lib/nokogiri/xml/document_fragment.rb +159 -0
- data/lib/nokogiri/xml/dtd.rb +33 -0
- data/lib/nokogiri/xml/element_content.rb +37 -0
- data/lib/nokogiri/xml/element_decl.rb +14 -0
- data/lib/nokogiri/xml/entity_decl.rb +20 -0
- data/lib/nokogiri/xml/entity_reference.rb +19 -0
- data/lib/nokogiri/xml/namespace.rb +14 -0
- data/lib/nokogiri/xml/node/save_options.rb +62 -0
- data/lib/nokogiri/xml/node.rb +1240 -0
- data/lib/nokogiri/xml/node_set.rb +372 -0
- data/lib/nokogiri/xml/notation.rb +7 -0
- data/lib/nokogiri/xml/parse_options.rb +127 -0
- data/lib/nokogiri/xml/pp/character_data.rb +19 -0
- data/lib/nokogiri/xml/pp/node.rb +57 -0
- data/lib/nokogiri/xml/pp.rb +3 -0
- data/lib/nokogiri/xml/processing_instruction.rb +9 -0
- data/lib/nokogiri/xml/reader.rb +116 -0
- data/lib/nokogiri/xml/relax_ng.rb +37 -0
- data/lib/nokogiri/xml/sax/document.rb +172 -0
- data/lib/nokogiri/xml/sax/parser.rb +123 -0
- data/lib/nokogiri/xml/sax/parser_context.rb +17 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +61 -0
- data/lib/nokogiri/xml/sax.rb +5 -0
- data/lib/nokogiri/xml/schema.rb +72 -0
- data/lib/nokogiri/xml/searchable.rb +239 -0
- data/lib/nokogiri/xml/syntax_error.rb +71 -0
- data/lib/nokogiri/xml/text.rb +10 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +12 -0
- data/lib/nokogiri/xml/xpath.rb +11 -0
- data/lib/nokogiri/xml/xpath_context.rb +17 -0
- data/lib/nokogiri/xml.rb +76 -0
- data/lib/nokogiri/xslt/stylesheet.rb +26 -0
- data/lib/nokogiri/xslt.rb +57 -0
- data/lib/nokogiri.rb +144 -0
- data/lib/serializer.jar +0 -0
- data/lib/xalan.jar +0 -0
- data/lib/xercesImpl.jar +0 -0
- data/lib/xml-apis.jar +0 -0
- data/lib/xsd/xmlparser/nokogiri.rb +103 -0
- metadata +531 -0
@@ -0,0 +1,630 @@
|
|
1
|
+
/**
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
4
|
+
* distributed with this work for additional information
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
7
|
+
* "License"); you may not use this file except in compliance
|
8
|
+
* with the License. You may obtain a copy of the License at
|
9
|
+
*
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
*
|
12
|
+
* Unless required by applicable law or agreed to in writing,
|
13
|
+
* software distributed under the License is distributed on an
|
14
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
* KIND, either express or implied. See the License for the
|
16
|
+
* specific language governing permissions and limitations
|
17
|
+
* under the License.
|
18
|
+
*/
|
19
|
+
package nokogiri.internals.c14n;
|
20
|
+
|
21
|
+
import java.io.ByteArrayOutputStream;
|
22
|
+
import java.io.IOException;
|
23
|
+
import java.io.OutputStream;
|
24
|
+
import java.io.UnsupportedEncodingException;
|
25
|
+
import java.util.ArrayList;
|
26
|
+
import java.util.HashMap;
|
27
|
+
import java.util.Iterator;
|
28
|
+
import java.util.List;
|
29
|
+
import java.util.ListIterator;
|
30
|
+
import java.util.Map;
|
31
|
+
import java.util.Set;
|
32
|
+
|
33
|
+
import javax.xml.parsers.DocumentBuilder;
|
34
|
+
import javax.xml.parsers.DocumentBuilderFactory;
|
35
|
+
|
36
|
+
|
37
|
+
import org.w3c.dom.Attr;
|
38
|
+
import org.w3c.dom.Comment;
|
39
|
+
import org.w3c.dom.Element;
|
40
|
+
import org.w3c.dom.NamedNodeMap;
|
41
|
+
import org.w3c.dom.Node;
|
42
|
+
import org.w3c.dom.ProcessingInstruction;
|
43
|
+
|
44
|
+
/**
|
45
|
+
* Abstract base class for canonicalization algorithms.
|
46
|
+
*
|
47
|
+
* @author Christian Geuer-Pollmann <geuerp@apache.org>
|
48
|
+
*/
|
49
|
+
public abstract class CanonicalizerBase extends CanonicalizerSpi {
|
50
|
+
public static final String XML = "xml";
|
51
|
+
public static final String XMLNS = "xmlns";
|
52
|
+
|
53
|
+
protected static final AttrCompare COMPARE = new AttrCompare();
|
54
|
+
protected static final Attr nullNode;
|
55
|
+
|
56
|
+
private static final byte[] END_PI = {'?','>'};
|
57
|
+
private static final byte[] BEGIN_PI = {'<','?'};
|
58
|
+
private static final byte[] END_COMM = {'-','-','>'};
|
59
|
+
private static final byte[] BEGIN_COMM = {'<','!','-','-'};
|
60
|
+
private static final byte[] XA = {'&','#','x','A',';'};
|
61
|
+
private static final byte[] X9 = {'&','#','x','9',';'};
|
62
|
+
private static final byte[] QUOT = {'&','q','u','o','t',';'};
|
63
|
+
private static final byte[] XD = {'&','#','x','D',';'};
|
64
|
+
private static final byte[] GT = {'&','g','t',';'};
|
65
|
+
private static final byte[] LT = {'&','l','t',';'};
|
66
|
+
private static final byte[] END_TAG = {'<','/'};
|
67
|
+
private static final byte[] AMP = {'&','a','m','p',';'};
|
68
|
+
private static final byte[] equalsStr = {'=','\"'};
|
69
|
+
|
70
|
+
protected static final int NODE_BEFORE_DOCUMENT_ELEMENT = -1;
|
71
|
+
protected static final int NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT = 0;
|
72
|
+
protected static final int NODE_AFTER_DOCUMENT_ELEMENT = 1;
|
73
|
+
|
74
|
+
static {
|
75
|
+
// The null xmlns definition.
|
76
|
+
try {
|
77
|
+
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
78
|
+
nullNode = documentBuilder.newDocument().createAttributeNS(Constants.NamespaceSpecNS, XMLNS);
|
79
|
+
nullNode.setValue("");
|
80
|
+
} catch (Exception e) {
|
81
|
+
throw new RuntimeException("Unable to create nullNode: " + e);
|
82
|
+
}
|
83
|
+
}
|
84
|
+
|
85
|
+
private List<NodeFilter> nodeFilter;
|
86
|
+
|
87
|
+
private final boolean includeComments;
|
88
|
+
//private Set<Node> xpathNodeSet;
|
89
|
+
/**
|
90
|
+
* The node to be skipped/excluded from the DOM tree
|
91
|
+
* in subtree canonicalizations.
|
92
|
+
*/
|
93
|
+
private Node excludeNode;
|
94
|
+
private OutputStream writer = new ByteArrayOutputStream();
|
95
|
+
|
96
|
+
/**
|
97
|
+
* Constructor CanonicalizerBase
|
98
|
+
*
|
99
|
+
* @param includeComments
|
100
|
+
*/
|
101
|
+
public CanonicalizerBase(boolean includeComments) {
|
102
|
+
this.includeComments = includeComments;
|
103
|
+
}
|
104
|
+
|
105
|
+
/**
|
106
|
+
* Method engineCanonicalizeSubTree
|
107
|
+
* @inheritDoc
|
108
|
+
* @param rootNode
|
109
|
+
* @throws CanonicalizationException
|
110
|
+
*/
|
111
|
+
@Override
|
112
|
+
public byte[] engineCanonicalizeSubTree(Node rootNode, CanonicalFilter filter)
|
113
|
+
throws CanonicalizationException {
|
114
|
+
return engineCanonicalizeSubTree(rootNode, (Node)null, filter);
|
115
|
+
}
|
116
|
+
|
117
|
+
/**
|
118
|
+
* @param writer The writer to set.
|
119
|
+
*/
|
120
|
+
@Override
|
121
|
+
public void setWriter(OutputStream writer) {
|
122
|
+
this.writer = writer;
|
123
|
+
}
|
124
|
+
|
125
|
+
/**
|
126
|
+
* Canonicalizes a Subtree node.
|
127
|
+
*
|
128
|
+
* @param rootNode
|
129
|
+
* the root of the subtree to canonicalize
|
130
|
+
* @param excludeNode
|
131
|
+
* a node to be excluded from the canonicalize operation
|
132
|
+
* @return The canonicalize stream.
|
133
|
+
* @throws CanonicalizationException
|
134
|
+
*/
|
135
|
+
protected byte[] engineCanonicalizeSubTree(Node rootNode, Node excludeNode, CanonicalFilter filter)
|
136
|
+
throws CanonicalizationException {
|
137
|
+
this.excludeNode = excludeNode;
|
138
|
+
try {
|
139
|
+
NameSpaceSymbTable ns = new NameSpaceSymbTable();
|
140
|
+
int nodeLevel = NODE_BEFORE_DOCUMENT_ELEMENT;
|
141
|
+
if (rootNode != null && Node.ELEMENT_NODE == rootNode.getNodeType()) {
|
142
|
+
//Fills the nssymbtable with the definitions of the parent of the root subnode
|
143
|
+
getParentNameSpaces((Element)rootNode, ns);
|
144
|
+
nodeLevel = NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
|
145
|
+
}
|
146
|
+
this.canonicalizeSubTree(rootNode, ns, rootNode, nodeLevel, filter);
|
147
|
+
this.writer.flush();
|
148
|
+
if (this.writer instanceof ByteArrayOutputStream) {
|
149
|
+
byte[] result = ((ByteArrayOutputStream)this.writer).toByteArray();
|
150
|
+
if (reset) {
|
151
|
+
((ByteArrayOutputStream)this.writer).reset();
|
152
|
+
} else {
|
153
|
+
this.writer.close();
|
154
|
+
}
|
155
|
+
return result;
|
156
|
+
} else {
|
157
|
+
this.writer.close();
|
158
|
+
}
|
159
|
+
return null;
|
160
|
+
|
161
|
+
} catch (UnsupportedEncodingException ex) {
|
162
|
+
throw new CanonicalizationException("empty", ex);
|
163
|
+
} catch (IOException ex) {
|
164
|
+
throw new CanonicalizationException("empty", ex);
|
165
|
+
}
|
166
|
+
}
|
167
|
+
|
168
|
+
|
169
|
+
/**
|
170
|
+
* Method canonicalizeSubTree, this function is a recursive one.
|
171
|
+
*
|
172
|
+
* @param currentNode
|
173
|
+
* @param ns
|
174
|
+
* @param endnode
|
175
|
+
* @throws CanonicalizationException
|
176
|
+
* @throws IOException
|
177
|
+
*/
|
178
|
+
protected final void canonicalizeSubTree(
|
179
|
+
Node currentNode, NameSpaceSymbTable ns, Node endnode, int documentLevel, CanonicalFilter filter
|
180
|
+
) throws CanonicalizationException, IOException {
|
181
|
+
if (isVisibleInt(currentNode) == -1) {
|
182
|
+
return;
|
183
|
+
}
|
184
|
+
Node sibling = null;
|
185
|
+
Node parentNode = null;
|
186
|
+
final OutputStream writer = this.writer;
|
187
|
+
final Node excludeNode = this.excludeNode;
|
188
|
+
final boolean includeComments = this.includeComments;
|
189
|
+
Map<String, byte[]> cache = new HashMap<String, byte[]>();
|
190
|
+
do {
|
191
|
+
switch (currentNode.getNodeType()) {
|
192
|
+
|
193
|
+
case Node.ENTITY_NODE :
|
194
|
+
case Node.NOTATION_NODE :
|
195
|
+
case Node.ATTRIBUTE_NODE :
|
196
|
+
// illegal node type during traversal
|
197
|
+
throw new CanonicalizationException("empty");
|
198
|
+
|
199
|
+
case Node.DOCUMENT_FRAGMENT_NODE :
|
200
|
+
case Node.DOCUMENT_NODE :
|
201
|
+
ns.outputNodePush();
|
202
|
+
sibling = currentNode.getFirstChild();
|
203
|
+
break;
|
204
|
+
|
205
|
+
case Node.COMMENT_NODE :
|
206
|
+
if (includeComments) {
|
207
|
+
outputCommentToWriter((Comment) currentNode, writer, documentLevel);
|
208
|
+
}
|
209
|
+
break;
|
210
|
+
|
211
|
+
case Node.PROCESSING_INSTRUCTION_NODE :
|
212
|
+
outputPItoWriter((ProcessingInstruction) currentNode, writer, documentLevel);
|
213
|
+
break;
|
214
|
+
|
215
|
+
case Node.TEXT_NODE :
|
216
|
+
case Node.CDATA_SECTION_NODE :
|
217
|
+
outputTextToWriter(currentNode.getNodeValue(), writer);
|
218
|
+
break;
|
219
|
+
|
220
|
+
case Node.ELEMENT_NODE :
|
221
|
+
documentLevel = NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
|
222
|
+
if (currentNode == excludeNode) {
|
223
|
+
break;
|
224
|
+
}
|
225
|
+
if (filter != null && !filter.includeNodes(currentNode, parentNode)) {
|
226
|
+
break;
|
227
|
+
}
|
228
|
+
|
229
|
+
Element currentElement = (Element)currentNode;
|
230
|
+
//Add a level to the nssymbtable. So latter can be pop-back.
|
231
|
+
ns.outputNodePush();
|
232
|
+
writer.write('<');
|
233
|
+
String name = currentElement.getTagName();
|
234
|
+
UtfHelpper.writeByte(name, writer, cache);
|
235
|
+
|
236
|
+
Iterator<Attr> attrs = this.handleAttributesSubtree(currentElement, ns);
|
237
|
+
if (attrs != null) {
|
238
|
+
//we output all Attrs which are available
|
239
|
+
while (attrs.hasNext()) {
|
240
|
+
Attr attr = attrs.next();
|
241
|
+
outputAttrToWriter(attr.getNodeName(), attr.getNodeValue(), writer, cache);
|
242
|
+
}
|
243
|
+
}
|
244
|
+
writer.write('>');
|
245
|
+
sibling = currentNode.getFirstChild();
|
246
|
+
if (sibling == null) {
|
247
|
+
writer.write(END_TAG);
|
248
|
+
UtfHelpper.writeStringToUtf8(name, writer);
|
249
|
+
writer.write('>');
|
250
|
+
//We finished with this level, pop to the previous definitions.
|
251
|
+
ns.outputNodePop();
|
252
|
+
if (parentNode != null) {
|
253
|
+
sibling = currentNode.getNextSibling();
|
254
|
+
}
|
255
|
+
} else {
|
256
|
+
parentNode = currentElement;
|
257
|
+
}
|
258
|
+
break;
|
259
|
+
case Node.DOCUMENT_TYPE_NODE :
|
260
|
+
default :
|
261
|
+
break;
|
262
|
+
}
|
263
|
+
while (sibling == null && parentNode != null) {
|
264
|
+
writer.write(END_TAG);
|
265
|
+
UtfHelpper.writeByte(((Element)parentNode).getTagName(), writer, cache);
|
266
|
+
writer.write('>');
|
267
|
+
//We finished with this level, pop to the previous definitions.
|
268
|
+
ns.outputNodePop();
|
269
|
+
if (parentNode == endnode) {
|
270
|
+
return;
|
271
|
+
}
|
272
|
+
sibling = parentNode.getNextSibling();
|
273
|
+
parentNode = parentNode.getParentNode();
|
274
|
+
if (parentNode == null || Node.ELEMENT_NODE != parentNode.getNodeType()) {
|
275
|
+
documentLevel = NODE_AFTER_DOCUMENT_ELEMENT;
|
276
|
+
parentNode = null;
|
277
|
+
}
|
278
|
+
}
|
279
|
+
if (sibling == null) {
|
280
|
+
return;
|
281
|
+
}
|
282
|
+
currentNode = sibling;
|
283
|
+
sibling = currentNode.getNextSibling();
|
284
|
+
} while(true);
|
285
|
+
}
|
286
|
+
|
287
|
+
protected int isVisibleDO(Node currentNode, int level) {
|
288
|
+
if (nodeFilter != null) {
|
289
|
+
Iterator<NodeFilter> it = nodeFilter.iterator();
|
290
|
+
while (it.hasNext()) {
|
291
|
+
int i = (it.next()).isNodeIncludeDO(currentNode, level);
|
292
|
+
if (i != 1) {
|
293
|
+
return i;
|
294
|
+
}
|
295
|
+
}
|
296
|
+
}
|
297
|
+
//if ((this.xpathNodeSet != null) && !this.xpathNodeSet.contains(currentNode)) {
|
298
|
+
// return 0;
|
299
|
+
//}
|
300
|
+
return 1;
|
301
|
+
}
|
302
|
+
|
303
|
+
protected int isVisibleInt(Node currentNode) {
|
304
|
+
if (nodeFilter != null) {
|
305
|
+
Iterator<NodeFilter> it = nodeFilter.iterator();
|
306
|
+
while (it.hasNext()) {
|
307
|
+
int i = (it.next()).isNodeInclude(currentNode);
|
308
|
+
if (i != 1) {
|
309
|
+
return i;
|
310
|
+
}
|
311
|
+
}
|
312
|
+
}
|
313
|
+
//if ((this.xpathNodeSet != null) && !this.xpathNodeSet.contains(currentNode)) {
|
314
|
+
// return 0;
|
315
|
+
//}
|
316
|
+
return 1;
|
317
|
+
}
|
318
|
+
|
319
|
+
protected boolean isVisible(Node currentNode) {
|
320
|
+
if (nodeFilter != null) {
|
321
|
+
Iterator<NodeFilter> it = nodeFilter.iterator();
|
322
|
+
while (it.hasNext()) {
|
323
|
+
if (it.next().isNodeInclude(currentNode) != 1) {
|
324
|
+
return false;
|
325
|
+
}
|
326
|
+
}
|
327
|
+
}
|
328
|
+
//if ((this.xpathNodeSet != null) && !this.xpathNodeSet.contains(currentNode)) {
|
329
|
+
// return false;
|
330
|
+
//}
|
331
|
+
return true;
|
332
|
+
}
|
333
|
+
|
334
|
+
protected void handleParent(Element e, NameSpaceSymbTable ns) {
|
335
|
+
if (!e.hasAttributes() && e.getNamespaceURI() == null) {
|
336
|
+
return;
|
337
|
+
}
|
338
|
+
NamedNodeMap attrs = e.getAttributes();
|
339
|
+
int attrsLength = attrs.getLength();
|
340
|
+
for (int i = 0; i < attrsLength; i++) {
|
341
|
+
Attr attribute = (Attr) attrs.item(i);
|
342
|
+
String NName = attribute.getLocalName();
|
343
|
+
String NValue = attribute.getNodeValue();
|
344
|
+
|
345
|
+
if (Constants.NamespaceSpecNS.equals(attribute.getNamespaceURI())
|
346
|
+
&& (!XML.equals(NName) || !Constants.XML_LANG_SPACE_SpecNS.equals(NValue))) {
|
347
|
+
ns.addMapping(NName, NValue, attribute);
|
348
|
+
}
|
349
|
+
}
|
350
|
+
if (e.getNamespaceURI() != null) {
|
351
|
+
String NName = e.getPrefix();
|
352
|
+
String NValue = e.getNamespaceURI();
|
353
|
+
String Name;
|
354
|
+
if (NName == null || NName.equals("")) {
|
355
|
+
NName = XMLNS;
|
356
|
+
Name = XMLNS;
|
357
|
+
} else {
|
358
|
+
Name = XMLNS + ":" + NName;
|
359
|
+
}
|
360
|
+
Attr n = e.getOwnerDocument().createAttributeNS("http://www.w3.org/2000/xmlns/", Name);
|
361
|
+
n.setValue(NValue);
|
362
|
+
ns.addMapping(NName, NValue, n);
|
363
|
+
}
|
364
|
+
}
|
365
|
+
|
366
|
+
/**
|
367
|
+
* Adds to ns the definitions from the parent elements of el
|
368
|
+
* @param el
|
369
|
+
* @param ns
|
370
|
+
*/
|
371
|
+
protected final void getParentNameSpaces(Element el, NameSpaceSymbTable ns) {
|
372
|
+
Node n1 = el.getParentNode();
|
373
|
+
if (n1 == null || Node.ELEMENT_NODE != n1.getNodeType()) {
|
374
|
+
return;
|
375
|
+
}
|
376
|
+
//Obtain all the parents of the element
|
377
|
+
List<Element> parents = new ArrayList<Element>();
|
378
|
+
Node parent = n1;
|
379
|
+
while (parent != null && Node.ELEMENT_NODE == parent.getNodeType()) {
|
380
|
+
parents.add((Element)parent);
|
381
|
+
parent = parent.getParentNode();
|
382
|
+
}
|
383
|
+
//Visit them in reverse order.
|
384
|
+
ListIterator<Element> it = parents.listIterator(parents.size());
|
385
|
+
while (it.hasPrevious()) {
|
386
|
+
Element ele = it.previous();
|
387
|
+
handleParent(ele, ns);
|
388
|
+
}
|
389
|
+
parents.clear();
|
390
|
+
Attr nsprefix;
|
391
|
+
if (((nsprefix = ns.getMappingWithoutRendered(XMLNS)) != null)
|
392
|
+
&& "".equals(nsprefix.getValue())) {
|
393
|
+
ns.addMappingAndRender(XMLNS, "", nullNode);
|
394
|
+
}
|
395
|
+
}
|
396
|
+
|
397
|
+
/**
|
398
|
+
* Obtain the attributes to output for this node in XPathNodeSet c14n.
|
399
|
+
*
|
400
|
+
* @param element
|
401
|
+
* @param ns
|
402
|
+
* @return the attributes nodes to output.
|
403
|
+
* @throws CanonicalizationException
|
404
|
+
*/
|
405
|
+
abstract Iterator<Attr> handleAttributes(Element element, NameSpaceSymbTable ns)
|
406
|
+
throws CanonicalizationException;
|
407
|
+
|
408
|
+
/**
|
409
|
+
* Obtain the attributes to output for this node in a Subtree c14n.
|
410
|
+
*
|
411
|
+
* @param element
|
412
|
+
* @param ns
|
413
|
+
* @return the attributes nodes to output.
|
414
|
+
* @throws CanonicalizationException
|
415
|
+
*/
|
416
|
+
abstract Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns)
|
417
|
+
throws CanonicalizationException;
|
418
|
+
|
419
|
+
//abstract void circumventBugIfNeeded(XMLSignatureInput input)
|
420
|
+
// throws CanonicalizationException, ParserConfigurationException, IOException, SAXException;
|
421
|
+
|
422
|
+
/**
|
423
|
+
* Outputs an Attribute to the internal Writer.
|
424
|
+
*
|
425
|
+
* The string value of the node is modified by replacing
|
426
|
+
* <UL>
|
427
|
+
* <LI>all ampersands (&) with <CODE>&amp;</CODE></LI>
|
428
|
+
* <LI>all open angle brackets (<) with <CODE>&lt;</CODE></LI>
|
429
|
+
* <LI>all quotation mark characters with <CODE>&quot;</CODE></LI>
|
430
|
+
* <LI>and the whitespace characters <CODE>#x9</CODE>, #xA, and #xD, with character
|
431
|
+
* references. The character references are written in uppercase
|
432
|
+
* hexadecimal with no leading zeroes (for example, <CODE>#xD</CODE> is represented
|
433
|
+
* by the character reference <CODE>&#xD;</CODE>)</LI>
|
434
|
+
* </UL>
|
435
|
+
*
|
436
|
+
* @param name
|
437
|
+
* @param value
|
438
|
+
* @param writer
|
439
|
+
* @throws IOException
|
440
|
+
*/
|
441
|
+
protected static final void outputAttrToWriter(
|
442
|
+
final String name, final String value,
|
443
|
+
final OutputStream writer, final Map<String, byte[]> cache
|
444
|
+
) throws IOException {
|
445
|
+
writer.write(' ');
|
446
|
+
UtfHelpper.writeByte(name, writer, cache);
|
447
|
+
writer.write(equalsStr);
|
448
|
+
byte[] toWrite;
|
449
|
+
final int length = value.length();
|
450
|
+
int i = 0;
|
451
|
+
while (i < length) {
|
452
|
+
char c = value.charAt(i++);
|
453
|
+
|
454
|
+
switch (c) {
|
455
|
+
|
456
|
+
case '&' :
|
457
|
+
toWrite = AMP;
|
458
|
+
break;
|
459
|
+
|
460
|
+
case '<' :
|
461
|
+
toWrite = LT;
|
462
|
+
break;
|
463
|
+
|
464
|
+
case '"' :
|
465
|
+
toWrite = QUOT;
|
466
|
+
break;
|
467
|
+
|
468
|
+
case 0x09 : // '\t'
|
469
|
+
toWrite = X9;
|
470
|
+
break;
|
471
|
+
|
472
|
+
case 0x0A : // '\n'
|
473
|
+
toWrite = XA;
|
474
|
+
break;
|
475
|
+
|
476
|
+
case 0x0D : // '\r'
|
477
|
+
toWrite = XD;
|
478
|
+
break;
|
479
|
+
|
480
|
+
default :
|
481
|
+
if (c < 0x80) {
|
482
|
+
writer.write(c);
|
483
|
+
} else {
|
484
|
+
UtfHelpper.writeCharToUtf8(c, writer);
|
485
|
+
}
|
486
|
+
continue;
|
487
|
+
}
|
488
|
+
writer.write(toWrite);
|
489
|
+
}
|
490
|
+
|
491
|
+
writer.write('\"');
|
492
|
+
}
|
493
|
+
|
494
|
+
/**
|
495
|
+
* Outputs a PI to the internal Writer.
|
496
|
+
*
|
497
|
+
* @param currentPI
|
498
|
+
* @param writer where to write the things
|
499
|
+
* @throws IOException
|
500
|
+
*/
|
501
|
+
protected void outputPItoWriter(
|
502
|
+
ProcessingInstruction currentPI, OutputStream writer, int position
|
503
|
+
) throws IOException {
|
504
|
+
if (position == NODE_AFTER_DOCUMENT_ELEMENT) {
|
505
|
+
writer.write('\n');
|
506
|
+
}
|
507
|
+
writer.write(BEGIN_PI);
|
508
|
+
|
509
|
+
final String target = currentPI.getTarget();
|
510
|
+
int length = target.length();
|
511
|
+
|
512
|
+
for (int i = 0; i < length; i++) {
|
513
|
+
char c = target.charAt(i);
|
514
|
+
if (c == 0x0D) {
|
515
|
+
writer.write(XD);
|
516
|
+
} else {
|
517
|
+
if (c < 0x80) {
|
518
|
+
writer.write(c);
|
519
|
+
} else {
|
520
|
+
UtfHelpper.writeCharToUtf8(c, writer);
|
521
|
+
}
|
522
|
+
}
|
523
|
+
}
|
524
|
+
|
525
|
+
final String data = currentPI.getData();
|
526
|
+
|
527
|
+
length = data.length();
|
528
|
+
|
529
|
+
if (length > 0) {
|
530
|
+
writer.write(' ');
|
531
|
+
|
532
|
+
for (int i = 0; i < length; i++) {
|
533
|
+
char c = data.charAt(i);
|
534
|
+
if (c == 0x0D) {
|
535
|
+
writer.write(XD);
|
536
|
+
} else {
|
537
|
+
UtfHelpper.writeCharToUtf8(c, writer);
|
538
|
+
}
|
539
|
+
}
|
540
|
+
}
|
541
|
+
|
542
|
+
writer.write(END_PI);
|
543
|
+
if (position == NODE_BEFORE_DOCUMENT_ELEMENT) {
|
544
|
+
writer.write('\n');
|
545
|
+
}
|
546
|
+
}
|
547
|
+
|
548
|
+
/**
|
549
|
+
* Method outputCommentToWriter
|
550
|
+
*
|
551
|
+
* @param currentComment
|
552
|
+
* @param writer writer where to write the things
|
553
|
+
* @throws IOException
|
554
|
+
*/
|
555
|
+
protected void outputCommentToWriter(
|
556
|
+
Comment currentComment, OutputStream writer, int position
|
557
|
+
) throws IOException {
|
558
|
+
if (position == NODE_AFTER_DOCUMENT_ELEMENT) {
|
559
|
+
writer.write('\n');
|
560
|
+
}
|
561
|
+
writer.write(BEGIN_COMM);
|
562
|
+
|
563
|
+
final String data = currentComment.getData();
|
564
|
+
final int length = data.length();
|
565
|
+
|
566
|
+
for (int i = 0; i < length; i++) {
|
567
|
+
char c = data.charAt(i);
|
568
|
+
if (c == 0x0D) {
|
569
|
+
writer.write(XD);
|
570
|
+
} else {
|
571
|
+
if (c < 0x80) {
|
572
|
+
writer.write(c);
|
573
|
+
} else {
|
574
|
+
UtfHelpper.writeCharToUtf8(c, writer);
|
575
|
+
}
|
576
|
+
}
|
577
|
+
}
|
578
|
+
|
579
|
+
writer.write(END_COMM);
|
580
|
+
if (position == NODE_BEFORE_DOCUMENT_ELEMENT) {
|
581
|
+
writer.write('\n');
|
582
|
+
}
|
583
|
+
}
|
584
|
+
|
585
|
+
/**
|
586
|
+
* Outputs a Text of CDATA section to the internal Writer.
|
587
|
+
*
|
588
|
+
* @param text
|
589
|
+
* @param writer writer where to write the things
|
590
|
+
* @throws IOException
|
591
|
+
*/
|
592
|
+
protected static final void outputTextToWriter(
|
593
|
+
final String text, final OutputStream writer
|
594
|
+
) throws IOException {
|
595
|
+
final int length = text.length();
|
596
|
+
byte[] toWrite;
|
597
|
+
for (int i = 0; i < length; i++) {
|
598
|
+
char c = text.charAt(i);
|
599
|
+
|
600
|
+
switch (c) {
|
601
|
+
|
602
|
+
case '&' :
|
603
|
+
toWrite = AMP;
|
604
|
+
break;
|
605
|
+
|
606
|
+
case '<' :
|
607
|
+
toWrite = LT;
|
608
|
+
break;
|
609
|
+
|
610
|
+
case '>' :
|
611
|
+
toWrite = GT;
|
612
|
+
break;
|
613
|
+
|
614
|
+
case 0xD :
|
615
|
+
toWrite = XD;
|
616
|
+
break;
|
617
|
+
|
618
|
+
default :
|
619
|
+
if (c < 0x80) {
|
620
|
+
writer.write(c);
|
621
|
+
} else {
|
622
|
+
UtfHelpper.writeCharToUtf8(c, writer);
|
623
|
+
}
|
624
|
+
continue;
|
625
|
+
}
|
626
|
+
writer.write(toWrite);
|
627
|
+
}
|
628
|
+
}
|
629
|
+
|
630
|
+
}
|