nokogiri-maven 1.5.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (278) hide show
  1. data/CHANGELOG.ja.rdoc +544 -0
  2. data/CHANGELOG.rdoc +532 -0
  3. data/Manifest.txt +283 -0
  4. data/README.ja.rdoc +106 -0
  5. data/README.rdoc +174 -0
  6. data/Rakefile +164 -0
  7. data/bin/nokogiri +53 -0
  8. data/ext/java/nokogiri/EncodingHandler.java +124 -0
  9. data/ext/java/nokogiri/HtmlDocument.java +119 -0
  10. data/ext/java/nokogiri/HtmlElementDescription.java +145 -0
  11. data/ext/java/nokogiri/HtmlEntityLookup.java +79 -0
  12. data/ext/java/nokogiri/HtmlSaxParserContext.java +259 -0
  13. data/ext/java/nokogiri/NokogiriService.java +590 -0
  14. data/ext/java/nokogiri/XmlAttr.java +180 -0
  15. data/ext/java/nokogiri/XmlAttributeDecl.java +130 -0
  16. data/ext/java/nokogiri/XmlCdata.java +84 -0
  17. data/ext/java/nokogiri/XmlComment.java +86 -0
  18. data/ext/java/nokogiri/XmlDocument.java +519 -0
  19. data/ext/java/nokogiri/XmlDocumentFragment.java +223 -0
  20. data/ext/java/nokogiri/XmlDtd.java +469 -0
  21. data/ext/java/nokogiri/XmlElement.java +195 -0
  22. data/ext/java/nokogiri/XmlElementContent.java +382 -0
  23. data/ext/java/nokogiri/XmlElementDecl.java +152 -0
  24. data/ext/java/nokogiri/XmlEntityDecl.java +162 -0
  25. data/ext/java/nokogiri/XmlEntityReference.java +97 -0
  26. data/ext/java/nokogiri/XmlNamespace.java +183 -0
  27. data/ext/java/nokogiri/XmlNode.java +1378 -0
  28. data/ext/java/nokogiri/XmlNodeSet.java +267 -0
  29. data/ext/java/nokogiri/XmlProcessingInstruction.java +99 -0
  30. data/ext/java/nokogiri/XmlReader.java +408 -0
  31. data/ext/java/nokogiri/XmlRelaxng.java +144 -0
  32. data/ext/java/nokogiri/XmlSaxParserContext.java +367 -0
  33. data/ext/java/nokogiri/XmlSaxPushParser.java +184 -0
  34. data/ext/java/nokogiri/XmlSchema.java +324 -0
  35. data/ext/java/nokogiri/XmlSyntaxError.java +119 -0
  36. data/ext/java/nokogiri/XmlText.java +119 -0
  37. data/ext/java/nokogiri/XmlXpathContext.java +199 -0
  38. data/ext/java/nokogiri/XsltStylesheet.java +197 -0
  39. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +204 -0
  40. data/ext/java/nokogiri/internals/NokogiriDocumentCache.java +73 -0
  41. data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +86 -0
  42. data/ext/java/nokogiri/internals/NokogiriHandler.java +327 -0
  43. data/ext/java/nokogiri/internals/NokogiriHelpers.java +639 -0
  44. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +167 -0
  45. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +130 -0
  46. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +74 -0
  47. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +121 -0
  48. data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +79 -0
  49. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +141 -0
  50. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +73 -0
  51. data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +67 -0
  52. data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +86 -0
  53. data/ext/java/nokogiri/internals/ParserContext.java +276 -0
  54. data/ext/java/nokogiri/internals/PushInputStream.java +411 -0
  55. data/ext/java/nokogiri/internals/ReaderNode.java +531 -0
  56. data/ext/java/nokogiri/internals/SaveContextVisitor.java +567 -0
  57. data/ext/java/nokogiri/internals/SchemaErrorHandler.java +76 -0
  58. data/ext/java/nokogiri/internals/XmlDeclHandler.java +42 -0
  59. data/ext/java/nokogiri/internals/XmlDomParser.java +76 -0
  60. data/ext/java/nokogiri/internals/XmlDomParserContext.java +244 -0
  61. data/ext/java/nokogiri/internals/XmlSaxParser.java +65 -0
  62. data/ext/java/nokogiri/internals/XsltExtensionFunction.java +72 -0
  63. data/ext/nokogiri/depend +358 -0
  64. data/ext/nokogiri/extconf.rb +124 -0
  65. data/ext/nokogiri/html_document.c +154 -0
  66. data/ext/nokogiri/html_document.h +10 -0
  67. data/ext/nokogiri/html_element_description.c +276 -0
  68. data/ext/nokogiri/html_element_description.h +10 -0
  69. data/ext/nokogiri/html_entity_lookup.c +32 -0
  70. data/ext/nokogiri/html_entity_lookup.h +8 -0
  71. data/ext/nokogiri/html_sax_parser_context.c +94 -0
  72. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  73. data/ext/nokogiri/nokogiri.c +115 -0
  74. data/ext/nokogiri/nokogiri.h +160 -0
  75. data/ext/nokogiri/xml_attr.c +94 -0
  76. data/ext/nokogiri/xml_attr.h +9 -0
  77. data/ext/nokogiri/xml_attribute_decl.c +70 -0
  78. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  79. data/ext/nokogiri/xml_cdata.c +56 -0
  80. data/ext/nokogiri/xml_cdata.h +9 -0
  81. data/ext/nokogiri/xml_comment.c +54 -0
  82. data/ext/nokogiri/xml_comment.h +9 -0
  83. data/ext/nokogiri/xml_document.c +478 -0
  84. data/ext/nokogiri/xml_document.h +23 -0
  85. data/ext/nokogiri/xml_document_fragment.c +48 -0
  86. data/ext/nokogiri/xml_document_fragment.h +10 -0
  87. data/ext/nokogiri/xml_dtd.c +202 -0
  88. data/ext/nokogiri/xml_dtd.h +10 -0
  89. data/ext/nokogiri/xml_element_content.c +123 -0
  90. data/ext/nokogiri/xml_element_content.h +10 -0
  91. data/ext/nokogiri/xml_element_decl.c +69 -0
  92. data/ext/nokogiri/xml_element_decl.h +9 -0
  93. data/ext/nokogiri/xml_encoding_handler.c +79 -0
  94. data/ext/nokogiri/xml_encoding_handler.h +8 -0
  95. data/ext/nokogiri/xml_entity_decl.c +110 -0
  96. data/ext/nokogiri/xml_entity_decl.h +10 -0
  97. data/ext/nokogiri/xml_entity_reference.c +52 -0
  98. data/ext/nokogiri/xml_entity_reference.h +9 -0
  99. data/ext/nokogiri/xml_io.c +56 -0
  100. data/ext/nokogiri/xml_io.h +11 -0
  101. data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
  102. data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
  103. data/ext/nokogiri/xml_namespace.c +84 -0
  104. data/ext/nokogiri/xml_namespace.h +13 -0
  105. data/ext/nokogiri/xml_node.c +1385 -0
  106. data/ext/nokogiri/xml_node.h +13 -0
  107. data/ext/nokogiri/xml_node_set.c +418 -0
  108. data/ext/nokogiri/xml_node_set.h +9 -0
  109. data/ext/nokogiri/xml_processing_instruction.c +56 -0
  110. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  111. data/ext/nokogiri/xml_reader.c +684 -0
  112. data/ext/nokogiri/xml_reader.h +10 -0
  113. data/ext/nokogiri/xml_relax_ng.c +161 -0
  114. data/ext/nokogiri/xml_relax_ng.h +9 -0
  115. data/ext/nokogiri/xml_sax_parser.c +293 -0
  116. data/ext/nokogiri/xml_sax_parser.h +39 -0
  117. data/ext/nokogiri/xml_sax_parser_context.c +199 -0
  118. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  119. data/ext/nokogiri/xml_sax_push_parser.c +115 -0
  120. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  121. data/ext/nokogiri/xml_schema.c +205 -0
  122. data/ext/nokogiri/xml_schema.h +9 -0
  123. data/ext/nokogiri/xml_syntax_error.c +58 -0
  124. data/ext/nokogiri/xml_syntax_error.h +13 -0
  125. data/ext/nokogiri/xml_text.c +50 -0
  126. data/ext/nokogiri/xml_text.h +9 -0
  127. data/ext/nokogiri/xml_xpath_context.c +309 -0
  128. data/ext/nokogiri/xml_xpath_context.h +9 -0
  129. data/ext/nokogiri/xslt_stylesheet.c +264 -0
  130. data/ext/nokogiri/xslt_stylesheet.h +9 -0
  131. data/lib/nokogiri.rb +127 -0
  132. data/lib/nokogiri/css.rb +27 -0
  133. data/lib/nokogiri/css/node.rb +99 -0
  134. data/lib/nokogiri/css/parser.rb +677 -0
  135. data/lib/nokogiri/css/parser.y +237 -0
  136. data/lib/nokogiri/css/parser_extras.rb +91 -0
  137. data/lib/nokogiri/css/syntax_error.rb +7 -0
  138. data/lib/nokogiri/css/tokenizer.rb +152 -0
  139. data/lib/nokogiri/css/tokenizer.rex +55 -0
  140. data/lib/nokogiri/css/xpath_visitor.rb +171 -0
  141. data/lib/nokogiri/decorators/slop.rb +35 -0
  142. data/lib/nokogiri/html.rb +36 -0
  143. data/lib/nokogiri/html/builder.rb +35 -0
  144. data/lib/nokogiri/html/document.rb +213 -0
  145. data/lib/nokogiri/html/document_fragment.rb +41 -0
  146. data/lib/nokogiri/html/element_description.rb +23 -0
  147. data/lib/nokogiri/html/element_description_defaults.rb +671 -0
  148. data/lib/nokogiri/html/entity_lookup.rb +13 -0
  149. data/lib/nokogiri/html/sax/parser.rb +52 -0
  150. data/lib/nokogiri/html/sax/parser_context.rb +16 -0
  151. data/lib/nokogiri/nokogiri.jar +0 -0
  152. data/lib/nokogiri/syntax_error.rb +4 -0
  153. data/lib/nokogiri/version.rb +88 -0
  154. data/lib/nokogiri/xml.rb +67 -0
  155. data/lib/nokogiri/xml/attr.rb +14 -0
  156. data/lib/nokogiri/xml/attribute_decl.rb +18 -0
  157. data/lib/nokogiri/xml/builder.rb +425 -0
  158. data/lib/nokogiri/xml/cdata.rb +11 -0
  159. data/lib/nokogiri/xml/character_data.rb +7 -0
  160. data/lib/nokogiri/xml/document.rb +234 -0
  161. data/lib/nokogiri/xml/document_fragment.rb +98 -0
  162. data/lib/nokogiri/xml/dtd.rb +22 -0
  163. data/lib/nokogiri/xml/element_content.rb +36 -0
  164. data/lib/nokogiri/xml/element_decl.rb +13 -0
  165. data/lib/nokogiri/xml/entity_decl.rb +19 -0
  166. data/lib/nokogiri/xml/namespace.rb +13 -0
  167. data/lib/nokogiri/xml/node.rb +915 -0
  168. data/lib/nokogiri/xml/node/save_options.rb +61 -0
  169. data/lib/nokogiri/xml/node_set.rb +357 -0
  170. data/lib/nokogiri/xml/notation.rb +6 -0
  171. data/lib/nokogiri/xml/parse_options.rb +93 -0
  172. data/lib/nokogiri/xml/pp.rb +2 -0
  173. data/lib/nokogiri/xml/pp/character_data.rb +18 -0
  174. data/lib/nokogiri/xml/pp/node.rb +56 -0
  175. data/lib/nokogiri/xml/processing_instruction.rb +8 -0
  176. data/lib/nokogiri/xml/reader.rb +112 -0
  177. data/lib/nokogiri/xml/relax_ng.rb +32 -0
  178. data/lib/nokogiri/xml/sax.rb +4 -0
  179. data/lib/nokogiri/xml/sax/document.rb +164 -0
  180. data/lib/nokogiri/xml/sax/parser.rb +115 -0
  181. data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
  182. data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
  183. data/lib/nokogiri/xml/schema.rb +63 -0
  184. data/lib/nokogiri/xml/syntax_error.rb +47 -0
  185. data/lib/nokogiri/xml/text.rb +9 -0
  186. data/lib/nokogiri/xml/xpath.rb +10 -0
  187. data/lib/nokogiri/xml/xpath/syntax_error.rb +11 -0
  188. data/lib/nokogiri/xml/xpath_context.rb +16 -0
  189. data/lib/nokogiri/xslt.rb +52 -0
  190. data/lib/nokogiri/xslt/stylesheet.rb +25 -0
  191. data/lib/xsd/xmlparser/nokogiri.rb +90 -0
  192. data/nokogiri_help_responses.md +40 -0
  193. data/tasks/cross_compile.rb +152 -0
  194. data/tasks/nokogiri.org.rb +18 -0
  195. data/tasks/test.rb +94 -0
  196. data/test/css/test_nthiness.rb +159 -0
  197. data/test/css/test_parser.rb +303 -0
  198. data/test/css/test_tokenizer.rb +198 -0
  199. data/test/css/test_xpath_visitor.rb +85 -0
  200. data/test/decorators/test_slop.rb +16 -0
  201. data/test/files/2ch.html +108 -0
  202. data/test/files/address_book.rlx +12 -0
  203. data/test/files/address_book.xml +10 -0
  204. data/test/files/bar/bar.xsd +4 -0
  205. data/test/files/dont_hurt_em_why.xml +422 -0
  206. data/test/files/encoding.html +82 -0
  207. data/test/files/encoding.xhtml +84 -0
  208. data/test/files/exslt.xml +8 -0
  209. data/test/files/exslt.xslt +35 -0
  210. data/test/files/foo/foo.xsd +4 -0
  211. data/test/files/metacharset.html +10 -0
  212. data/test/files/noencoding.html +47 -0
  213. data/test/files/po.xml +32 -0
  214. data/test/files/po.xsd +66 -0
  215. data/test/files/shift_jis.html +10 -0
  216. data/test/files/shift_jis.xml +5 -0
  217. data/test/files/snuggles.xml +3 -0
  218. data/test/files/staff.dtd +10 -0
  219. data/test/files/staff.xml +59 -0
  220. data/test/files/staff.xslt +32 -0
  221. data/test/files/tlm.html +850 -0
  222. data/test/files/valid_bar.xml +2 -0
  223. data/test/helper.rb +173 -0
  224. data/test/html/sax/test_parser.rb +136 -0
  225. data/test/html/sax/test_parser_context.rb +48 -0
  226. data/test/html/test_builder.rb +164 -0
  227. data/test/html/test_document.rb +472 -0
  228. data/test/html/test_document_encoding.rb +138 -0
  229. data/test/html/test_document_fragment.rb +255 -0
  230. data/test/html/test_element_description.rb +100 -0
  231. data/test/html/test_named_characters.rb +14 -0
  232. data/test/html/test_node.rb +190 -0
  233. data/test/html/test_node_encoding.rb +27 -0
  234. data/test/test_convert_xpath.rb +135 -0
  235. data/test/test_css_cache.rb +45 -0
  236. data/test/test_encoding_handler.rb +46 -0
  237. data/test/test_memory_leak.rb +72 -0
  238. data/test/test_nokogiri.rb +132 -0
  239. data/test/test_reader.rb +425 -0
  240. data/test/test_soap4r_sax.rb +52 -0
  241. data/test/test_xslt_transforms.rb +193 -0
  242. data/test/xml/node/test_save_options.rb +28 -0
  243. data/test/xml/node/test_subclass.rb +44 -0
  244. data/test/xml/sax/test_parser.rb +338 -0
  245. data/test/xml/sax/test_parser_context.rb +113 -0
  246. data/test/xml/sax/test_push_parser.rb +156 -0
  247. data/test/xml/test_attr.rb +65 -0
  248. data/test/xml/test_attribute_decl.rb +86 -0
  249. data/test/xml/test_builder.rb +227 -0
  250. data/test/xml/test_cdata.rb +50 -0
  251. data/test/xml/test_comment.rb +29 -0
  252. data/test/xml/test_document.rb +697 -0
  253. data/test/xml/test_document_encoding.rb +26 -0
  254. data/test/xml/test_document_fragment.rb +192 -0
  255. data/test/xml/test_dtd.rb +107 -0
  256. data/test/xml/test_dtd_encoding.rb +33 -0
  257. data/test/xml/test_element_content.rb +56 -0
  258. data/test/xml/test_element_decl.rb +73 -0
  259. data/test/xml/test_entity_decl.rb +122 -0
  260. data/test/xml/test_entity_reference.rb +21 -0
  261. data/test/xml/test_namespace.rb +70 -0
  262. data/test/xml/test_node.rb +917 -0
  263. data/test/xml/test_node_attributes.rb +34 -0
  264. data/test/xml/test_node_encoding.rb +107 -0
  265. data/test/xml/test_node_reparenting.rb +334 -0
  266. data/test/xml/test_node_set.rb +742 -0
  267. data/test/xml/test_parse_options.rb +52 -0
  268. data/test/xml/test_processing_instruction.rb +30 -0
  269. data/test/xml/test_reader_encoding.rb +126 -0
  270. data/test/xml/test_relax_ng.rb +60 -0
  271. data/test/xml/test_schema.rb +94 -0
  272. data/test/xml/test_syntax_error.rb +12 -0
  273. data/test/xml/test_text.rb +47 -0
  274. data/test/xml/test_unparented_node.rb +381 -0
  275. data/test/xml/test_xpath.rb +237 -0
  276. data/test/xslt/test_custom_functions.rb +94 -0
  277. data/test/xslt/test_exception_handling.rb +37 -0
  278. metadata +552 -0
@@ -0,0 +1,1378 @@
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 java.lang.Math.max;
36
+ import static nokogiri.internals.NokogiriHelpers.getCachedNodeOrCreate;
37
+ import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
38
+ import static nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray;
39
+ import static nokogiri.internals.NokogiriHelpers.nonEmptyStringOrNil;
40
+ import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
41
+ import static nokogiri.internals.NokogiriHelpers.stringOrNil;
42
+
43
+ import java.io.ByteArrayInputStream;
44
+ import java.io.InputStream;
45
+ import java.util.ArrayList;
46
+ import java.util.List;
47
+
48
+ import nokogiri.internals.HtmlDomParserContext;
49
+ import nokogiri.internals.NokogiriHelpers;
50
+ import nokogiri.internals.NokogiriNamespaceCache;
51
+ import nokogiri.internals.SaveContextVisitor;
52
+ import nokogiri.internals.XmlDomParserContext;
53
+
54
+ import org.jruby.Ruby;
55
+ import org.jruby.RubyArray;
56
+ import org.jruby.RubyBoolean;
57
+ import org.jruby.RubyClass;
58
+ import org.jruby.RubyFixnum;
59
+ import org.jruby.RubyModule;
60
+ import org.jruby.RubyObject;
61
+ import org.jruby.RubyString;
62
+ import org.jruby.anno.JRubyClass;
63
+ import org.jruby.anno.JRubyMethod;
64
+ import org.jruby.javasupport.util.RuntimeHelpers;
65
+ import org.jruby.runtime.Block;
66
+ import org.jruby.runtime.ThreadContext;
67
+ import org.jruby.runtime.Visibility;
68
+ import org.jruby.runtime.builtin.IRubyObject;
69
+ import org.w3c.dom.Attr;
70
+ import org.w3c.dom.Document;
71
+ import org.w3c.dom.DocumentFragment;
72
+ import org.w3c.dom.Element;
73
+ import org.w3c.dom.NamedNodeMap;
74
+ import org.w3c.dom.Node;
75
+ import org.w3c.dom.NodeList;
76
+ import org.w3c.dom.Text;
77
+
78
+ /**
79
+ * Class for Nokogiri::XML::Node
80
+ *
81
+ * @author sergio
82
+ * @author Patrick Mahoney <pat@polycrystal.org>
83
+ * @author Yoko Harada <yokolet@gmail.com>
84
+ */
85
+ @JRubyClass(name="Nokogiri::XML::Node")
86
+ public class XmlNode extends RubyObject {
87
+
88
+ /** The underlying Node object. */
89
+ protected Node node;
90
+
91
+ /* Cached objects */
92
+ protected IRubyObject content = null;
93
+ protected IRubyObject doc = null;
94
+ protected IRubyObject name = null;
95
+
96
+ /*
97
+ * Taken from http://ejohn.org/blog/comparing-document-position/
98
+ * Used for compareDocumentPosition.
99
+ * <ironic>Thanks to both java api and w3 doc for its helpful documentation</ironic>
100
+ */
101
+
102
+ protected static final int IDENTICAL_ELEMENTS = 0;
103
+ protected static final int IN_DIFFERENT_DOCUMENTS = 1;
104
+ protected static final int SECOND_PRECEDES_FIRST = 2;
105
+ protected static final int FIRST_PRECEDES_SECOND = 4;
106
+ protected static final int SECOND_CONTAINS_FIRST = 8;
107
+ protected static final int FIRST_CONTAINS_SECOND = 16;
108
+
109
+ /**
110
+ * Cast <code>node</code> to an XmlNode or raise a type error
111
+ * in <code>context</code>.
112
+ */
113
+ protected static XmlNode asXmlNode(ThreadContext context, IRubyObject node) {
114
+ if (node == null || !(node instanceof XmlNode)) {
115
+ Ruby ruby = context.getRuntime();
116
+ throw ruby.newTypeError(node, getNokogiriClass(ruby, "Nokogiri::XML::Node"));
117
+ } else {
118
+ return (XmlNode) node;
119
+ }
120
+ }
121
+
122
+ /**
123
+ * Cast <code>node</code> to an XmlNode, or null if RubyNil, or
124
+ * raise a type error in <code>context</code>.
125
+ */
126
+ protected static XmlNode asXmlNodeOrNull(ThreadContext context, IRubyObject node) {
127
+ if (node == null || node.isNil()) {
128
+ return null;
129
+ } else {
130
+ return asXmlNode(context, node);
131
+ }
132
+ }
133
+
134
+ /**
135
+ * Coalesce to adjacent TextNodes.
136
+ * @param context
137
+ * @param prev Previous node to cur.
138
+ * @param cur Next node to prev.
139
+ */
140
+ public static void coalesceTextNodes(ThreadContext context, IRubyObject prev, IRubyObject cur) {
141
+ XmlNode p = asXmlNode(context, prev);
142
+ XmlNode c = asXmlNode(context, cur);
143
+
144
+ Node pNode = p.node;
145
+ Node cNode = c.node;
146
+
147
+ pNode.setNodeValue(pNode.getNodeValue()+cNode.getNodeValue());
148
+ p.content = null; // clear cached content
149
+
150
+ c.assimilateXmlNode(context, p);
151
+ }
152
+
153
+ /**
154
+ * Coalesce text nodes around <code>anchorNode</code>. If
155
+ * <code>anchorNode</code> has siblings (previous or next) that
156
+ * are text nodes, the content will be merged into
157
+ * <code>anchorNode</code> and the redundant nodes will be removed
158
+ * from the DOM.
159
+ *
160
+ * To match libxml behavior (?) the final content of
161
+ * <code>anchorNode</code> and any removed nodes will be
162
+ * identical.
163
+ *
164
+ * @param context
165
+ * @param anchorNode
166
+ */
167
+ protected static void coalesceTextNodes(ThreadContext context,
168
+ IRubyObject anchorNode) {
169
+ XmlNode xa = asXmlNode(context, anchorNode);
170
+
171
+ XmlNode xp = asXmlNodeOrNull(context, xa.previous_sibling(context));
172
+ XmlNode xn = asXmlNodeOrNull(context, xa.next_sibling(context));
173
+
174
+ Node p = xp == null ? null : xp.node;
175
+ Node a = xa.node;
176
+ Node n = xn == null ? null : xn.node;
177
+
178
+ Node parent = a.getParentNode();
179
+
180
+ if (p != null && p.getNodeType() == Node.TEXT_NODE) {
181
+ xa.setContent(p.getNodeValue() + a.getNodeValue());
182
+ parent.removeChild(p);
183
+ xp.assimilateXmlNode(context, xa);
184
+ }
185
+ if (n != null && n.getNodeType() == Node.TEXT_NODE) {
186
+ xa.setContent(a.getNodeValue() + n.getNodeValue());
187
+ parent.removeChild(n);
188
+ xn.assimilateXmlNode(context, xa);
189
+ }
190
+ }
191
+
192
+ /**
193
+ * This is the allocator for XmlNode class. It should only be
194
+ * called from Ruby code.
195
+ */
196
+ public XmlNode(Ruby ruby, RubyClass cls) {
197
+ super(ruby, cls);
198
+ }
199
+
200
+ /**
201
+ * This is a constructor to create an XmlNode from an already
202
+ * existing node. It may be called by Java code.
203
+ */
204
+ public XmlNode(Ruby ruby, RubyClass cls, Node node) {
205
+ super(ruby, cls);
206
+ this.node = node;
207
+
208
+ if (node != null) {
209
+ resetCache();
210
+
211
+ if (node.getNodeType() != Node.DOCUMENT_NODE) {
212
+ doc = document(ruby.getCurrentContext());
213
+
214
+ if (doc != null) {
215
+ RuntimeHelpers.invoke(ruby.getCurrentContext(), doc, "decorate", this);
216
+ }
217
+ }
218
+ }
219
+
220
+ }
221
+
222
+ /**
223
+ * Create and return a copy of this object.
224
+ *
225
+ * @return a clone of this object
226
+ */
227
+ @Override
228
+ public Object clone() throws CloneNotSupportedException {
229
+ return super.clone();
230
+ }
231
+
232
+ protected void resetCache() {
233
+ node.setUserData(NokogiriHelpers.CACHED_NODE, this, null);
234
+ }
235
+
236
+ /**
237
+ * Allocate a new object, perform initialization, call that
238
+ * object's initialize method, and call any block passing the
239
+ * object as the only argument. If <code>cls</code> is
240
+ * Nokogiri::XML::Node, creates a new Nokogiri::XML::Element
241
+ * instead.
242
+ *
243
+ * This static method seems to be inherited, strangely enough.
244
+ * E.g. creating a new XmlAttr from Ruby code calls this method if
245
+ * XmlAttr does not define its own 'new' method.
246
+ *
247
+ * Since there is some Java bookkeeping that always needs to
248
+ * happen, we don't define the 'initialize' method in Java because
249
+ * we'd have to count on subclasses calling 'super'.
250
+ *
251
+ * The main consequence of this is that every subclass needs to
252
+ * define its own 'new' method.
253
+ *
254
+ * As a convenience, this method does the following:
255
+ *
256
+ * <ul>
257
+ *
258
+ * <li>allocates a new object using the allocator assigned to
259
+ * <code>cls</code></li>
260
+ *
261
+ * <li>calls the Java method init(); subclasses can override this,
262
+ * otherwise they should implement a specific 'new' method</li>
263
+ *
264
+ * <li>invokes the Ruby initializer</li>
265
+ *
266
+ * <li>if a block is given, calls the block with the new node as
267
+ * the argument</li>
268
+ *
269
+ * </ul>
270
+ *
271
+ * -pmahoney
272
+ */
273
+ @JRubyMethod(name = "new", meta = true, rest = true)
274
+ public static IRubyObject rbNew(ThreadContext context, IRubyObject cls,
275
+ IRubyObject[] args, Block block) {
276
+ Ruby ruby = context.getRuntime();
277
+ RubyClass klazz = (RubyClass) cls;
278
+
279
+ if (cls.equals(getNokogiriClass(ruby, "Nokogiri::XML::Node"))) {
280
+ klazz = getNokogiriClass(ruby, "Nokogiri::XML::Element");
281
+ }
282
+
283
+ XmlNode xmlNode = (XmlNode) klazz.allocate();
284
+ xmlNode.init(context, args);
285
+ xmlNode.callInit(args, block);
286
+ if (xmlNode.node == null) context.getRuntime().newRuntimeError("NODE IS NULL");
287
+ if (block.isGiven()) block.call(context, xmlNode);
288
+ return xmlNode;
289
+ }
290
+
291
+ /**
292
+ * Initialize the object from Ruby arguments. Should be
293
+ * overridden by subclasses. Should check for a minimum number of
294
+ * args but not for an exact number. Any extra args will then be
295
+ * passed to 'initialize'. The way 'new' and this 'init' function
296
+ * interact means that subclasses cannot arbitrarily change the
297
+ * require aruments by defining an 'initialize' method. This is
298
+ * how the C libxml wrapper works also.
299
+ *
300
+ * As written it performs initialization for a new Element with
301
+ * the given <code>name</code> within the document
302
+ * <code>doc</code>. So XmlElement need not override this. This
303
+ * implementation cannot be moved to XmlElement however, because
304
+ * subclassing XmlNode must result in something that behaves much
305
+ * like XmlElement.
306
+ */
307
+ protected void init(ThreadContext context, IRubyObject[] args) {
308
+ if (args.length < 2)
309
+ throw context.getRuntime().newArgumentError(args.length, 2);
310
+
311
+ IRubyObject name = args[0];
312
+ IRubyObject doc = args[1];
313
+
314
+ Document document = asXmlNode(context, doc).getOwnerDocument();
315
+ if (document == null) {
316
+ throw getRuntime().newArgumentError("node must have owner document");
317
+ }
318
+
319
+ Element element = document.createElementNS(null, rubyStringToString(name));
320
+ setNode(context, element);
321
+ }
322
+
323
+ /**
324
+ * Set the underlying node of this node to the underlying node of
325
+ * <code>otherNode</code>.
326
+ *
327
+ * FIXME: also update the cached node?
328
+ */
329
+ protected void assimilateXmlNode(ThreadContext context, IRubyObject otherNode) {
330
+ XmlNode toAssimilate = asXmlNode(context, otherNode);
331
+
332
+ this.node = toAssimilate.node;
333
+ content = null; // clear cache
334
+ }
335
+
336
+ /**
337
+ * See org.w3.dom.Node#normalize.
338
+ */
339
+ public void normalize() {
340
+ node.normalize();
341
+ }
342
+
343
+ public Node getNode() {
344
+ return node;
345
+ }
346
+
347
+ public static Node getNodeFromXmlNode(ThreadContext context, IRubyObject xmlNode) {
348
+ return asXmlNode(context, xmlNode).node;
349
+ }
350
+
351
+ protected String indentString(IRubyObject indentStringObject, String xml) {
352
+ String[] lines = xml.split("\n");
353
+
354
+ if(lines.length <= 1) return xml;
355
+
356
+ String[] resultLines = new String[lines.length];
357
+
358
+ String curLine;
359
+ boolean closingTag = false;
360
+ String indentString = rubyStringToString(indentStringObject);
361
+ int lengthInd = indentString.length();
362
+ StringBuffer curInd = new StringBuffer();
363
+
364
+ resultLines[0] = lines[0];
365
+
366
+ for(int i = 1; i < lines.length; i++) {
367
+
368
+ curLine = lines[i].trim();
369
+
370
+ if(curLine.length() == 0) continue;
371
+
372
+ if(curLine.startsWith("</")) {
373
+ closingTag = true;
374
+ curInd.setLength(max(0,curInd.length() - lengthInd));
375
+ }
376
+
377
+ resultLines[i] = curInd.toString() + curLine;
378
+
379
+ if(!curLine.endsWith("/>") && !closingTag) {
380
+ curInd.append(indentString);
381
+ }
382
+
383
+ closingTag = false;
384
+ }
385
+
386
+ StringBuffer result = new StringBuffer();
387
+ for(int i = 0; i < resultLines.length; i++) {
388
+ result.append(resultLines[i]);
389
+ result.append("\n");
390
+ }
391
+
392
+ return result.toString();
393
+ }
394
+
395
+ public boolean isComment() { return false; }
396
+
397
+ public boolean isElement() { return false; }
398
+
399
+ public boolean isProcessingInstruction() { return false; }
400
+
401
+ /**
402
+ * Return the string value of the attribute <code>key</code> or
403
+ * nil.
404
+ *
405
+ * Only applies where the underlying Node is an Element node, but
406
+ * implemented here in XmlNode because not all nodes with
407
+ * underlying Element nodes subclass XmlElement, such as the DTD
408
+ * declarations like XmlElementDecl.
409
+ */
410
+ protected IRubyObject getAttribute(ThreadContext context, String key) {
411
+ return getAttribute(context.getRuntime(), key);
412
+ }
413
+
414
+ protected IRubyObject getAttribute(Ruby runtime, String key) {
415
+ String value = getAttribute(key);
416
+ return nonEmptyStringOrNil(runtime, value);
417
+ }
418
+
419
+ protected String getAttribute(String key) {
420
+ if (node.getNodeType() != Node.ELEMENT_NODE) return null;
421
+
422
+ String value = ((Element)node).getAttribute(key);
423
+ return value.length() == 0 ? null : value;
424
+ }
425
+
426
+
427
+ public void post_add_child(ThreadContext context, XmlNode current, XmlNode child) {
428
+ }
429
+
430
+ public void relink_namespace(ThreadContext context) {
431
+ //this should delegate to subclasses' implementation
432
+ }
433
+
434
+ public void accept(ThreadContext context, SaveContextVisitor visitor) {}
435
+
436
+ public void setName(IRubyObject name) {
437
+ this.name = name;
438
+ }
439
+
440
+ public void setDocument(ThreadContext context, IRubyObject doc) {
441
+ this.doc = doc;
442
+ setInstanceVariable("@document", doc);
443
+ if (doc != null) {
444
+ RuntimeHelpers.invoke(context, doc, "decorate", this);
445
+ }
446
+ }
447
+
448
+ public void setNode(ThreadContext context, Node node) {
449
+ this.node = node;
450
+
451
+ if (node != null) {
452
+ resetCache();
453
+ if (node.getNodeType() != Node.DOCUMENT_NODE) {
454
+ doc = document(context);
455
+ }
456
+ }
457
+ }
458
+
459
+ public void updateNodeNamespaceIfNecessary(ThreadContext context, XmlNamespace ns) {
460
+ String oldPrefix = this.node.getPrefix();
461
+ String uri = rubyStringToString(ns.href(context));
462
+
463
+ /*
464
+ * Update if both prefixes are null or equal
465
+ */
466
+ boolean update = (oldPrefix == null && ns.prefix(context).isNil()) ||
467
+ (oldPrefix != null && !ns.prefix(context).isNil()
468
+ && oldPrefix.equals(rubyStringToString(ns.prefix(context))));
469
+
470
+ if(update) {
471
+ this.node.getOwnerDocument().renameNode(this.node, uri, this.node.getNodeName());
472
+ }
473
+ }
474
+
475
+ protected IRubyObject getNodeName(ThreadContext context) {
476
+ if (name != null) return name;
477
+ String str = null;
478
+
479
+ if (this.name == null && node != null) {
480
+ str = node.getNodeName();
481
+ str = NokogiriHelpers.getLocalPart(str);
482
+ }
483
+ if (str == null) str = "";
484
+ name = context.getRuntime().newString(str);
485
+ return name;
486
+ }
487
+
488
+ /**
489
+ * Add a namespace definition to this node. To the underlying
490
+ * node, add an attribute of the form
491
+ * <code>xmlns:prefix="uri"</code>.
492
+ */
493
+ @JRubyMethod(name = {"add_namespace_definition", "add_namespace"})
494
+ public IRubyObject add_namespace_definition(ThreadContext context,
495
+ IRubyObject prefix,
496
+ IRubyObject href) {
497
+ Node namespaceOwner;
498
+ if (node.getNodeType() == Node.ELEMENT_NODE) namespaceOwner = node;
499
+ else if (node.getNodeType() == Node.ATTRIBUTE_NODE) namespaceOwner = ((Attr)node).getOwnerElement();
500
+ else namespaceOwner = node.getParentNode();
501
+ XmlNamespace ns = XmlNamespace.createFromPrefixAndHref(namespaceOwner, prefix, href);
502
+ if (node != namespaceOwner) {
503
+ node.getOwnerDocument().renameNode(node, ns.getHref(), ns.getPrefix() + node.getLocalName());
504
+ }
505
+
506
+ return ns;
507
+ }
508
+
509
+ @JRubyMethod(name = {"attribute", "attr"})
510
+ public IRubyObject attribute(ThreadContext context, IRubyObject name){
511
+ NamedNodeMap attrs = this.node.getAttributes();
512
+ Node attr = attrs.getNamedItem(rubyStringToString(name));
513
+ if(attr == null) {
514
+ return context.getRuntime().getNil();
515
+ }
516
+ return getCachedNodeOrCreate(context.getRuntime(), attr);
517
+ }
518
+
519
+ @JRubyMethod
520
+ public IRubyObject attribute_nodes(ThreadContext context) {
521
+ NamedNodeMap nodeMap = this.node.getAttributes();
522
+
523
+ Ruby ruby = context.getRuntime();
524
+ if(nodeMap == null){
525
+ return ruby.newEmptyArray();
526
+ }
527
+
528
+ RubyArray attr = ruby.newArray();
529
+
530
+ for(int i = 0; i < nodeMap.getLength(); i++) {
531
+ if (!NokogiriHelpers.isNamespace(nodeMap.item(i))) {
532
+ attr.append(getCachedNodeOrCreate(context.getRuntime(), nodeMap.item(i)));
533
+ }
534
+ }
535
+
536
+ return attr;
537
+ }
538
+
539
+ @JRubyMethod
540
+ public IRubyObject attribute_with_ns(ThreadContext context, IRubyObject name, IRubyObject namespace) {
541
+ String namej = rubyStringToString(name);
542
+ String nsj = (namespace.isNil()) ? null : rubyStringToString(namespace);
543
+
544
+ Node el = this.node.getAttributes().getNamedItemNS(nsj, namej);
545
+
546
+ if(el == null) {
547
+ return context.getRuntime().getNil();
548
+ }
549
+ return NokogiriHelpers.getCachedNodeOrCreate(context.getRuntime(), el);
550
+ }
551
+
552
+ @JRubyMethod(name = "blank?")
553
+ public IRubyObject blank_p(ThreadContext context) {
554
+ String data = node.getTextContent();
555
+ if ("".equals(data.trim())) return context.getRuntime().getTrue();
556
+ return context.getRuntime().getFalse();
557
+ }
558
+
559
+ @JRubyMethod
560
+ public IRubyObject child(ThreadContext context) {
561
+ return getCachedNodeOrCreate(context.getRuntime(), node.getFirstChild());
562
+ }
563
+
564
+ @JRubyMethod
565
+ public IRubyObject children(ThreadContext context) {
566
+ XmlNodeSet xmlNodeSet = (XmlNodeSet) NokogiriService.XML_NODESET_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::NodeSet"));
567
+ xmlNodeSet.setNodeList(node.getChildNodes());
568
+ return xmlNodeSet;
569
+ }
570
+
571
+ @JRubyMethod
572
+ public IRubyObject first_element_child(ThreadContext context) {
573
+ List<Node> elementNodes = new ArrayList<Node>();
574
+ addElements(node, elementNodes, true);
575
+ if (elementNodes.size() == 0) return context.getRuntime().getNil();
576
+ return getCachedNodeOrCreate(context.getRuntime(), elementNodes.get(0));
577
+ }
578
+
579
+ @JRubyMethod
580
+ public IRubyObject last_element_child(ThreadContext context) {
581
+ List<Node> elementNodes = new ArrayList<Node>();
582
+ addElements(node, elementNodes, false);
583
+ if (elementNodes.size() == 0) return context.getRuntime().getNil();
584
+ return getCachedNodeOrCreate(context.getRuntime(), elementNodes.get(elementNodes.size()-1));
585
+ }
586
+
587
+ @JRubyMethod(name = {"element_children", "elements"})
588
+ public IRubyObject element_children(ThreadContext context) {
589
+ List<Node> elementNodes = new ArrayList<Node>();
590
+ addElements(node, elementNodes, false);
591
+ if (elementNodes.size() == 0) return XmlNodeSet.newEmptyNodeSet(context);
592
+ RubyArray array = NokogiriHelpers.nodeArrayToRubyArray(context.getRuntime(), elementNodes.toArray(new Node[0]));
593
+ XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(context, array);
594
+ return xmlNodeSet;
595
+ }
596
+
597
+ private void addElements(Node n, List<Node> nodes, boolean isFirstOnly) {
598
+ NodeList children = n.getChildNodes();
599
+ if (children.getLength() == 0) return;
600
+ for (int i=0; i< children.getLength(); i++) {
601
+ Node child = children.item(i);
602
+ if (child.getNodeType() == Node.ELEMENT_NODE) {
603
+ nodes.add(child);
604
+ if (isFirstOnly) return;
605
+ }
606
+ }
607
+ }
608
+
609
+ /**
610
+ * call-seq:
611
+ * compare(other)
612
+ *
613
+ * Compare this Node to +other+ with respect to their Document
614
+ */
615
+ @JRubyMethod(visibility=Visibility.PRIVATE)
616
+ public IRubyObject compare(ThreadContext context, IRubyObject other) {
617
+ if (!(other instanceof XmlNode)) {
618
+ return context.getRuntime().newFixnum(-2);
619
+ }
620
+
621
+ Node otherNode = asXmlNode(context, other).node;
622
+
623
+ // Do not touch this if, if it's not for a good reason.
624
+ if (node.getNodeType() == Node.DOCUMENT_NODE ||
625
+ otherNode.getNodeType() == Node.DOCUMENT_NODE) {
626
+ return context.getRuntime().newFixnum(-1);
627
+ }
628
+
629
+ try{
630
+ int res = node.compareDocumentPosition(otherNode);
631
+ if ((res & FIRST_PRECEDES_SECOND) == FIRST_PRECEDES_SECOND) {
632
+ return context.getRuntime().newFixnum(-1);
633
+ } else if ((res & SECOND_PRECEDES_FIRST) == SECOND_PRECEDES_FIRST) {
634
+ return context.getRuntime().newFixnum(1);
635
+ } else if (res == IDENTICAL_ELEMENTS) {
636
+ return context.getRuntime().newFixnum(0);
637
+ }
638
+
639
+ return context.getRuntime().newFixnum(-2);
640
+ } catch (Exception ex) {
641
+ return context.getRuntime().newFixnum(-2);
642
+ }
643
+ }
644
+
645
+ /**
646
+ * TODO: this is a stub implementation. It's not clear what
647
+ * 'in_context' is supposed to do. Also should take
648
+ * <code>options</code> into account.
649
+ */
650
+ @JRubyMethod(required = 2, visibility = Visibility.PRIVATE)
651
+ public IRubyObject in_context(ThreadContext context,
652
+ IRubyObject str,
653
+ IRubyObject options) {
654
+ RubyModule klass;
655
+ XmlDomParserContext ctx;
656
+ InputStream istream;
657
+ XmlDocument document;
658
+
659
+ IRubyObject d = document(context);
660
+ Ruby runtime = context.getRuntime();
661
+ if (d != null && d instanceof XmlDocument) {
662
+ document = (XmlDocument)d;
663
+ } else {
664
+ return runtime.getNil();
665
+ }
666
+
667
+ if (document instanceof HtmlDocument) {
668
+ klass = getNokogiriClass(runtime, "Nokogiri::HTML::Document");
669
+ ctx = new HtmlDomParserContext(runtime, options);
670
+ ((HtmlDomParserContext)ctx).enableDocumentFragment();
671
+ istream = new ByteArrayInputStream((rubyStringToString(str)).getBytes());
672
+ } else if (document instanceof XmlDocument) {
673
+ klass = getNokogiriClass(runtime, "Nokogiri::XML::Document");
674
+ ctx = new XmlDomParserContext(runtime, options);
675
+ String input = rubyStringToString(str);
676
+ istream = new ByteArrayInputStream(input.getBytes());
677
+ } else {
678
+ return runtime.getNil();
679
+ }
680
+
681
+ ctx.setInputSource(istream);
682
+ XmlDocument doc = ctx.parse(context, klass, runtime.getNil());
683
+
684
+ RubyArray documentErrors = getErrorArray(document);
685
+ RubyArray docErrors = getErrorArray(doc);
686
+ if (isErrorIncreated(documentErrors, docErrors)) {
687
+ for (int i = 0; i < docErrors.getLength(); i++) {
688
+ documentErrors.add(docErrors.get(i));
689
+ }
690
+ document.setInstanceVariable("@errors", documentErrors);
691
+ XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(context, RubyArray.newArray(runtime));
692
+ return xmlNodeSet;
693
+ }
694
+
695
+ // The first child might be document type node (dtd declaration).
696
+ // XmlNodeSet to be return should not have dtd decl in its list.
697
+ Node first;
698
+ if (doc.node.getFirstChild().getNodeType() == Node.DOCUMENT_TYPE_NODE) {
699
+ first = doc.node.getFirstChild().getNextSibling();
700
+ } else {
701
+ first = doc.node.getFirstChild();
702
+ }
703
+ RubyArray nodeArray = RubyArray.newArray(runtime);
704
+ nodeArray.add(NokogiriHelpers.getCachedNodeOrCreate(runtime, first));
705
+
706
+ NokogiriHelpers.nodeListToRubyArray(runtime, first.getChildNodes(), nodeArray);
707
+ XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(context, nodeArray);
708
+ return xmlNodeSet;
709
+ }
710
+
711
+ private RubyArray getErrorArray(XmlDocument document) {
712
+ IRubyObject obj = document.getInstanceVariable("@errors");
713
+ if (obj != null && obj instanceof RubyArray) {
714
+ return (RubyArray)obj;
715
+ }
716
+ return RubyArray.newArray(document.getRuntime());
717
+ }
718
+
719
+ private boolean isErrorIncreated(RubyArray baseErrors, RubyArray createdErrors) {
720
+ RubyBoolean result = baseErrors.compare(baseErrors.getRuntime().getCurrentContext(), "eql?", createdErrors, null);
721
+ return result.isFalse();
722
+ }
723
+
724
+ @JRubyMethod(name = {"content", "text", "inner_text"})
725
+ public IRubyObject content(ThreadContext context) {
726
+ if (content != null && content.isNil()) return content;
727
+ String textContent;
728
+ if (content != null) textContent = rubyStringToString(content);
729
+ else if (this instanceof XmlDocument) {
730
+ textContent = ((Document)this.node).getDocumentElement().getTextContent().trim();
731
+ } else {
732
+ textContent = this.node.getTextContent();
733
+ }
734
+ String decodedText = null;
735
+ if (textContent != null) decodedText = NokogiriHelpers.decodeJavaString(textContent);
736
+ return stringOrNil(context.getRuntime(), decodedText);
737
+ }
738
+
739
+ @JRubyMethod
740
+ public IRubyObject document(ThreadContext context) {
741
+ if (doc == null) {
742
+ doc = (XmlDocument) node.getOwnerDocument().getUserData(NokogiriHelpers.CACHED_NODE);
743
+ }
744
+ if (doc == null) {
745
+ doc = getCachedNodeOrCreate(context.getRuntime(), node.getOwnerDocument());
746
+ node.getOwnerDocument().setUserData(NokogiriHelpers.CACHED_NODE, doc, null);
747
+ }
748
+ return doc;
749
+ }
750
+
751
+ @JRubyMethod
752
+ public IRubyObject dup(ThreadContext context) {
753
+ return this.dup_implementation(context, true);
754
+ }
755
+
756
+ @JRubyMethod
757
+ public IRubyObject dup(ThreadContext context, IRubyObject depth) {
758
+ boolean deep = (Integer)depth.toJava(Integer.class) != 0;
759
+
760
+ return this.dup_implementation(context, deep);
761
+ }
762
+
763
+ protected IRubyObject dup_implementation(ThreadContext context, boolean deep) {
764
+ XmlNode clone;
765
+ try {
766
+ clone = (XmlNode) clone();
767
+ } catch (CloneNotSupportedException e) {
768
+ throw context.getRuntime().newRuntimeError(e.toString());
769
+ }
770
+ if (node == null) throw context.getRuntime().newRuntimeError("FFFFFFFFFUUUUUUU");
771
+ Node newNode = node.cloneNode(deep);
772
+ clone.node = newNode;
773
+ return clone;
774
+ }
775
+
776
+ public static IRubyObject encode_special_chars(ThreadContext context,
777
+ IRubyObject string) {
778
+ String s = rubyStringToString(string);
779
+ String enc = NokogiriHelpers.encodeJavaString(s);
780
+ return context.getRuntime().newString(enc);
781
+ }
782
+
783
+ /**
784
+ * Instance method version of the above static method.
785
+ */
786
+ @JRubyMethod(name="encode_special_chars")
787
+ public IRubyObject i_encode_special_chars(ThreadContext context,
788
+ IRubyObject string) {
789
+ return encode_special_chars(context, string);
790
+ }
791
+
792
+ /**
793
+ * Get the attribute at the given key, <code>key</code>.
794
+ * Assumes that this node has attributes (i.e. that key? returned
795
+ * true). Overridden in XmlElement.
796
+ */
797
+ @JRubyMethod(visibility = Visibility.PRIVATE)
798
+ public IRubyObject get(ThreadContext context, IRubyObject key) {
799
+ return context.getRuntime().getNil();
800
+ }
801
+
802
+ /**
803
+ * Returns the owner document, checking if this node is the
804
+ * document, or returns null if there is no owner.
805
+ */
806
+ protected Document getOwnerDocument() {
807
+ if (node.getNodeType() == Node.DOCUMENT_NODE) {
808
+ return (Document) node;
809
+ } else {
810
+ return node.getOwnerDocument();
811
+ }
812
+ }
813
+
814
+ @JRubyMethod
815
+ public IRubyObject internal_subset(ThreadContext context) {
816
+ Document document = getOwnerDocument();
817
+
818
+ if(document == null) {
819
+ return context.getRuntime().getNil();
820
+ }
821
+
822
+ XmlDocument xdoc =
823
+ (XmlDocument) getCachedNodeOrCreate(context.getRuntime(), document);
824
+ IRubyObject xdtd = xdoc.getInternalSubset(context);
825
+ return xdtd;
826
+ }
827
+
828
+ @JRubyMethod
829
+ public IRubyObject create_internal_subset(ThreadContext context,
830
+ IRubyObject name,
831
+ IRubyObject external_id,
832
+ IRubyObject system_id) {
833
+ IRubyObject subset = internal_subset(context);
834
+ if (!subset.isNil()) {
835
+ throw context.getRuntime()
836
+ .newRuntimeError("Document already has internal subset");
837
+ }
838
+
839
+ Document document = getOwnerDocument();
840
+ if(document == null) {
841
+ return context.getRuntime().getNil();
842
+ }
843
+
844
+ XmlDocument xdoc =
845
+ (XmlDocument) getCachedNodeOrCreate(context.getRuntime(), document);
846
+ IRubyObject xdtd = xdoc.createInternalSubset(context, name,
847
+ external_id, system_id);
848
+ return xdtd;
849
+ }
850
+
851
+ @JRubyMethod
852
+ public IRubyObject external_subset(ThreadContext context) {
853
+ Document document = getOwnerDocument();
854
+
855
+ if (document == null) {
856
+ return context.getRuntime().getNil();
857
+ }
858
+
859
+ XmlDocument xdoc =
860
+ (XmlDocument) getCachedNodeOrCreate(context.getRuntime(), document);
861
+ IRubyObject xdtd = xdoc.getExternalSubset(context);
862
+ return xdtd;
863
+ }
864
+
865
+ @JRubyMethod
866
+ public IRubyObject create_external_subset(ThreadContext context,
867
+ IRubyObject name,
868
+ IRubyObject external_id,
869
+ IRubyObject system_id) {
870
+ IRubyObject subset = external_subset(context);
871
+ if (!subset.isNil()) {
872
+ throw context.getRuntime()
873
+ .newRuntimeError("Document already has external subset");
874
+ }
875
+
876
+ Document document = getOwnerDocument();
877
+ if(document == null) {
878
+ return context.getRuntime().getNil();
879
+ }
880
+ XmlDocument xdoc = (XmlDocument) getCachedNodeOrCreate(context.getRuntime(), document);
881
+ IRubyObject xdtd = xdoc.createExternalSubset(context, name, external_id, system_id);
882
+ return xdtd;
883
+ }
884
+
885
+ /**
886
+ * Test if this node has an attribute named <code>rbkey</code>.
887
+ * Overridden in XmlElement.
888
+ */
889
+ @JRubyMethod(name = {"key?", "has_attribute?"})
890
+ public IRubyObject key_p(ThreadContext context, IRubyObject rbkey) {
891
+ return context.getRuntime().getNil();
892
+ }
893
+
894
+ @JRubyMethod
895
+ public IRubyObject namespace(ThreadContext context){
896
+ XmlDocument xmlDocument = (XmlDocument) doc;
897
+ NokogiriNamespaceCache nsCache = xmlDocument.getNamespaceCache();
898
+ String prefix = node.getPrefix();
899
+ XmlNamespace namespace = nsCache.get(prefix == null ? "" : prefix, node.getNamespaceURI());
900
+ if (namespace == null || ((XmlNamespace) namespace).isEmpty()) {
901
+ return context.getRuntime().getNil();
902
+ }
903
+
904
+ return namespace;
905
+ }
906
+
907
+ /**
908
+ * Return an array of XmlNamespace nodes based on the attributes
909
+ * of this node.
910
+ */
911
+ @JRubyMethod
912
+ public IRubyObject namespace_definitions(ThreadContext context) {
913
+ // don't use namespace_definitions cache anymore since
914
+ // namespaces might be deleted. Reflecting the result of
915
+ // namesapce removals is complicated, so the cache might not be
916
+ // updated.
917
+ Ruby ruby = context.getRuntime();
918
+ RubyArray namespace_definitions = ruby.newArray();
919
+ if (doc == null) return namespace_definitions;
920
+ List<XmlNamespace> namespaces = ((XmlDocument)doc).getNamespaceCache().get(node);
921
+ for (XmlNamespace namespace : namespaces) {
922
+ ((RubyArray)namespace_definitions).append(namespace);
923
+ }
924
+
925
+ return (RubyArray) namespace_definitions;
926
+ }
927
+
928
+ /**
929
+ * Return an array of XmlNamespace nodes defined on this node and
930
+ * on any ancestor node.
931
+ */
932
+ @JRubyMethod
933
+ public IRubyObject namespace_scopes(ThreadContext context) {
934
+ RubyArray parentNamespaces;
935
+ RubyArray namespaces = (RubyArray) namespace_definitions(context);
936
+
937
+ IRubyObject parent = parent(context);
938
+ if (!parent.isNil()) {
939
+ parentNamespaces = (RubyArray)
940
+ ((XmlNode) parent).namespace_scopes(context);
941
+ } else {
942
+ parentNamespaces = getRuntime().newEmptyArray();
943
+ }
944
+
945
+ return parentNamespaces.op_plus(namespaces);
946
+ }
947
+
948
+ @JRubyMethod(name="namespaced_key?")
949
+ public IRubyObject namespaced_key_p(ThreadContext context, IRubyObject elementLName, IRubyObject namespaceUri) {
950
+ return this.attribute_with_ns(context, elementLName, namespaceUri).isNil() ?
951
+ context.getRuntime().getFalse() : context.getRuntime().getTrue();
952
+ }
953
+
954
+ protected void setContent(IRubyObject content) {
955
+ this.content = content;
956
+ this.node.setTextContent(rubyStringToString(content));
957
+ }
958
+
959
+ private void setContent(String content) {
960
+ node.setTextContent(content);
961
+ this.content = null; // clear cache
962
+ }
963
+
964
+ @JRubyMethod(name = "native_content=", visibility = Visibility.PRIVATE)
965
+ public IRubyObject native_content_set(ThreadContext context, IRubyObject content) {
966
+ setContent(content);
967
+ return content;
968
+ }
969
+
970
+ /**
971
+ * @param args {IRubyObject io,
972
+ * IRubyObject encoding,
973
+ * IRubyObject indentString,
974
+ * IRubyObject options}
975
+ */
976
+ @JRubyMethod(required=4, visibility=Visibility.PRIVATE)
977
+ public IRubyObject native_write_to(ThreadContext context,
978
+ IRubyObject[] args) {
979
+
980
+ IRubyObject io = args[0];
981
+ IRubyObject encoding = args[1];
982
+ IRubyObject indentString = args[2];
983
+ IRubyObject options = args[3];
984
+
985
+ String encString = encoding.isNil() ? null : rubyStringToString(encoding);
986
+
987
+ SaveContextVisitor visitor =
988
+ new SaveContextVisitor((Integer)options.toJava(Integer.class), rubyStringToString(indentString), encString,
989
+ isHtmlDoc(context), isFragment());
990
+ accept(context, visitor);
991
+ IRubyObject rubyString = stringOrNil(context.getRuntime(), visitor.toString());
992
+ RuntimeHelpers.invoke(context, io, "write", rubyString);
993
+
994
+ return io;
995
+ }
996
+
997
+ private boolean isHtmlDoc(ThreadContext context) {
998
+ return document(context).getMetaClass().isKindOfModule(getNokogiriClass(context.getRuntime(), "Nokogiri::HTML::Document"));
999
+ }
1000
+
1001
+ private boolean isFragment() {
1002
+ if (node instanceof DocumentFragment) return true;
1003
+ if (node.getParentNode() != null && node.getParentNode() instanceof DocumentFragment) return true;
1004
+ return false;
1005
+ }
1006
+
1007
+ @JRubyMethod(name = {"next_sibling", "next"})
1008
+ public IRubyObject next_sibling(ThreadContext context) {
1009
+ return getCachedNodeOrCreate(context.getRuntime(), node.getNextSibling());
1010
+ }
1011
+
1012
+ @JRubyMethod(name = {"previous_sibling", "previous"})
1013
+ public IRubyObject previous_sibling(ThreadContext context) {
1014
+ return getCachedNodeOrCreate(context.getRuntime(), node.getPreviousSibling());
1015
+ }
1016
+
1017
+ @JRubyMethod(meta = true, rest = true)
1018
+ public static IRubyObject new_from_str(ThreadContext context,
1019
+ IRubyObject cls,
1020
+ IRubyObject[] args) {
1021
+ XmlDocument doc = (XmlDocument) XmlDocument.read_memory(context, args);
1022
+ return doc.root(context);
1023
+ }
1024
+
1025
+ @JRubyMethod(name = {"node_name", "name"})
1026
+ public IRubyObject node_name(ThreadContext context) {
1027
+ return getNodeName(context);
1028
+ }
1029
+
1030
+ @JRubyMethod(name = {"node_name=", "name="})
1031
+ public IRubyObject node_name_set(ThreadContext context, IRubyObject nodeName) {
1032
+ String newName = rubyStringToString(nodeName);
1033
+ getOwnerDocument().renameNode(node, null, newName);
1034
+ setName(nodeName);
1035
+ return this;
1036
+ }
1037
+
1038
+ @JRubyMethod(name = {"[]=", "set_attribute"})
1039
+ public IRubyObject op_aset(ThreadContext context, IRubyObject index, IRubyObject val) {
1040
+ return val;
1041
+ }
1042
+
1043
+ @JRubyMethod
1044
+ public IRubyObject parent(ThreadContext context) {
1045
+ /*
1046
+ * Check if this node is the root node of the document.
1047
+ * If so, parent is the document.
1048
+ */
1049
+ if (node.getOwnerDocument() != null &&
1050
+ node.getOwnerDocument().getDocumentElement() == node) {
1051
+ return document(context);
1052
+ } else {
1053
+ return getCachedNodeOrCreate(context.getRuntime(), node.getParentNode());
1054
+ }
1055
+ }
1056
+
1057
+ @JRubyMethod
1058
+ public IRubyObject path(ThreadContext context) {
1059
+ return RubyString.newString(context.getRuntime(), NokogiriHelpers.getNodeCompletePath(this.node));
1060
+ }
1061
+
1062
+ @JRubyMethod
1063
+ public IRubyObject pointer_id(ThreadContext context) {
1064
+ return RubyFixnum.newFixnum(context.getRuntime(), this.node.hashCode());
1065
+ }
1066
+
1067
+ @JRubyMethod(name = {"remove_attribute", "delete"})
1068
+ public IRubyObject remove_attribute(ThreadContext context, IRubyObject name) {
1069
+ return this;
1070
+ }
1071
+
1072
+ @JRubyMethod(visibility=Visibility.PRIVATE)
1073
+ public IRubyObject set_namespace(ThreadContext context, IRubyObject namespace) {
1074
+ if (namespace.isNil()) {
1075
+ if (doc != null) {
1076
+ Node n = node;
1077
+ String prefix = n.getPrefix();
1078
+ String href = n.getNamespaceURI();
1079
+ ((XmlDocument)doc).getNamespaceCache().remove(prefix == null ? "" : prefix, href);
1080
+ n.getOwnerDocument().renameNode(n, null, n.getNodeName());
1081
+ }
1082
+ } else {
1083
+ XmlNamespace ns = (XmlNamespace) namespace;
1084
+ String prefix = rubyStringToString(ns.prefix(context));
1085
+ String href = rubyStringToString(ns.href(context));
1086
+
1087
+ // Assigning node = ...renameNode() or not seems to make no
1088
+ // difference. Why not? -pmahoney
1089
+ node = node.getOwnerDocument().renameNode(node, href, NokogiriHelpers.newQName(prefix, node));
1090
+ }
1091
+
1092
+ return this;
1093
+ }
1094
+
1095
+ @JRubyMethod(name = {"unlink", "remove"})
1096
+ public IRubyObject unlink(ThreadContext context) {
1097
+ if(node.getParentNode() == null) {
1098
+ throw context.getRuntime().newRuntimeError("TYPE: " + node.getNodeType()+ " PARENT NULL");
1099
+ } else {
1100
+ node.getParentNode().removeChild(node);
1101
+ }
1102
+
1103
+ return this;
1104
+ }
1105
+
1106
+ /**
1107
+ * The C-library simply returns libxml2 magic numbers. Here we
1108
+ * convert Java Xml nodes to the appropriate constant defined in
1109
+ * xml/node.rb.
1110
+ */
1111
+ @JRubyMethod(name = {"node_type", "type"})
1112
+ public IRubyObject node_type(ThreadContext context) {
1113
+ String type;
1114
+ switch (node.getNodeType()) {
1115
+ case Node.ELEMENT_NODE:
1116
+ if (this instanceof XmlElementDecl)
1117
+ type = "ELEMENT_DECL";
1118
+ else if (this instanceof XmlAttributeDecl)
1119
+ type = "ATTRIBUTE_DECL";
1120
+ else if (this instanceof XmlEntityDecl)
1121
+ type = "ENTITY_DECL";
1122
+ else
1123
+ type = "ELEMENT_NODE";
1124
+ break;
1125
+ case Node.ATTRIBUTE_NODE: type = "ATTRIBUTE_NODE"; break;
1126
+ case Node.TEXT_NODE: type = "TEXT_NODE"; break;
1127
+ case Node.CDATA_SECTION_NODE: type = "CDATA_SECTION_NODE"; break;
1128
+ case Node.ENTITY_REFERENCE_NODE: type = "ENTITY_REF_NODE"; break;
1129
+ case Node.ENTITY_NODE: type = "ENTITY_NODE"; break;
1130
+ case Node.PROCESSING_INSTRUCTION_NODE: type = "PI_NODE"; break;
1131
+ case Node.COMMENT_NODE: type = "COMMENT_NODE"; break;
1132
+ case Node.DOCUMENT_NODE:
1133
+ if (this instanceof HtmlDocument)
1134
+ type = "HTML_DOCUMENT_NODE";
1135
+ else
1136
+ type = "DOCUMENT_NODE";
1137
+ break;
1138
+ case Node.DOCUMENT_TYPE_NODE: type = "DOCUMENT_TYPE_NODE"; break;
1139
+ case Node.DOCUMENT_FRAGMENT_NODE: type = "DOCUMENT_FRAG_NODE"; break;
1140
+ case Node.NOTATION_NODE: type = "NOTATION_NODE"; break;
1141
+ default:
1142
+ return context.getRuntime().newFixnum(0);
1143
+ }
1144
+
1145
+ return getNokogiriClass(context.getRuntime(), "Nokogiri::XML::Node").getConstant(type);
1146
+ }
1147
+
1148
+ @JRubyMethod
1149
+ public IRubyObject line(ThreadContext context) {
1150
+ Node root = getOwnerDocument();
1151
+ int[] counter = new int[1];
1152
+ count(root, counter);
1153
+ return RubyFixnum.newFixnum(context.getRuntime(), counter[0]+1);
1154
+ }
1155
+
1156
+ private boolean count(Node node, int[] counter) {
1157
+ if (node == this.node) {
1158
+ return true;
1159
+ }
1160
+ NodeList list = node.getChildNodes();
1161
+ for (int i=0; i<list.getLength(); i++) {
1162
+ Node n = list.item(i);
1163
+ if (n instanceof Text
1164
+ && ((Text)n).getData().contains("\n")) {
1165
+ counter[0] += 1;
1166
+ }
1167
+ if (count(n, counter)) return true;
1168
+ }
1169
+ return false;
1170
+ }
1171
+
1172
+ @JRubyMethod
1173
+ public IRubyObject next_element(ThreadContext context) {
1174
+ Node nextNode = node.getNextSibling();
1175
+ Ruby ruby = context.getRuntime();
1176
+ if (nextNode == null) return ruby.getNil();
1177
+ if (nextNode instanceof Element) {
1178
+ return getCachedNodeOrCreate(context.getRuntime(), nextNode);
1179
+ }
1180
+ Node deeper = nextNode.getNextSibling();
1181
+ if (deeper == null) return ruby.getNil();
1182
+ return getCachedNodeOrCreate(context.getRuntime(), deeper);
1183
+ }
1184
+
1185
+ @JRubyMethod
1186
+ public IRubyObject previous_element(ThreadContext context) {
1187
+ Node prevNode = node.getPreviousSibling();
1188
+ Ruby ruby = context.getRuntime();
1189
+ if (prevNode == null) return ruby.getNil();
1190
+ if (prevNode instanceof Element) {
1191
+ return getCachedNodeOrCreate(context.getRuntime(), prevNode);
1192
+ }
1193
+ Node shallower = prevNode.getPreviousSibling();
1194
+ if (shallower == null) return ruby.getNil();
1195
+ return getCachedNodeOrCreate(context.getRuntime(), shallower);
1196
+ }
1197
+
1198
+ protected enum AdoptScheme {
1199
+ CHILD, PREV_SIBLING, NEXT_SIBLING, REPLACEMENT;
1200
+ }
1201
+
1202
+ /**
1203
+ * Adopt XmlNode <code>other</code> into the document of
1204
+ * <code>this</code> using the specified scheme.
1205
+ */
1206
+ protected IRubyObject adoptAs(ThreadContext context, AdoptScheme scheme,
1207
+ IRubyObject other_) {
1208
+ XmlNode other = asXmlNode(context, other_);
1209
+ // this.doc might be null since this node can be empty node.
1210
+ if (this.doc != null) {
1211
+ other.setDocument(context, this.doc);
1212
+ }
1213
+ IRubyObject nodeOrTags = other;
1214
+ Node thisNode = node;
1215
+ Node otherNode = other.node;
1216
+
1217
+ try {
1218
+ Document doc = thisNode.getOwnerDocument();
1219
+ if (doc != null && doc != otherNode.getOwnerDocument()) {
1220
+ Node ret = doc.adoptNode(otherNode);
1221
+ if (ret == null) {
1222
+ throw context.getRuntime().newRuntimeError("Failed to take ownership of node");
1223
+ }
1224
+ }
1225
+
1226
+ Node parent = thisNode.getParentNode();
1227
+
1228
+ switch (scheme) {
1229
+ case CHILD:
1230
+ Node[] children = adoptAsChild(context, thisNode, otherNode);
1231
+ if (children.length == 1 && otherNode == children[0]) {
1232
+ break;
1233
+ } else {
1234
+ nodeOrTags = nodeArrayToRubyArray(context.getRuntime(), children);
1235
+ }
1236
+ break;
1237
+ case PREV_SIBLING:
1238
+ adoptAsPrevSibling(context, parent, thisNode, otherNode);
1239
+ break;
1240
+ case NEXT_SIBLING:
1241
+ adoptAsNextSibling(context, parent, thisNode, otherNode);
1242
+ break;
1243
+ case REPLACEMENT:
1244
+ adoptAsReplacement(context, parent, thisNode, otherNode);
1245
+ break;
1246
+ }
1247
+ } catch (Exception e) {
1248
+ throw context.getRuntime().newRuntimeError(e.toString());
1249
+ }
1250
+
1251
+ if (otherNode.getNodeType() == Node.TEXT_NODE) {
1252
+ coalesceTextNodes(context, other);
1253
+ }
1254
+
1255
+ relink_namespace(context);
1256
+ // post_add_child(context, this, other);
1257
+
1258
+ return nodeOrTags;
1259
+ }
1260
+
1261
+ protected Node[] adoptAsChild(ThreadContext context, Node parent,
1262
+ Node otherNode) {
1263
+ /*
1264
+ * This is a bit of a hack. C-Nokogiri allows adding a bare
1265
+ * text node as the root element. Java (and XML spec?) does
1266
+ * not. So we wrap the text node in an element.
1267
+ */
1268
+ if (parent.getNodeType() == Node.DOCUMENT_NODE &&
1269
+ otherNode.getNodeType() == Node.TEXT_NODE) {
1270
+ Element e = ((Document)parent).createElement("text");
1271
+ e.appendChild(otherNode);
1272
+ otherNode = e;
1273
+ }
1274
+ addNamespaceURIIfNeeded(otherNode);
1275
+ parent.appendChild(otherNode);
1276
+ Node[] nodes = new Node[1];
1277
+ nodes[0] = otherNode;
1278
+ return nodes;
1279
+ }
1280
+
1281
+ private void addNamespaceURIIfNeeded(Node child) {
1282
+ if (this instanceof XmlDocumentFragment && ((XmlDocumentFragment)this).getFragmentContext() != null) {
1283
+ XmlElement fragmentContext = ((XmlDocumentFragment)this).getFragmentContext();
1284
+ String namespace_uri = fragmentContext.node.getNamespaceURI();
1285
+ if (namespace_uri != null && namespace_uri.length() > 0) {
1286
+ node.getOwnerDocument().renameNode(child, namespace_uri, child.getNodeName());
1287
+ }
1288
+ }
1289
+ }
1290
+
1291
+ protected void adoptAsPrevSibling(ThreadContext context,
1292
+ Node parent,
1293
+ Node thisNode, Node otherNode) {
1294
+ if (parent == null) {
1295
+ /* I'm not sure what do do here... A node with no
1296
+ * parent can't exactly have a 'sibling', so we make
1297
+ * otherNode parentless also. */
1298
+ if (otherNode.getParentNode() != null)
1299
+ otherNode.getParentNode().removeChild(otherNode);
1300
+
1301
+ return;
1302
+ }
1303
+
1304
+ parent.insertBefore(otherNode, thisNode);
1305
+ }
1306
+
1307
+ protected void adoptAsNextSibling(ThreadContext context,
1308
+ Node parent,
1309
+ Node thisNode, Node otherNode) {
1310
+ if (parent == null) {
1311
+ /* I'm not sure what do do here... A node with no
1312
+ * parent can't exactly have a 'sibling', so we make
1313
+ * otherNode parentless also. */
1314
+ if (otherNode.getParentNode() != null)
1315
+ otherNode.getParentNode().removeChild(otherNode);
1316
+
1317
+ return;
1318
+ }
1319
+
1320
+ Node nextSib = thisNode.getNextSibling();
1321
+ if (nextSib != null) {
1322
+ parent.insertBefore(otherNode, nextSib);
1323
+ } else {
1324
+ parent.appendChild(otherNode);
1325
+ }
1326
+ }
1327
+
1328
+ protected void adoptAsReplacement(ThreadContext context,
1329
+ Node parentNode,
1330
+ Node thisNode, Node otherNode) {
1331
+ if (parentNode == null) {
1332
+ /* nothing to replace? */
1333
+ return;
1334
+ }
1335
+
1336
+ try {
1337
+ parentNode.replaceChild(otherNode, thisNode);
1338
+ if (otherNode.getNodeType() != Node.TEXT_NODE) {
1339
+ otherNode.getOwnerDocument().renameNode(otherNode, thisNode.getNamespaceURI(), otherNode.getNodeName());
1340
+ }
1341
+ } catch (Exception e) {
1342
+ String prefix = "could not replace child: ";
1343
+ throw context.getRuntime().newRuntimeError(prefix + e.toString());
1344
+ }
1345
+ }
1346
+
1347
+ /**
1348
+ * Add <code>other</code> as a child of <code>this</code>.
1349
+ */
1350
+ @JRubyMethod(visibility=Visibility.PRIVATE)
1351
+ public IRubyObject add_child_node(ThreadContext context, IRubyObject other) {
1352
+ return adoptAs(context, AdoptScheme.CHILD, other);
1353
+ }
1354
+
1355
+ /**
1356
+ * Replace <code>this</code> with <code>other</code>.
1357
+ */
1358
+ @JRubyMethod(visibility=Visibility.PRIVATE)
1359
+ public IRubyObject replace_node(ThreadContext context, IRubyObject other) {
1360
+ return adoptAs(context, AdoptScheme.REPLACEMENT, other);
1361
+ }
1362
+
1363
+ /**
1364
+ * Add <code>other</code> as a sibling before <code>this</code>.
1365
+ */
1366
+ @JRubyMethod(visibility=Visibility.PRIVATE)
1367
+ public IRubyObject add_previous_sibling_node(ThreadContext context, IRubyObject other) {
1368
+ return adoptAs(context, AdoptScheme.PREV_SIBLING, other);
1369
+ }
1370
+
1371
+ /**
1372
+ * Add <code>other</code> as a sibling after <code>this</code>.
1373
+ */
1374
+ @JRubyMethod(visibility=Visibility.PRIVATE)
1375
+ public IRubyObject add_next_sibling_node(ThreadContext context, IRubyObject other) {
1376
+ return adoptAs(context, AdoptScheme.NEXT_SIBLING, other);
1377
+ }
1378
+ }