nokogiri 1.6.1-java → 1.6.2-java

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of nokogiri might be problematic. Click here for more details.

Files changed (137) hide show
  1. checksums.yaml +4 -4
  2. data/.editorconfig +17 -0
  3. data/.travis.yml +6 -6
  4. data/CHANGELOG.ja.rdoc +61 -8
  5. data/CHANGELOG.rdoc +58 -3
  6. data/Gemfile +3 -3
  7. data/Manifest.txt +57 -1
  8. data/README.ja.rdoc +22 -17
  9. data/README.rdoc +23 -18
  10. data/ROADMAP.md +1 -2
  11. data/Rakefile +162 -58
  12. data/build_all +56 -31
  13. data/dependencies.yml +3 -3
  14. data/ext/java/nokogiri/NokogiriService.java +9 -5
  15. data/ext/java/nokogiri/XmlDocument.java +95 -54
  16. data/ext/java/nokogiri/XmlNode.java +93 -42
  17. data/ext/java/nokogiri/XmlReader.java +1 -1
  18. data/ext/java/nokogiri/XmlSaxParserContext.java +33 -0
  19. data/ext/java/nokogiri/XmlSchema.java +4 -2
  20. data/ext/java/nokogiri/XmlXpathContext.java +118 -76
  21. data/ext/java/nokogiri/internals/IgnoreSchemaErrorsErrorHandler.java +20 -0
  22. data/ext/java/nokogiri/internals/NokogiriHandler.java +3 -10
  23. data/ext/java/nokogiri/internals/NokogiriHelpers.java +40 -23
  24. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +59 -54
  25. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +1 -1
  26. data/ext/java/nokogiri/internals/ParserContext.java +1 -4
  27. data/ext/java/nokogiri/internals/SaveContextVisitor.java +6 -2
  28. data/ext/java/nokogiri/internals/c14n/AttrCompare.java +119 -0
  29. data/ext/java/nokogiri/internals/c14n/C14nHelper.java +159 -0
  30. data/ext/java/nokogiri/internals/c14n/CanonicalFilter.java +37 -0
  31. data/ext/java/nokogiri/internals/c14n/CanonicalizationException.java +93 -0
  32. data/ext/java/nokogiri/internals/c14n/Canonicalizer.java +252 -0
  33. data/ext/java/nokogiri/internals/c14n/Canonicalizer11.java +639 -0
  34. data/ext/java/nokogiri/internals/c14n/Canonicalizer11_OmitComments.java +38 -0
  35. data/ext/java/nokogiri/internals/c14n/Canonicalizer11_WithComments.java +38 -0
  36. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315.java +368 -0
  37. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315Excl.java +295 -0
  38. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclOmitComments.java +40 -0
  39. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclWithComments.java +44 -0
  40. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315OmitComments.java +44 -0
  41. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315WithComments.java +43 -0
  42. data/ext/java/nokogiri/internals/c14n/CanonicalizerBase.java +630 -0
  43. data/ext/java/nokogiri/internals/c14n/CanonicalizerPhysical.java +173 -0
  44. data/ext/java/nokogiri/internals/c14n/CanonicalizerSpi.java +76 -0
  45. data/ext/java/nokogiri/internals/c14n/Constants.java +42 -0
  46. data/ext/java/nokogiri/internals/c14n/ElementProxy.java +293 -0
  47. data/ext/java/nokogiri/internals/c14n/HelperNodeList.java +93 -0
  48. data/ext/java/nokogiri/internals/c14n/IgnoreAllErrorHandler.java +79 -0
  49. data/ext/java/nokogiri/internals/c14n/InclusiveNamespaces.java +165 -0
  50. data/ext/java/nokogiri/internals/c14n/InvalidCanonicalizerException.java +76 -0
  51. data/ext/java/nokogiri/internals/c14n/NameSpaceSymbTable.java +402 -0
  52. data/ext/java/nokogiri/internals/c14n/NodeFilter.java +51 -0
  53. data/ext/java/nokogiri/internals/c14n/UtfHelpper.java +179 -0
  54. data/ext/java/nokogiri/internals/c14n/XMLUtils.java +507 -0
  55. data/ext/nokogiri/extconf.rb +429 -128
  56. data/ext/nokogiri/html_document.c +2 -2
  57. data/ext/nokogiri/nokogiri.c +6 -1
  58. data/ext/nokogiri/xml_document.c +5 -4
  59. data/ext/nokogiri/xml_node.c +76 -7
  60. data/ext/nokogiri/xml_reader.c +1 -1
  61. data/ext/nokogiri/xml_sax_parser_context.c +40 -0
  62. data/ext/nokogiri/xml_syntax_error.c +10 -5
  63. data/ext/nokogiri/xml_syntax_error.h +1 -1
  64. data/ext/nokogiri/xml_xpath_context.c +2 -14
  65. data/ext/nokogiri/xslt_stylesheet.c +1 -1
  66. data/lib/nokogiri.rb +31 -22
  67. data/lib/nokogiri/css/node.rb +0 -50
  68. data/lib/nokogiri/css/parser.rb +213 -218
  69. data/lib/nokogiri/css/parser.y +21 -30
  70. data/lib/nokogiri/css/xpath_visitor.rb +62 -14
  71. data/lib/nokogiri/html/document.rb +97 -18
  72. data/lib/nokogiri/html/sax/parser.rb +2 -2
  73. data/lib/nokogiri/nokogiri.jar +0 -0
  74. data/lib/nokogiri/version.rb +1 -1
  75. data/lib/nokogiri/xml/builder.rb +1 -1
  76. data/lib/nokogiri/xml/document.rb +2 -2
  77. data/lib/nokogiri/xml/dtd.rb +10 -0
  78. data/lib/nokogiri/xml/node.rb +26 -1
  79. data/lib/nokogiri/xml/sax/parser.rb +1 -1
  80. data/ports/patches/libxml2/0001-Fix-parser-local-buffers-size-problems.patch +265 -0
  81. data/ports/patches/libxml2/0002-Fix-entities-local-buffers-size-problems.patch +102 -0
  82. data/ports/patches/libxml2/0003-Fix-an-error-in-previous-commit.patch +26 -0
  83. data/ports/patches/libxml2/0004-Fix-potential-out-of-bound-access.patch +26 -0
  84. data/ports/patches/libxml2/0005-Detect-excessive-entities-expansion-upon-replacement.patch +158 -0
  85. data/ports/patches/libxml2/0006-Do-not-fetch-external-parsed-entities.patch +78 -0
  86. data/ports/patches/libxml2/0007-Enforce-XML_PARSER_EOF-state-handling-through-the-pa.patch +480 -0
  87. data/ports/patches/libxml2/0008-Improve-handling-of-xmlStopParser.patch +315 -0
  88. data/ports/patches/libxml2/0009-Fix-a-couple-of-return-without-value.patch +37 -0
  89. data/ports/patches/libxslt/0001-Adding-doc-update-related-to-1.1.28.patch +222 -0
  90. data/ports/patches/libxslt/0002-Fix-a-couple-of-places-where-f-printf-parameters-wer.patch +53 -0
  91. data/ports/patches/libxslt/0003-Initialize-pseudo-random-number-generator-with-curre.patch +60 -0
  92. data/ports/patches/libxslt/0004-EXSLT-function-str-replace-is-broken-as-is.patch +42 -0
  93. data/ports/patches/libxslt/0006-Fix-str-padding-to-work-with-UTF-8-strings.patch +164 -0
  94. data/ports/patches/libxslt/0007-Separate-function-for-predicate-matching-in-patterns.patch +587 -0
  95. data/ports/patches/libxslt/0008-Fix-direct-pattern-matching.patch +80 -0
  96. data/ports/patches/libxslt/0009-Fix-certain-patterns-with-predicates.patch +185 -0
  97. data/ports/patches/libxslt/0010-Fix-handling-of-UTF-8-strings-in-EXSLT-crypto-module.patch +126 -0
  98. data/ports/patches/libxslt/0013-Memory-leak-in-xsltCompileIdKeyPattern-error-path.patch +25 -0
  99. data/ports/patches/libxslt/0014-Fix-for-bug-436589.patch +43 -0
  100. data/ports/patches/libxslt/0015-Fix-mkdir-for-mingw.patch +41 -0
  101. data/suppressions/README.txt +1 -0
  102. data/suppressions/nokogiri_ree-1.8.7.358.supp +61 -0
  103. data/suppressions/nokogiri_ruby-1.8.7.370.supp +0 -0
  104. data/suppressions/nokogiri_ruby-1.9.2.320.supp +28 -0
  105. data/suppressions/nokogiri_ruby-1.9.3.327.supp +28 -0
  106. data/test/css/test_nthiness.rb +65 -2
  107. data/test/css/test_parser.rb +27 -10
  108. data/test/css/test_tokenizer.rb +1 -1
  109. data/test/css/test_xpath_visitor.rb +6 -1
  110. data/test/files/atom.xml +344 -0
  111. data/test/files/shift_jis_no_charset.html +9 -0
  112. data/test/helper.rb +10 -0
  113. data/test/html/test_document.rb +74 -7
  114. data/test/html/test_document_encoding.rb +10 -0
  115. data/test/html/test_document_fragment.rb +9 -3
  116. data/test/namespaces/test_namespaces_aliased_default.rb +24 -0
  117. data/test/namespaces/test_namespaces_in_cloned_doc.rb +31 -0
  118. data/test/namespaces/test_namespaces_preservation.rb +31 -0
  119. data/test/test_nokogiri.rb +6 -0
  120. data/test/test_reader.rb +7 -4
  121. data/test/test_xslt_transforms.rb +25 -0
  122. data/test/xml/sax/test_parser.rb +16 -0
  123. data/test/xml/sax/test_parser_context.rb +9 -0
  124. data/test/xml/test_builder.rb +9 -0
  125. data/test/xml/test_c14n.rb +12 -2
  126. data/test/xml/test_document.rb +66 -0
  127. data/test/xml/test_document_fragment.rb +5 -0
  128. data/test/xml/test_dtd.rb +84 -0
  129. data/test/xml/test_entity_reference.rb +3 -3
  130. data/test/xml/test_node.rb +21 -3
  131. data/test/xml/test_node_attributes.rb +17 -0
  132. data/test/xml/test_schema.rb +26 -0
  133. data/test/xml/test_text.rb +15 -0
  134. data/test/xml/test_xpath.rb +87 -0
  135. data/test_all +3 -3
  136. metadata +119 -68
  137. data/tasks/cross_compile.rb +0 -134
@@ -0,0 +1,40 @@
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
+
22
+ public class Canonicalizer20010315ExclOmitComments extends Canonicalizer20010315Excl {
23
+
24
+ /**
25
+ *
26
+ */
27
+ public Canonicalizer20010315ExclOmitComments() {
28
+ super(false);
29
+ }
30
+
31
+ /** @inheritDoc */
32
+ public final String engineGetURI() {
33
+ return Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS;
34
+ }
35
+
36
+ /** @inheritDoc */
37
+ public final boolean engineGetIncludeComments() {
38
+ return false;
39
+ }
40
+ }
@@ -0,0 +1,44 @@
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
+
22
+ /**
23
+ * Class Canonicalizer20010315ExclWithComments
24
+ */
25
+ public class Canonicalizer20010315ExclWithComments extends Canonicalizer20010315Excl {
26
+
27
+ /**
28
+ * Constructor Canonicalizer20010315ExclWithComments
29
+ *
30
+ */
31
+ public Canonicalizer20010315ExclWithComments() {
32
+ super(true);
33
+ }
34
+
35
+ /** @inheritDoc */
36
+ public final String engineGetURI() {
37
+ return Canonicalizer.ALGO_ID_C14N_EXCL_WITH_COMMENTS;
38
+ }
39
+
40
+ /** @inheritDoc */
41
+ public final boolean engineGetIncludeComments() {
42
+ return true;
43
+ }
44
+ }
@@ -0,0 +1,44 @@
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
+
22
+ /**
23
+ * @author Christian Geuer-Pollmann
24
+ */
25
+ public class Canonicalizer20010315OmitComments extends Canonicalizer20010315 {
26
+
27
+ /**
28
+ * Constructor Canonicalizer20010315WithXPathOmitComments
29
+ *
30
+ */
31
+ public Canonicalizer20010315OmitComments() {
32
+ super(false);
33
+ }
34
+
35
+ /** @inheritDoc */
36
+ public final String engineGetURI() {
37
+ return Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
38
+ }
39
+
40
+ /** @inheritDoc */
41
+ public final boolean engineGetIncludeComments() {
42
+ return false;
43
+ }
44
+ }
@@ -0,0 +1,43 @@
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
+
22
+ /**
23
+ * @author Christian Geuer-Pollmann
24
+ */
25
+ public class Canonicalizer20010315WithComments extends Canonicalizer20010315 {
26
+
27
+ /**
28
+ * Constructor Canonicalizer20010315WithXPathWithComments
29
+ */
30
+ public Canonicalizer20010315WithComments() {
31
+ super(true);
32
+ }
33
+
34
+ /** @inheritDoc */
35
+ public final String engineGetURI() {
36
+ return Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS;
37
+ }
38
+
39
+ /** @inheritDoc */
40
+ public final boolean engineGetIncludeComments() {
41
+ return true;
42
+ }
43
+ }
@@ -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;amp;</CODE></LI>
428
+ * <LI>all open angle brackets (<) with <CODE>&amp;lt;</CODE></LI>
429
+ * <LI>all quotation mark characters with <CODE>&amp;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>&amp;#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
+ }