nokogiri 1.11.1-java → 1.11.6-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 (143) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE-DEPENDENCIES.md +12 -12
  3. data/LICENSE.md +1 -1
  4. data/README.md +21 -16
  5. data/dependencies.yml +12 -12
  6. data/ext/java/nokogiri/EncodingHandler.java +76 -89
  7. data/ext/java/nokogiri/HtmlDocument.java +135 -144
  8. data/ext/java/nokogiri/HtmlElementDescription.java +102 -117
  9. data/ext/java/nokogiri/HtmlEntityLookup.java +33 -60
  10. data/ext/java/nokogiri/HtmlSaxParserContext.java +218 -222
  11. data/ext/java/nokogiri/HtmlSaxPushParser.java +162 -169
  12. data/ext/java/nokogiri/NokogiriService.java +595 -556
  13. data/ext/java/nokogiri/XmlAttr.java +118 -126
  14. data/ext/java/nokogiri/XmlAttributeDecl.java +95 -106
  15. data/ext/java/nokogiri/XmlCdata.java +35 -58
  16. data/ext/java/nokogiri/XmlComment.java +46 -67
  17. data/ext/java/nokogiri/XmlDocument.java +645 -572
  18. data/ext/java/nokogiri/XmlDocumentFragment.java +125 -137
  19. data/ext/java/nokogiri/XmlDtd.java +448 -414
  20. data/ext/java/nokogiri/XmlElement.java +23 -48
  21. data/ext/java/nokogiri/XmlElementContent.java +343 -316
  22. data/ext/java/nokogiri/XmlElementDecl.java +124 -125
  23. data/ext/java/nokogiri/XmlEntityDecl.java +119 -127
  24. data/ext/java/nokogiri/XmlEntityReference.java +49 -72
  25. data/ext/java/nokogiri/XmlNamespace.java +175 -175
  26. data/ext/java/nokogiri/XmlNode.java +1843 -1620
  27. data/ext/java/nokogiri/XmlNodeSet.java +361 -331
  28. data/ext/java/nokogiri/XmlProcessingInstruction.java +47 -69
  29. data/ext/java/nokogiri/XmlReader.java +513 -450
  30. data/ext/java/nokogiri/XmlRelaxng.java +85 -104
  31. data/ext/java/nokogiri/XmlSaxParserContext.java +328 -315
  32. data/ext/java/nokogiri/XmlSaxPushParser.java +227 -220
  33. data/ext/java/nokogiri/XmlSchema.java +328 -295
  34. data/ext/java/nokogiri/XmlSyntaxError.java +113 -115
  35. data/ext/java/nokogiri/XmlText.java +55 -76
  36. data/ext/java/nokogiri/XmlXpathContext.java +240 -238
  37. data/ext/java/nokogiri/XsltStylesheet.java +280 -269
  38. data/ext/java/nokogiri/internals/ClosedStreamException.java +5 -2
  39. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +201 -202
  40. data/ext/java/nokogiri/internals/IgnoreSchemaErrorsErrorHandler.java +17 -10
  41. data/ext/java/nokogiri/internals/NokogiriBlockingQueueInputStream.java +43 -16
  42. data/ext/java/nokogiri/internals/NokogiriDomParser.java +63 -80
  43. data/ext/java/nokogiri/internals/NokogiriEntityResolver.java +107 -88
  44. data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +27 -52
  45. data/ext/java/nokogiri/internals/NokogiriHandler.java +316 -286
  46. data/ext/java/nokogiri/internals/NokogiriHelpers.java +736 -652
  47. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +184 -173
  48. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +81 -98
  49. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +64 -79
  50. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +84 -99
  51. data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +48 -65
  52. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +116 -131
  53. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +34 -56
  54. data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +23 -46
  55. data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +55 -72
  56. data/ext/java/nokogiri/internals/ParserContext.java +206 -211
  57. data/ext/java/nokogiri/internals/ReaderNode.java +478 -403
  58. data/ext/java/nokogiri/internals/SaveContextVisitor.java +822 -739
  59. data/ext/java/nokogiri/internals/SchemaErrorHandler.java +31 -54
  60. data/ext/java/nokogiri/internals/XalanDTMManagerPatch.java +129 -123
  61. data/ext/java/nokogiri/internals/XmlDeclHandler.java +3 -34
  62. data/ext/java/nokogiri/internals/XmlDomParserContext.java +206 -207
  63. data/ext/java/nokogiri/internals/XmlSaxParser.java +22 -47
  64. data/ext/java/nokogiri/internals/c14n/AttrCompare.java +71 -68
  65. data/ext/java/nokogiri/internals/c14n/C14nHelper.java +137 -118
  66. data/ext/java/nokogiri/internals/c14n/CanonicalFilter.java +27 -21
  67. data/ext/java/nokogiri/internals/c14n/CanonicalizationException.java +74 -61
  68. data/ext/java/nokogiri/internals/c14n/Canonicalizer.java +230 -205
  69. data/ext/java/nokogiri/internals/c14n/Canonicalizer11.java +572 -547
  70. data/ext/java/nokogiri/internals/c14n/Canonicalizer11_OmitComments.java +17 -10
  71. data/ext/java/nokogiri/internals/c14n/Canonicalizer11_WithComments.java +17 -10
  72. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315.java +323 -302
  73. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315Excl.java +232 -219
  74. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclOmitComments.java +22 -15
  75. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclWithComments.java +23 -16
  76. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315OmitComments.java +23 -16
  77. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315WithComments.java +22 -15
  78. data/ext/java/nokogiri/internals/c14n/CanonicalizerBase.java +575 -545
  79. data/ext/java/nokogiri/internals/c14n/CanonicalizerPhysical.java +141 -120
  80. data/ext/java/nokogiri/internals/c14n/CanonicalizerSpi.java +39 -38
  81. data/ext/java/nokogiri/internals/c14n/Constants.java +13 -10
  82. data/ext/java/nokogiri/internals/c14n/ElementProxy.java +279 -247
  83. data/ext/java/nokogiri/internals/c14n/HelperNodeList.java +66 -53
  84. data/ext/java/nokogiri/internals/c14n/IgnoreAllErrorHandler.java +44 -37
  85. data/ext/java/nokogiri/internals/c14n/InclusiveNamespaces.java +135 -120
  86. data/ext/java/nokogiri/internals/c14n/InvalidCanonicalizerException.java +59 -48
  87. data/ext/java/nokogiri/internals/c14n/NameSpaceSymbTable.java +384 -334
  88. data/ext/java/nokogiri/internals/c14n/NodeFilter.java +25 -24
  89. data/ext/java/nokogiri/internals/c14n/UtfHelpper.java +151 -140
  90. data/ext/java/nokogiri/internals/c14n/XMLUtils.java +456 -423
  91. data/ext/java/nokogiri/internals/dom2dtm/DOM2DTM.java +1466 -1500
  92. data/ext/java/nokogiri/internals/dom2dtm/DOM2DTMdefaultNamespaceDeclarationNode.java +626 -574
  93. data/ext/nokogiri/depend +34 -474
  94. data/ext/nokogiri/extconf.rb +253 -183
  95. data/ext/nokogiri/html_document.c +10 -15
  96. data/ext/nokogiri/html_element_description.c +84 -71
  97. data/ext/nokogiri/html_entity_lookup.c +21 -16
  98. data/ext/nokogiri/html_sax_parser_context.c +66 -65
  99. data/ext/nokogiri/html_sax_push_parser.c +29 -27
  100. data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
  101. data/ext/nokogiri/nokogiri.c +190 -63
  102. data/ext/nokogiri/test_global_handlers.c +3 -4
  103. data/ext/nokogiri/xml_attr.c +15 -15
  104. data/ext/nokogiri/xml_attribute_decl.c +18 -18
  105. data/ext/nokogiri/xml_cdata.c +13 -18
  106. data/ext/nokogiri/xml_comment.c +19 -26
  107. data/ext/nokogiri/xml_document.c +246 -188
  108. data/ext/nokogiri/xml_document_fragment.c +13 -15
  109. data/ext/nokogiri/xml_dtd.c +54 -48
  110. data/ext/nokogiri/xml_element_content.c +30 -27
  111. data/ext/nokogiri/xml_element_decl.c +22 -22
  112. data/ext/nokogiri/xml_encoding_handler.c +17 -11
  113. data/ext/nokogiri/xml_entity_decl.c +32 -30
  114. data/ext/nokogiri/xml_entity_reference.c +16 -18
  115. data/ext/nokogiri/xml_namespace.c +56 -49
  116. data/ext/nokogiri/xml_node.c +385 -326
  117. data/ext/nokogiri/xml_node_set.c +168 -156
  118. data/ext/nokogiri/xml_processing_instruction.c +17 -19
  119. data/ext/nokogiri/xml_reader.c +191 -157
  120. data/ext/nokogiri/xml_relax_ng.c +29 -23
  121. data/ext/nokogiri/xml_sax_parser.c +117 -112
  122. data/ext/nokogiri/xml_sax_parser_context.c +100 -85
  123. data/ext/nokogiri/xml_sax_push_parser.c +34 -27
  124. data/ext/nokogiri/xml_schema.c +48 -42
  125. data/ext/nokogiri/xml_syntax_error.c +21 -23
  126. data/ext/nokogiri/xml_text.c +13 -17
  127. data/ext/nokogiri/xml_xpath_context.c +134 -127
  128. data/ext/nokogiri/xslt_stylesheet.c +157 -157
  129. data/lib/nokogiri.rb +1 -22
  130. data/lib/nokogiri/css/parser.rb +1 -1
  131. data/lib/nokogiri/extension.rb +26 -0
  132. data/lib/nokogiri/html/document_fragment.rb +15 -15
  133. data/lib/nokogiri/nokogiri.jar +0 -0
  134. data/lib/nokogiri/version/constant.rb +1 -1
  135. data/lib/nokogiri/version/info.rb +32 -8
  136. data/lib/nokogiri/xml/document.rb +74 -28
  137. data/lib/nokogiri/xml/node.rb +39 -42
  138. data/lib/nokogiri/xml/reader.rb +2 -9
  139. data/lib/nokogiri/xml/xpath.rb +1 -3
  140. data/lib/nokogiri/xml/xpath/syntax_error.rb +1 -1
  141. metadata +7 -8
  142. data/ext/nokogiri/xml_io.c +0 -63
  143. data/ext/nokogiri/xml_libxml2_hacks.c +0 -112
@@ -1,35 +1,3 @@
1
- /**
2
- * (The MIT License)
3
- *
4
- * Copyright (c) 2008 - 2012:
5
- *
6
- * * {Aaron Patterson}[http://tenderlovemaking.com]
7
- * * {Mike Dalessio}[http://mike.daless.io]
8
- * * {Charles Nutter}[http://blog.headius.com]
9
- * * {Sergio Arbeo}[http://www.serabe.com]
10
- * * {Patrick Mahoney}[http://polycrystal.org]
11
- * * {Yoko Harada}[http://yokolet.blogspot.com]
12
- *
13
- * Permission is hereby granted, free of charge, to any person obtaining
14
- * a copy of this software and associated documentation files (the
15
- * 'Software'), to deal in the Software without restriction, including
16
- * without limitation the rights to use, copy, modify, merge, publish,
17
- * distribute, sublicense, and/or sell copies of the Software, and to
18
- * permit persons to whom the Software is furnished to do so, subject to
19
- * the following conditions:
20
- *
21
- * The above copyright notice and this permission notice shall be
22
- * included in all copies or substantial portions of the Software.
23
- *
24
- * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
25
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
- */
32
-
33
1
  package nokogiri;
34
2
 
35
3
  import static nokogiri.internals.NokogiriHelpers.stringOrNil;
@@ -50,89 +18,119 @@ import org.xml.sax.SAXParseException;
50
18
  * @author sergio
51
19
  * @author Yoko Harada <yokolet@gmail.com>
52
20
  */
53
- @JRubyClass(name="Nokogiri::XML::SyntaxError", parent="Nokogiri::SyntaxError")
54
- public class XmlSyntaxError extends RubyException {
55
-
56
- private Exception exception;
57
- private boolean messageSet; // whether a custom error message was set
58
-
59
- public XmlSyntaxError(Ruby runtime, RubyClass klazz) {
60
- super(runtime, klazz);
61
- }
62
-
63
- public XmlSyntaxError(Ruby runtime, RubyClass rubyClass, Exception ex) {
64
- super(runtime, rubyClass, ex.getMessage());
65
- this.exception = ex;
66
- }
67
-
68
- public XmlSyntaxError(Ruby runtime, RubyClass rubyClass, String message, Exception ex) {
69
- super(runtime, rubyClass, message);
70
- this.exception = ex; this.messageSet = true;
71
- }
72
-
73
- public static XmlSyntaxError createXMLSyntaxError(final Ruby runtime) {
74
- RubyClass klazz = (RubyClass) runtime.getClassFromPath("Nokogiri::XML::SyntaxError");
75
- return new XmlSyntaxError(runtime, klazz);
76
- }
77
-
78
- public static XmlSyntaxError createXMLSyntaxError(final Ruby runtime, final Exception ex) {
79
- RubyClass klazz = (RubyClass) runtime.getClassFromPath("Nokogiri::XML::SyntaxError");
80
- return new XmlSyntaxError(runtime, klazz, ex);
81
- }
82
-
83
- public static XmlSyntaxError createHTMLSyntaxError(final Ruby runtime) {
84
- RubyClass klazz = (RubyClass) runtime.getClassFromPath("Nokogiri::HTML::SyntaxError");
85
- return new XmlSyntaxError(runtime, klazz);
86
- }
87
-
88
- public static RubyException createXMLXPathSyntaxError(final Ruby runtime, final String msg, final Exception ex) {
89
- RubyClass klazz = (RubyClass) runtime.getClassFromPath("Nokogiri::XML::XPath::SyntaxError");
90
- return new XmlSyntaxError(runtime, klazz, msg, ex);
91
- }
92
-
93
- public static XmlSyntaxError createWarning(Ruby runtime, SAXParseException e) {
94
- XmlSyntaxError xmlSyntaxError = createXMLSyntaxError(runtime);
95
- xmlSyntaxError.setException(runtime, e, 1);
96
- return xmlSyntaxError;
97
- }
98
-
99
- public static XmlSyntaxError createError(Ruby runtime, SAXParseException e) {
100
- XmlSyntaxError xmlSyntaxError = createXMLSyntaxError(runtime);
101
- xmlSyntaxError.setException(runtime, e, 2);
102
- return xmlSyntaxError;
103
- }
104
-
105
- public static XmlSyntaxError createFatalError(Ruby runtime, SAXParseException e) {
106
- XmlSyntaxError xmlSyntaxError = createXMLSyntaxError(runtime);
107
- xmlSyntaxError.setException(runtime, e, 3);
108
- return xmlSyntaxError;
109
- }
110
-
111
- public void setException(Exception exception) {
112
- this.exception = exception;
113
- }
114
-
115
- public void setException(Ruby runtime, SAXParseException exception, int level) {
116
- this.exception = exception;
117
- setInstanceVariable("@level", runtime.newFixnum(level));
118
- setInstanceVariable("@line", runtime.newFixnum(exception.getLineNumber()));
119
- setInstanceVariable("@column", runtime.newFixnum(exception.getColumnNumber()));
120
- setInstanceVariable("@file", stringOrNil(runtime, exception.getSystemId()));
121
- }
122
-
123
- @JRubyMethod(name = "to_s")
124
- @Override
125
- public IRubyObject to_s(ThreadContext context) {
126
- RubyString msg = msg(context.runtime);
127
- return msg != null ? msg : super.to_s(context).asString();
128
- }
129
-
130
- private RubyString msg(final Ruby runtime) {
131
- if (exception != null && exception.getMessage() != null) {
132
- if (messageSet) return null;
133
- return runtime.newString( exception.getMessage() );
134
- }
135
- return null;
21
+ @JRubyClass(name = "Nokogiri::XML::SyntaxError", parent = "Nokogiri::SyntaxError")
22
+ public class XmlSyntaxError extends RubyException
23
+ {
24
+
25
+ private Exception exception;
26
+ private boolean messageSet; // whether a custom error message was set
27
+
28
+ public
29
+ XmlSyntaxError(Ruby runtime, RubyClass klazz)
30
+ {
31
+ super(runtime, klazz);
32
+ }
33
+
34
+ public
35
+ XmlSyntaxError(Ruby runtime, RubyClass rubyClass, Exception ex)
36
+ {
37
+ super(runtime, rubyClass, ex.getMessage());
38
+ this.exception = ex;
39
+ }
40
+
41
+ public
42
+ XmlSyntaxError(Ruby runtime, RubyClass rubyClass, String message, Exception ex)
43
+ {
44
+ super(runtime, rubyClass, message);
45
+ this.exception = ex;
46
+ this.messageSet = true;
47
+ }
48
+
49
+ public static XmlSyntaxError
50
+ createXMLSyntaxError(final Ruby runtime)
51
+ {
52
+ RubyClass klazz = (RubyClass) runtime.getClassFromPath("Nokogiri::XML::SyntaxError");
53
+ return new XmlSyntaxError(runtime, klazz);
54
+ }
55
+
56
+ public static XmlSyntaxError
57
+ createXMLSyntaxError(final Ruby runtime, final Exception ex)
58
+ {
59
+ RubyClass klazz = (RubyClass) runtime.getClassFromPath("Nokogiri::XML::SyntaxError");
60
+ return new XmlSyntaxError(runtime, klazz, ex);
61
+ }
62
+
63
+ public static XmlSyntaxError
64
+ createHTMLSyntaxError(final Ruby runtime)
65
+ {
66
+ RubyClass klazz = (RubyClass) runtime.getClassFromPath("Nokogiri::HTML::SyntaxError");
67
+ return new XmlSyntaxError(runtime, klazz);
68
+ }
69
+
70
+ public static RubyException
71
+ createXMLXPathSyntaxError(final Ruby runtime, final String msg, final Exception ex)
72
+ {
73
+ RubyClass klazz = (RubyClass) runtime.getClassFromPath("Nokogiri::XML::XPath::SyntaxError");
74
+ return new XmlSyntaxError(runtime, klazz, msg, ex);
75
+ }
76
+
77
+ public static XmlSyntaxError
78
+ createWarning(Ruby runtime, SAXParseException e)
79
+ {
80
+ XmlSyntaxError xmlSyntaxError = createXMLSyntaxError(runtime);
81
+ xmlSyntaxError.setException(runtime, e, 1);
82
+ return xmlSyntaxError;
83
+ }
84
+
85
+ public static XmlSyntaxError
86
+ createError(Ruby runtime, SAXParseException e)
87
+ {
88
+ XmlSyntaxError xmlSyntaxError = createXMLSyntaxError(runtime);
89
+ xmlSyntaxError.setException(runtime, e, 2);
90
+ return xmlSyntaxError;
91
+ }
92
+
93
+ public static XmlSyntaxError
94
+ createFatalError(Ruby runtime, SAXParseException e)
95
+ {
96
+ XmlSyntaxError xmlSyntaxError = createXMLSyntaxError(runtime);
97
+ xmlSyntaxError.setException(runtime, e, 3);
98
+ return xmlSyntaxError;
99
+ }
100
+
101
+ public void
102
+ setException(Exception exception)
103
+ {
104
+ this.exception = exception;
105
+ }
106
+
107
+ public void
108
+ setException(Ruby runtime, SAXParseException exception, int level)
109
+ {
110
+ this.exception = exception;
111
+ setInstanceVariable("@level", runtime.newFixnum(level));
112
+ setInstanceVariable("@line", runtime.newFixnum(exception.getLineNumber()));
113
+ setInstanceVariable("@column", runtime.newFixnum(exception.getColumnNumber()));
114
+ setInstanceVariable("@file", stringOrNil(runtime, exception.getSystemId()));
115
+ }
116
+
117
+ @JRubyMethod(name = "to_s")
118
+ @Override
119
+ public IRubyObject
120
+ to_s(ThreadContext context)
121
+ {
122
+ RubyString msg = msg(context.runtime);
123
+ return msg != null ? msg : super.to_s(context).asString();
124
+ }
125
+
126
+ private RubyString
127
+ msg(final Ruby runtime)
128
+ {
129
+ if (exception != null && exception.getMessage() != null) {
130
+ if (messageSet) { return null; }
131
+ return runtime.newString(exception.getMessage());
136
132
  }
133
+ return null;
134
+ }
137
135
 
138
136
  }
@@ -1,35 +1,3 @@
1
- /**
2
- * (The MIT License)
3
- *
4
- * Copyright (c) 2008 - 2011:
5
- *
6
- * * {Aaron Patterson}[http://tenderlovemaking.com]
7
- * * {Mike Dalessio}[http://mike.daless.io]
8
- * * {Charles Nutter}[http://blog.headius.com]
9
- * * {Sergio Arbeo}[http://www.serabe.com]
10
- * * {Patrick Mahoney}[http://polycrystal.org]
11
- * * {Yoko Harada}[http://yokolet.blogspot.com]
12
- *
13
- * Permission is hereby granted, free of charge, to any person obtaining
14
- * a copy of this software and associated documentation files (the
15
- * 'Software'), to deal in the Software without restriction, including
16
- * without limitation the rights to use, copy, modify, merge, publish,
17
- * distribute, sublicense, and/or sell copies of the Software, and to
18
- * permit persons to whom the Software is furnished to do so, subject to
19
- * the following conditions:
20
- *
21
- * The above copyright notice and this permission notice shall be
22
- * included in all copies or substantial portions of the Software.
23
- *
24
- * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
25
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
- */
32
-
33
1
  package nokogiri;
34
2
 
35
3
  import static nokogiri.internals.NokogiriHelpers.getCachedNodeOrCreate;
@@ -54,57 +22,68 @@ import org.w3c.dom.Text;
54
22
  * @author sergio
55
23
  * @author Yoko Harada <yokolet@gmail.com>
56
24
  */
57
- @JRubyClass(name="Nokogiri::XML::Text", parent="Nokogiri::XML::CharacterData")
58
- public class XmlText extends XmlNode {
25
+ @JRubyClass(name = "Nokogiri::XML::Text", parent = "Nokogiri::XML::CharacterData")
26
+ public class XmlText extends XmlNode
27
+ {
59
28
 
60
- private static final ByteList TEXT = ByteList.create("text");
61
- static { TEXT.setEncoding(USASCIIEncoding.INSTANCE); }
29
+ private static final ByteList TEXT = ByteList.create("text");
30
+ static { TEXT.setEncoding(USASCIIEncoding.INSTANCE); }
62
31
 
63
- public XmlText(Ruby runtime, RubyClass rubyClass, Node node) {
64
- super(runtime, rubyClass, node);
65
- }
32
+ public
33
+ XmlText(Ruby runtime, RubyClass rubyClass, Node node)
34
+ {
35
+ super(runtime, rubyClass, node);
36
+ }
37
+
38
+ public
39
+ XmlText(Ruby runtime, RubyClass klass)
40
+ {
41
+ super(runtime, klass);
42
+ }
66
43
 
67
- public XmlText(Ruby runtime, RubyClass klass) {
68
- super(runtime, klass);
44
+ @Override
45
+ protected void
46
+ init(ThreadContext context, IRubyObject[] args)
47
+ {
48
+ if (args.length < 2) {
49
+ throw context.runtime.newArgumentError(args.length, 2);
69
50
  }
70
51
 
71
- @Override
72
- protected void init(ThreadContext context, IRubyObject[] args) {
73
- if (args.length < 2) {
74
- throw context.runtime.newArgumentError(args.length, 2);
75
- }
52
+ content = args[0];
53
+ IRubyObject xNode = args[1];
76
54
 
77
- content = args[0];
78
- IRubyObject xNode = args[1];
55
+ Document document = asXmlNode(context, xNode).getOwnerDocument();
56
+ // text node content should not be encoded when it is created by Text node.
57
+ // while content should be encoded when it is created by Element node.
58
+ Node node = document.createTextNode(rubyStringToString(content));
59
+ setNode(context.runtime, node);
60
+ }
79
61
 
80
- Document document = asXmlNode(context, xNode).getOwnerDocument();
81
- // text node content should not be encoded when it is created by Text node.
82
- // while content should be encoded when it is created by Element node.
83
- Node node = document.createTextNode(rubyStringToString(content));
84
- setNode(context.runtime, node);
85
- }
86
-
87
- @Override
88
- protected IRubyObject getNodeName(ThreadContext context) {
89
- if (name == null) name = RubyString.newStringShared(context.runtime, TEXT);
90
- return name;
91
- }
62
+ @Override
63
+ protected IRubyObject
64
+ getNodeName(ThreadContext context)
65
+ {
66
+ if (name == null) { name = RubyString.newStringShared(context.runtime, TEXT); }
67
+ return name;
68
+ }
92
69
 
93
- @Override
94
- public void accept(ThreadContext context, SaveContextVisitor visitor) {
95
- visitor.enter((Text) node);
96
- Node child = node.getFirstChild();
97
- while (child != null) {
98
- IRubyObject nokoNode = getCachedNodeOrCreate(context.runtime, child);
99
- if (nokoNode instanceof XmlNode) {
100
- XmlNode cur = (XmlNode) nokoNode;
101
- cur.accept(context, visitor);
102
- } else if (nokoNode instanceof XmlNamespace) {
103
- XmlNamespace cur = (XmlNamespace) nokoNode;
104
- cur.accept(context, visitor);
105
- }
106
- child = child.getNextSibling();
107
- }
108
- visitor.leave(node);
70
+ @Override
71
+ public void
72
+ accept(ThreadContext context, SaveContextVisitor visitor)
73
+ {
74
+ visitor.enter((Text) node);
75
+ Node child = node.getFirstChild();
76
+ while (child != null) {
77
+ IRubyObject nokoNode = getCachedNodeOrCreate(context.runtime, child);
78
+ if (nokoNode instanceof XmlNode) {
79
+ XmlNode cur = (XmlNode) nokoNode;
80
+ cur.accept(context, visitor);
81
+ } else if (nokoNode instanceof XmlNamespace) {
82
+ XmlNamespace cur = (XmlNamespace) nokoNode;
83
+ cur.accept(context, visitor);
84
+ }
85
+ child = child.getNextSibling();
109
86
  }
87
+ visitor.leave(node);
88
+ }
110
89
  }
@@ -1,35 +1,3 @@
1
- /**
2
- * (The MIT License)
3
- *
4
- * Copyright (c) 2008 - 2014:
5
- *
6
- * * {Aaron Patterson}[http://tenderlovemaking.com]
7
- * * {Mike Dalessio}[http://mike.daless.io]
8
- * * {Charles Nutter}[http://blog.headius.com]
9
- * * {Sergio Arbeo}[http://www.serabe.com]
10
- * * {Patrick Mahoney}[http://polycrystal.org]
11
- * * {Yoko Harada}[http://yokolet.blogspot.com]
12
- *
13
- * Permission is hereby granted, free of charge, to any person obtaining
14
- * a copy of this software and associated documentation files (the
15
- * 'Software'), to deal in the Software without restriction, including
16
- * without limitation the rights to use, copy, modify, merge, publish,
17
- * distribute, sublicense, and/or sell copies of the Software, and to
18
- * permit persons to whom the Software is furnished to do so, subject to
19
- * the following conditions:
20
- *
21
- * The above copyright notice and this permission notice shall be
22
- * included in all copies or substantial portions of the Software.
23
- *
24
- * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
25
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
- */
32
-
33
1
  package nokogiri;
34
2
 
35
3
  import java.util.Set;
@@ -68,234 +36,268 @@ import static nokogiri.internals.NokogiriHelpers.nodeListToRubyArray;
68
36
  * @author Yoko Harada <yokolet@gmail.com>
69
37
  * @author John Shahid <jvshahid@gmail.com>
70
38
  */
71
- @JRubyClass(name="Nokogiri::XML::XPathContext")
72
- public class XmlXpathContext extends RubyObject {
73
- static {
74
- final String DTMManager = "org.apache.xml.dtm.DTMManager";
75
- if (SafePropertyAccessor.getProperty(DTMManager) == null) {
76
- try { // use patched "org.apache.xml.dtm.ref.DTMManagerDefault"
77
- System.setProperty(DTMManager, nokogiri.internals.XalanDTMManagerPatch.class.getName());
78
- }
79
- catch (SecurityException ex) { /* no-op - will work although might be slower */ }
80
- }
39
+ @JRubyClass(name = "Nokogiri::XML::XPathContext")
40
+ public class XmlXpathContext extends RubyObject
41
+ {
42
+ static
43
+ {
44
+ final String DTMManager = "org.apache.xml.dtm.DTMManager";
45
+ if (SafePropertyAccessor.getProperty(DTMManager) == null) {
46
+ try { // use patched "org.apache.xml.dtm.ref.DTMManagerDefault"
47
+ System.setProperty(DTMManager, nokogiri.internals.XalanDTMManagerPatch.class.getName());
48
+ } catch (SecurityException ex) { /* no-op - will work although might be slower */ }
81
49
  }
82
-
83
- /**
84
- * user-data key for (cached) {@link XPathContext}
85
- */
86
- public static final String XPATH_CONTEXT = "CACHED_XPATH_CONTEXT";
87
-
88
- private XmlNode context;
89
-
90
- public XmlXpathContext(Ruby runtime, RubyClass klass) {
91
- super(runtime, klass);
50
+ }
51
+
52
+ /**
53
+ * user-data key for (cached) {@link XPathContext}
54
+ */
55
+ public static final String XPATH_CONTEXT = "CACHED_XPATH_CONTEXT";
56
+
57
+ private XmlNode context;
58
+
59
+ public
60
+ XmlXpathContext(Ruby runtime, RubyClass klass)
61
+ {
62
+ super(runtime, klass);
63
+ }
64
+
65
+ public
66
+ XmlXpathContext(Ruby runtime, RubyClass klass, XmlNode node)
67
+ {
68
+ this(runtime, klass);
69
+ initNode(node);
70
+ }
71
+
72
+ private void
73
+ initNode(XmlNode node)
74
+ {
75
+ context = node;
76
+ }
77
+
78
+ @JRubyMethod(name = "new", meta = true)
79
+ public static IRubyObject
80
+ rbNew(ThreadContext context, IRubyObject klazz, IRubyObject node)
81
+ {
82
+ try {
83
+ return new XmlXpathContext(context.runtime, (RubyClass) klazz, (XmlNode) node);
84
+ } catch (IllegalArgumentException e) {
85
+ throw context.getRuntime().newRuntimeError(e.getMessage());
92
86
  }
87
+ }
88
+
89
+
90
+ // see https://en.wikipedia.org/wiki/QName
91
+ private static final String NameStartCharStr =
92
+ "[_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]"
93
+ ;
94
+ private static final String NameCharStr = "[-\\.0-9\u00B7\u0300-\u036F\u203F-\u2040]|" + NameStartCharStr ;
95
+ private static final String NCNameStr = "(?:" + NameStartCharStr + ")(?:" + NameCharStr + ")*";
96
+ private static final String XPathFunctionCaptureStr = "(" + NCNameStr + "(?=\\())";
97
+ private static final Pattern XPathFunctionCaptureRE = Pattern.compile(XPathFunctionCaptureStr);
98
+
99
+ @JRubyMethod
100
+ public IRubyObject
101
+ evaluate(ThreadContext context, IRubyObject rbQuery, IRubyObject handler)
102
+ {
103
+ String query = rbQuery.convertToString().asJavaString();
104
+
105
+ if (!handler.isNil() && !isContainsPrefix(query)) {
106
+ //
107
+ // The user has passed in a handler, but isn't using the `nokogiri:` prefix as
108
+ // instructed in JRuby land, so let's try to be clever and rewrite the query, inserting
109
+ // the nokogiri namespace where appropriate.
110
+ //
111
+ StringBuilder namespacedQuery = new StringBuilder();
112
+ int jchar = 0;
113
+
114
+ // Find the methods on the handler object
115
+ Set<String> methodNames = handler.getMetaClass().getMethods().keySet();
116
+
117
+ // Find the function calls in the xpath query
118
+ Matcher xpathFunctionCalls = XPathFunctionCaptureRE.matcher(query);
119
+
120
+ while (xpathFunctionCalls.find()) {
121
+ namespacedQuery.append(query.subSequence(jchar, xpathFunctionCalls.start()));
122
+ jchar = xpathFunctionCalls.start();
123
+
124
+ if (methodNames.contains(xpathFunctionCalls.group())) {
125
+ namespacedQuery.append(NokogiriNamespaceContext.NOKOGIRI_PREFIX);
126
+ namespacedQuery.append(":");
127
+ }
93
128
 
94
- public XmlXpathContext(Ruby runtime, RubyClass klass, XmlNode node) {
95
- this(runtime, klass);
96
- initNode(node);
97
- }
129
+ namespacedQuery.append(query.subSequence(xpathFunctionCalls.start(), xpathFunctionCalls.end()));
130
+ jchar = xpathFunctionCalls.end();
131
+ }
98
132
 
99
- private void initNode(XmlNode node) {
100
- context = node;
133
+ if (jchar < query.length() - 1) {
134
+ namespacedQuery.append(query.subSequence(jchar, query.length()));
135
+ }
136
+ query = namespacedQuery.toString();
101
137
  }
102
138
 
103
- @JRubyMethod(name = "new", meta = true)
104
- public static IRubyObject rbNew(ThreadContext context, IRubyObject klazz, IRubyObject node) {
105
- try {
106
- return new XmlXpathContext(context.runtime, (RubyClass) klazz, (XmlNode) node);
107
- }
108
- catch (IllegalArgumentException e) {
109
- throw context.getRuntime().newRuntimeError(e.getMessage());
110
- }
139
+ return node_set(context, query, handler);
140
+ }
141
+
142
+ @JRubyMethod
143
+ public IRubyObject
144
+ evaluate(ThreadContext context, IRubyObject expr)
145
+ {
146
+ return this.evaluate(context, expr, context.getRuntime().getNil());
147
+ }
148
+
149
+ private final NokogiriNamespaceContext nsContext = NokogiriNamespaceContext.create();
150
+
151
+ @JRubyMethod
152
+ public IRubyObject
153
+ register_ns(IRubyObject prefix, IRubyObject uri)
154
+ {
155
+ nsContext.registerNamespace(prefix.asJavaString(), uri.asJavaString());
156
+ return this;
157
+ }
158
+
159
+ private NokogiriXPathVariableResolver variableResolver; // binds (if any)
160
+
161
+ @JRubyMethod
162
+ public IRubyObject
163
+ register_variable(IRubyObject name, IRubyObject value)
164
+ {
165
+ NokogiriXPathVariableResolver variableResolver = this.variableResolver;
166
+ if (variableResolver == null) {
167
+ variableResolver = NokogiriXPathVariableResolver.create();
168
+ this.variableResolver = variableResolver;
111
169
  }
112
-
113
-
114
- // see https://en.wikipedia.org/wiki/QName
115
- private static final String NameStartCharStr = "[_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]" ;
116
- private static final String NameCharStr = "[-\\.0-9\u00B7\u0300-\u036F\u203F-\u2040]|" + NameStartCharStr ;
117
- private static final String NCNameStr = "(?:" + NameStartCharStr + ")(?:" + NameCharStr + ")*";
118
- private static final String XPathFunctionCaptureStr = "(" + NCNameStr + "(?=\\())";
119
- private static final Pattern XPathFunctionCaptureRE = Pattern.compile(XPathFunctionCaptureStr);
120
-
121
- @JRubyMethod
122
- public IRubyObject evaluate(ThreadContext context, IRubyObject rbQuery, IRubyObject handler) {
123
- String query = rbQuery.convertToString().asJavaString();
124
-
125
- if (!handler.isNil() && !isContainsPrefix(query)) {
126
- //
127
- // The user has passed in a handler, but isn't using the `nokogiri:` prefix as
128
- // instructed in JRuby land, so let's try to be clever and rewrite the query, inserting
129
- // the nokogiri namespace where appropriate.
130
- //
131
- StringBuilder namespacedQuery = new StringBuilder();
132
- int jchar = 0;
133
-
134
- // Find the methods on the handler object
135
- Set<String> methodNames = handler.getMetaClass().getMethods().keySet();
136
-
137
- // Find the function calls in the xpath query
138
- Matcher xpathFunctionCalls = XPathFunctionCaptureRE.matcher(query);
139
-
140
- while (xpathFunctionCalls.find()) {
141
- namespacedQuery.append(query.subSequence(jchar, xpathFunctionCalls.start()));
142
- jchar = xpathFunctionCalls.start();
143
-
144
- if (methodNames.contains(xpathFunctionCalls.group())) {
145
- namespacedQuery.append(NokogiriNamespaceContext.NOKOGIRI_PREFIX);
146
- namespacedQuery.append(":");
147
- }
148
-
149
- namespacedQuery.append(query.subSequence(xpathFunctionCalls.start(), xpathFunctionCalls.end()));
150
- jchar = xpathFunctionCalls.end();
151
- }
152
-
153
- if (jchar < query.length()-1) {
154
- namespacedQuery.append(query.subSequence(jchar, query.length()));
155
- }
156
- query = namespacedQuery.toString();
157
- }
158
-
159
- return node_set(context, query, handler);
170
+ variableResolver.registerVariable(name.asJavaString(), value.asJavaString());
171
+ return this;
172
+ }
173
+
174
+ private IRubyObject
175
+ node_set(ThreadContext context, String expr, IRubyObject handler)
176
+ {
177
+ final NokogiriXPathFunctionResolver fnResolver = NokogiriXPathFunctionResolver.create(handler);
178
+ try {
179
+ return tryGetNodeSet(context, expr, fnResolver);
180
+ } catch (TransformerException ex) {
181
+ throw XmlSyntaxError.createXMLXPathSyntaxError(context.runtime,
182
+ (expr + ": " + ex.toString()),
183
+ ex).toThrowable();
160
184
  }
161
-
162
- @JRubyMethod
163
- public IRubyObject evaluate(ThreadContext context, IRubyObject expr) {
164
- return this.evaluate(context, expr, context.getRuntime().getNil());
185
+ }
186
+
187
+ private IRubyObject
188
+ tryGetNodeSet(ThreadContext context, String expr, NokogiriXPathFunctionResolver fnResolver) throws TransformerException
189
+ {
190
+ final Node contextNode = this.context.node;
191
+
192
+ final JAXPPrefixResolver prefixResolver = new JAXPPrefixResolver(nsContext);
193
+ XPath xpathInternal = new XPath(expr, null, prefixResolver, XPath.SELECT);
194
+
195
+ // We always need to have a ContextNode with Xalan XPath implementation
196
+ // To allow simple expression evaluation like 1+1 we are setting
197
+ // dummy Document as Context Node
198
+ final XObject xobj;
199
+ if (contextNode == null) {
200
+ xobj = xpathInternal.execute(getXPathContext(fnResolver), DTM.NULL, prefixResolver);
201
+ } else {
202
+ xobj = xpathInternal.execute(getXPathContext(fnResolver), contextNode, prefixResolver);
165
203
  }
166
204
 
167
- private final NokogiriNamespaceContext nsContext = NokogiriNamespaceContext.create();
168
-
169
- @JRubyMethod
170
- public IRubyObject register_ns(IRubyObject prefix, IRubyObject uri) {
171
- nsContext.registerNamespace(prefix.asJavaString(), uri.asJavaString());
172
- return this;
205
+ switch (xobj.getType()) {
206
+ case XObject.CLASS_BOOLEAN :
207
+ return context.runtime.newBoolean(xobj.bool());
208
+ case XObject.CLASS_NUMBER :
209
+ return context.runtime.newFloat(xobj.num());
210
+ case XObject.CLASS_NODESET :
211
+ IRubyObject[] nodes = nodeListToRubyArray(context.runtime, xobj.nodelist());
212
+ return XmlNodeSet.newNodeSet(context.runtime, nodes, this.context);
213
+ default :
214
+ return context.runtime.newString(xobj.str());
173
215
  }
174
-
175
- private NokogiriXPathVariableResolver variableResolver; // binds (if any)
176
-
177
- @JRubyMethod
178
- public IRubyObject register_variable(IRubyObject name, IRubyObject value) {
179
- NokogiriXPathVariableResolver variableResolver = this.variableResolver;
180
- if (variableResolver == null) {
181
- variableResolver = NokogiriXPathVariableResolver.create();
182
- this.variableResolver = variableResolver;
183
- }
184
- variableResolver.registerVariable(name.asJavaString(), value.asJavaString());
185
- return this;
216
+ }
217
+
218
+ private XPathContext
219
+ getXPathContext(final NokogiriXPathFunctionResolver fnResolver)
220
+ {
221
+ Node doc = context.getNode().getOwnerDocument();
222
+ if (doc == null) { doc = context.getNode(); }
223
+
224
+ XPathContext xpathContext = (XPathContext) doc.getUserData(XPATH_CONTEXT);
225
+
226
+ if (xpathContext == null) {
227
+ xpathContext = newXPathContext(fnResolver);
228
+ if (variableResolver == null) {
229
+ // NOTE: only caching without variables - could be improved by more sophisticated caching
230
+ doc.setUserData(XPATH_CONTEXT, xpathContext, null);
231
+ }
232
+ } else {
233
+ Object owner = xpathContext.getOwnerObject();
234
+ if ((owner == null && fnResolver == null) ||
235
+ (owner instanceof JAXPExtensionsProvider && ((JAXPExtensionsProvider) owner).hasSameResolver(fnResolver))) {
236
+ // can be re-used assuming it has the same variable-stack (for now only cached if no variables)
237
+ if (variableResolver == null) { return xpathContext; }
238
+ }
239
+ xpathContext = newXPathContext(fnResolver); // otherwise we can not use the cached xpath-context
186
240
  }
187
241
 
188
- private IRubyObject node_set(ThreadContext context, String expr, IRubyObject handler) {
189
- final NokogiriXPathFunctionResolver fnResolver = NokogiriXPathFunctionResolver.create(handler);
190
- try {
191
- return tryGetNodeSet(context, expr, fnResolver);
192
- }
193
- catch (TransformerException ex) {
194
- throw XmlSyntaxError.createXMLXPathSyntaxError(context.runtime,
195
- (expr + ": " + ex.toString()),
196
- ex).toThrowable();
197
- }
242
+ if (variableResolver != null) {
243
+ xpathContext.setVarStack(new JAXPVariableStack(variableResolver));
198
244
  }
199
245
 
200
- private IRubyObject tryGetNodeSet(ThreadContext context, String expr, NokogiriXPathFunctionResolver fnResolver) throws TransformerException {
201
- final Node contextNode = this.context.node;
202
-
203
- final JAXPPrefixResolver prefixResolver = new JAXPPrefixResolver(nsContext);
204
- XPath xpathInternal = new XPath(expr, null, prefixResolver, XPath.SELECT);
205
-
206
- // We always need to have a ContextNode with Xalan XPath implementation
207
- // To allow simple expression evaluation like 1+1 we are setting
208
- // dummy Document as Context Node
209
- final XObject xobj;
210
- if ( contextNode == null )
211
- xobj = xpathInternal.execute(getXPathContext(fnResolver), DTM.NULL, prefixResolver);
212
- else
213
- xobj = xpathInternal.execute(getXPathContext(fnResolver), contextNode, prefixResolver);
214
-
215
- switch (xobj.getType()) {
216
- case XObject.CLASS_BOOLEAN : return context.runtime.newBoolean(xobj.bool());
217
- case XObject.CLASS_NUMBER : return context.runtime.newFloat(xobj.num());
218
- case XObject.CLASS_NODESET :
219
- IRubyObject[] nodes = nodeListToRubyArray(context.runtime, xobj.nodelist());
220
- return XmlNodeSet.newNodeSet(context.runtime, nodes, this.context);
221
- default : return context.runtime.newString(xobj.str());
222
- }
246
+ return xpathContext;
247
+ }
248
+
249
+ private static XPathContext
250
+ newXPathContext(final NokogiriXPathFunctionResolver functionResolver)
251
+ {
252
+ if (functionResolver == null) { return new XPathContext(false); }
253
+ return new XPathContext(new JAXPExtensionsProvider(functionResolver), false);
254
+ }
255
+
256
+ private boolean
257
+ isContainsPrefix(final String str)
258
+ {
259
+ final StringBuilder prefix_ = new StringBuilder();
260
+ for (String prefix : nsContext.getAllPrefixes()) {
261
+ prefix_.setLength(0);
262
+ prefix_.ensureCapacity(prefix.length() + 1);
263
+ prefix_.append(prefix).append(':');
264
+ if (str.contains(prefix_)) { // prefix + ':'
265
+ return true;
266
+ }
223
267
  }
268
+ return false;
269
+ }
224
270
 
225
- private XPathContext getXPathContext(final NokogiriXPathFunctionResolver fnResolver) {
226
- Node doc = context.getNode().getOwnerDocument();
227
- if (doc == null) doc = context.getNode();
271
+ private static final class JAXPExtensionsProvider extends org.apache.xpath.jaxp.JAXPExtensionsProvider
272
+ {
228
273
 
229
- XPathContext xpathContext = (XPathContext) doc.getUserData(XPATH_CONTEXT);
230
-
231
- if ( xpathContext == null ) {
232
- xpathContext = newXPathContext(fnResolver);
233
- if ( variableResolver == null ) {
234
- // NOTE: only caching without variables - could be improved by more sophisticated caching
235
- doc.setUserData(XPATH_CONTEXT, xpathContext, null);
236
- }
237
- }
238
- else {
239
- Object owner = xpathContext.getOwnerObject();
240
- if ( ( owner == null && fnResolver == null ) ||
241
- ( owner instanceof JAXPExtensionsProvider && ((JAXPExtensionsProvider) owner).hasSameResolver(fnResolver) ) ) {
242
- // can be re-used assuming it has the same variable-stack (for now only cached if no variables)
243
- if ( variableResolver == null ) return xpathContext;
244
- }
245
- xpathContext = newXPathContext(fnResolver); // otherwise we can not use the cached xpath-context
246
- }
247
-
248
- if ( variableResolver != null ) {
249
- xpathContext.setVarStack(new JAXPVariableStack(variableResolver));
250
- }
274
+ final NokogiriXPathFunctionResolver resolver;
251
275
 
252
- return xpathContext;
276
+ JAXPExtensionsProvider(NokogiriXPathFunctionResolver resolver)
277
+ {
278
+ super(resolver, false);
279
+ this.resolver = resolver;
253
280
  }
254
281
 
255
- private static XPathContext newXPathContext(final NokogiriXPathFunctionResolver functionResolver) {
256
- if ( functionResolver == null ) return new XPathContext(false);
257
- return new XPathContext(new JAXPExtensionsProvider(functionResolver), false);
282
+ //@Override
283
+ //public boolean equals(Object obj) {
284
+ // if (obj instanceof JAXPExtensionsProvider) {
285
+ // return hasSameResolver(((JAXPExtensionsProvider) obj).resolver);
286
+ // }
287
+ // return false;
288
+ //}
289
+
290
+ final boolean
291
+ hasSameResolver(final NokogiriXPathFunctionResolver resolver)
292
+ {
293
+ return resolver == this.resolver || resolver != null && (
294
+ resolver.getHandler() == null ? this.resolver.getHandler() == null : (
295
+ resolver.getHandler() == this.resolver.getHandler()
296
+ // resolver.getHandler().eql( this.resolver.getHandler() )
297
+ )
298
+ );
258
299
  }
259
300
 
260
- private boolean isContainsPrefix(final String str) {
261
- final StringBuilder prefix_ = new StringBuilder();
262
- for ( String prefix : nsContext.getAllPrefixes() ) {
263
- prefix_.setLength(0);
264
- prefix_.ensureCapacity(prefix.length() + 1);
265
- prefix_.append(prefix).append(':');
266
- if ( str.contains(prefix_) ) { // prefix + ':'
267
- return true;
268
- }
269
- }
270
- return false;
271
- }
272
-
273
- private static final class JAXPExtensionsProvider extends org.apache.xpath.jaxp.JAXPExtensionsProvider {
274
-
275
- final NokogiriXPathFunctionResolver resolver;
276
-
277
- JAXPExtensionsProvider(NokogiriXPathFunctionResolver resolver) {
278
- super(resolver, false);
279
- this.resolver = resolver;
280
- }
281
-
282
- //@Override
283
- //public boolean equals(Object obj) {
284
- // if (obj instanceof JAXPExtensionsProvider) {
285
- // return hasSameResolver(((JAXPExtensionsProvider) obj).resolver);
286
- // }
287
- // return false;
288
- //}
289
-
290
- final boolean hasSameResolver(final NokogiriXPathFunctionResolver resolver) {
291
- return resolver == this.resolver || resolver != null && (
292
- resolver.getHandler() == null ? this.resolver.getHandler() == null : (
293
- resolver.getHandler() == this.resolver.getHandler()
294
- // resolver.getHandler().eql( this.resolver.getHandler() )
295
- )
296
- );
297
- }
298
-
299
- }
301
+ }
300
302
 
301
303
  }