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,199 @@
1
+ /**
2
+ * (The MIT License)
3
+ *
4
+ * Copyright (c) 2008 - 2011:
5
+ *
6
+ * * {Aaron Patterson}[http://tenderlovemaking.com]
7
+ * * {Mike Dalessio}[http://mike.daless.io]
8
+ * * {Charles Nutter}[http://blog.headius.com]
9
+ * * {Sergio Arbeo}[http://www.serabe.com]
10
+ * * {Patrick Mahoney}[http://polycrystal.org]
11
+ * * {Yoko Harada}[http://yokolet.blogspot.com]
12
+ *
13
+ * Permission is hereby granted, free of charge, to any person obtaining
14
+ * a copy of this software and associated documentation files (the
15
+ * 'Software'), to deal in the Software without restriction, including
16
+ * without limitation the rights to use, copy, modify, merge, publish,
17
+ * distribute, sublicense, and/or sell copies of the Software, and to
18
+ * permit persons to whom the Software is furnished to do so, subject to
19
+ * the following conditions:
20
+ *
21
+ * The above copyright notice and this permission notice shall be
22
+ * included in all copies or substantial portions of the Software.
23
+ *
24
+ * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
25
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
+ */
32
+
33
+ package nokogiri;
34
+
35
+ import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
36
+
37
+ import java.util.Set;
38
+ import java.util.regex.Matcher;
39
+ import java.util.regex.Pattern;
40
+
41
+ import javax.xml.xpath.XPath;
42
+ import javax.xml.xpath.XPathConstants;
43
+ import javax.xml.xpath.XPathExpression;
44
+ import javax.xml.xpath.XPathExpressionException;
45
+ import javax.xml.xpath.XPathFactory;
46
+
47
+ import nokogiri.internals.NokogiriNamespaceContext;
48
+ import nokogiri.internals.NokogiriXPathFunctionResolver;
49
+ import nokogiri.internals.NokogiriXPathVariableResolver;
50
+
51
+ import org.jruby.Ruby;
52
+ import org.jruby.RubyBoolean;
53
+ import org.jruby.RubyClass;
54
+ import org.jruby.RubyException;
55
+ import org.jruby.RubyNumeric;
56
+ import org.jruby.RubyObject;
57
+ import org.jruby.RubyString;
58
+ import org.jruby.anno.JRubyClass;
59
+ import org.jruby.anno.JRubyMethod;
60
+ import org.jruby.exceptions.RaiseException;
61
+ import org.jruby.runtime.ThreadContext;
62
+ import org.jruby.runtime.builtin.IRubyObject;
63
+ import org.w3c.dom.NodeList;
64
+
65
+ /**
66
+ * Class for Nokogiri::XML::XpathContext
67
+ *
68
+ * @author sergio
69
+ * @author Yoko Harada <yokolet@gmail.com>
70
+ */
71
+ @JRubyClass(name="Nokogiri::XML::XPathContext")
72
+ public class XmlXpathContext extends RubyObject {
73
+ private XmlNode context;
74
+ private static final XPath xpath = XPathFactory.newInstance().newXPath();;
75
+
76
+ public XmlXpathContext(Ruby ruby, RubyClass rubyClass) {
77
+ super(ruby, rubyClass);
78
+ }
79
+
80
+ public void setNode(XmlNode node) {
81
+ context = node;
82
+ xpath.setNamespaceContext(NokogiriNamespaceContext.create());
83
+ xpath.setXPathVariableResolver(NokogiriXPathVariableResolver.create());
84
+ }
85
+
86
+ /**
87
+ * Create and return a copy of this object.
88
+ *
89
+ * @return a clone of this object
90
+ */
91
+ @Override
92
+ public Object clone() throws CloneNotSupportedException {
93
+ return super.clone();
94
+ }
95
+
96
+ @JRubyMethod(name = "new", meta = true)
97
+ public static IRubyObject rbNew(ThreadContext context, IRubyObject klazz, IRubyObject node) {
98
+ XmlNode xmlNode = (XmlNode)node;
99
+ XmlXpathContext xmlXpathContext = (XmlXpathContext) NokogiriService.XML_XPATHCONTEXT_ALLOCATOR.allocate(context.getRuntime(), (RubyClass)klazz);
100
+ xmlXpathContext.setNode(xmlNode);
101
+ return xmlXpathContext;
102
+ }
103
+
104
+ @JRubyMethod
105
+ public IRubyObject evaluate(ThreadContext context, IRubyObject expr, IRubyObject handler) {
106
+ String src = (String) expr.toJava(String.class);
107
+ try {
108
+ if(!handler.isNil()) {
109
+ if (!isContainsPrefix(src)) {
110
+ Set<String> methodNames = handler.getMetaClass().getMethods().keySet();
111
+ for (String name : methodNames) {
112
+ src = src.replaceAll(name, NokogiriNamespaceContext.NOKOGIRI_PREFIX+":"+name);
113
+ }
114
+ }
115
+ xpath.setXPathFunctionResolver(NokogiriXPathFunctionResolver.create(handler));
116
+ }
117
+ XPathExpression xpathExpression = xpath.compile(src);
118
+ return node_set(context, xpathExpression);
119
+ } catch (XPathExpressionException xpee) {
120
+ xpee = new XPathExpressionException(src);
121
+ RubyException e = XmlSyntaxError.createXPathSyntaxError(getRuntime(), xpee);
122
+ throw new RaiseException(e);
123
+ }
124
+ }
125
+
126
+ protected IRubyObject node_set(ThreadContext rbctx, XPathExpression xpathExpression) {
127
+ XmlNodeSet result = null;
128
+ try {
129
+ result = tryGetNodeSet(xpathExpression);
130
+ return result;
131
+ } catch (XPathExpressionException xpee) {
132
+ try {
133
+ return tryGetOpaqueValue(xpathExpression);
134
+ } catch (XPathExpressionException xpee_opaque) {
135
+ RubyException e = XmlSyntaxError.createXPathSyntaxError(getRuntime(), xpee_opaque);
136
+ throw new RaiseException(e);
137
+ }
138
+ }
139
+ }
140
+
141
+ private XmlNodeSet tryGetNodeSet(XPathExpression xpathExpression) throws XPathExpressionException {
142
+ NodeList nodeList = (NodeList)xpathExpression.evaluate(context.node, XPathConstants.NODESET);
143
+ XmlNodeSet xmlNodeSet = (XmlNodeSet) NokogiriService.XML_NODESET_ALLOCATOR.allocate(getRuntime(), getNokogiriClass(getRuntime(), "Nokogiri::XML::NodeSet"));
144
+ xmlNodeSet.setNodeList(nodeList);
145
+ return xmlNodeSet;
146
+ }
147
+
148
+ private static Pattern boolean_pattern = Pattern.compile("true|false");
149
+
150
+ private IRubyObject tryGetOpaqueValue(XPathExpression xpathExpression) throws XPathExpressionException {
151
+ String string = (String)xpathExpression.evaluate(context.node, XPathConstants.STRING);
152
+ Double value = null;
153
+ if ((value = getDoubleValue(string)) != null) return RubyNumeric.dbl2num(getRuntime(), value);
154
+ if (doesMatch(boolean_pattern, string.toLowerCase())) return RubyBoolean.newBoolean(getRuntime(), Boolean.parseBoolean(string));
155
+ return RubyString.newString(getRuntime(), string);
156
+ }
157
+
158
+ private Double getDoubleValue(String value) {
159
+ try {
160
+ return Double.valueOf(value);
161
+ } catch (NumberFormatException e) {
162
+ return null;
163
+ }
164
+ }
165
+
166
+ private boolean doesMatch(Pattern pattern, String string) {
167
+ Matcher m = pattern.matcher(string);
168
+ return m.matches();
169
+ }
170
+
171
+ private boolean isContainsPrefix(String str) {
172
+ Set<String> prefixes = ((NokogiriNamespaceContext)xpath.getNamespaceContext()).getAllPrefixes();
173
+ for (String prefix : prefixes) {
174
+ if (str.contains(prefix + ":")) {
175
+ return true;
176
+ }
177
+ }
178
+ return false;
179
+ }
180
+
181
+
182
+ @JRubyMethod
183
+ public IRubyObject evaluate(ThreadContext context, IRubyObject expr) {
184
+ return this.evaluate(context, expr, context.getRuntime().getNil());
185
+ }
186
+
187
+ @JRubyMethod
188
+ public IRubyObject register_ns(ThreadContext context, IRubyObject prefix, IRubyObject uri) {
189
+ ((NokogiriNamespaceContext) xpath.getNamespaceContext()).registerNamespace((String)prefix.toJava(String.class), (String)uri.toJava(String.class));
190
+ return this;
191
+ }
192
+
193
+ @JRubyMethod
194
+ public IRubyObject register_variable(ThreadContext context, IRubyObject name, IRubyObject value) {
195
+ ((NokogiriXPathVariableResolver) xpath.getXPathVariableResolver()).
196
+ registerVariable((String)name.toJava(String.class), (String)value.toJava(String.class));
197
+ return this;
198
+ }
199
+ }
@@ -0,0 +1,197 @@
1
+ /**
2
+ * (The MIT License)
3
+ *
4
+ * Copyright (c) 2008 - 2011:
5
+ *
6
+ * * {Aaron Patterson}[http://tenderlovemaking.com]
7
+ * * {Mike Dalessio}[http://mike.daless.io]
8
+ * * {Charles Nutter}[http://blog.headius.com]
9
+ * * {Sergio Arbeo}[http://www.serabe.com]
10
+ * * {Patrick Mahoney}[http://polycrystal.org]
11
+ * * {Yoko Harada}[http://yokolet.blogspot.com]
12
+ *
13
+ * Permission is hereby granted, free of charge, to any person obtaining
14
+ * a copy of this software and associated documentation files (the
15
+ * 'Software'), to deal in the Software without restriction, including
16
+ * without limitation the rights to use, copy, modify, merge, publish,
17
+ * distribute, sublicense, and/or sell copies of the Software, and to
18
+ * permit persons to whom the Software is furnished to do so, subject to
19
+ * the following conditions:
20
+ *
21
+ * The above copyright notice and this permission notice shall be
22
+ * included in all copies or substantial portions of the Software.
23
+ *
24
+ * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
25
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
+ */
32
+
33
+ package nokogiri;
34
+
35
+ import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
36
+
37
+ import java.util.HashMap;
38
+ import java.util.Map;
39
+ import java.util.regex.Matcher;
40
+ import java.util.regex.Pattern;
41
+
42
+ import javax.xml.transform.ErrorListener;
43
+ import javax.xml.transform.Templates;
44
+ import javax.xml.transform.Transformer;
45
+ import javax.xml.transform.TransformerConfigurationException;
46
+ import javax.xml.transform.TransformerException;
47
+ import javax.xml.transform.TransformerFactory;
48
+ import javax.xml.transform.dom.DOMResult;
49
+ import javax.xml.transform.dom.DOMSource;
50
+
51
+ import nokogiri.internals.NokogiriXsltErrorListener;
52
+
53
+ import org.jruby.Ruby;
54
+ import org.jruby.RubyArray;
55
+ import org.jruby.RubyClass;
56
+ import org.jruby.RubyObject;
57
+ import org.jruby.anno.JRubyClass;
58
+ import org.jruby.anno.JRubyMethod;
59
+ import org.jruby.javasupport.util.RuntimeHelpers;
60
+ import org.jruby.runtime.ThreadContext;
61
+ import org.jruby.runtime.builtin.IRubyObject;
62
+ import org.w3c.dom.Document;
63
+
64
+ /**
65
+ * Class for Nokogiri::XSLT::Stylesheet
66
+ *
67
+ * @author sergio
68
+ * @author Yoko Harada <yokolet@gmail.com>
69
+ */
70
+ @JRubyClass(name="Nokogiri::XSLT::Stylesheet")
71
+ public class XsltStylesheet extends RubyObject {
72
+ private static Map<String, Object> registry = new HashMap<String, Object>();
73
+ private static TransformerFactory factory = null;
74
+ private Templates sheet;
75
+
76
+ public static Map<String, Object> getRegistry() {
77
+ return registry;
78
+ }
79
+
80
+ public XsltStylesheet(Ruby ruby, RubyClass rubyClass) {
81
+ super(ruby, rubyClass);
82
+ }
83
+
84
+ private void addParametersToTransformer(ThreadContext context, Transformer transf, IRubyObject parameters) {
85
+ Ruby ruby = context.getRuntime();
86
+ RubyArray params = parameters.convertToArray();
87
+ int limit = params.getLength();
88
+ if(limit % 2 == 1) limit--;
89
+
90
+ for(int i = 0; i < limit; i+=2) {
91
+ String name = params.aref(ruby.newFixnum(i)).asJavaString();
92
+ String value = params.aref(ruby.newFixnum(i+1)).asJavaString();
93
+ transf.setParameter(name, unparseValue(value));
94
+ }
95
+ }
96
+
97
+ private Pattern p = Pattern.compile("'.{1,}'");
98
+
99
+ private String unparseValue(String orig) {
100
+ Matcher m = p.matcher(orig);
101
+ if ((orig.startsWith("\"") && orig.endsWith("\"")) || m.matches()) {
102
+ orig = orig.substring(1, orig.length()-1);
103
+ }
104
+
105
+ return orig;
106
+ }
107
+
108
+ @JRubyMethod(meta = true)
109
+ public static IRubyObject parse_stylesheet_doc(ThreadContext context, IRubyObject cls, IRubyObject document) {
110
+
111
+ Ruby ruby = context.getRuntime();
112
+
113
+ if(!(document instanceof XmlDocument)) {
114
+ throw ruby.newArgumentError("doc must be a Nokogiri::XML::Document instance");
115
+ }
116
+
117
+ XmlDocument xmlDoc = (XmlDocument) document;
118
+
119
+ RubyArray errors = (RubyArray) xmlDoc.getInstanceVariable("@errors");
120
+
121
+ if(!errors.isEmpty()) {
122
+ throw ruby.newRuntimeError(errors.first().asJavaString());
123
+ }
124
+
125
+ Document doc = ((XmlDocument) xmlDoc.dup_implementation(context, true)).getDocument();
126
+
127
+ XsltStylesheet xslt = new XsltStylesheet(ruby, (RubyClass) cls);
128
+
129
+ try {
130
+ if (factory == null) factory = TransformerFactory.newInstance();
131
+ xslt.sheet = factory.newTemplates(new DOMSource(doc));
132
+ } catch (TransformerConfigurationException ex) {
133
+ ruby.newRuntimeError("could not parse xslt stylesheet");
134
+ }
135
+
136
+ return xslt;
137
+ }
138
+
139
+ @JRubyMethod
140
+ public IRubyObject serialize(ThreadContext context, IRubyObject doc) {
141
+ return RuntimeHelpers.invoke(context,
142
+ RuntimeHelpers.invoke(context, doc, "root"),
143
+ "to_s");
144
+ }
145
+
146
+ @JRubyMethod(rest = true, required=1, optional=2)
147
+ public IRubyObject transform(ThreadContext context, IRubyObject[] args) {
148
+ Ruby runtime = context.getRuntime();
149
+
150
+ DOMSource docSource = new DOMSource(((XmlDocument) args[0]).getDocument());
151
+ DOMResult result = new DOMResult();
152
+
153
+ NokogiriXsltErrorListener elistener = new NokogiriXsltErrorListener();
154
+ try{
155
+ Transformer transf = this.sheet.newTransformer();
156
+ transf.setErrorListener(elistener);
157
+ if(args.length > 1) {
158
+ addParametersToTransformer(context, transf, args[1]);
159
+ }
160
+ transf.transform(docSource, result);
161
+ } catch(TransformerConfigurationException ex) {
162
+ // processes later
163
+ } catch(TransformerException ex) {
164
+ // processes later
165
+ }
166
+
167
+ switch (elistener.getErrorType()) {
168
+ case ERROR:
169
+ case FATAL:
170
+ throw runtime.newRuntimeError(elistener.getErrorMessage());
171
+ case WARNING:
172
+ default:
173
+ // no-op
174
+ }
175
+
176
+ if ("html".equals(result.getNode().getFirstChild().getNodeName())) {
177
+ HtmlDocument htmlDocument = (HtmlDocument) getNokogiriClass(runtime, "Nokogiri::HTML::Document").allocate();
178
+ htmlDocument.setNode(context, (Document) result.getNode());
179
+ return htmlDocument;
180
+ } else {
181
+ XmlDocument xmlDocument = (XmlDocument) NokogiriService.XML_DOCUMENT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Document"));
182
+ xmlDocument.setNode(context, (Document) result.getNode());
183
+ return xmlDocument;
184
+ }
185
+ }
186
+
187
+ @JRubyMethod(name = {"registr", "register"}, meta = true)
188
+ public static IRubyObject register(ThreadContext context, IRubyObject cls, IRubyObject uri, IRubyObject receiver) {
189
+ throw context.getRuntime().newNotImplementedError("Nokogiri::XSLT.register method is not implemented");
190
+ /* When API conflict is solved, this method should be below:
191
+ // ThreadContext is used while executing xslt extension function
192
+ registry.put("context", context);
193
+ registry.put("receiver", receiver);
194
+ return context.getRuntime().getNil();
195
+ */
196
+ }
197
+ }
@@ -0,0 +1,204 @@
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
+ import static nokogiri.internals.NokogiriHelpers.isNamespace;
37
+ import nokogiri.HtmlDocument;
38
+ import nokogiri.NokogiriService;
39
+ import nokogiri.XmlDocument;
40
+
41
+ import org.apache.xerces.parsers.DOMParser;
42
+ import org.apache.xerces.xni.Augmentations;
43
+ import org.apache.xerces.xni.QName;
44
+ import org.apache.xerces.xni.XMLAttributes;
45
+ import org.apache.xerces.xni.XNIException;
46
+ import org.apache.xerces.xni.parser.XMLDocumentFilter;
47
+ import org.apache.xerces.xni.parser.XMLParserConfiguration;
48
+ import org.cyberneko.html.HTMLConfiguration;
49
+ import org.cyberneko.html.filters.DefaultFilter;
50
+ import org.jruby.Ruby;
51
+ import org.jruby.RubyClass;
52
+ import org.jruby.runtime.ThreadContext;
53
+ import org.jruby.runtime.builtin.IRubyObject;
54
+ import org.w3c.dom.Document;
55
+
56
+ /**
57
+ * Parser for HtmlDocument. This class actually parses HtmlDocument using NekoHtml.
58
+ *
59
+ * @author sergio
60
+ * @author Patrick Mahoney <pat@polycrystal.org>
61
+ * @author Yoko Harada <yokolet@gmail.com>
62
+ */
63
+ public class HtmlDomParserContext extends XmlDomParserContext {
64
+
65
+ public HtmlDomParserContext(Ruby runtime, IRubyObject options) {
66
+ super(runtime, options);
67
+ }
68
+
69
+ public HtmlDomParserContext(Ruby runtime, IRubyObject encoding, IRubyObject options) {
70
+ super(runtime, encoding, options);
71
+ }
72
+
73
+ @Override
74
+ protected void initErrorHandler() {
75
+ if (options.strict) {
76
+ errorHandler = new NokogiriStrictErrorHandler(options.noError, options.noWarning);
77
+ } else {
78
+ errorHandler = new NokogiriNonStrictErrorHandler4NekoHtml(options.noError, options.noWarning);
79
+ }
80
+ }
81
+
82
+ @Override
83
+ protected void initParser(Ruby runtime) {
84
+ XMLParserConfiguration config = new HTMLConfiguration();
85
+ XMLDocumentFilter removeNSAttrsFilter = new RemoveNSAttrsFilter();
86
+ XMLDocumentFilter elementValidityCheckFilter = new ElementValidityCheckFilter(errorHandler);
87
+ XMLDocumentFilter[] filters = { removeNSAttrsFilter, elementValidityCheckFilter};
88
+
89
+ config.setErrorHandler(this.errorHandler);
90
+ parser = new DOMParser(config);
91
+
92
+ // see http://nekohtml.sourceforge.net/settings.html for details
93
+ setProperty("http://cyberneko.org/html/properties/default-encoding", java_encoding);
94
+ setProperty("http://cyberneko.org/html/properties/names/elems", "lower");
95
+ setProperty("http://cyberneko.org/html/properties/names/attrs", "lower");
96
+ setProperty("http://cyberneko.org/html/properties/filters", filters);
97
+ setFeature("http://cyberneko.org/html/features/report-errors", true);
98
+ setFeature("http://xml.org/sax/features/namespaces", false);
99
+ setFeature("http://cyberneko.org/html/features/insert-doctype", true);
100
+ }
101
+
102
+ /**
103
+ * Enable NekoHTML feature for balancing tags in a document fragment.
104
+ *
105
+ * This method is used in XmlNode#in_context method.
106
+ */
107
+ public void enableDocumentFragment() {
108
+ setFeature("http://cyberneko.org/html/features/balance-tags/document-fragment", true);
109
+ }
110
+
111
+ @Override
112
+ protected XmlDocument getNewEmptyDocument(ThreadContext context) {
113
+ IRubyObject[] args = new IRubyObject[0];
114
+ return (XmlDocument) XmlDocument.rbNew(context, getNokogiriClass(context.getRuntime(), "Nokogiri::HTML::Document"), args);
115
+ }
116
+
117
+ @Override
118
+ protected XmlDocument wrapDocument(ThreadContext context,
119
+ RubyClass klazz,
120
+ Document document) {
121
+ HtmlDocument htmlDocument = (HtmlDocument) NokogiriService.HTML_DOCUMENT_ALLOCATOR.allocate(context.getRuntime(), klazz);
122
+ htmlDocument.setNode(context, document);
123
+ htmlDocument.setEncoding(ruby_encoding);
124
+ return htmlDocument;
125
+ }
126
+
127
+ /**
128
+ * Filter to strip out attributes that pertain to XML namespaces.
129
+ */
130
+ public static class RemoveNSAttrsFilter extends DefaultFilter {
131
+ @Override
132
+ public void startElement(QName element, XMLAttributes attrs,
133
+ Augmentations augs) throws XNIException {
134
+ int i;
135
+ for (i = 0; i < attrs.getLength(); ++i) {
136
+ if (isNamespace(attrs.getQName(i))) {
137
+ attrs.removeAttributeAt(i);
138
+ --i;
139
+ }
140
+ }
141
+
142
+ element.uri = null;
143
+ super.startElement(element, attrs, augs);
144
+ }
145
+ }
146
+
147
+ public static class ElementValidityCheckFilter extends DefaultFilter {
148
+ private NokogiriErrorHandler errorHandler;
149
+
150
+ private ElementValidityCheckFilter(NokogiriErrorHandler errorHandler) {
151
+ this.errorHandler = errorHandler;
152
+ }
153
+
154
+ // element names from xhtml1-strict.dtd
155
+ private static String[][] element_names = {
156
+ {"a", "abbr", "acronym", "address", "area"},
157
+ {"b", "base", "basefont", "bdo", "big", "blockquote", "body", "br", "button"},
158
+ {"caption", "cite", "code", "col", "colgroup"},
159
+ {"dd", "del", "dfn", "div", "dl", "dt"},
160
+ {"em"},
161
+ {"fieldset", "font", "form", "frame", "frameset"},
162
+ {}, // g
163
+ {"h1", "h2", "h3", "h4", "h5", "h6", "head", "hr", "html"},
164
+ {"i", "iframe", "img", "input", "ins"},
165
+ {}, // j
166
+ {"kbd"},
167
+ {"label", "legend", "li", "link"},
168
+ {"map", "meta"},
169
+ {"noframes", "noscript"},
170
+ {"object", "ol", "optgroup", "option"},
171
+ {"p", "param", "pre"},
172
+ {"q"},
173
+ {}, // r
174
+ {"s", "samp", "script", "select", "small", "span", "strike", "strong", "style", "sub", "sup"},
175
+ {"table", "tbody", "td", "textarea", "tfoot", "th", "thead", "title", "tr", "tt"},
176
+ {"u", "ul"},
177
+ {"var"},
178
+ {}, // w
179
+ {}, // x
180
+ {}, // y
181
+ {} // z
182
+ };
183
+
184
+ private boolean isValid(String testee) {
185
+ char[] c = testee.toCharArray();
186
+ int index = new Integer(c[0]) - 97;
187
+ if (index > 25) return false;
188
+ for (int i=0; i<element_names[index].length; i++) {
189
+ if (testee.equals(element_names[index][i])) {
190
+ return true;
191
+ }
192
+ }
193
+ return false;
194
+ }
195
+
196
+ @Override
197
+ public void startElement(QName name, XMLAttributes attrs, Augmentations augs) throws XNIException {
198
+ if (!isValid(name.rawname)) {
199
+ errorHandler.getErrors().add(new Exception("Tag " + name.rawname + " invalid"));
200
+ }
201
+ super.startElement(name, attrs, augs);
202
+ }
203
+ }
204
+ }
@@ -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 java.util.Hashtable;
36
+ import nokogiri.XmlDocument;
37
+ import org.w3c.dom.Document;
38
+
39
+ /**
40
+ * Currently, this class is not used anywhere.
41
+ * I'm not sure what for this class was written.(Yoko)
42
+ *
43
+ * @author sergio
44
+ */
45
+ public class NokogiriDocumentCache {
46
+
47
+ private static NokogiriDocumentCache instance;
48
+ protected Hashtable<Document, XmlDocument> cache;
49
+
50
+ private NokogiriDocumentCache() {
51
+ this.cache = new Hashtable<Document, XmlDocument>();
52
+ }
53
+
54
+ public static NokogiriDocumentCache getInstance() {
55
+ if(instance == null) {
56
+ instance = new NokogiriDocumentCache();
57
+ }
58
+ return instance;
59
+ }
60
+
61
+ public XmlDocument getXmlDocument(Document doc) {
62
+ return this.cache.get(doc);
63
+ }
64
+
65
+ public void putDocument(Document doc, XmlDocument xmlDoc) {
66
+ this.cache.put(doc, xmlDoc);
67
+ }
68
+
69
+ public XmlDocument removeDocument(Document doc) {
70
+ return this.cache.remove(doc);
71
+ }
72
+
73
+ }