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,639 @@
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.internals;
34
+
35
+ import java.io.File;
36
+ import java.io.UnsupportedEncodingException;
37
+ import java.nio.ByteBuffer;
38
+ import java.nio.charset.Charset;
39
+ import java.util.regex.Matcher;
40
+ import java.util.regex.Pattern;
41
+
42
+ import nokogiri.NokogiriService;
43
+ import nokogiri.XmlAttr;
44
+ import nokogiri.XmlCdata;
45
+ import nokogiri.XmlComment;
46
+ import nokogiri.XmlDocument;
47
+ import nokogiri.XmlDtd;
48
+ import nokogiri.XmlElement;
49
+ import nokogiri.XmlEntityReference;
50
+ import nokogiri.XmlNamespace;
51
+ import nokogiri.XmlNode;
52
+ import nokogiri.XmlProcessingInstruction;
53
+ import nokogiri.XmlText;
54
+
55
+ import org.jruby.Ruby;
56
+ import org.jruby.RubyArray;
57
+ import org.jruby.RubyClass;
58
+ import org.jruby.RubyEncoding;
59
+ import org.jruby.RubyString;
60
+ import org.jruby.runtime.builtin.IRubyObject;
61
+ import org.jruby.util.ByteList;
62
+ import org.w3c.dom.Attr;
63
+ import org.w3c.dom.NamedNodeMap;
64
+ import org.w3c.dom.Node;
65
+ import org.w3c.dom.NodeList;
66
+
67
+ /**
68
+ * A class for various utility methods.
69
+ *
70
+ * @author serabe
71
+ * @author Patrick Mahoney <pat@polycrystal.org>
72
+ * @author Yoko Harada <yokolet@gmail.com>
73
+ */
74
+ public class NokogiriHelpers {
75
+ public static final String CACHED_NODE = "NOKOGIRI_CACHED_NODE";
76
+ public static final String VALID_ROOT_NODE = "NOKOGIRI_VALIDE_ROOT_NODE";
77
+
78
+ public static XmlNode getCachedNode(Node node) {
79
+ return (XmlNode) node.getUserData(CACHED_NODE);
80
+ }
81
+
82
+ /**
83
+ * Get the XmlNode associated with the underlying
84
+ * <code>node</code>. Creates a new XmlNode (or appropriate subclass)
85
+ * or XmlNamespace wrapping <code>node</code> if there is no cached
86
+ * value.
87
+ */
88
+ public static IRubyObject getCachedNodeOrCreate(Ruby ruby, Node node) {
89
+ if(node == null) return ruby.getNil();
90
+ if (node.getNodeType() == Node.ATTRIBUTE_NODE && isNamespace(node.getNodeName())) {
91
+ XmlDocument xmlDocument = (XmlDocument)node.getOwnerDocument().getUserData(CACHED_NODE);
92
+ String prefix = getLocalNameForNamespace(((Attr)node).getName());
93
+ prefix = prefix != null ? prefix : "";
94
+ String href = ((Attr)node).getValue();
95
+ XmlNamespace xmlNamespace = xmlDocument.getNamespaceCache().get(prefix, href);
96
+ if (xmlNamespace != null) return xmlNamespace;
97
+ else return XmlNamespace.createFromAttr(ruby, (Attr)node);
98
+ }
99
+ XmlNode xmlNode = getCachedNode(node);
100
+ if(xmlNode == null) {
101
+ xmlNode = (XmlNode)constructNode(ruby, node);
102
+ node.setUserData(CACHED_NODE, xmlNode, null);
103
+ }
104
+ return xmlNode;
105
+ }
106
+
107
+ /**
108
+ * Construct a new XmlNode wrapping <code>node</code>. The proper
109
+ * subclass of XmlNode is chosen based on the type of
110
+ * <code>node</code>.
111
+ */
112
+ public static IRubyObject constructNode(Ruby runtime, Node node) {
113
+ if (node == null) return runtime.getNil();
114
+ // this is slow; need a way to cache nokogiri classes/modules somewhere
115
+ switch (node.getNodeType()) {
116
+ case Node.ELEMENT_NODE:
117
+ XmlElement xmlElement = (XmlElement) NokogiriService.XML_ELEMENT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Element"));
118
+ xmlElement.setNode(runtime.getCurrentContext(), node);
119
+ return xmlElement;
120
+ case Node.ATTRIBUTE_NODE:
121
+ XmlAttr xmlAttr = (XmlAttr) NokogiriService.XML_ATTR_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Attr"));
122
+ xmlAttr.setNode(runtime.getCurrentContext(), node);
123
+ return xmlAttr;
124
+ case Node.TEXT_NODE:
125
+ XmlText xmlText = (XmlText) NokogiriService.XML_TEXT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Text"));
126
+ xmlText.setNode(runtime.getCurrentContext(), node);
127
+ return xmlText;
128
+ case Node.COMMENT_NODE:
129
+ XmlComment xmlComment = (XmlComment) NokogiriService.XML_COMMENT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Comment"));
130
+ xmlComment.setNode(runtime.getCurrentContext(), node);
131
+ return xmlComment;
132
+ case Node.ENTITY_NODE:
133
+ return new XmlNode(runtime, getNokogiriClass(runtime, "Nokogiri::XML::EntityDecl"), node);
134
+ case Node.ENTITY_REFERENCE_NODE:
135
+ XmlEntityReference xmlEntityRef = (XmlEntityReference) NokogiriService.XML_ENTITY_REFERENCE_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::EntityReference"));
136
+ xmlEntityRef.setNode(runtime.getCurrentContext(), node);
137
+ return xmlEntityRef;
138
+ case Node.PROCESSING_INSTRUCTION_NODE:
139
+ XmlProcessingInstruction xmlProcessingInstruction = (XmlProcessingInstruction) NokogiriService.XML_PROCESSING_INSTRUCTION_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::ProcessingInstruction"));
140
+ xmlProcessingInstruction.setNode(runtime.getCurrentContext(), node);
141
+ return xmlProcessingInstruction;
142
+ case Node.CDATA_SECTION_NODE:
143
+ XmlCdata xmlCdata = (XmlCdata) NokogiriService.XML_CDATA_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::CDATA"));
144
+ xmlCdata.setNode(runtime.getCurrentContext(), node);
145
+ return xmlCdata;
146
+ case Node.DOCUMENT_NODE:
147
+ XmlDocument xmlDocument = (XmlDocument) NokogiriService.XML_DOCUMENT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Document"));
148
+ xmlDocument.setNode(runtime.getCurrentContext(), node);
149
+ return xmlDocument;
150
+ case Node.DOCUMENT_TYPE_NODE:
151
+ XmlDtd xmlDtd = (XmlDtd) NokogiriService.XML_DTD_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::DTD"));
152
+ xmlDtd.setNode(runtime, node);
153
+ return xmlDtd;
154
+ default:
155
+ XmlNode xmlNode = (XmlNode) NokogiriService.XML_NODE_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Node"));
156
+ xmlNode.setNode(runtime.getCurrentContext(), node);
157
+ return xmlNode;
158
+ }
159
+ }
160
+
161
+ public static RubyClass getNokogiriClass(Ruby ruby, String name) {
162
+ return NokogiriService.nokogiriClassCache.get(name);
163
+ }
164
+
165
+ public static IRubyObject stringOrNil(Ruby runtime, String s) {
166
+ if (s == null) return runtime.getNil();
167
+ return RubyString.newString(runtime, s);
168
+ }
169
+
170
+ public static IRubyObject stringOrBlank(Ruby runtime, String s) {
171
+ if (s == null) return runtime.newString();
172
+ return RubyString.newString(runtime, s);
173
+ }
174
+
175
+ /**
176
+ * Convert <code>s</code> to a RubyString, or if s is null or
177
+ * empty return RubyNil.
178
+ */
179
+ public static IRubyObject nonEmptyStringOrNil(Ruby runtime, String s) {
180
+ if (s == null || s.length() == 0) return runtime.getNil();
181
+ return RubyString.newString(runtime, s);
182
+ }
183
+
184
+ /**
185
+ * Return the prefix of a qualified name like "prefix:local".
186
+ * Returns null if there is no prefix.
187
+ */
188
+ public static String getPrefix(String qName) {
189
+ if (qName == null) return null;
190
+
191
+ int pos = qName.indexOf(':');
192
+ if (pos > 0)
193
+ return qName.substring(0, pos);
194
+ else
195
+ return null;
196
+ }
197
+
198
+ /**
199
+ * Return the local part of a qualified name like "prefix:local".
200
+ * Returns <code>qName</code> if there is no prefix.
201
+ */
202
+ public static String getLocalPart(String qName) {
203
+ if (qName == null) return null;
204
+
205
+ int pos = qName.indexOf(':');
206
+ if (pos > 0)
207
+ return qName.substring(pos + 1);
208
+ else
209
+ return qName;
210
+ }
211
+
212
+ public static String getLocalNameForNamespace(String name) {
213
+ String localName = getLocalPart(name);
214
+ return ("xmlns".equals(localName)) ? null : localName;
215
+ }
216
+
217
+ private static Charset utf8 = null;
218
+
219
+ private static Charset getCharsetUTF8() {
220
+ if (utf8 == null) utf8 = Charset.forName("UTF-8");
221
+ return utf8;
222
+ }
223
+
224
+ /**
225
+ * Converts a RubyString in to a Java String. Assumes the
226
+ * RubyString is encoded as UTF-8. This is generally the case for
227
+ * RubyStrings created with getRuntime().newString("java string").
228
+ * It also seems to be the case for strings created within Ruby
229
+ * where $KCODE has not been set.
230
+ *
231
+ * Note that RubyString#toString() decodes the string data as
232
+ * ISO-8859-1 (See org.jruby.util.ByteList.java). This is not
233
+ * what you want if you have any multibyte characters in your
234
+ * UTF-8 string.
235
+ *
236
+ * FIXME: This really needs to be more robust in terms of
237
+ * detecting the encoding and properly converting to a Java
238
+ * String. It's unfortunate that RubyString#toString() doesn't do
239
+ * this for us.
240
+ */
241
+ public static String rubyStringToString(IRubyObject str) {
242
+ //return rubyStringToString(str.convertToString());
243
+ return toJavaString(str.convertToString());
244
+ }
245
+
246
+ private static String toJavaString(RubyString str) {
247
+ ByteList value = str.getByteList();
248
+ try {
249
+ if (str.getRuntime().is1_9()) {
250
+ return new String(value.getUnsafeBytes(), value.begin(), value.length(), str.getEncoding().toString());
251
+ }
252
+ return RubyEncoding.decodeUTF8(value.getUnsafeBytes(), value.begin(), value.length());
253
+ } catch (UnsupportedEncodingException uee) {
254
+ return str.toString();
255
+ }
256
+ }
257
+
258
+ public static String rubyStringToString(RubyString str) {
259
+ ByteList byteList = str.getByteList();
260
+ byte[] data = byteList.unsafeBytes();
261
+ int offset = byteList.begin();
262
+ int len = byteList.length();
263
+ ByteBuffer buf = ByteBuffer.wrap(data, offset, len);
264
+ return getCharsetUTF8().decode(buf).toString();
265
+ }
266
+
267
+ public static String getNodeCompletePath(Node node) {
268
+
269
+ Node cur, tmp, next;
270
+
271
+ // TODO: Rename buffer to path.
272
+ String buffer = "";
273
+ String sep;
274
+ String name;
275
+
276
+ int occur = 0;
277
+ boolean generic;
278
+
279
+ cur = node;
280
+
281
+ do {
282
+ name = "";
283
+ sep = "?";
284
+ occur = 0;
285
+ generic = false;
286
+
287
+ if(cur.getNodeType() == Node.DOCUMENT_NODE) {
288
+ if(buffer.startsWith("/")) break;
289
+
290
+ sep = "/";
291
+ next = null;
292
+ } else if(cur.getNodeType() == Node.ELEMENT_NODE) {
293
+ generic = false;
294
+ sep = "/";
295
+
296
+ name = cur.getLocalName();
297
+ if (name == null) name = cur.getNodeName();
298
+ if(cur.getNamespaceURI() != null) {
299
+ if(cur.getPrefix() != null) {
300
+ name = cur.getPrefix() + ":" + name;
301
+ } else {
302
+ generic = true;
303
+ name = "*";
304
+ }
305
+ }
306
+
307
+ next = cur.getParentNode();
308
+
309
+ /*
310
+ * Thumbler index computation
311
+ */
312
+
313
+ tmp = cur.getPreviousSibling();
314
+
315
+ while(tmp != null) {
316
+ if((tmp.getNodeType() == Node.ELEMENT_NODE) &&
317
+ (generic || fullNamesMatch(tmp, cur))) {
318
+ occur++;
319
+ }
320
+ tmp = tmp.getPreviousSibling();
321
+ }
322
+
323
+ if(occur == 0) {
324
+ tmp = cur.getNextSibling();
325
+
326
+ while(tmp != null && occur == 0) {
327
+ if((tmp.getNodeType() == Node.ELEMENT_NODE) &&
328
+ (generic || fullNamesMatch(tmp,cur))) {
329
+ occur++;
330
+ }
331
+ tmp = tmp.getNextSibling();
332
+ }
333
+
334
+ if(occur != 0) occur = 1;
335
+
336
+ } else {
337
+ occur++;
338
+ }
339
+ } else if(cur.getNodeType() == Node.COMMENT_NODE) {
340
+ sep = "/";
341
+ name = "comment()";
342
+ next = cur.getParentNode();
343
+
344
+ /*
345
+ * Thumbler index computation.
346
+ */
347
+
348
+ tmp = cur.getPreviousSibling();
349
+
350
+ while(tmp != null) {
351
+ if(tmp.getNodeType() == Node.COMMENT_NODE) {
352
+ occur++;
353
+ }
354
+ tmp = tmp.getPreviousSibling();
355
+ }
356
+
357
+ if(occur == 0) {
358
+ tmp = cur.getNextSibling();
359
+ while(tmp != null && occur == 0) {
360
+ if(tmp.getNodeType() == Node.COMMENT_NODE) {
361
+ occur++;
362
+ }
363
+ tmp = tmp.getNextSibling();
364
+ }
365
+ if(occur != 0) occur = 1;
366
+ } else {
367
+ occur = 1;
368
+ }
369
+
370
+ } else if(cur.getNodeType() == Node.TEXT_NODE ||
371
+ cur.getNodeType() == Node.CDATA_SECTION_NODE) {
372
+ // I'm here. gist:129
373
+ // http://gist.github.com/144923
374
+
375
+ sep = "/";
376
+ name = "text()";
377
+ next = cur.getParentNode();
378
+
379
+ /*
380
+ * Thumbler index computation.
381
+ */
382
+
383
+ tmp = cur.getPreviousSibling();
384
+ while(tmp != null) {
385
+ if(tmp.getNodeType() == Node.TEXT_NODE ||
386
+ tmp.getNodeType() == Node.CDATA_SECTION_NODE) {
387
+ occur++;
388
+ }
389
+ tmp = tmp.getPreviousSibling();
390
+ }
391
+
392
+ if(occur == 0) {
393
+ tmp = cur.getNextSibling();
394
+
395
+ while(tmp != null && occur == 0) {
396
+ if(tmp.getNodeType() == Node.TEXT_NODE ||
397
+ tmp.getNodeType() == Node.CDATA_SECTION_NODE) {
398
+ occur++;
399
+ }
400
+ tmp = tmp.getNextSibling();
401
+ }
402
+ } else {
403
+ occur++;
404
+ }
405
+
406
+ } else if(cur.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
407
+ sep = "/";
408
+ name = "processing-instruction('"+cur.getLocalName()+"')";
409
+ next = cur.getParentNode();
410
+
411
+ /*
412
+ * Thumbler index computation.
413
+ */
414
+
415
+ tmp = cur.getParentNode();
416
+
417
+ while(tmp != null) {
418
+ if(tmp.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE &&
419
+ tmp.getLocalName().equals(cur.getLocalName())) {
420
+ occur++;
421
+ }
422
+ tmp = tmp.getPreviousSibling();
423
+ }
424
+
425
+ if(occur == 0) {
426
+ tmp = cur.getNextSibling();
427
+
428
+ while(tmp != null && occur == 0) {
429
+ if(tmp.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE &&
430
+ tmp.getLocalName().equals(cur.getLocalName())){
431
+ occur++;
432
+ }
433
+ tmp = tmp.getNextSibling();
434
+ }
435
+
436
+ if(occur != 0) {
437
+ occur = 1;
438
+ }
439
+
440
+ } else {
441
+ occur++;
442
+ }
443
+
444
+ } else if(cur.getNodeType() == Node.ATTRIBUTE_NODE) {
445
+ sep = "/@";
446
+ name = cur.getLocalName();
447
+
448
+ if(cur.getNamespaceURI() != null) {
449
+ if(cur.getPrefix() != null) {
450
+ name = cur.getPrefix() + ":" + name;
451
+ }
452
+ }
453
+
454
+ next = ((Attr) cur).getOwnerElement();
455
+
456
+ } else {
457
+ next = cur.getParentNode();
458
+ }
459
+
460
+ if(occur == 0){
461
+ buffer = sep+name+buffer;
462
+ } else {
463
+ buffer = sep+name+"["+occur+"]"+buffer;
464
+ }
465
+
466
+ cur = next;
467
+
468
+ } while(cur != null);
469
+
470
+ return buffer;
471
+ }
472
+
473
+ protected static boolean compareTwoNodes(Node m, Node n) {
474
+ return nodesAreEqual(m.getLocalName(), n.getLocalName()) &&
475
+ nodesAreEqual(m.getPrefix(), n.getPrefix());
476
+ }
477
+
478
+ protected static boolean fullNamesMatch(Node a, Node b) {
479
+ return a.getNodeName().equals(b.getNodeName());
480
+ }
481
+
482
+ protected static String getFullName(Node n) {
483
+ String lname = n.getLocalName();
484
+ String prefix = n.getPrefix();
485
+ if (lname != null) {
486
+ if (prefix != null)
487
+ return prefix + ":" + lname;
488
+ else
489
+ return lname;
490
+ } else {
491
+ return n.getNodeName();
492
+ }
493
+ }
494
+
495
+ private static boolean nodesAreEqual(Object a, Object b) {
496
+ return (((a == null) && (a == null)) ||
497
+ (a != null) && (b != null) &&
498
+ (b.equals(a)));
499
+ }
500
+
501
+ private static Pattern encoded_pattern = Pattern.compile("&amp;|&gt;|&lt;|&#13;");
502
+ private static Pattern decoded_pattern = Pattern.compile("&|>|<|\r");
503
+ private static String[] encoded = {"&amp;", "&gt;", "&lt;", "&#13;"};
504
+ private static String[] decoded = {"&", ">", "<", "\r"};
505
+
506
+ private static String convert(Pattern ptn, String input, String[] oldChars, String[] newChars) {
507
+ Matcher matcher = ptn.matcher(input);
508
+ boolean result = matcher.find();
509
+ StringBuffer sb = new StringBuffer();
510
+ while(result) {
511
+ String matched = matcher.group();
512
+ String replacement = "";
513
+ for (int i=0; i<oldChars.length; i++) {
514
+ if (matched.contains(oldChars[i])) {
515
+ replacement = matched.replace(oldChars[i], newChars[i]);
516
+ break;
517
+ }
518
+ }
519
+ matcher.appendReplacement(sb, replacement);
520
+ result = matcher.find();
521
+ }
522
+ matcher.appendTail(sb);
523
+ return sb.toString();
524
+ }
525
+
526
+ public static String encodeJavaString(String s) {
527
+ return convert(decoded_pattern, s, decoded, encoded);
528
+ }
529
+
530
+ public static String decodeJavaString(String s) {
531
+ return convert(encoded_pattern, s, encoded, decoded);
532
+ }
533
+
534
+ private static Pattern not_escaped_pattern = Pattern.compile("\\&(?!(amp;|gt;|lt;))|<|>");
535
+ public static boolean isNotXmlEscaped(String s) {
536
+ if (s == null) return false;
537
+ Matcher matcher = not_escaped_pattern.matcher(s);
538
+ return (matcher.find());
539
+ }
540
+
541
+ public static String getNodeName(Node node) {
542
+ if(node == null) { System.out.println("node is null"); return ""; }
543
+ String name = node.getNodeName();
544
+ if(name == null) { System.out.println("name is null"); return ""; }
545
+ if(name.equals("#document")) {
546
+ return "document";
547
+ } else if(name.equals("#text")) {
548
+ return "text";
549
+ } else {
550
+ name = getLocalPart(name);
551
+ return (name == null) ? "" : name;
552
+ }
553
+ }
554
+
555
+ public static final String XMLNS_URI = "http://www.w3.org/2000/xmlns/";
556
+ public static boolean isNamespace(Node node) {
557
+ return (XMLNS_URI.equals(node.getNamespaceURI()) ||
558
+ isNamespace(node.getNodeName()));
559
+ }
560
+
561
+ public static boolean isNamespace(String nodeName) {
562
+ return (nodeName.equals("xmlns") || nodeName.startsWith("xmlns:"));
563
+ }
564
+
565
+ public static boolean isNonDefaultNamespace(Node node) {
566
+ return (isNamespace(node) && ! "xmlns".equals(node.getNodeName()));
567
+ }
568
+
569
+ public static boolean isXmlBase(String attrName) {
570
+ return "xml:base".equals(attrName) || "xlink:href".equals(attrName);
571
+ }
572
+
573
+ public static String newQName(String newPrefix, Node node) {
574
+ if(newPrefix == null) {
575
+ return node.getLocalName();
576
+ } else {
577
+ return newPrefix + ":" + node.getLocalName();
578
+ }
579
+ }
580
+
581
+ public static RubyArray nodeListToRubyArray(Ruby ruby, NodeList nodes) {
582
+ RubyArray array = RubyArray.newArray(ruby, nodes.getLength());
583
+ return nodeListToRubyArray(ruby, nodes, array);
584
+ }
585
+
586
+ public static RubyArray nodeListToRubyArray(Ruby ruby, NodeList nodes, RubyArray array) {
587
+ for(int i = 0; i < nodes.getLength(); i++) {
588
+ array.append(NokogiriHelpers.getCachedNodeOrCreate(ruby, nodes.item(i)));
589
+ }
590
+ return array;
591
+ }
592
+
593
+ public static RubyArray nodeArrayToRubyArray(Ruby ruby, Node[] nodes) {
594
+ RubyArray n = RubyArray.newArray(ruby, nodes.length);
595
+ for(int i = 0; i < nodes.length; i++) {
596
+ n.append(NokogiriHelpers.getCachedNodeOrCreate(ruby, nodes[i]));
597
+ }
598
+ return n;
599
+ }
600
+
601
+ public static RubyArray namedNodeMapToRubyArray(Ruby ruby, NamedNodeMap map) {
602
+ RubyArray n = RubyArray.newArray(ruby, map.getLength());
603
+ for(int i = 0; i < map.getLength(); i++) {
604
+ n.append(NokogiriHelpers.getCachedNodeOrCreate(ruby, map.item(i)));
605
+ }
606
+ return n;
607
+ }
608
+
609
+ public static String guessEncoding(Ruby ruby) {
610
+ String name = null;
611
+ if (name == null) name = System.getProperty("file.encoding");
612
+ if (name == null) name = "UTF-8";
613
+ return name;
614
+ }
615
+
616
+ public static String adjustSystemIdIfNecessary(String currentDir, String scriptFileName, String baseURI, String systemId) {
617
+ if (systemId == null) return systemId;
618
+ File file = new File(systemId);
619
+ if (file.isAbsolute()) return systemId;
620
+ String path = resolveSystemId(baseURI, systemId);
621
+ if (path != null) return path;
622
+ path = resolveSystemId(currentDir, systemId);
623
+ if (path != null) return path;
624
+ return resolveSystemId(scriptFileName, systemId);
625
+ }
626
+
627
+ private static String resolveSystemId(String baseName, String systemId) {
628
+ if (baseName == null || baseName.length() < 1) return null;
629
+ String parentName = null;
630
+ baseName = baseName.replaceAll("%20", " ");
631
+ File base = new File(baseName);
632
+ if (base.isDirectory()) parentName = baseName;
633
+ else parentName = base.getParent();
634
+ if (parentName.toLowerCase().startsWith("file:")) parentName = parentName.substring("file:".length());
635
+ File dtdFile = new File(parentName + "/" + systemId);
636
+ if (dtdFile.exists()) return dtdFile.getPath();
637
+ return null;
638
+ }
639
+ }