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,51 @@
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 org.w3c.dom.Node;
22
+
23
+ /**
24
+ * An interface to tell to the c14n if a node is included or not in the output
25
+ */
26
+ public interface NodeFilter {
27
+
28
+ /**
29
+ * Tells if a node must be output in c14n.
30
+ * @param n
31
+ * @return 1 if the node should be output.
32
+ * 0 if node must not be output,
33
+ * -1 if the node and all it's child must not be output.
34
+ *
35
+ */
36
+ int isNodeInclude(Node n);
37
+
38
+ /**
39
+ * Tells if a node must be output in a c14n.
40
+ * The caller must assured that this method is always call
41
+ * in document order. The implementations can use this
42
+ * restriction to optimize the transformation.
43
+ * @param n
44
+ * @param level the relative level in the tree
45
+ * @return 1 if the node should be output.
46
+ * 0 if node must not be output,
47
+ * -1 if the node and all it's child must not be output.
48
+ */
49
+ int isNodeIncludeDO(Node n, int level);
50
+
51
+ }
@@ -0,0 +1,179 @@
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.IOException;
22
+ import java.io.OutputStream;
23
+ import java.util.Map;
24
+
25
+ public final class UtfHelpper {
26
+
27
+ private UtfHelpper() {
28
+ // complete
29
+ }
30
+
31
+ public static void writeByte(
32
+ final String str,
33
+ final OutputStream out,
34
+ Map<String, byte[]> cache
35
+ ) throws IOException {
36
+ byte[] result = cache.get(str);
37
+ if (result == null) {
38
+ result = getStringInUtf8(str);
39
+ cache.put(str, result);
40
+ }
41
+
42
+ out.write(result);
43
+ }
44
+
45
+ public static void writeCharToUtf8(final char c, final OutputStream out) throws IOException {
46
+ if (c < 0x80) {
47
+ out.write(c);
48
+ return;
49
+ }
50
+ if ((c >= 0xD800 && c <= 0xDBFF) || (c >= 0xDC00 && c <= 0xDFFF)) {
51
+ //No Surrogates in sun java
52
+ out.write(0x3f);
53
+ return;
54
+ }
55
+ int bias;
56
+ int write;
57
+ char ch;
58
+ if (c > 0x07FF) {
59
+ ch = (char)(c>>>12);
60
+ write = 0xE0;
61
+ if (ch > 0) {
62
+ write |= (ch & 0x0F);
63
+ }
64
+ out.write(write);
65
+ write = 0x80;
66
+ bias = 0x3F;
67
+ } else {
68
+ write = 0xC0;
69
+ bias = 0x1F;
70
+ }
71
+ ch = (char)(c>>>6);
72
+ if (ch > 0) {
73
+ write |= (ch & bias);
74
+ }
75
+ out.write(write);
76
+ out.write(0x80 | ((c) & 0x3F));
77
+
78
+ }
79
+
80
+ public static void writeStringToUtf8(
81
+ final String str,
82
+ final OutputStream out
83
+ ) throws IOException{
84
+ final int length = str.length();
85
+ int i = 0;
86
+ char c;
87
+ while (i < length) {
88
+ c = str.charAt(i++);
89
+ if (c < 0x80) {
90
+ out.write(c);
91
+ continue;
92
+ }
93
+ if ((c >= 0xD800 && c <= 0xDBFF) || (c >= 0xDC00 && c <= 0xDFFF)) {
94
+ //No Surrogates in sun java
95
+ out.write(0x3f);
96
+ continue;
97
+ }
98
+ char ch;
99
+ int bias;
100
+ int write;
101
+ if (c > 0x07FF) {
102
+ ch = (char)(c>>>12);
103
+ write = 0xE0;
104
+ if (ch > 0) {
105
+ write |= (ch & 0x0F);
106
+ }
107
+ out.write(write);
108
+ write = 0x80;
109
+ bias = 0x3F;
110
+ } else {
111
+ write = 0xC0;
112
+ bias = 0x1F;
113
+ }
114
+ ch = (char)(c>>>6);
115
+ if (ch > 0) {
116
+ write |= (ch & bias);
117
+ }
118
+ out.write(write);
119
+ out.write(0x80 | ((c) & 0x3F));
120
+
121
+ }
122
+
123
+ }
124
+
125
+ public static byte[] getStringInUtf8(final String str) {
126
+ final int length = str.length();
127
+ boolean expanded = false;
128
+ byte[] result = new byte[length];
129
+ int i = 0;
130
+ int out = 0;
131
+ char c;
132
+ while (i < length) {
133
+ c = str.charAt(i++);
134
+ if (c < 0x80) {
135
+ result[out++] = (byte)c;
136
+ continue;
137
+ }
138
+ if ((c >= 0xD800 && c <= 0xDBFF) || (c >= 0xDC00 && c <= 0xDFFF)) {
139
+ //No Surrogates in sun java
140
+ result[out++] = 0x3f;
141
+ continue;
142
+ }
143
+ if (!expanded) {
144
+ byte newResult[] = new byte[3*length];
145
+ System.arraycopy(result, 0, newResult, 0, out);
146
+ result = newResult;
147
+ expanded = true;
148
+ }
149
+ char ch;
150
+ int bias;
151
+ byte write;
152
+ if (c > 0x07FF) {
153
+ ch = (char)(c>>>12);
154
+ write = (byte)0xE0;
155
+ if (ch > 0) {
156
+ write |= (ch & 0x0F);
157
+ }
158
+ result[out++] = write;
159
+ write = (byte)0x80;
160
+ bias = 0x3F;
161
+ } else {
162
+ write = (byte)0xC0;
163
+ bias = 0x1F;
164
+ }
165
+ ch = (char)(c>>>6);
166
+ if (ch > 0) {
167
+ write |= (ch & bias);
168
+ }
169
+ result[out++] = write;
170
+ result[out++] = (byte)(0x80 | ((c) & 0x3F));
171
+ }
172
+ if (expanded) {
173
+ byte newResult[] = new byte[out];
174
+ System.arraycopy(result, 0, newResult, 0, out);
175
+ result = newResult;
176
+ }
177
+ return result;
178
+ }
179
+ }
@@ -0,0 +1,507 @@
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.util.ArrayList;
22
+ import java.util.HashSet;
23
+ import java.util.Iterator;
24
+ import java.util.List;
25
+ import java.util.Set;
26
+
27
+
28
+ import org.w3c.dom.Attr;
29
+ import org.w3c.dom.Document;
30
+ import org.w3c.dom.Element;
31
+ import org.w3c.dom.NamedNodeMap;
32
+ import org.w3c.dom.Node;
33
+ import org.w3c.dom.NodeList;
34
+ import org.w3c.dom.ProcessingInstruction;
35
+ import org.w3c.dom.Text;
36
+
37
+ /**
38
+ * DOM and XML accessibility and comfort functions.
39
+ *
40
+ * @author Christian Geuer-Pollmann
41
+ */
42
+ public class XMLUtils {
43
+
44
+ /**
45
+ * Constructor XMLUtils
46
+ *
47
+ */
48
+ private XMLUtils() {
49
+ // we don't allow instantiation
50
+ }
51
+
52
+ /**
53
+ * Method getFullTextChildrenFromElement
54
+ *
55
+ * @param element
56
+ * @return the string of children
57
+ */
58
+ public static String getFullTextChildrenFromElement(Element element) {
59
+ StringBuilder sb = new StringBuilder();
60
+
61
+ Node child = element.getFirstChild();
62
+ while (child != null) {
63
+ if (child.getNodeType() == Node.TEXT_NODE) {
64
+ sb.append(((Text)child).getData());
65
+ }
66
+ child = child.getNextSibling();
67
+ }
68
+
69
+ return sb.toString();
70
+ }
71
+
72
+ /**
73
+ * This method returns the owner document of a particular node.
74
+ * This method is necessary because it <I>always</I> returns a
75
+ * {@link Document}. {@link Node#getOwnerDocument} returns <CODE>null</CODE>
76
+ * if the {@link Node} is a {@link Document}.
77
+ *
78
+ * @param node
79
+ * @return the owner document of the node
80
+ */
81
+ public static Document getOwnerDocument(Node node) {
82
+ if (node.getNodeType() == Node.DOCUMENT_NODE) {
83
+ return (Document) node;
84
+ }
85
+ try {
86
+ return node.getOwnerDocument();
87
+ } catch (NullPointerException npe) {
88
+ throw new NullPointerException(npe.getMessage());
89
+ }
90
+ }
91
+
92
+ /**
93
+ * This method returns the first non-null owner document of the Nodes in this Set.
94
+ * This method is necessary because it <I>always</I> returns a
95
+ * {@link Document}. {@link Node#getOwnerDocument} returns <CODE>null</CODE>
96
+ * if the {@link Node} is a {@link Document}.
97
+ *
98
+ * @param xpathNodeSet
99
+ * @return the owner document
100
+ */
101
+ public static Document getOwnerDocument(Set<Node> xpathNodeSet) {
102
+ NullPointerException npe = null;
103
+ for (Node node : xpathNodeSet) {
104
+ int nodeType = node.getNodeType();
105
+ if (nodeType == Node.DOCUMENT_NODE) {
106
+ return (Document) node;
107
+ }
108
+ try {
109
+ if (nodeType == Node.ATTRIBUTE_NODE) {
110
+ return ((Attr)node).getOwnerElement().getOwnerDocument();
111
+ }
112
+ return node.getOwnerDocument();
113
+ } catch (NullPointerException e) {
114
+ npe = e;
115
+ }
116
+ }
117
+
118
+ throw new NullPointerException(npe.getMessage());
119
+ }
120
+
121
+ /**
122
+ * Method convertNodelistToSet
123
+ *
124
+ * @param xpathNodeSet
125
+ * @return the set with the nodelist
126
+ */
127
+ public static Set<Node> convertNodelistToSet(NodeList xpathNodeSet) {
128
+ if (xpathNodeSet == null) {
129
+ return new HashSet<Node>();
130
+ }
131
+
132
+ int length = xpathNodeSet.getLength();
133
+ Set<Node> set = new HashSet<Node>(length);
134
+
135
+ for (int i = 0; i < length; i++) {
136
+ set.add(xpathNodeSet.item(i));
137
+ }
138
+
139
+ return set;
140
+ }
141
+
142
+ /**
143
+ * This method spreads all namespace attributes in a DOM document to their
144
+ * children. This is needed because the XML Signature XPath transform
145
+ * must evaluate the XPath against all nodes in the input, even against
146
+ * XPath namespace nodes. Through a bug in XalanJ2, the namespace nodes are
147
+ * not fully visible in the Xalan XPath model, so we have to do this by
148
+ * hand in DOM spaces so that the nodes become visible in XPath space.
149
+ *
150
+ * @param doc
151
+ * @see <A HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2650">
152
+ * Namespace axis resolution is not XPath compliant </A>
153
+ */
154
+ public static void circumventBug2650(Document doc) {
155
+
156
+ Element documentElement = doc.getDocumentElement();
157
+
158
+ // if the document element has no xmlns definition, we add xmlns=""
159
+ Attr xmlnsAttr =
160
+ documentElement.getAttributeNodeNS(Constants.NamespaceSpecNS, "xmlns");
161
+
162
+ if (xmlnsAttr == null) {
163
+ documentElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "");
164
+ }
165
+
166
+ XMLUtils.circumventBug2650internal(doc);
167
+ }
168
+
169
+ /**
170
+ * This is the work horse for {@link #circumventBug2650}.
171
+ *
172
+ * @param node
173
+ * @see <A HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2650">
174
+ * Namespace axis resolution is not XPath compliant </A>
175
+ */
176
+ @SuppressWarnings("fallthrough")
177
+ private static void circumventBug2650internal(Node node) {
178
+ Node parent = null;
179
+ Node sibling = null;
180
+ final String namespaceNs = Constants.NamespaceSpecNS;
181
+ do {
182
+ switch (node.getNodeType()) {
183
+ case Node.ELEMENT_NODE :
184
+ Element element = (Element) node;
185
+ if (!element.hasChildNodes()) {
186
+ break;
187
+ }
188
+ if (element.hasAttributes()) {
189
+ NamedNodeMap attributes = element.getAttributes();
190
+ int attributesLength = attributes.getLength();
191
+
192
+ for (Node child = element.getFirstChild(); child!=null;
193
+ child = child.getNextSibling()) {
194
+
195
+ if (child.getNodeType() != Node.ELEMENT_NODE) {
196
+ continue;
197
+ }
198
+ Element childElement = (Element) child;
199
+
200
+ for (int i = 0; i < attributesLength; i++) {
201
+ Attr currentAttr = (Attr) attributes.item(i);
202
+ if (!namespaceNs.equals(currentAttr.getNamespaceURI())) {
203
+ continue;
204
+ }
205
+ if (childElement.hasAttributeNS(namespaceNs,
206
+ currentAttr.getLocalName())) {
207
+ continue;
208
+ }
209
+ childElement.setAttributeNS(namespaceNs,
210
+ currentAttr.getName(),
211
+ currentAttr.getNodeValue());
212
+ }
213
+ }
214
+ }
215
+ case Node.ENTITY_REFERENCE_NODE :
216
+ case Node.DOCUMENT_NODE :
217
+ parent = node;
218
+ sibling = node.getFirstChild();
219
+ break;
220
+ }
221
+ while ((sibling == null) && (parent != null)) {
222
+ sibling = parent.getNextSibling();
223
+ parent = parent.getParentNode();
224
+ }
225
+ if (sibling == null) {
226
+ return;
227
+ }
228
+
229
+ node = sibling;
230
+ sibling = node.getNextSibling();
231
+ } while (true);
232
+ }
233
+
234
+ /**
235
+ * @param sibling
236
+ * @param uri
237
+ * @param nodeName
238
+ * @param number
239
+ * @return nodes with the constrain
240
+ */
241
+ public static Text selectNodeText(Node sibling, String uri, String nodeName, int number) {
242
+ Node n = selectNode(sibling,uri,nodeName,number);
243
+ if (n == null) {
244
+ return null;
245
+ }
246
+ n = n.getFirstChild();
247
+ while (n != null && n.getNodeType() != Node.TEXT_NODE) {
248
+ n = n.getNextSibling();
249
+ }
250
+ return (Text)n;
251
+ }
252
+
253
+ /**
254
+ * @param sibling
255
+ * @param uri
256
+ * @param nodeName
257
+ * @param number
258
+ * @return nodes with the constrain
259
+ */
260
+ public static Element selectNode(Node sibling, String uri, String nodeName, int number) {
261
+ while (sibling != null) {
262
+ if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri)
263
+ && sibling.getLocalName().equals(nodeName)) {
264
+ if (number == 0){
265
+ return (Element)sibling;
266
+ }
267
+ number--;
268
+ }
269
+ sibling = sibling.getNextSibling();
270
+ }
271
+ return null;
272
+ }
273
+
274
+ /**
275
+ * @param sibling
276
+ * @param uri
277
+ * @param nodeName
278
+ * @return nodes with the constraint
279
+ */
280
+ public static Element[] selectNodes(Node sibling, String uri, String nodeName) {
281
+ List<Element> list = new ArrayList<Element>();
282
+ while (sibling != null) {
283
+ if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri)
284
+ && sibling.getLocalName().equals(nodeName)) {
285
+ list.add((Element)sibling);
286
+ }
287
+ sibling = sibling.getNextSibling();
288
+ }
289
+ return list.toArray(new Element[list.size()]);
290
+ }
291
+
292
+ /**
293
+ * @param signatureElement
294
+ * @param inputSet
295
+ * @return nodes with the constrain
296
+ */
297
+ public static Set<Node> excludeNodeFromSet(Node signatureElement, Set<Node> inputSet) {
298
+ Set<Node> resultSet = new HashSet<Node>();
299
+ Iterator<Node> iterator = inputSet.iterator();
300
+
301
+ while (iterator.hasNext()) {
302
+ Node inputNode = iterator.next();
303
+
304
+ if (!XMLUtils.isDescendantOrSelf(signatureElement, inputNode)) {
305
+ resultSet.add(inputNode);
306
+ }
307
+ }
308
+ return resultSet;
309
+ }
310
+
311
+ /**
312
+ * Method getStrFromNode
313
+ *
314
+ * @param xpathnode
315
+ * @return the string for the node.
316
+ */
317
+ public static String getStrFromNode(Node xpathnode) {
318
+ if (xpathnode.getNodeType() == Node.TEXT_NODE) {
319
+ // we iterate over all siblings of the context node because eventually,
320
+ // the text is "polluted" with pi's or comments
321
+ StringBuilder sb = new StringBuilder();
322
+
323
+ for (Node currentSibling = xpathnode.getParentNode().getFirstChild();
324
+ currentSibling != null;
325
+ currentSibling = currentSibling.getNextSibling()) {
326
+ if (currentSibling.getNodeType() == Node.TEXT_NODE) {
327
+ sb.append(((Text) currentSibling).getData());
328
+ }
329
+ }
330
+
331
+ return sb.toString();
332
+ } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) {
333
+ return ((Attr) xpathnode).getNodeValue();
334
+ } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
335
+ return ((ProcessingInstruction) xpathnode).getNodeValue();
336
+ }
337
+
338
+ return null;
339
+ }
340
+
341
+ /**
342
+ * Returns true if the descendantOrSelf is on the descendant-or-self axis
343
+ * of the context node.
344
+ *
345
+ * @param ctx
346
+ * @param descendantOrSelf
347
+ * @return true if the node is descendant
348
+ */
349
+ public static boolean isDescendantOrSelf(Node ctx, Node descendantOrSelf) {
350
+ if (ctx == descendantOrSelf) {
351
+ return true;
352
+ }
353
+
354
+ Node parent = descendantOrSelf;
355
+
356
+ while (true) {
357
+ if (parent == null) {
358
+ return false;
359
+ }
360
+
361
+ if (parent == ctx) {
362
+ return true;
363
+ }
364
+
365
+ if (parent.getNodeType() == Node.ATTRIBUTE_NODE) {
366
+ parent = ((Attr) parent).getOwnerElement();
367
+ } else {
368
+ parent = parent.getParentNode();
369
+ }
370
+ }
371
+ }
372
+
373
+ /**
374
+ * Returns the attribute value for the attribute with the specified name.
375
+ * Returns null if there is no such attribute, or
376
+ * the empty string if the attribute value is empty.
377
+ *
378
+ * <p>This works around a limitation of the DOM
379
+ * <code>Element.getAttributeNode</code> method, which does not distinguish
380
+ * between an unspecified attribute and an attribute with a value of
381
+ * "" (it returns "" for both cases).
382
+ *
383
+ * @param elem the element containing the attribute
384
+ * @param name the name of the attribute
385
+ * @return the attribute value (may be null if unspecified)
386
+ */
387
+ public static String getAttributeValue(Element elem, String name) {
388
+ Attr attr = elem.getAttributeNodeNS(null, name);
389
+ return (attr == null) ? null : attr.getValue();
390
+ }
391
+
392
+ /**
393
+ * This method is a tree-search to help prevent against wrapping attacks. It checks that no
394
+ * two Elements have ID Attributes that match the "value" argument, if this is the case then
395
+ * "false" is returned. Note that a return value of "true" does not necessarily mean that
396
+ * a matching Element has been found, just that no wrapping attack has been detected.
397
+ */
398
+ public static boolean protectAgainstWrappingAttack(Node startNode, String value) {
399
+ Node startParent = startNode.getParentNode();
400
+ Node processedNode = null;
401
+ Element foundElement = null;
402
+
403
+ String id = value.trim();
404
+ if (id.charAt(0) == '#') {
405
+ id = id.substring(1);
406
+ }
407
+
408
+ while (startNode != null) {
409
+ if (startNode.getNodeType() == Node.ELEMENT_NODE) {
410
+ Element se = (Element) startNode;
411
+
412
+ NamedNodeMap attributes = se.getAttributes();
413
+ if (attributes != null) {
414
+ for (int i = 0; i < attributes.getLength(); i++) {
415
+ Attr attr = (Attr)attributes.item(i);
416
+ if (attr.isId() && id.equals(attr.getValue())) {
417
+ if (foundElement == null) {
418
+ // Continue searching to find duplicates
419
+ foundElement = attr.getOwnerElement();
420
+ } else {
421
+ //log.debug("Multiple elements with the same 'Id' attribute value!");
422
+ return false;
423
+ }
424
+ }
425
+ }
426
+ }
427
+ }
428
+
429
+ processedNode = startNode;
430
+ startNode = startNode.getFirstChild();
431
+
432
+ // no child, this node is done.
433
+ if (startNode == null) {
434
+ // close node processing, get sibling
435
+ startNode = processedNode.getNextSibling();
436
+ }
437
+
438
+ // no more siblings, get parent, all children
439
+ // of parent are processed.
440
+ while (startNode == null) {
441
+ processedNode = processedNode.getParentNode();
442
+ if (processedNode == startParent) {
443
+ return true;
444
+ }
445
+ // close parent node processing (processed node now)
446
+ startNode = processedNode.getNextSibling();
447
+ }
448
+ }
449
+ return true;
450
+ }
451
+
452
+ /**
453
+ * This method is a tree-search to help prevent against wrapping attacks. It checks that no other
454
+ * Element than the given "knownElement" argument has an ID attribute that matches the "value"
455
+ * argument, which is the ID value of "knownElement". If this is the case then "false" is returned.
456
+ */
457
+ public static boolean protectAgainstWrappingAttack(
458
+ Node startNode, Element knownElement, String value
459
+ ) {
460
+ Node startParent = startNode.getParentNode();
461
+ Node processedNode = null;
462
+
463
+ String id = value.trim();
464
+ if (id.charAt(0) == '#') {
465
+ id = id.substring(1);
466
+ }
467
+
468
+ while (startNode != null) {
469
+ if (startNode.getNodeType() == Node.ELEMENT_NODE) {
470
+ Element se = (Element) startNode;
471
+
472
+ NamedNodeMap attributes = se.getAttributes();
473
+ if (attributes != null) {
474
+ for (int i = 0; i < attributes.getLength(); i++) {
475
+ Attr attr = (Attr)attributes.item(i);
476
+ if (attr.isId() && id.equals(attr.getValue()) && se != knownElement) {
477
+ //log.debug("Multiple elements with the same 'Id' attribute value!");
478
+ return false;
479
+ }
480
+ }
481
+ }
482
+ }
483
+
484
+ processedNode = startNode;
485
+ startNode = startNode.getFirstChild();
486
+
487
+ // no child, this node is done.
488
+ if (startNode == null) {
489
+ // close node processing, get sibling
490
+ startNode = processedNode.getNextSibling();
491
+ }
492
+
493
+ // no more siblings, get parent, all children
494
+ // of parent are processed.
495
+ while (startNode == null) {
496
+ processedNode = processedNode.getParentNode();
497
+ if (processedNode == startParent) {
498
+ return true;
499
+ }
500
+ // close parent node processing (processed node now)
501
+ startNode = processedNode.getNextSibling();
502
+ }
503
+ }
504
+ return true;
505
+ }
506
+
507
+ }