nokogiri-fitzsimmons 1.5.5.3-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (297) hide show
  1. data/.autotest +26 -0
  2. data/.gemtest +0 -0
  3. data/CHANGELOG.ja.rdoc +666 -0
  4. data/CHANGELOG.rdoc +659 -0
  5. data/C_CODING_STYLE.rdoc +33 -0
  6. data/Manifest.txt +295 -0
  7. data/README.ja.rdoc +106 -0
  8. data/README.rdoc +178 -0
  9. data/ROADMAP.md +86 -0
  10. data/Rakefile +194 -0
  11. data/STANDARD_RESPONSES.md +47 -0
  12. data/Y_U_NO_GEMSPEC.md +155 -0
  13. data/bin/nokogiri +63 -0
  14. data/build_all +58 -0
  15. data/ext/java/nokogiri/EncodingHandler.java +124 -0
  16. data/ext/java/nokogiri/HtmlDocument.java +163 -0
  17. data/ext/java/nokogiri/HtmlElementDescription.java +145 -0
  18. data/ext/java/nokogiri/HtmlEntityLookup.java +79 -0
  19. data/ext/java/nokogiri/HtmlSaxParserContext.java +259 -0
  20. data/ext/java/nokogiri/NokogiriService.java +598 -0
  21. data/ext/java/nokogiri/XmlAttr.java +190 -0
  22. data/ext/java/nokogiri/XmlAttributeDecl.java +130 -0
  23. data/ext/java/nokogiri/XmlCdata.java +84 -0
  24. data/ext/java/nokogiri/XmlComment.java +95 -0
  25. data/ext/java/nokogiri/XmlDocument.java +580 -0
  26. data/ext/java/nokogiri/XmlDocumentFragment.java +234 -0
  27. data/ext/java/nokogiri/XmlDtd.java +469 -0
  28. data/ext/java/nokogiri/XmlElement.java +97 -0
  29. data/ext/java/nokogiri/XmlElementContent.java +382 -0
  30. data/ext/java/nokogiri/XmlElementDecl.java +152 -0
  31. data/ext/java/nokogiri/XmlEntityDecl.java +162 -0
  32. data/ext/java/nokogiri/XmlEntityReference.java +97 -0
  33. data/ext/java/nokogiri/XmlNamespace.java +215 -0
  34. data/ext/java/nokogiri/XmlNode.java +1534 -0
  35. data/ext/java/nokogiri/XmlNodeSet.java +270 -0
  36. data/ext/java/nokogiri/XmlProcessingInstruction.java +99 -0
  37. data/ext/java/nokogiri/XmlReader.java +456 -0
  38. data/ext/java/nokogiri/XmlRelaxng.java +144 -0
  39. data/ext/java/nokogiri/XmlSaxParserContext.java +356 -0
  40. data/ext/java/nokogiri/XmlSaxPushParser.java +215 -0
  41. data/ext/java/nokogiri/XmlSchema.java +324 -0
  42. data/ext/java/nokogiri/XmlSyntaxError.java +136 -0
  43. data/ext/java/nokogiri/XmlText.java +119 -0
  44. data/ext/java/nokogiri/XmlXpathContext.java +203 -0
  45. data/ext/java/nokogiri/XsltStylesheet.java +360 -0
  46. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +243 -0
  47. data/ext/java/nokogiri/internals/NokogiriDocumentCache.java +73 -0
  48. data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +86 -0
  49. data/ext/java/nokogiri/internals/NokogiriHandler.java +333 -0
  50. data/ext/java/nokogiri/internals/NokogiriHelpers.java +800 -0
  51. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +163 -0
  52. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +130 -0
  53. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +74 -0
  54. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +121 -0
  55. data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +79 -0
  56. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +141 -0
  57. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +73 -0
  58. data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +67 -0
  59. data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +87 -0
  60. data/ext/java/nokogiri/internals/ParserContext.java +288 -0
  61. data/ext/java/nokogiri/internals/ReaderNode.java +531 -0
  62. data/ext/java/nokogiri/internals/SaveContextVisitor.java +775 -0
  63. data/ext/java/nokogiri/internals/SchemaErrorHandler.java +76 -0
  64. data/ext/java/nokogiri/internals/XmlDeclHandler.java +42 -0
  65. data/ext/java/nokogiri/internals/XmlDomParser.java +105 -0
  66. data/ext/java/nokogiri/internals/XmlDomParserContext.java +266 -0
  67. data/ext/java/nokogiri/internals/XmlSaxParser.java +65 -0
  68. data/ext/java/nokogiri/internals/XsltExtensionFunction.java +72 -0
  69. data/ext/nokogiri/depend +358 -0
  70. data/ext/nokogiri/extconf.rb +142 -0
  71. data/ext/nokogiri/html_document.c +170 -0
  72. data/ext/nokogiri/html_document.h +10 -0
  73. data/ext/nokogiri/html_element_description.c +276 -0
  74. data/ext/nokogiri/html_element_description.h +10 -0
  75. data/ext/nokogiri/html_entity_lookup.c +32 -0
  76. data/ext/nokogiri/html_entity_lookup.h +8 -0
  77. data/ext/nokogiri/html_sax_parser_context.c +116 -0
  78. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  79. data/ext/nokogiri/html_sax_push_parser.c +87 -0
  80. data/ext/nokogiri/html_sax_push_parser.h +9 -0
  81. data/ext/nokogiri/nokogiri.c +133 -0
  82. data/ext/nokogiri/nokogiri.h +160 -0
  83. data/ext/nokogiri/xml_attr.c +94 -0
  84. data/ext/nokogiri/xml_attr.h +9 -0
  85. data/ext/nokogiri/xml_attribute_decl.c +70 -0
  86. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  87. data/ext/nokogiri/xml_cdata.c +56 -0
  88. data/ext/nokogiri/xml_cdata.h +9 -0
  89. data/ext/nokogiri/xml_comment.c +54 -0
  90. data/ext/nokogiri/xml_comment.h +9 -0
  91. data/ext/nokogiri/xml_document.c +576 -0
  92. data/ext/nokogiri/xml_document.h +23 -0
  93. data/ext/nokogiri/xml_document_fragment.c +48 -0
  94. data/ext/nokogiri/xml_document_fragment.h +10 -0
  95. data/ext/nokogiri/xml_dtd.c +202 -0
  96. data/ext/nokogiri/xml_dtd.h +10 -0
  97. data/ext/nokogiri/xml_element_content.c +123 -0
  98. data/ext/nokogiri/xml_element_content.h +10 -0
  99. data/ext/nokogiri/xml_element_decl.c +69 -0
  100. data/ext/nokogiri/xml_element_decl.h +9 -0
  101. data/ext/nokogiri/xml_encoding_handler.c +79 -0
  102. data/ext/nokogiri/xml_encoding_handler.h +8 -0
  103. data/ext/nokogiri/xml_entity_decl.c +110 -0
  104. data/ext/nokogiri/xml_entity_decl.h +10 -0
  105. data/ext/nokogiri/xml_entity_reference.c +52 -0
  106. data/ext/nokogiri/xml_entity_reference.h +9 -0
  107. data/ext/nokogiri/xml_io.c +56 -0
  108. data/ext/nokogiri/xml_io.h +11 -0
  109. data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
  110. data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
  111. data/ext/nokogiri/xml_namespace.c +78 -0
  112. data/ext/nokogiri/xml_namespace.h +13 -0
  113. data/ext/nokogiri/xml_node.c +1480 -0
  114. data/ext/nokogiri/xml_node.h +13 -0
  115. data/ext/nokogiri/xml_node_set.c +467 -0
  116. data/ext/nokogiri/xml_node_set.h +14 -0
  117. data/ext/nokogiri/xml_processing_instruction.c +56 -0
  118. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  119. data/ext/nokogiri/xml_reader.c +684 -0
  120. data/ext/nokogiri/xml_reader.h +10 -0
  121. data/ext/nokogiri/xml_relax_ng.c +161 -0
  122. data/ext/nokogiri/xml_relax_ng.h +9 -0
  123. data/ext/nokogiri/xml_sax_parser.c +293 -0
  124. data/ext/nokogiri/xml_sax_parser.h +39 -0
  125. data/ext/nokogiri/xml_sax_parser_context.c +222 -0
  126. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  127. data/ext/nokogiri/xml_sax_push_parser.c +115 -0
  128. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  129. data/ext/nokogiri/xml_schema.c +205 -0
  130. data/ext/nokogiri/xml_schema.h +9 -0
  131. data/ext/nokogiri/xml_syntax_error.c +58 -0
  132. data/ext/nokogiri/xml_syntax_error.h +13 -0
  133. data/ext/nokogiri/xml_text.c +52 -0
  134. data/ext/nokogiri/xml_text.h +9 -0
  135. data/ext/nokogiri/xml_xpath_context.c +319 -0
  136. data/ext/nokogiri/xml_xpath_context.h +10 -0
  137. data/ext/nokogiri/xslt_stylesheet.c +270 -0
  138. data/ext/nokogiri/xslt_stylesheet.h +14 -0
  139. data/lib/isorelax.jar +0 -0
  140. data/lib/jing.jar +0 -0
  141. data/lib/nekodtd.jar +0 -0
  142. data/lib/nekohtml.jar +0 -0
  143. data/lib/nokogiri.rb +127 -0
  144. data/lib/nokogiri/css.rb +27 -0
  145. data/lib/nokogiri/css/node.rb +102 -0
  146. data/lib/nokogiri/css/parser.rb +720 -0
  147. data/lib/nokogiri/css/parser.y +258 -0
  148. data/lib/nokogiri/css/parser_extras.rb +91 -0
  149. data/lib/nokogiri/css/syntax_error.rb +7 -0
  150. data/lib/nokogiri/css/tokenizer.rb +152 -0
  151. data/lib/nokogiri/css/tokenizer.rex +55 -0
  152. data/lib/nokogiri/css/xpath_visitor.rb +171 -0
  153. data/lib/nokogiri/decorators/slop.rb +35 -0
  154. data/lib/nokogiri/html.rb +37 -0
  155. data/lib/nokogiri/html/builder.rb +35 -0
  156. data/lib/nokogiri/html/document.rb +254 -0
  157. data/lib/nokogiri/html/document_fragment.rb +41 -0
  158. data/lib/nokogiri/html/element_description.rb +23 -0
  159. data/lib/nokogiri/html/element_description_defaults.rb +671 -0
  160. data/lib/nokogiri/html/entity_lookup.rb +13 -0
  161. data/lib/nokogiri/html/sax/parser.rb +52 -0
  162. data/lib/nokogiri/html/sax/parser_context.rb +16 -0
  163. data/lib/nokogiri/html/sax/push_parser.rb +16 -0
  164. data/lib/nokogiri/nokogiri.jar +0 -0
  165. data/lib/nokogiri/syntax_error.rb +4 -0
  166. data/lib/nokogiri/version.rb +88 -0
  167. data/lib/nokogiri/xml.rb +73 -0
  168. data/lib/nokogiri/xml/attr.rb +14 -0
  169. data/lib/nokogiri/xml/attribute_decl.rb +18 -0
  170. data/lib/nokogiri/xml/builder.rb +431 -0
  171. data/lib/nokogiri/xml/cdata.rb +11 -0
  172. data/lib/nokogiri/xml/character_data.rb +7 -0
  173. data/lib/nokogiri/xml/document.rb +267 -0
  174. data/lib/nokogiri/xml/document_fragment.rb +103 -0
  175. data/lib/nokogiri/xml/dtd.rb +22 -0
  176. data/lib/nokogiri/xml/element_content.rb +36 -0
  177. data/lib/nokogiri/xml/element_decl.rb +13 -0
  178. data/lib/nokogiri/xml/entity_decl.rb +19 -0
  179. data/lib/nokogiri/xml/namespace.rb +13 -0
  180. data/lib/nokogiri/xml/node.rb +946 -0
  181. data/lib/nokogiri/xml/node/save_options.rb +61 -0
  182. data/lib/nokogiri/xml/node_set.rb +357 -0
  183. data/lib/nokogiri/xml/notation.rb +6 -0
  184. data/lib/nokogiri/xml/parse_options.rb +98 -0
  185. data/lib/nokogiri/xml/pp.rb +2 -0
  186. data/lib/nokogiri/xml/pp/character_data.rb +18 -0
  187. data/lib/nokogiri/xml/pp/node.rb +56 -0
  188. data/lib/nokogiri/xml/processing_instruction.rb +8 -0
  189. data/lib/nokogiri/xml/reader.rb +112 -0
  190. data/lib/nokogiri/xml/relax_ng.rb +32 -0
  191. data/lib/nokogiri/xml/sax.rb +4 -0
  192. data/lib/nokogiri/xml/sax/document.rb +164 -0
  193. data/lib/nokogiri/xml/sax/parser.rb +115 -0
  194. data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
  195. data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
  196. data/lib/nokogiri/xml/schema.rb +63 -0
  197. data/lib/nokogiri/xml/syntax_error.rb +47 -0
  198. data/lib/nokogiri/xml/text.rb +9 -0
  199. data/lib/nokogiri/xml/xpath.rb +10 -0
  200. data/lib/nokogiri/xml/xpath/syntax_error.rb +11 -0
  201. data/lib/nokogiri/xml/xpath_context.rb +16 -0
  202. data/lib/nokogiri/xslt.rb +56 -0
  203. data/lib/nokogiri/xslt/stylesheet.rb +25 -0
  204. data/lib/xercesImpl.jar +0 -0
  205. data/lib/xsd/xmlparser/nokogiri.rb +90 -0
  206. data/tasks/cross_compile.rb +153 -0
  207. data/tasks/nokogiri.org.rb +24 -0
  208. data/tasks/test.rb +95 -0
  209. data/test/css/test_nthiness.rb +159 -0
  210. data/test/css/test_parser.rb +341 -0
  211. data/test/css/test_tokenizer.rb +198 -0
  212. data/test/css/test_xpath_visitor.rb +91 -0
  213. data/test/decorators/test_slop.rb +16 -0
  214. data/test/files/2ch.html +108 -0
  215. data/test/files/address_book.rlx +12 -0
  216. data/test/files/address_book.xml +10 -0
  217. data/test/files/bar/bar.xsd +4 -0
  218. data/test/files/dont_hurt_em_why.xml +422 -0
  219. data/test/files/encoding.html +82 -0
  220. data/test/files/encoding.xhtml +84 -0
  221. data/test/files/exslt.xml +8 -0
  222. data/test/files/exslt.xslt +35 -0
  223. data/test/files/foo/foo.xsd +4 -0
  224. data/test/files/metacharset.html +10 -0
  225. data/test/files/noencoding.html +47 -0
  226. data/test/files/po.xml +32 -0
  227. data/test/files/po.xsd +66 -0
  228. data/test/files/shift_jis.html +10 -0
  229. data/test/files/shift_jis.xml +5 -0
  230. data/test/files/snuggles.xml +3 -0
  231. data/test/files/staff.dtd +10 -0
  232. data/test/files/staff.xml +59 -0
  233. data/test/files/staff.xslt +32 -0
  234. data/test/files/tlm.html +850 -0
  235. data/test/files/to_be_xincluded.xml +2 -0
  236. data/test/files/valid_bar.xml +2 -0
  237. data/test/files/xinclude.xml +4 -0
  238. data/test/helper.rb +147 -0
  239. data/test/html/sax/test_parser.rb +138 -0
  240. data/test/html/sax/test_parser_context.rb +46 -0
  241. data/test/html/test_builder.rb +164 -0
  242. data/test/html/test_document.rb +529 -0
  243. data/test/html/test_document_encoding.rb +138 -0
  244. data/test/html/test_document_fragment.rb +254 -0
  245. data/test/html/test_element_description.rb +100 -0
  246. data/test/html/test_named_characters.rb +14 -0
  247. data/test/html/test_node.rb +188 -0
  248. data/test/html/test_node_encoding.rb +27 -0
  249. data/test/test_convert_xpath.rb +135 -0
  250. data/test/test_css_cache.rb +45 -0
  251. data/test/test_encoding_handler.rb +46 -0
  252. data/test/test_memory_leak.rb +152 -0
  253. data/test/test_nokogiri.rb +132 -0
  254. data/test/test_reader.rb +488 -0
  255. data/test/test_soap4r_sax.rb +52 -0
  256. data/test/test_xslt_transforms.rb +254 -0
  257. data/test/xml/node/test_save_options.rb +28 -0
  258. data/test/xml/node/test_subclass.rb +44 -0
  259. data/test/xml/sax/test_parser.rb +338 -0
  260. data/test/xml/sax/test_parser_context.rb +106 -0
  261. data/test/xml/sax/test_push_parser.rb +157 -0
  262. data/test/xml/test_attr.rb +64 -0
  263. data/test/xml/test_attribute_decl.rb +86 -0
  264. data/test/xml/test_builder.rb +248 -0
  265. data/test/xml/test_c14n.rb +151 -0
  266. data/test/xml/test_cdata.rb +48 -0
  267. data/test/xml/test_comment.rb +29 -0
  268. data/test/xml/test_document.rb +742 -0
  269. data/test/xml/test_document_encoding.rb +28 -0
  270. data/test/xml/test_document_fragment.rb +216 -0
  271. data/test/xml/test_dtd.rb +103 -0
  272. data/test/xml/test_dtd_encoding.rb +33 -0
  273. data/test/xml/test_element_content.rb +56 -0
  274. data/test/xml/test_element_decl.rb +73 -0
  275. data/test/xml/test_entity_decl.rb +122 -0
  276. data/test/xml/test_entity_reference.rb +235 -0
  277. data/test/xml/test_namespace.rb +75 -0
  278. data/test/xml/test_node.rb +1029 -0
  279. data/test/xml/test_node_attributes.rb +53 -0
  280. data/test/xml/test_node_encoding.rb +107 -0
  281. data/test/xml/test_node_inheritance.rb +32 -0
  282. data/test/xml/test_node_reparenting.rb +374 -0
  283. data/test/xml/test_node_set.rb +755 -0
  284. data/test/xml/test_parse_options.rb +64 -0
  285. data/test/xml/test_processing_instruction.rb +30 -0
  286. data/test/xml/test_reader_encoding.rb +142 -0
  287. data/test/xml/test_relax_ng.rb +60 -0
  288. data/test/xml/test_schema.rb +94 -0
  289. data/test/xml/test_syntax_error.rb +12 -0
  290. data/test/xml/test_text.rb +45 -0
  291. data/test/xml/test_unparented_node.rb +413 -0
  292. data/test/xml/test_xinclude.rb +83 -0
  293. data/test/xml/test_xpath.rb +295 -0
  294. data/test/xslt/test_custom_functions.rb +129 -0
  295. data/test/xslt/test_exception_handling.rb +37 -0
  296. data/test_all +84 -0
  297. metadata +571 -0
@@ -0,0 +1,270 @@
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
+ package nokogiri;
34
+
35
+ import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
36
+ import static nokogiri.internals.NokogiriHelpers.nodeListToRubyArray;
37
+
38
+ import java.util.List;
39
+
40
+ import org.jruby.Ruby;
41
+ import org.jruby.RubyArray;
42
+ import org.jruby.RubyClass;
43
+ import org.jruby.RubyObject;
44
+ import org.jruby.anno.JRubyClass;
45
+ import org.jruby.anno.JRubyMethod;
46
+ import org.jruby.javasupport.util.RuntimeHelpers;
47
+ import org.jruby.runtime.Block;
48
+ import org.jruby.runtime.ThreadContext;
49
+ import org.jruby.runtime.builtin.IRubyObject;
50
+ import org.w3c.dom.Node;
51
+ import org.w3c.dom.NodeList;
52
+
53
+ /**
54
+ * Class for Nokogiri::XML::NodeSet
55
+ *
56
+ * @author sergio
57
+ * @author Yoko Harada <yokolet@gmail.com>
58
+ */
59
+ @JRubyClass(name="Nokogiri::XML::NodeSet")
60
+ public class XmlNodeSet extends RubyObject implements NodeList {
61
+ private List<?> list;
62
+ private RubyArray nodes;
63
+ private IRubyObject doc;
64
+
65
+ public XmlNodeSet(Ruby ruby, RubyClass klazz) {
66
+ super(ruby, klazz);
67
+ }
68
+
69
+ /**
70
+ * Create and return a copy of this object.
71
+ *
72
+ * @return a clone of this object
73
+ */
74
+ @Override
75
+ public Object clone() throws CloneNotSupportedException {
76
+ return super.clone();
77
+ }
78
+
79
+ void setNodes(RubyArray array) {
80
+ this.nodes = array;
81
+
82
+ IRubyObject first = array.first();
83
+ initialize(array.getRuntime(), first);
84
+ }
85
+
86
+ private void setReference(XmlNodeSet reference) {
87
+ this.nodes = null;
88
+ IRubyObject first = reference.nodes.first();
89
+ initialize(reference.getRuntime(), first);
90
+ }
91
+
92
+ public void setNodeList(NodeList nodeList) {
93
+ setNodes(nodeListToRubyArray(getRuntime(), nodeList));
94
+ }
95
+
96
+ public void initialize(Ruby ruby, IRubyObject refNode) {
97
+ if (refNode instanceof XmlNode) {
98
+ XmlNode n = (XmlNode)refNode;
99
+ doc = n.document(ruby.getCurrentContext());
100
+ setInstanceVariable("@document", doc);
101
+ if (doc != null) {
102
+ RuntimeHelpers.invoke(ruby.getCurrentContext(), doc, "decorate", this);
103
+ }
104
+ }
105
+ }
106
+
107
+ public static IRubyObject newEmptyNodeSet(ThreadContext context) {
108
+ return (XmlNodeSet)NokogiriService.XML_NODESET_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::NodeSet"));
109
+ }
110
+
111
+ public long length() {
112
+ if (nodes == null) return 0L;
113
+ return nodes.length().getLongValue();
114
+ }
115
+
116
+ public void relink_namespace(ThreadContext context) {
117
+ List<?> n = nodes.getList();
118
+
119
+ for (int i = 0; i < n.size(); i++) {
120
+ if (n.get(i) instanceof XmlNode) {
121
+ ((XmlNode) n.get(i)).relink_namespace(context);
122
+ }
123
+ }
124
+ }
125
+
126
+ @JRubyMethod(name="&")
127
+ public IRubyObject and(ThreadContext context, IRubyObject nodeSet){
128
+ if (nodes == null) setNodes(RubyArray.newEmptyArray(context.getRuntime()));
129
+ return newXmlNodeSet(context, (RubyArray) nodes.op_and(asXmlNodeSet(context, nodeSet).nodes));
130
+ }
131
+
132
+ @JRubyMethod
133
+ public IRubyObject delete(ThreadContext context, IRubyObject node_or_namespace){
134
+ if (nodes == null) return context.getRuntime().getNil();
135
+ if (node_or_namespace instanceof XmlNamespace) {
136
+ ((XmlNamespace)node_or_namespace).deleteHref();
137
+ }
138
+ return nodes.delete(context, asXmlNodeOrNamespace(context, node_or_namespace), Block.NULL_BLOCK);
139
+ }
140
+
141
+ @JRubyMethod
142
+ public IRubyObject dup(ThreadContext context){
143
+ if (nodes == null) setNodes(RubyArray.newEmptyArray(context.getRuntime()));
144
+ return newXmlNodeSet(context, nodes.aryDup());
145
+ }
146
+
147
+ @JRubyMethod(name = "include?")
148
+ public IRubyObject include_p(ThreadContext context, IRubyObject node_or_namespace){
149
+ if (nodes == null) setNodes(RubyArray.newEmptyArray(context.getRuntime()));
150
+ return nodes.include_p(context, asXmlNodeOrNamespace(context, node_or_namespace));
151
+ }
152
+
153
+ @JRubyMethod(name = {"length", "size"})
154
+ public IRubyObject length(ThreadContext context) {
155
+ if (nodes != null) return nodes.length();
156
+ else return context.getRuntime().newFixnum(0);
157
+ }
158
+
159
+ @JRubyMethod(name="-")
160
+ public IRubyObject op_diff(ThreadContext context, IRubyObject nodeSet){
161
+ XmlNodeSet xmlNodeSet = newXmlNodeSet(context, this);
162
+ if (nodes == null) setNodes(RubyArray.newEmptyArray(context.getRuntime()));
163
+ xmlNodeSet.setNodes((RubyArray) nodes.op_diff(asXmlNodeSet(context, nodeSet).nodes));
164
+ return xmlNodeSet;
165
+ }
166
+
167
+ @JRubyMethod(name={"|", "+"})
168
+ public IRubyObject op_or(ThreadContext context, IRubyObject nodeSet){
169
+ if (nodes == null) setNodes(RubyArray.newEmptyArray(context.getRuntime()));
170
+ return newXmlNodeSet(context, (RubyArray) nodes.op_or(asXmlNodeSet(context, nodeSet).nodes));
171
+ }
172
+
173
+ @JRubyMethod(name = {"push", "<<"})
174
+ public IRubyObject push(ThreadContext context, IRubyObject node_or_namespace) {
175
+ if (nodes == null) setNodes(RubyArray.newEmptyArray(context.getRuntime()));
176
+ nodes.append(asXmlNodeOrNamespace(context, node_or_namespace));
177
+ return this;
178
+ }
179
+
180
+ @JRubyMethod(name={"[]", "slice"})
181
+ public IRubyObject slice(ThreadContext context, IRubyObject indexOrRange){
182
+ IRubyObject result;
183
+ if (nodes == null) return context.getRuntime().getNil();
184
+ if (context.getRuntime().is1_9()) {
185
+ result = nodes.aref19(indexOrRange);
186
+ } else {
187
+ result = nodes.aref(indexOrRange);
188
+ }
189
+ if (result instanceof RubyArray) {
190
+ return newXmlNodeSet(context, (RubyArray)result);
191
+ } else {
192
+ return result;
193
+ }
194
+ }
195
+
196
+ @JRubyMethod(name={"[]", "slice"})
197
+ public IRubyObject slice(ThreadContext context, IRubyObject start, IRubyObject length){
198
+ IRubyObject result;
199
+ if (nodes == null) return context.getRuntime().getNil();
200
+ if (context.getRuntime().is1_9()) {
201
+ result = nodes.aref19(start, length);
202
+ } else {
203
+ result = nodes.aref(start, length);
204
+ }
205
+ if (result instanceof RubyArray) return newXmlNodeSet(context, (RubyArray)result);
206
+ else return context.getRuntime().getNil();
207
+ }
208
+
209
+ @JRubyMethod(name = {"to_a", "to_ary"})
210
+ public IRubyObject to_a(ThreadContext context) {
211
+ return nodes;
212
+ }
213
+
214
+ @JRubyMethod(name = {"unlink", "remove"})
215
+ public IRubyObject unlink(ThreadContext context){
216
+ if (nodes == null) setNodes(RubyArray.newEmptyArray(context.getRuntime()));
217
+ IRubyObject[] arr = this.nodes.toJavaArrayUnsafe();
218
+ long length = arr.length;
219
+ for (int i = 0; i < length; i++) {
220
+ if (arr[i] instanceof XmlNode) {
221
+ ((XmlNode) arr[i] ).unlink(context);
222
+ }
223
+ }
224
+ return this;
225
+ }
226
+
227
+ public static XmlNodeSet newXmlNodeSet(ThreadContext context, RubyArray array) {
228
+ XmlNodeSet xmlNodeSet = (XmlNodeSet)NokogiriService.XML_NODESET_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::NodeSet"));
229
+ xmlNodeSet.setNodes(array);
230
+ return xmlNodeSet;
231
+ }
232
+
233
+ private XmlNodeSet newXmlNodeSet(ThreadContext context, XmlNodeSet reference) {
234
+ XmlNodeSet xmlNodeSet = (XmlNodeSet)NokogiriService.XML_NODESET_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::NodeSet"));
235
+ xmlNodeSet.setReference(reference);
236
+ return xmlNodeSet;
237
+ }
238
+
239
+ private IRubyObject asXmlNodeOrNamespace(ThreadContext context, IRubyObject possibleNode) {
240
+ if (possibleNode instanceof XmlNode || possibleNode instanceof XmlNamespace) {
241
+ return possibleNode;
242
+ } else {
243
+ throw context.getRuntime().newArgumentError("node must be a Nokogiri::XML::Node or Nokogiri::XML::Namespace");
244
+ }
245
+ }
246
+
247
+ private XmlNodeSet asXmlNodeSet(ThreadContext context, IRubyObject possibleNodeSet) {
248
+ // if(!(possibleNodeSet instanceof XmlNodeSet)) {
249
+ if(!RuntimeHelpers.invoke(context, possibleNodeSet, "is_a?",
250
+ getNokogiriClass(context.getRuntime(), "Nokogiri::XML::NodeSet")).isTrue()) {
251
+ throw context.getRuntime().newArgumentError("node must be a Nokogiri::XML::NodeSet");
252
+ }
253
+ XmlNodeSet xmlNodeSet = (XmlNodeSet)possibleNodeSet;
254
+ if (xmlNodeSet.nodes == null) xmlNodeSet.setNodes(RubyArray.newEmptyArray(context.getRuntime()));
255
+ return xmlNodeSet;
256
+ }
257
+
258
+ public int getLength() {
259
+ if (nodes == null) return 0 ;
260
+ return nodes.size();
261
+ }
262
+
263
+ public Node item(int index) {
264
+ if (nodes == null) return null ;
265
+ Object n = nodes.get(index);
266
+ if (n instanceof XmlNode) return ((XmlNode)n).node;
267
+ if (n instanceof XmlNamespace) return ((XmlNamespace)n).getNode();
268
+ return null;
269
+ }
270
+ }
@@ -0,0 +1,99 @@
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
+ package nokogiri;
34
+
35
+ import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
36
+ import nokogiri.internals.SaveContextVisitor;
37
+
38
+ import org.jruby.Ruby;
39
+ import org.jruby.RubyClass;
40
+ import org.jruby.anno.JRubyClass;
41
+ import org.jruby.anno.JRubyMethod;
42
+ import org.jruby.javasupport.util.RuntimeHelpers;
43
+ import org.jruby.runtime.ThreadContext;
44
+ import org.jruby.runtime.builtin.IRubyObject;
45
+ import org.w3c.dom.Document;
46
+ import org.w3c.dom.Node;
47
+ import org.w3c.dom.ProcessingInstruction;
48
+
49
+ /**
50
+ * Class for Nokogiri::XML::ProcessingInstruction
51
+ *
52
+ * @author sergio
53
+ * @author Yoko Harada <yokolet@gmail.com>
54
+ */
55
+ @JRubyClass(name="Nokogiri::XML::ProcessingInstruction", parent="Nokogiri::XML::Node")
56
+ public class XmlProcessingInstruction extends XmlNode {
57
+
58
+ public XmlProcessingInstruction(Ruby ruby, RubyClass klazz) {
59
+ super(ruby, klazz);
60
+ }
61
+
62
+ public XmlProcessingInstruction(Ruby ruby, RubyClass klazz, Node node) {
63
+ super(ruby, klazz, node);
64
+ }
65
+
66
+ @JRubyMethod(name="new", meta=true, rest=true, required=3)
67
+ public static IRubyObject rbNew(ThreadContext context,
68
+ IRubyObject klazz,
69
+ IRubyObject[] args) {
70
+
71
+ IRubyObject doc = args[0];
72
+ IRubyObject target = args[1];
73
+ IRubyObject data = args[2];
74
+
75
+ Document document = ((XmlNode) doc).getOwnerDocument();
76
+ Node node =
77
+ document.createProcessingInstruction(rubyStringToString(target),
78
+ rubyStringToString(data));
79
+ XmlProcessingInstruction self =
80
+ new XmlProcessingInstruction(context.getRuntime(),
81
+ (RubyClass) klazz,
82
+ node);
83
+
84
+ RuntimeHelpers.invoke(context, self, "initialize", args);
85
+
86
+ // TODO: if_block_given.
87
+
88
+ return self;
89
+ }
90
+
91
+ @Override
92
+ public boolean isProcessingInstruction() { return true; }
93
+
94
+ @Override
95
+ public void accept(ThreadContext context, SaveContextVisitor visitor) {
96
+ visitor.enter((ProcessingInstruction)node);
97
+ visitor.leave((ProcessingInstruction)node);
98
+ }
99
+ }
@@ -0,0 +1,456 @@
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
+ package nokogiri;
34
+
35
+ import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
36
+ import static nokogiri.internals.NokogiriHelpers.stringOrBlank;
37
+
38
+ import java.io.ByteArrayInputStream;
39
+ import java.io.IOException;
40
+ import java.util.ArrayDeque;
41
+ import java.util.Stack;
42
+
43
+ import nokogiri.internals.NokogiriEntityResolver;
44
+ import nokogiri.internals.ParserContext;
45
+ import nokogiri.internals.ParserContext.Options;
46
+ import nokogiri.internals.ReaderNode;
47
+ import nokogiri.internals.ReaderNode.ElementNode;
48
+
49
+ import org.jruby.Ruby;
50
+ import org.jruby.RubyArray;
51
+ import org.jruby.RubyBoolean;
52
+ import org.jruby.RubyClass;
53
+ import org.jruby.RubyFixnum;
54
+ import org.jruby.RubyObject;
55
+ import org.jruby.RubyString;
56
+ import org.jruby.anno.JRubyClass;
57
+ import org.jruby.anno.JRubyMethod;
58
+ import org.jruby.exceptions.RaiseException;
59
+ import org.jruby.javasupport.util.RuntimeHelpers;
60
+ import org.jruby.lexer.yacc.SyntaxException;
61
+ import org.jruby.runtime.ThreadContext;
62
+ import org.jruby.runtime.builtin.IRubyObject;
63
+ import org.jruby.util.ByteList;
64
+ import org.xml.sax.Attributes;
65
+ import org.xml.sax.InputSource;
66
+ import org.xml.sax.SAXException;
67
+ import org.xml.sax.SAXParseException;
68
+ import org.xml.sax.XMLReader;
69
+ import org.xml.sax.ext.DefaultHandler2;
70
+ import org.xml.sax.helpers.XMLReaderFactory;
71
+
72
+ /**
73
+ * Class for Nokogiri:XML::Reader
74
+ *
75
+ * @author sergio
76
+ * @author Yoko Harada <yokolet@gmail.com>
77
+ */
78
+ @JRubyClass(name="Nokogiri::XML::Reader")
79
+ public class XmlReader extends RubyObject {
80
+
81
+ private static final int XML_TEXTREADER_MODE_INITIAL = 0;
82
+ private static final int XML_TEXTREADER_MODE_INTERACTIVE = 1;
83
+ private static final int XML_TEXTREADER_MODE_ERROR = 2;
84
+ private static final int XML_TEXTREADER_MODE_EOF = 3;
85
+ private static final int XML_TEXTREADER_MODE_CLOSED = 4;
86
+ private static final int XML_TEXTREADER_MODE_READING = 5;
87
+
88
+ ArrayDeque<ReaderNode> nodeQueue;
89
+ private int state;
90
+
91
+ public XmlReader(Ruby runtime, RubyClass klazz) {
92
+ super(runtime, klazz);
93
+ }
94
+
95
+ /**
96
+ * Create and return a copy of this object.
97
+ *
98
+ * @return a clone of this object
99
+ */
100
+ @Override
101
+ public Object clone() throws CloneNotSupportedException {
102
+ return super.clone();
103
+ }
104
+
105
+ public void init(Ruby runtime) {
106
+ nodeQueue = new ArrayDeque<ReaderNode>();
107
+ nodeQueue.add(new ReaderNode.EmptyNode(runtime));
108
+ }
109
+
110
+ private void parseRubyString(ThreadContext context, RubyString content, IRubyObject url, Options options){
111
+ Ruby ruby = context.getRuntime();
112
+ try {
113
+ this.setState(XML_TEXTREADER_MODE_READING);
114
+ XMLReader reader = this.createReader(ruby, options);
115
+ ByteList byteList = content.getByteList();
116
+ ByteArrayInputStream bais = new ByteArrayInputStream(byteList.unsafeBytes(), byteList.begin(), byteList.length());
117
+ InputSource inputSource = new InputSource(bais);
118
+ ParserContext.setUrl(context, inputSource, url);
119
+ reader.parse(inputSource);
120
+ this.setState(XML_TEXTREADER_MODE_CLOSED);
121
+ } catch (SAXParseException spe) {
122
+ this.setState(XML_TEXTREADER_MODE_ERROR);
123
+ this.nodeQueue.add(new ReaderNode.ExceptionNode(ruby, spe));
124
+ } catch (IOException ioe) {
125
+ throw RaiseException.createNativeRaiseException(ruby, ioe);
126
+ } catch (SAXException saxe) {
127
+ throw RaiseException.createNativeRaiseException(ruby, saxe);
128
+ }
129
+ }
130
+
131
+ private void setState(int state) { this.state = state; }
132
+
133
+ @JRubyMethod
134
+ public IRubyObject attribute(ThreadContext context, IRubyObject name) {
135
+ return nodeQueue.peek().getAttributeByName(name);
136
+ }
137
+
138
+ @JRubyMethod
139
+ public IRubyObject attribute_at(ThreadContext context, IRubyObject index) {
140
+ return nodeQueue.peek().getAttributeByIndex(index);
141
+ }
142
+
143
+ @JRubyMethod
144
+ public IRubyObject attribute_count(ThreadContext context) {
145
+ return nodeQueue.peek().getAttributeCount();
146
+ }
147
+
148
+ @JRubyMethod
149
+ public IRubyObject attribute_nodes(ThreadContext context) {
150
+ return nodeQueue.peek().getAttributesNodes();
151
+ }
152
+
153
+ @JRubyMethod
154
+ public IRubyObject attr_nodes(ThreadContext context) {
155
+ return nodeQueue.peek().getAttributesNodes();
156
+ }
157
+
158
+ @JRubyMethod(name = "attributes?")
159
+ public IRubyObject attributes_p(ThreadContext context) {
160
+ return nodeQueue.peek().hasAttributes();
161
+ }
162
+
163
+ @JRubyMethod
164
+ public IRubyObject base_uri(ThreadContext context) {
165
+ return nodeQueue.peek().getXmlBase();
166
+ }
167
+
168
+ @JRubyMethod(name="default?")
169
+ public IRubyObject default_p(ThreadContext context){
170
+ return nodeQueue.peek().isDefault();
171
+ }
172
+
173
+ @JRubyMethod
174
+ public IRubyObject depth(ThreadContext context) {
175
+ return nodeQueue.peek().getDepth();
176
+ }
177
+
178
+ @JRubyMethod(name = {"empty_element?", "self_closing?"})
179
+ public IRubyObject empty_element_p(ThreadContext context) {
180
+ ReaderNode readerNode = nodeQueue.peek();
181
+ if (readerNode == null) return context.getRuntime().getNil();
182
+ if (!(readerNode instanceof ElementNode)) context.getRuntime().getFalse();
183
+ return RubyBoolean.newBoolean(context.getRuntime(), !readerNode.hasChildren);
184
+ }
185
+
186
+ @JRubyMethod(meta = true, rest = true)
187
+ public static IRubyObject from_io(ThreadContext context, IRubyObject cls, IRubyObject args[]) {
188
+ // Only to pass the source test.
189
+ Ruby runtime = context.getRuntime();
190
+ // Not nil allowed!
191
+ if(args[0].isNil()) throw runtime.newArgumentError("io cannot be nil");
192
+
193
+ XmlReader reader = (XmlReader) NokogiriService.XML_READER_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Reader"));
194
+ reader.init(runtime);
195
+ reader.setInstanceVariable("@source", args[0]);
196
+ reader.setInstanceVariable("@errors", runtime.newArray());
197
+ IRubyObject url = context.nil;
198
+ if (args.length > 1) url = args[1];
199
+ if (args.length > 2) reader.setInstanceVariable("@encoding", args[2]);
200
+
201
+ RubyString content = RuntimeHelpers.invoke(context, args[0], "read").convertToString();
202
+
203
+ Options options;
204
+ if (args.length > 3) {
205
+ options = new ParserContext.Options((Long)args[3].toJava(Long.class));
206
+ } else {
207
+ // use the default options RECOVER | NONET
208
+ options = new ParserContext.Options(2048 | 1);
209
+ }
210
+ reader.parseRubyString(context, content, url, options);
211
+ return reader;
212
+ }
213
+
214
+ @JRubyMethod(meta = true, rest = true)
215
+ public static IRubyObject from_memory(ThreadContext context, IRubyObject cls, IRubyObject args[]) {
216
+ // args[0]: string, args[1]: url, args[2]: encoding, args[3]: options
217
+ Ruby runtime = context.getRuntime();
218
+ // Not nil allowed!
219
+ if(args[0].isNil()) throw runtime.newArgumentError("string cannot be nil");
220
+
221
+ XmlReader reader = (XmlReader) NokogiriService.XML_READER_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Reader"));
222
+ reader.init(runtime);
223
+ reader.setInstanceVariable("@source", args[0]);
224
+ reader.setInstanceVariable("@errors", runtime.newArray());
225
+ IRubyObject url = context.nil;
226
+ if (args.length > 1) url = args[1];
227
+ if (args.length > 2) reader.setInstanceVariable("@encoding", args[2]);
228
+
229
+ Options options;
230
+ if (args.length > 3) {
231
+ options = new ParserContext.Options((Long)args[3].toJava(Long.class));
232
+ } else {
233
+ // use the default options RECOVER | NONET
234
+ options = new ParserContext.Options(2048 | 1);
235
+ }
236
+ reader.parseRubyString(context, args[0].convertToString(), url, options);
237
+ return reader;
238
+ }
239
+
240
+ @JRubyMethod
241
+ public IRubyObject node_type(ThreadContext context) {
242
+ IRubyObject node_type = nodeQueue.peek().getNodeType();
243
+ return node_type == null ? RubyFixnum.zero(context.getRuntime()) : node_type;
244
+ }
245
+
246
+ @JRubyMethod
247
+ public IRubyObject inner_xml(ThreadContext context) {
248
+ return stringOrBlank(context.getRuntime(), getInnerXml(nodeQueue, nodeQueue.peek()));
249
+ }
250
+
251
+ private String getInnerXml(ArrayDeque<ReaderNode> nodeQueue, ReaderNode current) {
252
+ if (current.depth < 0) return null;
253
+ if (!current.hasChildren) return null;
254
+ StringBuffer sb = new StringBuffer();
255
+ int currentDepth = current.depth;
256
+ int inner = 0;
257
+ for (ReaderNode node : nodeQueue) {
258
+ if (node.depth == currentDepth && node.getName().equals(current.getName())) {
259
+ inner++;
260
+ }
261
+ if (node.depth > currentDepth) {
262
+ sb.append(node.getString());
263
+ }
264
+ if (inner == 2) break;
265
+ }
266
+ return new String(sb);
267
+ }
268
+
269
+ @JRubyMethod
270
+ public IRubyObject outer_xml(ThreadContext context) {
271
+ return stringOrBlank(context.getRuntime(), getOuterXml(nodeQueue, nodeQueue.peek()));
272
+ }
273
+
274
+ private String getOuterXml(ArrayDeque<ReaderNode> nodeQueue, ReaderNode current) {
275
+ if (current.depth < 0) return null;
276
+ StringBuffer sb = new StringBuffer();
277
+ int initialDepth = current.depth;
278
+ int inner = 0;
279
+ for (ReaderNode node : nodeQueue) {
280
+ if (node.depth >= initialDepth) {
281
+ if (node.depth == initialDepth && node.getName().equals(current.getName())) {
282
+ inner++;
283
+ }
284
+
285
+ sb.append(node.getString());
286
+ }
287
+ if (inner == 2) break;
288
+ }
289
+ return new String(sb);
290
+ }
291
+
292
+ @JRubyMethod
293
+ public IRubyObject lang(ThreadContext context) {
294
+ return nodeQueue.peek().getLang();
295
+ }
296
+
297
+ @JRubyMethod
298
+ public IRubyObject local_name(ThreadContext context) {
299
+ return nodeQueue.peek().getLocalName();
300
+ }
301
+
302
+ @JRubyMethod
303
+ public IRubyObject name(ThreadContext context) {
304
+ return nodeQueue.peek().getName();
305
+ }
306
+
307
+ @JRubyMethod
308
+ public IRubyObject namespace_uri(ThreadContext context) {
309
+ return nodeQueue.peek().getUri();
310
+ }
311
+
312
+ @JRubyMethod
313
+ public IRubyObject namespaces(ThreadContext context) {
314
+ return nodeQueue.peek().getNamespaces(context);
315
+ }
316
+
317
+ @JRubyMethod
318
+ public IRubyObject prefix(ThreadContext context) {
319
+ return nodeQueue.peek().getPrefix();
320
+ }
321
+
322
+ @JRubyMethod
323
+ public IRubyObject read(ThreadContext context) {
324
+ this.nodeQueue.poll();
325
+ if(nodeQueue.peek() == null) {
326
+ return context.getRuntime().getNil();
327
+ } else if(nodeQueue.peek().isError()) {
328
+ RubyArray errors = (RubyArray) this.getInstanceVariable("@errors");
329
+ errors.append(nodeQueue.peek().toSyntaxError());
330
+
331
+ this.setInstanceVariable("@errors", errors);
332
+
333
+ throw new RaiseException((XmlSyntaxError) nodeQueue.peek().toSyntaxError());
334
+ } else {
335
+ return this;
336
+ }
337
+ }
338
+
339
+ @JRubyMethod
340
+ public IRubyObject state(ThreadContext context) {
341
+ return context.getRuntime().newFixnum(this.state);
342
+ }
343
+
344
+ @JRubyMethod
345
+ public IRubyObject value(ThreadContext context) {
346
+ return nodeQueue.peek().getValue();
347
+ }
348
+
349
+ @JRubyMethod(name = "value?")
350
+ public IRubyObject value_p(ThreadContext context) {
351
+ return nodeQueue.peek().hasValue();
352
+ }
353
+
354
+ @JRubyMethod
355
+ public IRubyObject xml_version(ThreadContext context) {
356
+ return nodeQueue.peek().getXmlVersion();
357
+ }
358
+
359
+ protected XMLReader createReader(final Ruby ruby, Options options) {
360
+ DefaultHandler2 handler = new DefaultHandler2() {
361
+
362
+ Stack<String> langStack;
363
+ int depth;
364
+ Stack<String> xmlBaseStack;
365
+ Stack<ReaderNode.ElementNode> elementStack;
366
+
367
+ @Override
368
+ public void characters(char[] chars, int start, int length) {
369
+ ReaderNode.TextNode node = ReaderNode.createTextNode(ruby, new String(chars, start, length), depth, langStack, xmlBaseStack);
370
+ nodeQueue.add(node);
371
+ }
372
+
373
+ @Override
374
+ public void endDocument() throws SAXException {
375
+ langStack = null;
376
+ xmlBaseStack = null;
377
+ elementStack = null;
378
+ }
379
+
380
+ @Override
381
+ public void endElement(String uri, String localName, String qName) {
382
+ depth--;
383
+ ReaderNode previous = nodeQueue.getLast();
384
+ ElementNode startElementNode = elementStack.pop();
385
+ if (previous instanceof ReaderNode.ElementNode && qName.equals(previous.name)) {
386
+ previous.hasChildren = false;
387
+ } else {
388
+ ReaderNode node = ReaderNode.createClosingNode(ruby, uri, localName, qName, depth, langStack, xmlBaseStack);
389
+ if (startElementNode != null) {
390
+ node.attributeList = startElementNode.attributeList;
391
+ node.namespaces = startElementNode.namespaces;
392
+ }
393
+ nodeQueue.add(node);
394
+ }
395
+ if (!langStack.isEmpty()) langStack.pop();
396
+ if (!xmlBaseStack.isEmpty()) xmlBaseStack.pop();
397
+ }
398
+
399
+ @Override
400
+ public void error(SAXParseException ex) throws SAXParseException {
401
+ nodeQueue.add(new ReaderNode.ExceptionNode(ruby, ex));
402
+ throw ex;
403
+ }
404
+
405
+ @Override
406
+ public void fatalError(SAXParseException ex) throws SAXParseException {
407
+ nodeQueue.add(new ReaderNode.ExceptionNode(ruby, ex));
408
+ throw ex;
409
+ }
410
+
411
+ @Override
412
+ public void startDocument() {
413
+ depth = 0;
414
+ langStack = new Stack<String>();
415
+ xmlBaseStack = new Stack<String>();
416
+ elementStack = new Stack<ReaderNode.ElementNode>();
417
+ }
418
+
419
+ @Override
420
+ public void startElement(String uri, String localName, String qName, Attributes attrs) {
421
+ ReaderNode readerNode = ReaderNode.createElementNode(ruby, uri, localName, qName, attrs, depth, langStack, xmlBaseStack);
422
+ nodeQueue.add(readerNode);
423
+ depth++;
424
+ if (readerNode.lang != null) langStack.push(readerNode.lang);
425
+ if (readerNode.xmlBase != null) xmlBaseStack.push(readerNode.xmlBase);
426
+ elementStack.push((ReaderNode.ElementNode)readerNode);
427
+ }
428
+
429
+ @Override
430
+ public void skippedEntity(String name) {
431
+ XmlSyntaxError error = XmlSyntaxError.createNokogiriXmlSyntaxError(ruby);
432
+ error.setException(new Exception("Unknown entity " + name));
433
+ throw new RaiseException(error);
434
+ }
435
+
436
+ @Override
437
+ public void warning(SAXParseException ex) throws SAXParseException {
438
+ nodeQueue.add(new ReaderNode.ExceptionNode(ruby, ex));
439
+ throw ex;
440
+ }
441
+ };
442
+ try {
443
+ XMLReader reader = XMLReaderFactory.createXMLReader();
444
+ reader.setContentHandler(handler);
445
+ reader.setDTDHandler(handler);
446
+ reader.setErrorHandler(handler);
447
+ reader.setEntityResolver(new NokogiriEntityResolver(ruby, null, options));
448
+ reader.setFeature("http://xml.org/sax/features/xmlns-uris", true);
449
+ reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
450
+ reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", options.dtdLoad || options.dtdValid);
451
+ return reader;
452
+ } catch (SAXException saxe) {
453
+ throw RaiseException.createNativeRaiseException(ruby, saxe);
454
+ }
455
+ }
456
+ }