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
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * (The MIT License)
3
3
  *
4
- * Copyright (c) 2008 - 2012:
4
+ * Copyright (c) 2008 - 2014:
5
5
  *
6
6
  * * {Aaron Patterson}[http://tenderlovemaking.com]
7
7
  * * {Mike Dalessio}[http://mike.daless.io]
@@ -53,52 +53,65 @@ import org.w3c.dom.Node;
53
53
  */
54
54
  public class NokogiriNamespaceCache {
55
55
 
56
- private List<Long> keys; // order matters.
57
- private Map<Integer, CacheEntry> cache; // pair of the index of a given key and entry
56
+ private List<String[]> keys;
57
+ private Map<String[], CacheEntry> cache; // pair of the index of a given key and entry
58
58
  private XmlNamespace defaultNamespace = null;
59
59
 
60
60
  public NokogiriNamespaceCache() {
61
- keys = new ArrayList<Long>();
62
- cache = new LinkedHashMap<Integer, CacheEntry>();
61
+ keys = new ArrayList<String[]>(); // keys are [prefix, href]
62
+ cache = new LinkedHashMap<String[], CacheEntry>();
63
63
  }
64
-
65
- private Long hashCode(String prefix, String href) {
66
- long prefix_hash = prefix.hashCode();
67
- long href_hash = href.hashCode();
68
- return prefix_hash << 31 | href_hash;
64
+
65
+ public XmlNamespace getDefault() {
66
+ return defaultNamespace;
67
+ }
68
+
69
+ private String[] getKey(String prefix, String href) {
70
+ for (String[] key : keys) {
71
+ if (key[0].equals(prefix) && key[1].equals(href)) return key;
72
+ }
73
+ return null;
69
74
  }
70
75
 
71
76
  public XmlNamespace get(String prefix, String href) {
72
77
  // prefix should not be null.
73
78
  // In case of a default namespace, an empty string should be given to prefix argument.
74
79
  if (prefix == null || href == null) return null;
75
- Long hash = hashCode(prefix, href);
76
- Integer index = keys.indexOf(hash);
77
- if (index != -1) return cache.get(index).namespace;
80
+ String[] key = getKey(prefix, href);
81
+ if (key != null) {
82
+ return cache.get(key).namespace;
83
+ }
78
84
  return null;
79
85
  }
80
-
81
- public XmlNamespace getDefault() {
82
- return defaultNamespace;
83
- }
84
-
85
- public XmlNamespace get(String prefix) {
86
+
87
+ public XmlNamespace get(Node node, String prefix) {
86
88
  if (prefix == null) return defaultNamespace;
87
- long h = prefix.hashCode();
88
- Long hash = h << 31;
89
- Long mask = 0xFF00L;
90
- for (int i=0; i < keys.size(); i++) {
91
- if ((keys.get(i) & mask) == hash) {
92
- return cache.get(i).namespace;
89
+ for (String[] key : keys) {
90
+ if (key[0].equals(prefix) && cache.get(key) != null && cache.get(key).isOwner(node)) {
91
+ return cache.get(key).namespace;
93
92
  }
94
93
  }
95
94
  return null;
96
95
  }
97
-
96
+
97
+ public List<XmlNamespace> get(String prefix) {
98
+ List<XmlNamespace> namespaces = new ArrayList<XmlNamespace>();
99
+ if (prefix == null) {
100
+ namespaces.add(defaultNamespace);
101
+ return namespaces;
102
+ }
103
+ for (String[] key : keys) {
104
+ if (key[0].equals(prefix) && cache.get(key) != null) {
105
+ namespaces.add(cache.get(key).namespace);
106
+ }
107
+ }
108
+ return namespaces;
109
+ }
110
+
98
111
  public List<XmlNamespace> get(Node node) {
99
112
  List<XmlNamespace> namespaces = new ArrayList<XmlNamespace>();
100
- for (int i=0; i < keys.size(); i++) {
101
- CacheEntry entry = cache.get(i);
113
+ for (String[] key : keys) {
114
+ CacheEntry entry = cache.get(key);
102
115
  if (entry.isOwner(node)) {
103
116
  namespaces.add(entry.namespace);
104
117
  }
@@ -111,33 +124,25 @@ public class NokogiriNamespaceCache {
111
124
  // In case of a default namespace, an empty string should be given to prefix argument.
112
125
  String prefixString = namespace.getPrefix();
113
126
  String hrefString = namespace.getHref();
114
- Long hash = hashCode(prefixString, hrefString);
115
- Integer index;
116
- if ((index = keys.indexOf(hash)) != -1) {
117
- return;
118
- } else {
119
- keys.add(hash);
120
- index = keys.size() - 1;
121
- CacheEntry entry = new CacheEntry(namespace, ownerNode);
122
- cache.put(index, entry);
123
- if ("".equals(prefixString)) defaultNamespace = namespace;
124
- }
127
+ if (getKey(prefixString, hrefString) != null) return;
128
+ String[] key = {prefixString, hrefString};
129
+ keys.add(key);
130
+ CacheEntry entry = new CacheEntry(namespace, ownerNode);
131
+ cache.put(key, entry);
132
+ if ("".equals(prefixString)) defaultNamespace = namespace;
125
133
  }
126
134
 
127
135
  public void remove(String prefix, String href) {
128
- if (prefix == null || href == null) return;
129
- Long hash = hashCode(prefix, href);
130
- Integer index = keys.indexOf(hash);
131
- if (index != -1) {
132
- cache.remove(index);
133
- }
134
- keys.remove(index);
136
+ String[] key = getKey(prefix, href);
137
+ if (key == null) return;
138
+ keys.remove(key);
139
+ cache.remove(key);
135
140
  }
136
-
141
+
137
142
  public void clear() {
138
143
  // removes namespace declarations from node
139
- for (int i=0; i < keys.size(); i++) {
140
- CacheEntry entry = cache.get(i);
144
+ for (String[] key : cache.keySet()) {
145
+ CacheEntry entry = cache.get(key);
141
146
  NamedNodeMap attributes = entry.ownerNode.getAttributes();
142
147
  for (int j=0; j<attributes.getLength(); j++) {
143
148
  String name = ((Attr)attributes.item(j)).getName();
@@ -150,10 +155,10 @@ public class NokogiriNamespaceCache {
150
155
  cache.clear();
151
156
  defaultNamespace = null;
152
157
  }
153
-
158
+
154
159
  public void replaceNode(Node oldNode, Node newNode) {
155
- for (int i=0; i < keys.size(); i++) {
156
- CacheEntry entry = cache.get(i);
160
+ for (String[] key : keys) {
161
+ CacheEntry entry = cache.get(key);
157
162
  if (entry.isOwner(oldNode)) {
158
163
  entry.replaceOwner(newNode);
159
164
  }
@@ -163,14 +168,14 @@ public class NokogiriNamespaceCache {
163
168
  private class CacheEntry {
164
169
  private XmlNamespace namespace;
165
170
  private Node ownerNode;
166
-
171
+
167
172
  CacheEntry(XmlNamespace namespace, Node ownerNode) {
168
173
  this.namespace = namespace;
169
174
  this.ownerNode = ownerNode;
170
175
  }
171
176
 
172
177
  public Boolean isOwner(Node n) {
173
- return this.ownerNode.isSameNode(n);
178
+ return ownerNode.isSameNode(n);
174
179
  }
175
180
 
176
181
  public void replaceOwner(Node newNode) {
@@ -63,7 +63,7 @@ public class NokogiriXPathFunctionResolver implements XPathFunctionResolver {
63
63
 
64
64
  private NokogiriXPathFunctionResolver() {}
65
65
 
66
- private void setHandler(IRubyObject handler) {
66
+ public void setHandler(IRubyObject handler) {
67
67
  this.handler = handler;
68
68
  }
69
69
 
@@ -33,7 +33,7 @@
33
33
  package nokogiri.internals;
34
34
 
35
35
  import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
36
- import static org.jruby.javasupport.util.RuntimeHelpers.invoke;
36
+ import static org.jruby.runtime.Helpers.invoke;
37
37
 
38
38
  import java.io.ByteArrayInputStream;
39
39
  import java.io.File;
@@ -46,12 +46,9 @@ import java.nio.charset.UnsupportedCharsetException;
46
46
 
47
47
  import org.jruby.Ruby;
48
48
  import org.jruby.RubyClass;
49
- import org.jruby.RubyFixnum;
50
49
  import org.jruby.RubyIO;
51
50
  import org.jruby.RubyObject;
52
51
  import org.jruby.RubyString;
53
- import org.jruby.RubyStringIO;
54
- import org.jruby.exceptions.RaiseException;
55
52
  import org.jruby.runtime.ThreadContext;
56
53
  import org.jruby.runtime.builtin.IRubyObject;
57
54
  import org.jruby.util.ByteList;
@@ -439,9 +439,13 @@ public class SaveContextVisitor {
439
439
  buffer.append(">");
440
440
  } else if (asXml && noEmpty) {
441
441
  buffer.append(">");
442
+ } else if (asXhtml) {
443
+ if (isEmpty(name)) {
444
+ buffer.append(" />"); // see http://www.w3.org/TR/xhtml1/#C_2
445
+ } else {
446
+ buffer.append(">");
447
+ }
442
448
  } else {
443
- if (asXhtml)
444
- buffer.append(" "); // see http://www.w3.org/TR/xhtml1/#C_2
445
449
  buffer.append("/>");
446
450
  }
447
451
  if (needBreakInOpening(element)) {
@@ -0,0 +1,119 @@
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.Serializable;
22
+ import java.util.Comparator;
23
+
24
+
25
+ import org.w3c.dom.Attr;
26
+
27
+ /**
28
+ * Compares two attributes based on the C14n specification.
29
+ *
30
+ * <UL>
31
+ * <LI>Namespace nodes have a lesser document order position than attribute
32
+ * nodes.
33
+ * <LI> An element's namespace nodes are sorted lexicographically by
34
+ * local name (the default namespace node, if one exists, has no
35
+ * local name and is therefore lexicographically least).
36
+ * <LI> An element's attribute nodes are sorted lexicographically with
37
+ * namespace URI as the primary key and local name as the secondary
38
+ * key (an empty namespace URI is lexicographically least).
39
+ * </UL>
40
+ *
41
+ * @author Christian Geuer-Pollmann
42
+ */
43
+ public class AttrCompare implements Comparator<Attr>, Serializable {
44
+
45
+ private static final long serialVersionUID = -7113259629930576230L;
46
+ private static final int ATTR0_BEFORE_ATTR1 = -1;
47
+ private static final int ATTR1_BEFORE_ATTR0 = 1;
48
+ private static final String XMLNS = Constants.NamespaceSpecNS;
49
+
50
+ /**
51
+ * Compares two attributes based on the C14n specification.
52
+ *
53
+ * <UL>
54
+ * <LI>Namespace nodes have a lesser document order position than
55
+ * attribute nodes.
56
+ * <LI> An element's namespace nodes are sorted lexicographically by
57
+ * local name (the default namespace node, if one exists, has no
58
+ * local name and is therefore lexicographically least).
59
+ * <LI> An element's attribute nodes are sorted lexicographically with
60
+ * namespace URI as the primary key and local name as the secondary
61
+ * key (an empty namespace URI is lexicographically least).
62
+ * </UL>
63
+ *
64
+ * @param attr0
65
+ * @param attr1
66
+ * @return returns a negative integer, zero, or a positive integer as
67
+ * obj0 is less than, equal to, or greater than obj1
68
+ *
69
+ */
70
+ public int compare(Attr attr0, Attr attr1) {
71
+ String namespaceURI0 = attr0.getNamespaceURI();
72
+ String namespaceURI1 = attr1.getNamespaceURI();
73
+
74
+ boolean isNamespaceAttr0 = XMLNS.equals(namespaceURI0);
75
+ boolean isNamespaceAttr1 = XMLNS.equals(namespaceURI1);
76
+
77
+ if (isNamespaceAttr0) {
78
+ if (isNamespaceAttr1) {
79
+ // both are namespaces
80
+ String localname0 = attr0.getLocalName();
81
+ String localname1 = attr1.getLocalName();
82
+
83
+ if ("xmlns".equals(localname0)) {
84
+ localname0 = "";
85
+ }
86
+
87
+ if ("xmlns".equals(localname1)) {
88
+ localname1 = "";
89
+ }
90
+
91
+ return localname0.compareTo(localname1);
92
+ }
93
+ // attr0 is a namespace, attr1 is not
94
+ return ATTR0_BEFORE_ATTR1;
95
+ } else if (isNamespaceAttr1) {
96
+ // attr1 is a namespace, attr0 is not
97
+ return ATTR1_BEFORE_ATTR0;
98
+ }
99
+
100
+ // none is a namespace
101
+ if (namespaceURI0 == null) {
102
+ if (namespaceURI1 == null) {
103
+ String name0 = attr0.getName();
104
+ String name1 = attr1.getName();
105
+ return name0.compareTo(name1);
106
+ }
107
+ return ATTR0_BEFORE_ATTR1;
108
+ } else if (namespaceURI1 == null) {
109
+ return ATTR1_BEFORE_ATTR0;
110
+ }
111
+
112
+ int a = namespaceURI0.compareTo(namespaceURI1);
113
+ if (a != 0) {
114
+ return a;
115
+ }
116
+
117
+ return (attr0.getLocalName()).compareTo(attr1.getLocalName());
118
+ }
119
+ }
@@ -0,0 +1,159 @@
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
+ import org.w3c.dom.Attr;
23
+ import org.w3c.dom.Document;
24
+ import org.w3c.dom.Element;
25
+ import org.w3c.dom.NamedNodeMap;
26
+
27
+ /**
28
+ * Temporary swapped static functions from the normalizer Section
29
+ *
30
+ * @author Christian Geuer-Pollmann
31
+ */
32
+ public class C14nHelper {
33
+
34
+ /**
35
+ * Constructor C14nHelper
36
+ *
37
+ */
38
+ private C14nHelper() {
39
+ // don't allow instantiation
40
+ }
41
+
42
+ /**
43
+ * Method namespaceIsRelative
44
+ *
45
+ * @param namespace
46
+ * @return true if the given namespace is relative.
47
+ */
48
+ public static boolean namespaceIsRelative(Attr namespace) {
49
+ return !namespaceIsAbsolute(namespace);
50
+ }
51
+
52
+ /**
53
+ * Method namespaceIsRelative
54
+ *
55
+ * @param namespaceValue
56
+ * @return true if the given namespace is relative.
57
+ */
58
+ public static boolean namespaceIsRelative(String namespaceValue) {
59
+ return !namespaceIsAbsolute(namespaceValue);
60
+ }
61
+
62
+ /**
63
+ * Method namespaceIsAbsolute
64
+ *
65
+ * @param namespace
66
+ * @return true if the given namespace is absolute.
67
+ */
68
+ public static boolean namespaceIsAbsolute(Attr namespace) {
69
+ return namespaceIsAbsolute(namespace.getValue());
70
+ }
71
+
72
+ /**
73
+ * Method namespaceIsAbsolute
74
+ *
75
+ * @param namespaceValue
76
+ * @return true if the given namespace is absolute.
77
+ */
78
+ public static boolean namespaceIsAbsolute(String namespaceValue) {
79
+ // assume empty namespaces are absolute
80
+ if (namespaceValue.length() == 0) {
81
+ return true;
82
+ }
83
+ return namespaceValue.indexOf(':') > 0;
84
+ }
85
+
86
+ /**
87
+ * This method throws an exception if the Attribute value contains
88
+ * a relative URI.
89
+ *
90
+ * @param attr
91
+ * @throws CanonicalizationException
92
+ */
93
+ public static void assertNotRelativeNS(Attr attr) throws CanonicalizationException {
94
+ if (attr == null) {
95
+ return;
96
+ }
97
+
98
+ String nodeAttrName = attr.getNodeName();
99
+ boolean definesDefaultNS = nodeAttrName.equals("xmlns");
100
+ boolean definesNonDefaultNS = nodeAttrName.startsWith("xmlns:");
101
+
102
+ if ((definesDefaultNS || definesNonDefaultNS) && namespaceIsRelative(attr)) {
103
+ String parentName = attr.getOwnerElement().getTagName();
104
+ String attrValue = attr.getValue();
105
+ Object exArgs[] = { parentName, nodeAttrName, attrValue };
106
+
107
+ throw new CanonicalizationException(
108
+ "c14n.Canonicalizer.RelativeNamespace", exArgs
109
+ );
110
+ }
111
+ }
112
+
113
+ /**
114
+ * This method throws a CanonicalizationException if the supplied Document
115
+ * is not able to be traversed using a TreeWalker.
116
+ *
117
+ * @param document
118
+ * @throws CanonicalizationException
119
+ */
120
+ public static void checkTraversability(Document document)
121
+ throws CanonicalizationException {
122
+ if (!document.isSupported("Traversal", "2.0")) {
123
+ Object exArgs[] = {document.getImplementation().getClass().getName() };
124
+
125
+ throw new CanonicalizationException(
126
+ "c14n.Canonicalizer.TraversalNotSupported", exArgs
127
+ );
128
+ }
129
+ }
130
+
131
+ /**
132
+ * This method throws a CanonicalizationException if the supplied Element
133
+ * contains any relative namespaces.
134
+ *
135
+ * @param ctxNode
136
+ * @throws CanonicalizationException
137
+ * @see C14nHelper#assertNotRelativeNS(Attr)
138
+ */
139
+ public static void checkForRelativeNamespace(Element ctxNode)
140
+ throws CanonicalizationException {
141
+ if (ctxNode != null) {
142
+ NamedNodeMap attributes = ctxNode.getAttributes();
143
+
144
+ for (int i = 0; i < attributes.getLength(); i++) {
145
+ C14nHelper.assertNotRelativeNS((Attr) attributes.item(i));
146
+ }
147
+ } else {
148
+ throw new CanonicalizationException("Called checkForRelativeNamespace() on null");
149
+ }
150
+ }
151
+
152
+ public static String getErrorMessage(String message, Object... exArgs) {
153
+ StringBuffer sb = new StringBuffer(message);
154
+ for (Object arg : exArgs) {
155
+ sb.append(", ").append(arg.toString());
156
+ }
157
+ return new String(sb);
158
+ }
159
+ }