nokogiri 1.4.3.1-java → 1.5.0-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (231) hide show
  1. data/.gemtest +0 -0
  2. data/CHANGELOG.ja.rdoc +113 -0
  3. data/CHANGELOG.rdoc +113 -1
  4. data/Manifest.txt +72 -68
  5. data/README.ja.rdoc +4 -4
  6. data/README.rdoc +31 -7
  7. data/Rakefile +110 -132
  8. data/bin/nokogiri +7 -3
  9. data/ext/java/nokogiri/EncodingHandler.java +124 -0
  10. data/ext/java/nokogiri/HtmlDocument.java +119 -0
  11. data/ext/java/nokogiri/HtmlElementDescription.java +145 -0
  12. data/ext/java/nokogiri/HtmlEntityLookup.java +79 -0
  13. data/ext/java/nokogiri/HtmlSaxParserContext.java +259 -0
  14. data/ext/java/nokogiri/NokogiriService.java +590 -0
  15. data/ext/java/nokogiri/XmlAttr.java +180 -0
  16. data/ext/java/nokogiri/XmlAttributeDecl.java +130 -0
  17. data/ext/java/nokogiri/XmlCdata.java +84 -0
  18. data/ext/java/nokogiri/XmlComment.java +86 -0
  19. data/ext/java/nokogiri/XmlDocument.java +519 -0
  20. data/ext/java/nokogiri/XmlDocumentFragment.java +223 -0
  21. data/ext/java/nokogiri/XmlDtd.java +469 -0
  22. data/ext/java/nokogiri/XmlElement.java +195 -0
  23. data/ext/java/nokogiri/XmlElementContent.java +382 -0
  24. data/ext/java/nokogiri/XmlElementDecl.java +152 -0
  25. data/ext/java/nokogiri/XmlEntityDecl.java +162 -0
  26. data/ext/java/nokogiri/XmlEntityReference.java +97 -0
  27. data/ext/java/nokogiri/XmlNamespace.java +183 -0
  28. data/ext/java/nokogiri/XmlNode.java +1378 -0
  29. data/ext/java/nokogiri/XmlNodeSet.java +267 -0
  30. data/ext/java/nokogiri/XmlProcessingInstruction.java +99 -0
  31. data/ext/java/nokogiri/XmlReader.java +408 -0
  32. data/ext/java/nokogiri/XmlRelaxng.java +144 -0
  33. data/ext/java/nokogiri/XmlSaxParserContext.java +367 -0
  34. data/ext/java/nokogiri/XmlSaxPushParser.java +184 -0
  35. data/ext/java/nokogiri/XmlSchema.java +324 -0
  36. data/ext/java/nokogiri/XmlSyntaxError.java +119 -0
  37. data/ext/java/nokogiri/XmlText.java +119 -0
  38. data/ext/java/nokogiri/XmlXpathContext.java +199 -0
  39. data/ext/java/nokogiri/XsltStylesheet.java +197 -0
  40. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +204 -0
  41. data/ext/java/nokogiri/internals/NokogiriDocumentCache.java +73 -0
  42. data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +86 -0
  43. data/ext/java/nokogiri/internals/NokogiriHandler.java +327 -0
  44. data/ext/java/nokogiri/internals/NokogiriHelpers.java +639 -0
  45. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +167 -0
  46. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +130 -0
  47. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +74 -0
  48. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +121 -0
  49. data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +79 -0
  50. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +141 -0
  51. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +73 -0
  52. data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +67 -0
  53. data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +86 -0
  54. data/ext/java/nokogiri/internals/ParserContext.java +276 -0
  55. data/ext/java/nokogiri/internals/PushInputStream.java +411 -0
  56. data/ext/java/nokogiri/internals/ReaderNode.java +531 -0
  57. data/ext/java/nokogiri/internals/SaveContextVisitor.java +567 -0
  58. data/ext/java/nokogiri/internals/SchemaErrorHandler.java +76 -0
  59. data/ext/java/nokogiri/internals/XmlDeclHandler.java +42 -0
  60. data/ext/java/nokogiri/internals/XmlDomParser.java +76 -0
  61. data/ext/java/nokogiri/internals/XmlDomParserContext.java +244 -0
  62. data/ext/java/nokogiri/internals/XmlSaxParser.java +65 -0
  63. data/ext/java/nokogiri/internals/XsltExtensionFunction.java +72 -0
  64. data/ext/nokogiri/depend +358 -32
  65. data/ext/nokogiri/extconf.rb +10 -6
  66. data/ext/nokogiri/nokogiri.c +23 -3
  67. data/ext/nokogiri/nokogiri.h +7 -2
  68. data/ext/nokogiri/xml_document.c +9 -0
  69. data/ext/nokogiri/xml_dtd.c +2 -2
  70. data/ext/nokogiri/xml_io.c +32 -7
  71. data/ext/nokogiri/xml_node.c +45 -25
  72. data/ext/nokogiri/xml_node_set.c +1 -1
  73. data/ext/nokogiri/xml_relax_ng.c +0 -7
  74. data/ext/nokogiri/xml_sax_parser.c +14 -7
  75. data/ext/nokogiri/xml_sax_parser_context.c +40 -0
  76. data/ext/nokogiri/xml_xpath_context.c +33 -2
  77. data/ext/nokogiri/xslt_stylesheet.c +128 -6
  78. data/lib/isorelax.jar +0 -0
  79. data/lib/jing.jar +0 -0
  80. data/lib/nekodtd.jar +0 -0
  81. data/lib/nekohtml.jar +0 -0
  82. data/lib/nokogiri/css/parser.rb +665 -70
  83. data/lib/nokogiri/css/parser.y +3 -1
  84. data/lib/nokogiri/css/parser_extras.rb +91 -0
  85. data/lib/nokogiri/css/tokenizer.rb +148 -3
  86. data/lib/nokogiri/css/tokenizer.rex +1 -1
  87. data/lib/nokogiri/css/xpath_visitor.rb +15 -7
  88. data/lib/nokogiri/css.rb +5 -3
  89. data/lib/nokogiri/decorators/slop.rb +5 -3
  90. data/lib/nokogiri/html/document.rb +142 -19
  91. data/lib/nokogiri/html/document_fragment.rb +19 -17
  92. data/lib/nokogiri/html/element_description_defaults.rb +671 -0
  93. data/lib/nokogiri/html/sax/parser.rb +6 -2
  94. data/lib/nokogiri/html.rb +1 -0
  95. data/lib/nokogiri/nokogiri.jar +0 -0
  96. data/lib/nokogiri/version.rb +76 -26
  97. data/lib/nokogiri/xml/attribute_decl.rb +1 -1
  98. data/lib/nokogiri/xml/builder.rb +7 -0
  99. data/lib/nokogiri/xml/document.rb +43 -2
  100. data/lib/nokogiri/xml/document_fragment.rb +16 -2
  101. data/lib/nokogiri/xml/dtd.rb +12 -1
  102. data/lib/nokogiri/xml/element_decl.rb +1 -1
  103. data/lib/nokogiri/xml/entity_decl.rb +1 -1
  104. data/lib/nokogiri/xml/node/save_options.rb +20 -1
  105. data/lib/nokogiri/xml/node.rb +198 -78
  106. data/lib/nokogiri/xml/node_set.rb +10 -3
  107. data/lib/nokogiri/xml/parse_options.rb +8 -0
  108. data/lib/nokogiri/xml/reader.rb +42 -6
  109. data/lib/nokogiri/xml/sax/document.rb +4 -2
  110. data/lib/nokogiri/xml/schema.rb +7 -1
  111. data/lib/nokogiri/xslt/stylesheet.rb +1 -1
  112. data/lib/nokogiri/xslt.rb +9 -5
  113. data/lib/nokogiri.rb +19 -25
  114. data/lib/xercesImpl.jar +0 -0
  115. data/nokogiri_help_responses.md +40 -0
  116. data/tasks/cross_compile.rb +130 -136
  117. data/tasks/nokogiri.org.rb +18 -0
  118. data/tasks/test.rb +2 -2
  119. data/test/css/test_parser.rb +29 -18
  120. data/test/css/test_tokenizer.rb +8 -0
  121. data/test/decorators/test_slop.rb +16 -0
  122. data/test/files/encoding.html +82 -0
  123. data/test/files/encoding.xhtml +84 -0
  124. data/test/files/metacharset.html +10 -0
  125. data/test/files/noencoding.html +47 -0
  126. data/test/helper.rb +5 -1
  127. data/test/html/sax/test_parser.rb +65 -3
  128. data/test/html/test_document.rb +75 -1
  129. data/test/html/test_document_encoding.rb +61 -0
  130. data/test/html/test_document_fragment.rb +50 -5
  131. data/test/html/test_element_description.rb +4 -2
  132. data/test/html/test_node.rb +9 -0
  133. data/test/test_memory_leak.rb +20 -35
  134. data/test/test_nokogiri.rb +14 -20
  135. data/test/test_reader.rb +23 -6
  136. data/test/test_xslt_transforms.rb +6 -2
  137. data/test/xml/node/test_save_options.rb +10 -2
  138. data/test/xml/sax/test_parser.rb +39 -8
  139. data/test/xml/sax/test_parser_context.rb +50 -0
  140. data/test/xml/sax/test_push_parser.rb +18 -1
  141. data/test/xml/test_attribute_decl.rb +7 -3
  142. data/test/xml/test_builder.rb +17 -0
  143. data/test/xml/test_document.rb +34 -5
  144. data/test/xml/test_document_fragment.rb +14 -2
  145. data/test/xml/test_dtd.rb +28 -3
  146. data/test/xml/test_element_content.rb +1 -1
  147. data/test/xml/test_element_decl.rb +1 -1
  148. data/test/xml/test_entity_decl.rb +12 -10
  149. data/test/xml/test_namespace.rb +7 -5
  150. data/test/xml/test_node.rb +65 -13
  151. data/test/xml/test_node_reparenting.rb +72 -31
  152. data/test/xml/test_node_set.rb +57 -0
  153. data/test/xml/test_schema.rb +5 -0
  154. data/test/xml/test_xpath.rb +32 -0
  155. data/test/xslt/test_custom_functions.rb +94 -0
  156. data/test/xslt/test_exception_handling.rb +37 -0
  157. metadata +512 -517
  158. data/deps.rip +0 -5
  159. data/ext/nokogiri/libcharset-1.dll +0 -0
  160. data/ext/nokogiri/libexslt.dll +0 -0
  161. data/ext/nokogiri/libiconv-2.dll +0 -0
  162. data/ext/nokogiri/libxml2.dll +0 -0
  163. data/ext/nokogiri/libxslt.dll +0 -0
  164. data/ext/nokogiri/zlib1.dll +0 -0
  165. data/lib/nokogiri/css/generated_parser.rb +0 -676
  166. data/lib/nokogiri/css/generated_tokenizer.rb +0 -146
  167. data/lib/nokogiri/ffi/encoding_handler.rb +0 -42
  168. data/lib/nokogiri/ffi/html/document.rb +0 -28
  169. data/lib/nokogiri/ffi/html/element_description.rb +0 -81
  170. data/lib/nokogiri/ffi/html/entity_lookup.rb +0 -16
  171. data/lib/nokogiri/ffi/html/sax/parser_context.rb +0 -38
  172. data/lib/nokogiri/ffi/io_callbacks.rb +0 -42
  173. data/lib/nokogiri/ffi/libxml.rb +0 -411
  174. data/lib/nokogiri/ffi/structs/common_node.rb +0 -38
  175. data/lib/nokogiri/ffi/structs/html_elem_desc.rb +0 -24
  176. data/lib/nokogiri/ffi/structs/html_entity_desc.rb +0 -13
  177. data/lib/nokogiri/ffi/structs/xml_alloc.rb +0 -16
  178. data/lib/nokogiri/ffi/structs/xml_attr.rb +0 -19
  179. data/lib/nokogiri/ffi/structs/xml_attribute.rb +0 -27
  180. data/lib/nokogiri/ffi/structs/xml_buffer.rb +0 -16
  181. data/lib/nokogiri/ffi/structs/xml_char_encoding_handler.rb +0 -11
  182. data/lib/nokogiri/ffi/structs/xml_document.rb +0 -117
  183. data/lib/nokogiri/ffi/structs/xml_dtd.rb +0 -28
  184. data/lib/nokogiri/ffi/structs/xml_element.rb +0 -26
  185. data/lib/nokogiri/ffi/structs/xml_element_content.rb +0 -17
  186. data/lib/nokogiri/ffi/structs/xml_entity.rb +0 -32
  187. data/lib/nokogiri/ffi/structs/xml_enumeration.rb +0 -12
  188. data/lib/nokogiri/ffi/structs/xml_node.rb +0 -28
  189. data/lib/nokogiri/ffi/structs/xml_node_set.rb +0 -53
  190. data/lib/nokogiri/ffi/structs/xml_notation.rb +0 -11
  191. data/lib/nokogiri/ffi/structs/xml_ns.rb +0 -15
  192. data/lib/nokogiri/ffi/structs/xml_parser_context.rb +0 -19
  193. data/lib/nokogiri/ffi/structs/xml_relax_ng.rb +0 -14
  194. data/lib/nokogiri/ffi/structs/xml_sax_handler.rb +0 -51
  195. data/lib/nokogiri/ffi/structs/xml_sax_push_parser_context.rb +0 -124
  196. data/lib/nokogiri/ffi/structs/xml_schema.rb +0 -13
  197. data/lib/nokogiri/ffi/structs/xml_syntax_error.rb +0 -31
  198. data/lib/nokogiri/ffi/structs/xml_text_reader.rb +0 -12
  199. data/lib/nokogiri/ffi/structs/xml_xpath_context.rb +0 -38
  200. data/lib/nokogiri/ffi/structs/xml_xpath_object.rb +0 -35
  201. data/lib/nokogiri/ffi/structs/xml_xpath_parser_context.rb +0 -20
  202. data/lib/nokogiri/ffi/structs/xslt_stylesheet.rb +0 -13
  203. data/lib/nokogiri/ffi/weak_bucket.rb +0 -40
  204. data/lib/nokogiri/ffi/xml/attr.rb +0 -41
  205. data/lib/nokogiri/ffi/xml/attribute_decl.rb +0 -27
  206. data/lib/nokogiri/ffi/xml/cdata.rb +0 -19
  207. data/lib/nokogiri/ffi/xml/comment.rb +0 -18
  208. data/lib/nokogiri/ffi/xml/document.rb +0 -166
  209. data/lib/nokogiri/ffi/xml/document_fragment.rb +0 -21
  210. data/lib/nokogiri/ffi/xml/dtd.rb +0 -67
  211. data/lib/nokogiri/ffi/xml/element_content.rb +0 -43
  212. data/lib/nokogiri/ffi/xml/element_decl.rb +0 -19
  213. data/lib/nokogiri/ffi/xml/entity_decl.rb +0 -36
  214. data/lib/nokogiri/ffi/xml/entity_reference.rb +0 -19
  215. data/lib/nokogiri/ffi/xml/namespace.rb +0 -44
  216. data/lib/nokogiri/ffi/xml/node.rb +0 -554
  217. data/lib/nokogiri/ffi/xml/node_set.rb +0 -149
  218. data/lib/nokogiri/ffi/xml/processing_instruction.rb +0 -20
  219. data/lib/nokogiri/ffi/xml/reader.rb +0 -236
  220. data/lib/nokogiri/ffi/xml/relax_ng.rb +0 -85
  221. data/lib/nokogiri/ffi/xml/sax/parser.rb +0 -135
  222. data/lib/nokogiri/ffi/xml/sax/parser_context.rb +0 -67
  223. data/lib/nokogiri/ffi/xml/sax/push_parser.rb +0 -51
  224. data/lib/nokogiri/ffi/xml/schema.rb +0 -109
  225. data/lib/nokogiri/ffi/xml/syntax_error.rb +0 -98
  226. data/lib/nokogiri/ffi/xml/text.rb +0 -18
  227. data/lib/nokogiri/ffi/xml/xpath.rb +0 -9
  228. data/lib/nokogiri/ffi/xml/xpath_context.rb +0 -148
  229. data/lib/nokogiri/ffi/xslt/stylesheet.rb +0 -53
  230. data/lib/nokogiri/version_warning.rb +0 -14
  231. data/test/ffi/test_document.rb +0 -35
@@ -0,0 +1,141 @@
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 static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
36
+
37
+ import java.util.List;
38
+
39
+ import javax.xml.xpath.XPathFunction;
40
+ import javax.xml.xpath.XPathFunctionException;
41
+
42
+ import nokogiri.NokogiriService;
43
+ import nokogiri.XmlNode;
44
+ import nokogiri.XmlNodeSet;
45
+
46
+ import org.jruby.Ruby;
47
+ import org.jruby.RubyArray;
48
+ import org.jruby.RubyBoolean;
49
+ import org.jruby.RubyFloat;
50
+ import org.jruby.RubyString;
51
+ import org.jruby.javasupport.JavaUtil;
52
+ import org.jruby.javasupport.util.RuntimeHelpers;
53
+ import org.jruby.runtime.ThreadContext;
54
+ import org.jruby.runtime.builtin.IRubyObject;
55
+ import org.w3c.dom.NodeList;
56
+
57
+ /**
58
+ * Xpath function handler.
59
+ *
60
+ * @author sergio
61
+ * @author Yoko Harada <yokolet@gmail.com>
62
+ */
63
+ public class NokogiriXPathFunction implements XPathFunction {
64
+ private static NokogiriXPathFunction xpathFunction;
65
+ private IRubyObject handler;
66
+ private String name;
67
+ private int arity;
68
+
69
+ public static NokogiriXPathFunction create(IRubyObject handler, String name, int arity) {
70
+ if (xpathFunction == null) xpathFunction = new NokogiriXPathFunction();
71
+ try {
72
+ NokogiriXPathFunction clone = (NokogiriXPathFunction) xpathFunction.clone();
73
+ clone.init(handler, name, arity);
74
+ return clone;
75
+ } catch (CloneNotSupportedException e) {
76
+ NokogiriXPathFunction freshXpathFunction = new NokogiriXPathFunction();
77
+ freshXpathFunction.init(handler, name, arity);
78
+ return freshXpathFunction;
79
+ }
80
+ }
81
+
82
+ private void init(IRubyObject handler, String name, int arity) {
83
+ this.handler = handler;
84
+ this.name = name;
85
+ this.arity = arity;
86
+ }
87
+
88
+ private NokogiriXPathFunction() {}
89
+
90
+ public Object evaluate(List args) throws XPathFunctionException {
91
+ if(args.size() != this.arity) {
92
+ throw new XPathFunctionException("arity does not match");
93
+ }
94
+
95
+ Ruby ruby = this.handler.getRuntime();
96
+ ThreadContext context = ruby.getCurrentContext();
97
+
98
+ IRubyObject result = RuntimeHelpers.invoke(context, this.handler,
99
+ this.name, fromObjectToRubyArgs(args));
100
+
101
+ return fromRubyToObject(result);
102
+ }
103
+
104
+ private IRubyObject[] fromObjectToRubyArgs(List args) {
105
+ IRubyObject[] newArgs = new IRubyObject[args.size()];
106
+ for(int i = 0; i < args.size(); i++) {
107
+ newArgs[i] = fromObjectToRuby(args.get(i));
108
+ }
109
+ return newArgs;
110
+ }
111
+
112
+ private IRubyObject fromObjectToRuby(Object o) {
113
+ // argument object type is one of NodeList, String, Boolean, or Double.
114
+ Ruby runtime = this.handler.getRuntime();
115
+ if (o instanceof NodeList) {
116
+ XmlNodeSet xmlNodeSet = (XmlNodeSet)NokogiriService.XML_NODESET_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::NodeSet"));
117
+ xmlNodeSet.setNodeList((NodeList) o);
118
+ return xmlNodeSet;
119
+ } else {
120
+ return JavaUtil.convertJavaToUsableRubyObject(runtime, o);
121
+ }
122
+ }
123
+
124
+ private Object fromRubyToObject(IRubyObject o) {
125
+ Ruby runtime = this.handler.getRuntime();
126
+ if(o instanceof RubyString) {
127
+ return o.toJava(String.class);
128
+ } else if (o instanceof RubyFloat) {
129
+ return o.toJava(Double.class);
130
+ } else if (o instanceof RubyBoolean) {
131
+ return o.toJava(Boolean.class);
132
+ } else if (o instanceof XmlNodeSet) {
133
+ return (NodeList)o;
134
+ } else if (o instanceof RubyArray) {
135
+ XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(runtime.getCurrentContext(), (RubyArray)o);
136
+ return (NodeList)xmlNodeSet;
137
+ } else /*if (o instanceof XmlNode)*/ {
138
+ return ((XmlNode) o).getNode();
139
+ }
140
+ }
141
+ }
@@ -0,0 +1,73 @@
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 javax.xml.namespace.QName;
36
+ import javax.xml.xpath.XPathFunction;
37
+ import javax.xml.xpath.XPathFunctionResolver;
38
+
39
+ import org.jruby.runtime.builtin.IRubyObject;
40
+
41
+ /**
42
+ * Xpath function resolver class, which is used in XmlXpathContext.
43
+ *
44
+ * @author sergio
45
+ * @author Yoko Harada <yokolet@gmail.com>
46
+ */
47
+ public class NokogiriXPathFunctionResolver implements XPathFunctionResolver {
48
+ private static NokogiriXPathFunctionResolver resolver;
49
+ private IRubyObject handler;
50
+
51
+ public static NokogiriXPathFunctionResolver create(IRubyObject handler) {
52
+ if (resolver == null) resolver = new NokogiriXPathFunctionResolver();
53
+ try {
54
+ NokogiriXPathFunctionResolver clone = (NokogiriXPathFunctionResolver) resolver.clone();
55
+ clone.setHandler(handler);
56
+ return clone;
57
+ } catch (CloneNotSupportedException e) {
58
+ NokogiriXPathFunctionResolver freshResolver = new NokogiriXPathFunctionResolver();
59
+ freshResolver.setHandler(handler);
60
+ return freshResolver;
61
+ }
62
+ }
63
+
64
+ private NokogiriXPathFunctionResolver() {}
65
+
66
+ private void setHandler(IRubyObject handler) {
67
+ this.handler = handler;
68
+ }
69
+
70
+ public XPathFunction resolveFunction(QName name, int arity) {
71
+ return NokogiriXPathFunction.create(handler, name.getLocalPart(), arity);
72
+ }
73
+ }
@@ -0,0 +1,67 @@
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
+ package nokogiri.internals;
33
+
34
+ import java.util.HashMap;
35
+ import javax.xml.namespace.QName;
36
+ import javax.xml.xpath.XPathVariableResolver;
37
+
38
+ /**
39
+ * XPath variable support
40
+ *
41
+ * @author Ken Bloom <kbloom@gmail.com>
42
+ * @author Yoko Harada <yokolet@gmail.com>
43
+ */
44
+ public class NokogiriXPathVariableResolver implements XPathVariableResolver {
45
+ private static NokogiriXPathVariableResolver resolver;
46
+ private HashMap<QName,String> variables = new HashMap<QName,String>();
47
+
48
+ public static NokogiriXPathVariableResolver create() {
49
+ if (resolver == null) resolver = new NokogiriXPathVariableResolver();
50
+ try {
51
+ NokogiriXPathVariableResolver clone = (NokogiriXPathVariableResolver) resolver.clone();
52
+ return clone;
53
+ } catch (CloneNotSupportedException e) {
54
+ NokogiriXPathVariableResolver freshResolver = new NokogiriXPathVariableResolver();
55
+ return freshResolver;
56
+ }
57
+ }
58
+
59
+ private NokogiriXPathVariableResolver() {}
60
+
61
+ public Object resolveVariable(QName variableName){
62
+ return variables.get(variableName);
63
+ }
64
+ public void registerVariable(String name,String value){
65
+ variables.put(new QName(name),value);
66
+ }
67
+ }
@@ -0,0 +1,86 @@
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 javax.xml.transform.ErrorListener;
36
+ import javax.xml.transform.TransformerException;
37
+
38
+ /**
39
+ * Error Listener for XSLT transformer
40
+ *
41
+ * @author Yoko Harada <yokolet@gmail.com>
42
+ */
43
+ public class NokogiriXsltErrorListener implements ErrorListener {
44
+ public enum ErrorType {
45
+ WARNING,
46
+ ERROR,
47
+ FATAL
48
+ }
49
+
50
+ private ErrorType type;
51
+ private String errorMessage;
52
+ private Exception exception;
53
+
54
+ public void warning(TransformerException ex) throws TransformerException {
55
+ type = ErrorType.WARNING;
56
+ setError(ex);
57
+ }
58
+
59
+ public void error(TransformerException ex) throws TransformerException {
60
+ type = ErrorType.ERROR;
61
+ setError(ex);
62
+ }
63
+
64
+ public void fatalError(TransformerException ex) throws TransformerException {
65
+ type = ErrorType.FATAL;
66
+ setError(ex);
67
+ }
68
+
69
+ private void setError(TransformerException ex) {
70
+ errorMessage = ex.getMessage();
71
+ exception = ex;
72
+ }
73
+
74
+ public String getErrorMessage() {
75
+ return errorMessage;
76
+ }
77
+
78
+ public ErrorType getErrorType() {
79
+ return type;
80
+ }
81
+
82
+ public Exception getException() {
83
+ return exception;
84
+ }
85
+
86
+ }
@@ -0,0 +1,276 @@
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 static nokogiri.internals.NokogiriHelpers.rubyStringToString;
36
+ import static nokogiri.internals.NokogiriHelpers.adjustSystemIdIfNecessary;
37
+ import static org.jruby.javasupport.util.RuntimeHelpers.invoke;
38
+
39
+ import java.io.ByteArrayInputStream;
40
+ import java.io.File;
41
+ import java.io.IOException;
42
+ import java.io.InputStream;
43
+
44
+ import org.jruby.Ruby;
45
+ import org.jruby.RubyClass;
46
+ import org.jruby.RubyIO;
47
+ import org.jruby.RubyObject;
48
+ import org.jruby.RubyString;
49
+ import org.jruby.RubyStringIO;
50
+ import org.jruby.exceptions.RaiseException;
51
+ import org.jruby.runtime.ThreadContext;
52
+ import org.jruby.runtime.builtin.IRubyObject;
53
+ import org.jruby.util.ByteList;
54
+ import org.jruby.util.TypeConverter;
55
+ import org.xml.sax.InputSource;
56
+ import org.xml.sax.SAXException;
57
+ import org.xml.sax.ext.EntityResolver2;
58
+
59
+ /**
60
+ * Base class for the various parser contexts. Handles converting
61
+ * Ruby objects to InputSource objects.
62
+ *
63
+ * @author Patrick Mahoney <pat@polycrystal.org>
64
+ * @author Yoko Harada <yokolet@gmail.com>
65
+ */
66
+ public class ParserContext extends RubyObject {
67
+ protected InputSource source = null;
68
+
69
+ /**
70
+ * Create a file base input source taking into account the current
71
+ * directory of <code>runtime</code>.
72
+ */
73
+ public static InputSource resolveEntity(Ruby runtime, String publicId, String baseURI, String systemId)
74
+ throws IOException {
75
+ InputSource s = new InputSource();
76
+ String adjusted = adjustSystemIdIfNecessary(runtime.getCurrentDirectory(), runtime.getInstanceConfig().getScriptFileName(), baseURI, systemId);
77
+ if (adjusted == null && publicId == null) {
78
+ throw runtime.newRuntimeError("SystemId \"" + systemId + "\" is not correct.");
79
+ }
80
+ s.setSystemId(adjusted);
81
+ s.setPublicId(publicId);
82
+ return s;
83
+ }
84
+
85
+ public ParserContext(Ruby runtime) {
86
+ // default to class 'Object' because this class isn't exposed to Ruby
87
+ super(runtime, runtime.getObject());
88
+ }
89
+
90
+ public ParserContext(Ruby runtime, RubyClass klass) {
91
+ super(runtime, klass);
92
+ }
93
+
94
+ protected InputSource getInputSource() {
95
+ return source;
96
+ }
97
+
98
+ /**
99
+ * Set the InputSource from <code>url</code> or <code>data</code>,
100
+ * which may be an IO object, a String, or a StringIO.
101
+ */
102
+ public void setInputSource(ThreadContext context, IRubyObject data, IRubyObject url) {
103
+ Ruby ruby = context.getRuntime();
104
+ String path = (String) url.toJava(String.class);
105
+ if (isAbsolutePath(path)) {
106
+ source = new InputSource();
107
+ source.setSystemId(path);
108
+ return;
109
+ }
110
+ RubyString stringData = null;
111
+ if (invoke(context, data, "respond_to?",
112
+ ruby.newSymbol("to_io").to_sym()).isTrue()) {
113
+ /* IO or other object that responds to :to_io */
114
+ RubyIO io =
115
+ (RubyIO) TypeConverter.convertToType(data,
116
+ ruby.getIO(),
117
+ "to_io");
118
+ source = new InputSource(io.getInStream());
119
+ } else if (((RubyObject)data).getInstanceVariable("@io") != null) {
120
+ // in case of EncodingReader is used
121
+ // since EncodingReader won't respond to :to_io
122
+ RubyObject dataObject = (RubyObject) ((RubyObject)data).getInstanceVariable("@io");
123
+ if (dataObject instanceof RubyIO) {
124
+ RubyIO io = (RubyIO)dataObject;
125
+ source = new InputSource(io.getInStream());
126
+ } else if (dataObject instanceof RubyStringIO) {
127
+ stringData = (RubyString)((RubyStringIO)dataObject).string();
128
+ }
129
+ } else {
130
+ if (invoke(context, data, "respond_to?",
131
+ ruby.newSymbol("string").to_sym()).isTrue()) {
132
+ /* StringIO or other object that responds to :string */
133
+ stringData = invoke(context, data, "string").convertToString();
134
+ } else if (data instanceof RubyString) {
135
+ stringData = (RubyString) data;
136
+ } else {
137
+ throw ruby.newArgumentError(
138
+ "must be kind_of String or respond to :to_io or :string");
139
+ }
140
+ }
141
+ if (stringData != null) {
142
+ ByteList bytes = stringData.getByteList();
143
+ source = new InputSource(new ByteArrayInputStream(bytes.unsafeBytes(), bytes.begin(), bytes.length()));
144
+ }
145
+ }
146
+
147
+ private boolean isAbsolutePath(String url) {
148
+ if (url == null) return false;
149
+ return (new File(url)).isAbsolute();
150
+ }
151
+
152
+ /**
153
+ * Set the InputSource to read from <code>file</code>, a String filename.
154
+ */
155
+ public void setInputSourceFile(ThreadContext context, IRubyObject file) {
156
+ String filename = rubyStringToString(file);
157
+
158
+ try{
159
+ source = resolveEntity(context.getRuntime(), null, null, filename);
160
+ } catch (Exception e) {
161
+ throw RaiseException.createNativeRaiseException(context.getRuntime(), e);
162
+ }
163
+
164
+ }
165
+
166
+ /**
167
+ * Set the InputSource from <code>stream</code>.
168
+ */
169
+ public void setInputSource(InputStream stream) {
170
+ source = new InputSource(stream);
171
+ }
172
+
173
+ /**
174
+ * Wrap Nokogiri parser options in a utility class. This is
175
+ * read-only.
176
+ */
177
+ public static class Options {
178
+ protected static final long STRICT = 0;
179
+ protected static final long RECOVER = 1;
180
+ protected static final long NOENT = 2;
181
+ protected static final long DTDLOAD = 4;
182
+ protected static final long DTDATTR = 8;
183
+ protected static final long DTDVALID = 16;
184
+ protected static final long NOERROR = 32;
185
+ protected static final long NOWARNING = 64;
186
+ protected static final long PEDANTIC = 128;
187
+ protected static final long NOBLANKS = 256;
188
+ protected static final long SAX1 = 512;
189
+ protected static final long XINCLUDE = 1024;
190
+ protected static final long NONET = 2048;
191
+ protected static final long NODICT = 4096;
192
+ protected static final long NSCLEAN = 8192;
193
+ protected static final long NOCDATA = 16384;
194
+ protected static final long NOXINCNODE = 32768;
195
+
196
+ public boolean strict;
197
+ public boolean recover;
198
+ public boolean noEnt;
199
+ public boolean dtdLoad;
200
+ public boolean dtdAttr;
201
+ public boolean dtdValid;
202
+ public boolean noError;
203
+ public boolean noWarning;
204
+ public boolean pedantic;
205
+ public boolean noBlanks;
206
+ public boolean sax1;
207
+ public boolean xInclude;
208
+ public boolean noNet;
209
+ public boolean noDict;
210
+ public boolean nsClean;
211
+ public boolean noCdata;
212
+ public boolean noXIncNode;
213
+
214
+ protected static boolean test(long options, long mask) {
215
+ return ((options & mask) == mask);
216
+ }
217
+
218
+ public Options(long options) {
219
+ strict = ((options & RECOVER) == STRICT);
220
+ recover = test(options, RECOVER);
221
+ noEnt = test(options, NOENT);
222
+ dtdLoad = test(options, DTDLOAD);
223
+ dtdAttr = test(options, DTDATTR);
224
+ dtdValid = test(options, DTDVALID);
225
+ noError = test(options, NOERROR);
226
+ noWarning = test(options, NOWARNING);
227
+ pedantic = test(options, PEDANTIC);
228
+ noBlanks = test(options, NOBLANKS);
229
+ sax1 = test(options, SAX1);
230
+ xInclude = test(options, XINCLUDE);
231
+ noNet = test(options, NONET);
232
+ noDict = test(options, NODICT);
233
+ nsClean = test(options, NSCLEAN);
234
+ noCdata = test(options, NOCDATA);
235
+ noXIncNode = test(options, NOXINCNODE);
236
+ }
237
+ }
238
+
239
+ /**
240
+ * An entity resolver aware of the fact that the Ruby runtime can
241
+ * change directory but the JVM cannot. Thus any file based
242
+ * entity resolution that uses relative paths must be translated
243
+ * to be relative to the current directory of the Ruby runtime.
244
+ */
245
+ public static class ChdirEntityResolver implements EntityResolver2 {
246
+ protected Ruby runtime;
247
+
248
+ public ChdirEntityResolver(Ruby runtime) {
249
+ super();
250
+ this.runtime = runtime;
251
+ }
252
+
253
+ @Override
254
+ public InputSource getExternalSubset(String name, String baseURI)
255
+ throws SAXException, IOException {
256
+ return null;
257
+ }
258
+
259
+ @Override
260
+ public InputSource resolveEntity(String publicId, String systemId)
261
+ throws SAXException, IOException {
262
+ return resolveEntity(null, publicId, null, systemId);
263
+ }
264
+
265
+ @Override
266
+ public InputSource resolveEntity(String name,
267
+ String publicId,
268
+ String baseURI,
269
+ String systemId)
270
+ throws SAXException, IOException {
271
+ return ParserContext.resolveEntity(runtime, publicId, baseURI, systemId);
272
+ }
273
+
274
+ }
275
+
276
+ }