nokogiri 1.11.0.rc3-java → 1.11.4-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 (187) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +3 -0
  3. data/LICENSE-DEPENDENCIES.md +1015 -947
  4. data/LICENSE.md +1 -1
  5. data/README.md +168 -91
  6. data/dependencies.yml +12 -12
  7. data/ext/java/nokogiri/EncodingHandler.java +76 -89
  8. data/ext/java/nokogiri/HtmlDocument.java +135 -144
  9. data/ext/java/nokogiri/HtmlElementDescription.java +102 -117
  10. data/ext/java/nokogiri/HtmlEntityLookup.java +33 -60
  11. data/ext/java/nokogiri/HtmlSaxParserContext.java +218 -222
  12. data/ext/java/nokogiri/HtmlSaxPushParser.java +162 -169
  13. data/ext/java/nokogiri/NokogiriService.java +595 -556
  14. data/ext/java/nokogiri/XmlAttr.java +118 -126
  15. data/ext/java/nokogiri/XmlAttributeDecl.java +95 -106
  16. data/ext/java/nokogiri/XmlCdata.java +35 -58
  17. data/ext/java/nokogiri/XmlComment.java +46 -67
  18. data/ext/java/nokogiri/XmlDocument.java +645 -572
  19. data/ext/java/nokogiri/XmlDocumentFragment.java +125 -137
  20. data/ext/java/nokogiri/XmlDtd.java +448 -414
  21. data/ext/java/nokogiri/XmlElement.java +23 -48
  22. data/ext/java/nokogiri/XmlElementContent.java +343 -316
  23. data/ext/java/nokogiri/XmlElementDecl.java +124 -125
  24. data/ext/java/nokogiri/XmlEntityDecl.java +119 -127
  25. data/ext/java/nokogiri/XmlEntityReference.java +49 -72
  26. data/ext/java/nokogiri/XmlNamespace.java +175 -175
  27. data/ext/java/nokogiri/XmlNode.java +1843 -1622
  28. data/ext/java/nokogiri/XmlNodeSet.java +361 -331
  29. data/ext/java/nokogiri/XmlProcessingInstruction.java +47 -69
  30. data/ext/java/nokogiri/XmlReader.java +513 -450
  31. data/ext/java/nokogiri/XmlRelaxng.java +89 -101
  32. data/ext/java/nokogiri/XmlSaxParserContext.java +328 -310
  33. data/ext/java/nokogiri/XmlSaxPushParser.java +227 -220
  34. data/ext/java/nokogiri/XmlSchema.java +335 -242
  35. data/ext/java/nokogiri/XmlSyntaxError.java +113 -119
  36. data/ext/java/nokogiri/XmlText.java +55 -76
  37. data/ext/java/nokogiri/XmlXpathContext.java +242 -210
  38. data/ext/java/nokogiri/XsltStylesheet.java +280 -269
  39. data/ext/java/nokogiri/internals/ClosedStreamException.java +5 -2
  40. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +201 -190
  41. data/ext/java/nokogiri/internals/IgnoreSchemaErrorsErrorHandler.java +17 -10
  42. data/ext/java/nokogiri/internals/NokogiriBlockingQueueInputStream.java +43 -16
  43. data/ext/java/nokogiri/internals/NokogiriDomParser.java +63 -80
  44. data/ext/java/nokogiri/internals/NokogiriEntityResolver.java +107 -88
  45. data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +27 -52
  46. data/ext/java/nokogiri/internals/NokogiriHandler.java +316 -286
  47. data/ext/java/nokogiri/internals/NokogiriHelpers.java +736 -652
  48. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +184 -173
  49. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +79 -89
  50. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +64 -79
  51. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +84 -99
  52. data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +48 -65
  53. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +119 -78
  54. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +34 -54
  55. data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +23 -46
  56. data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +55 -72
  57. data/ext/java/nokogiri/internals/ParserContext.java +206 -211
  58. data/ext/java/nokogiri/internals/ReaderNode.java +478 -403
  59. data/ext/java/nokogiri/internals/SaveContextVisitor.java +822 -739
  60. data/ext/java/nokogiri/internals/SchemaErrorHandler.java +31 -54
  61. data/ext/java/nokogiri/internals/XalanDTMManagerPatch.java +129 -123
  62. data/ext/java/nokogiri/internals/XmlDeclHandler.java +3 -34
  63. data/ext/java/nokogiri/internals/XmlDomParserContext.java +206 -207
  64. data/ext/java/nokogiri/internals/XmlSaxParser.java +22 -47
  65. data/ext/java/nokogiri/internals/c14n/AttrCompare.java +71 -68
  66. data/ext/java/nokogiri/internals/c14n/C14nHelper.java +137 -118
  67. data/ext/java/nokogiri/internals/c14n/CanonicalFilter.java +27 -21
  68. data/ext/java/nokogiri/internals/c14n/CanonicalizationException.java +74 -61
  69. data/ext/java/nokogiri/internals/c14n/Canonicalizer.java +230 -205
  70. data/ext/java/nokogiri/internals/c14n/Canonicalizer11.java +572 -547
  71. data/ext/java/nokogiri/internals/c14n/Canonicalizer11_OmitComments.java +17 -10
  72. data/ext/java/nokogiri/internals/c14n/Canonicalizer11_WithComments.java +17 -10
  73. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315.java +323 -302
  74. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315Excl.java +232 -219
  75. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclOmitComments.java +22 -15
  76. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclWithComments.java +23 -16
  77. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315OmitComments.java +23 -16
  78. data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315WithComments.java +22 -15
  79. data/ext/java/nokogiri/internals/c14n/CanonicalizerBase.java +575 -545
  80. data/ext/java/nokogiri/internals/c14n/CanonicalizerPhysical.java +141 -120
  81. data/ext/java/nokogiri/internals/c14n/CanonicalizerSpi.java +39 -38
  82. data/ext/java/nokogiri/internals/c14n/Constants.java +13 -10
  83. data/ext/java/nokogiri/internals/c14n/ElementProxy.java +279 -247
  84. data/ext/java/nokogiri/internals/c14n/HelperNodeList.java +66 -53
  85. data/ext/java/nokogiri/internals/c14n/IgnoreAllErrorHandler.java +44 -37
  86. data/ext/java/nokogiri/internals/c14n/InclusiveNamespaces.java +135 -120
  87. data/ext/java/nokogiri/internals/c14n/InvalidCanonicalizerException.java +59 -48
  88. data/ext/java/nokogiri/internals/c14n/NameSpaceSymbTable.java +384 -334
  89. data/ext/java/nokogiri/internals/c14n/NodeFilter.java +25 -24
  90. data/ext/java/nokogiri/internals/c14n/UtfHelpper.java +151 -140
  91. data/ext/java/nokogiri/internals/c14n/XMLUtils.java +456 -423
  92. data/ext/java/nokogiri/internals/dom2dtm/DOM2DTM.java +1466 -1500
  93. data/ext/java/nokogiri/internals/dom2dtm/DOM2DTMdefaultNamespaceDeclarationNode.java +626 -574
  94. data/ext/nokogiri/depend +37 -358
  95. data/ext/nokogiri/extconf.rb +581 -374
  96. data/ext/nokogiri/html_document.c +78 -82
  97. data/ext/nokogiri/html_element_description.c +84 -71
  98. data/ext/nokogiri/html_entity_lookup.c +21 -16
  99. data/ext/nokogiri/html_sax_parser_context.c +69 -66
  100. data/ext/nokogiri/html_sax_push_parser.c +42 -34
  101. data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
  102. data/ext/nokogiri/nokogiri.c +192 -93
  103. data/ext/nokogiri/test_global_handlers.c +40 -0
  104. data/ext/nokogiri/xml_attr.c +15 -15
  105. data/ext/nokogiri/xml_attribute_decl.c +18 -18
  106. data/ext/nokogiri/xml_cdata.c +13 -18
  107. data/ext/nokogiri/xml_comment.c +19 -26
  108. data/ext/nokogiri/xml_document.c +246 -188
  109. data/ext/nokogiri/xml_document_fragment.c +13 -15
  110. data/ext/nokogiri/xml_dtd.c +54 -48
  111. data/ext/nokogiri/xml_element_content.c +30 -27
  112. data/ext/nokogiri/xml_element_decl.c +22 -22
  113. data/ext/nokogiri/xml_encoding_handler.c +17 -11
  114. data/ext/nokogiri/xml_entity_decl.c +32 -30
  115. data/ext/nokogiri/xml_entity_reference.c +16 -18
  116. data/ext/nokogiri/xml_namespace.c +56 -49
  117. data/ext/nokogiri/xml_node.c +371 -320
  118. data/ext/nokogiri/xml_node_set.c +168 -156
  119. data/ext/nokogiri/xml_processing_instruction.c +17 -19
  120. data/ext/nokogiri/xml_reader.c +191 -157
  121. data/ext/nokogiri/xml_relax_ng.c +52 -28
  122. data/ext/nokogiri/xml_sax_parser.c +118 -118
  123. data/ext/nokogiri/xml_sax_parser_context.c +103 -86
  124. data/ext/nokogiri/xml_sax_push_parser.c +36 -27
  125. data/ext/nokogiri/xml_schema.c +95 -47
  126. data/ext/nokogiri/xml_syntax_error.c +42 -21
  127. data/ext/nokogiri/xml_text.c +13 -17
  128. data/ext/nokogiri/xml_xpath_context.c +206 -123
  129. data/ext/nokogiri/xslt_stylesheet.c +158 -161
  130. data/lib/nokogiri.rb +3 -7
  131. data/lib/nokogiri/css/parser.rb +3 -3
  132. data/lib/nokogiri/css/parser.y +2 -2
  133. data/lib/nokogiri/css/xpath_visitor.rb +70 -42
  134. data/lib/nokogiri/extension.rb +26 -0
  135. data/lib/nokogiri/html/document.rb +12 -26
  136. data/lib/nokogiri/html/document_fragment.rb +15 -15
  137. data/lib/nokogiri/nokogiri.jar +0 -0
  138. data/lib/nokogiri/version.rb +2 -149
  139. data/lib/nokogiri/version/constant.rb +5 -0
  140. data/lib/nokogiri/version/info.rb +205 -0
  141. data/lib/nokogiri/xml/document.rb +91 -35
  142. data/lib/nokogiri/xml/document_fragment.rb +4 -6
  143. data/lib/nokogiri/xml/node.rb +89 -69
  144. data/lib/nokogiri/xml/parse_options.rb +6 -0
  145. data/lib/nokogiri/xml/reader.rb +2 -9
  146. data/lib/nokogiri/xml/relax_ng.rb +6 -2
  147. data/lib/nokogiri/xml/schema.rb +12 -4
  148. data/lib/nokogiri/xml/searchable.rb +3 -1
  149. data/lib/nokogiri/xml/xpath.rb +1 -3
  150. data/lib/nokogiri/xml/xpath/syntax_error.rb +1 -1
  151. metadata +86 -177
  152. data/ext/nokogiri/html_document.h +0 -10
  153. data/ext/nokogiri/html_element_description.h +0 -10
  154. data/ext/nokogiri/html_entity_lookup.h +0 -8
  155. data/ext/nokogiri/html_sax_parser_context.h +0 -11
  156. data/ext/nokogiri/html_sax_push_parser.h +0 -9
  157. data/ext/nokogiri/nokogiri.h +0 -134
  158. data/ext/nokogiri/xml_attr.h +0 -9
  159. data/ext/nokogiri/xml_attribute_decl.h +0 -9
  160. data/ext/nokogiri/xml_cdata.h +0 -9
  161. data/ext/nokogiri/xml_comment.h +0 -9
  162. data/ext/nokogiri/xml_document.h +0 -23
  163. data/ext/nokogiri/xml_document_fragment.h +0 -10
  164. data/ext/nokogiri/xml_dtd.h +0 -10
  165. data/ext/nokogiri/xml_element_content.h +0 -10
  166. data/ext/nokogiri/xml_element_decl.h +0 -9
  167. data/ext/nokogiri/xml_encoding_handler.h +0 -8
  168. data/ext/nokogiri/xml_entity_decl.h +0 -10
  169. data/ext/nokogiri/xml_entity_reference.h +0 -9
  170. data/ext/nokogiri/xml_io.c +0 -63
  171. data/ext/nokogiri/xml_io.h +0 -11
  172. data/ext/nokogiri/xml_libxml2_hacks.c +0 -112
  173. data/ext/nokogiri/xml_libxml2_hacks.h +0 -12
  174. data/ext/nokogiri/xml_namespace.h +0 -14
  175. data/ext/nokogiri/xml_node.h +0 -13
  176. data/ext/nokogiri/xml_node_set.h +0 -12
  177. data/ext/nokogiri/xml_processing_instruction.h +0 -9
  178. data/ext/nokogiri/xml_reader.h +0 -10
  179. data/ext/nokogiri/xml_relax_ng.h +0 -9
  180. data/ext/nokogiri/xml_sax_parser.h +0 -39
  181. data/ext/nokogiri/xml_sax_parser_context.h +0 -10
  182. data/ext/nokogiri/xml_sax_push_parser.h +0 -9
  183. data/ext/nokogiri/xml_schema.h +0 -9
  184. data/ext/nokogiri/xml_syntax_error.h +0 -13
  185. data/ext/nokogiri/xml_text.h +0 -9
  186. data/ext/nokogiri/xml_xpath_context.h +0 -10
  187. data/ext/nokogiri/xslt_stylesheet.h +0 -14
@@ -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,93 +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
- // NOTE: special care - due JRuby 1.7.x
124
-
125
- @Override
126
- public IRubyObject to_s(ThreadContext context) { return to_s19(context); }
127
-
128
- @JRubyMethod(name = "to_s")
129
- public RubyString to_s19(ThreadContext context) {
130
- RubyString msg = msg(context.runtime);
131
- return msg != null ? msg : super.to_s(context).asString();
132
- }
133
-
134
- private RubyString msg(final Ruby runtime) {
135
- if (exception != null && exception.getMessage() != null) {
136
- if (messageSet) return null;
137
- return runtime.newString( exception.getMessage() );
138
- }
139
- 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());
140
132
  }
133
+ return null;
134
+ }
141
135
 
142
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,38 +1,8 @@
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;
4
+ import java.util.regex.Matcher;
5
+ import java.util.regex.Pattern;
36
6
 
37
7
  import javax.xml.transform.TransformerException;
38
8
 
@@ -66,206 +36,268 @@ import static nokogiri.internals.NokogiriHelpers.nodeListToRubyArray;
66
36
  * @author Yoko Harada <yokolet@gmail.com>
67
37
  * @author John Shahid <jvshahid@gmail.com>
68
38
  */
69
- @JRubyClass(name="Nokogiri::XML::XPathContext")
70
- public class XmlXpathContext extends RubyObject {
71
-
72
- static {
73
- final String DTMManager = "org.apache.xml.dtm.DTMManager";
74
- if (SafePropertyAccessor.getProperty(DTMManager) == null) {
75
- try { // use patched "org.apache.xml.dtm.ref.DTMManagerDefault"
76
- System.setProperty(DTMManager, nokogiri.internals.XalanDTMManagerPatch.class.getName());
77
- }
78
- catch (SecurityException ex) { /* no-op - will work although might be slower */ }
79
- }
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 */ }
80
49
  }
81
-
82
- /**
83
- * user-data key for (cached) {@link XPathContext}
84
- */
85
- public static final String XPATH_CONTEXT = "CACHED_XPATH_CONTEXT";
86
-
87
- private XmlNode context;
88
-
89
- public XmlXpathContext(Ruby runtime, RubyClass klass) {
90
- 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());
91
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
+ }
92
128
 
93
- public XmlXpathContext(Ruby runtime, RubyClass klass, XmlNode node) {
94
- this(runtime, klass);
95
- initNode(node);
96
- }
129
+ namespacedQuery.append(query.subSequence(xpathFunctionCalls.start(), xpathFunctionCalls.end()));
130
+ jchar = xpathFunctionCalls.end();
131
+ }
97
132
 
98
- private void initNode(XmlNode node) {
99
- context = node;
133
+ if (jchar < query.length() - 1) {
134
+ namespacedQuery.append(query.subSequence(jchar, query.length()));
135
+ }
136
+ query = namespacedQuery.toString();
100
137
  }
101
138
 
102
- @JRubyMethod(name = "new", meta = true)
103
- public static IRubyObject rbNew(ThreadContext context, IRubyObject klazz, IRubyObject node) {
104
- try {
105
- return new XmlXpathContext(context.runtime, (RubyClass) klazz, (XmlNode) node);
106
- }
107
- catch (IllegalArgumentException e) {
108
- throw context.getRuntime().newRuntimeError(e.getMessage());
109
- }
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;
110
169
  }
111
-
112
- @JRubyMethod
113
- public IRubyObject evaluate(ThreadContext context, IRubyObject expr, IRubyObject handler) {
114
-
115
- String src = expr.convertToString().asJavaString();
116
- if (!handler.isNil()) {
117
- if (!isContainsPrefix(src)) {
118
- StringBuilder replacement = new StringBuilder();
119
- Set<String> methodNames = handler.getMetaClass().getMethods().keySet();
120
- final String PREFIX = NokogiriNamespaceContext.NOKOGIRI_PREFIX;
121
- for (String name : methodNames) {
122
- replacement.setLength(0);
123
- replacement.ensureCapacity(PREFIX.length() + 1 + name.length());
124
- replacement.append(PREFIX).append(':').append(name);
125
- src = src.replace(name, replacement); // replace(name, NOKOGIRI_PREFIX + ':' + name)
126
- }
127
- }
128
- }
129
-
130
- return node_set(context, src, 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();
131
184
  }
132
-
133
- @JRubyMethod
134
- public IRubyObject evaluate(ThreadContext context, IRubyObject expr) {
135
- 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);
136
203
  }
137
204
 
138
- private final NokogiriNamespaceContext nsContext = NokogiriNamespaceContext.create();
139
-
140
- @JRubyMethod
141
- public IRubyObject register_ns(IRubyObject prefix, IRubyObject uri) {
142
- nsContext.registerNamespace(prefix.asJavaString(), uri.asJavaString());
143
- 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());
144
215
  }
145
-
146
- private NokogiriXPathVariableResolver variableResolver; // binds (if any)
147
-
148
- @JRubyMethod
149
- public IRubyObject register_variable(IRubyObject name, IRubyObject value) {
150
- NokogiriXPathVariableResolver variableResolver = this.variableResolver;
151
- if (variableResolver == null) {
152
- variableResolver = NokogiriXPathVariableResolver.create();
153
- this.variableResolver = variableResolver;
154
- }
155
- variableResolver.registerVariable(name.asJavaString(), value.asJavaString());
156
- 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
157
240
  }
158
241
 
159
- private IRubyObject node_set(ThreadContext context, String expr, IRubyObject handler) {
160
- final NokogiriXPathFunctionResolver fnResolver =
161
- handler.isNil() ? null : NokogiriXPathFunctionResolver.create(handler);
162
- try {
163
- return tryGetNodeSet(context, expr, fnResolver);
164
- }
165
- catch (TransformerException ex) {
166
- throw new RaiseException(XmlSyntaxError.createXMLXPathSyntaxError(context.runtime, expr, ex)); // Nokogiri::XML::XPath::SyntaxError
167
- }
242
+ if (variableResolver != null) {
243
+ xpathContext.setVarStack(new JAXPVariableStack(variableResolver));
168
244
  }
169
245
 
170
- private IRubyObject tryGetNodeSet(ThreadContext context, String expr, NokogiriXPathFunctionResolver fnResolver) throws TransformerException {
171
- final Node contextNode = this.context.node;
172
-
173
- final JAXPPrefixResolver prefixResolver = new JAXPPrefixResolver(nsContext);
174
- XPath xpathInternal = new XPath(expr, null, prefixResolver, XPath.SELECT);
175
-
176
- // We always need to have a ContextNode with Xalan XPath implementation
177
- // To allow simple expression evaluation like 1+1 we are setting
178
- // dummy Document as Context Node
179
- final XObject xobj;
180
- if ( contextNode == null )
181
- xobj = xpathInternal.execute(getXPathContext(fnResolver), DTM.NULL, prefixResolver);
182
- else
183
- xobj = xpathInternal.execute(getXPathContext(fnResolver), contextNode, prefixResolver);
184
-
185
- switch (xobj.getType()) {
186
- case XObject.CLASS_BOOLEAN : return context.runtime.newBoolean(xobj.bool());
187
- case XObject.CLASS_NUMBER : return context.runtime.newFloat(xobj.num());
188
- case XObject.CLASS_NODESET :
189
- IRubyObject[] nodes = nodeListToRubyArray(context.runtime, xobj.nodelist());
190
- return XmlNodeSet.newNodeSet(context.runtime, nodes, this.context);
191
- default : return context.runtime.newString(xobj.str());
192
- }
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
+ }
193
267
  }
268
+ return false;
269
+ }
194
270
 
195
- private XPathContext getXPathContext(final NokogiriXPathFunctionResolver fnResolver) {
196
- Node doc = context.getNode().getOwnerDocument();
197
- if (doc == null) doc = context.getNode();
271
+ private static final class JAXPExtensionsProvider extends org.apache.xpath.jaxp.JAXPExtensionsProvider
272
+ {
198
273
 
199
- XPathContext xpathContext = (XPathContext) doc.getUserData(XPATH_CONTEXT);
200
-
201
- if ( xpathContext == null ) {
202
- xpathContext = newXPathContext(fnResolver);
203
- if ( variableResolver == null ) {
204
- // NOTE: only caching without variables - could be improved by more sophisticated caching
205
- doc.setUserData(XPATH_CONTEXT, xpathContext, null);
206
- }
207
- }
208
- else {
209
- Object owner = xpathContext.getOwnerObject();
210
- if ( ( owner == null && fnResolver == null ) ||
211
- ( owner instanceof JAXPExtensionsProvider && ((JAXPExtensionsProvider) owner).hasSameResolver(fnResolver) ) ) {
212
- // can be re-used assuming it has the same variable-stack (for now only cached if no variables)
213
- if ( variableResolver == null ) return xpathContext;
214
- }
215
- xpathContext = newXPathContext(fnResolver); // otherwise we can not use the cached xpath-context
216
- }
217
-
218
- if ( variableResolver != null ) {
219
- xpathContext.setVarStack(new JAXPVariableStack(variableResolver));
220
- }
274
+ final NokogiriXPathFunctionResolver resolver;
221
275
 
222
- return xpathContext;
276
+ JAXPExtensionsProvider(NokogiriXPathFunctionResolver resolver)
277
+ {
278
+ super(resolver, false);
279
+ this.resolver = resolver;
223
280
  }
224
281
 
225
- private static XPathContext newXPathContext(final NokogiriXPathFunctionResolver functionResolver) {
226
- if ( functionResolver == null ) return new XPathContext(false);
227
- 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
+ );
228
299
  }
229
300
 
230
- private boolean isContainsPrefix(final String str) {
231
- final StringBuilder prefix_ = new StringBuilder();
232
- for ( String prefix : nsContext.getAllPrefixes() ) {
233
- prefix_.setLength(0);
234
- prefix_.ensureCapacity(prefix.length() + 1);
235
- prefix_.append(prefix).append(':');
236
- if ( str.contains(prefix_) ) { // prefix + ':'
237
- return true;
238
- }
239
- }
240
- return false;
241
- }
242
-
243
- private static final class JAXPExtensionsProvider extends org.apache.xpath.jaxp.JAXPExtensionsProvider {
244
-
245
- final NokogiriXPathFunctionResolver resolver;
246
-
247
- JAXPExtensionsProvider(NokogiriXPathFunctionResolver resolver) {
248
- super(resolver, false);
249
- this.resolver = resolver;
250
- }
251
-
252
- //@Override
253
- //public boolean equals(Object obj) {
254
- // if (obj instanceof JAXPExtensionsProvider) {
255
- // return hasSameResolver(((JAXPExtensionsProvider) obj).resolver);
256
- // }
257
- // return false;
258
- //}
259
-
260
- final boolean hasSameResolver(final NokogiriXPathFunctionResolver resolver) {
261
- return resolver == this.resolver || resolver != null && (
262
- resolver.getHandler() == null ? this.resolver.getHandler() == null : (
263
- resolver.getHandler() == this.resolver.getHandler()
264
- // resolver.getHandler().eql( this.resolver.getHandler() )
265
- )
266
- );
267
- }
268
-
269
- }
301
+ }
270
302
 
271
303
  }