nokogiri 1.5.0.beta.4 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of nokogiri might be problematic. Click here for more details.

Files changed (107) hide show
  1. data/.gemtest +0 -0
  2. data/CHANGELOG.ja.rdoc +34 -0
  3. data/CHANGELOG.rdoc +40 -1
  4. data/Manifest.txt +11 -2
  5. data/README.rdoc +1 -1
  6. data/Rakefile +100 -104
  7. data/bin/nokogiri +1 -2
  8. data/ext/nokogiri/nokogiri.c +24 -1
  9. data/ext/nokogiri/xml_io.c +32 -7
  10. data/ext/nokogiri/xml_node.c +14 -13
  11. data/ext/nokogiri/xml_sax_parser.c +9 -4
  12. data/ext/nokogiri/xslt_stylesheet.c +7 -1
  13. data/lib/nokogiri.rb +3 -22
  14. data/lib/nokogiri/css.rb +4 -0
  15. data/lib/nokogiri/html/document.rb +10 -14
  16. data/lib/nokogiri/version.rb +76 -23
  17. data/lib/nokogiri/xml/builder.rb +7 -0
  18. data/lib/nokogiri/xml/document.rb +17 -1
  19. data/lib/nokogiri/xml/document_fragment.rb +14 -0
  20. data/lib/nokogiri/xml/node.rb +36 -28
  21. data/lib/nokogiri/xml/node/save_options.rb +17 -1
  22. data/lib/nokogiri/xml/node_set.rb +7 -0
  23. data/lib/nokogiri/xml/parse_options.rb +8 -0
  24. data/lib/nokogiri/xml/reader.rb +6 -6
  25. data/lib/nokogiri/xml/schema.rb +7 -1
  26. data/nokogiri_help_responses.md +40 -0
  27. data/tasks/cross_compile.rb +134 -159
  28. data/tasks/nokogiri.org.rb +18 -0
  29. data/tasks/test.rb +1 -1
  30. data/test/files/encoding.html +82 -0
  31. data/test/files/encoding.xhtml +84 -0
  32. data/test/files/metacharset.html +10 -0
  33. data/test/files/noencoding.html +47 -0
  34. data/test/helper.rb +2 -0
  35. data/test/html/test_document.rb +15 -0
  36. data/test/html/test_document_encoding.rb +13 -0
  37. data/test/test_memory_leak.rb +20 -0
  38. data/test/test_reader.rb +22 -0
  39. data/test/test_xslt_transforms.rb +6 -2
  40. data/test/xml/node/test_save_options.rb +10 -2
  41. data/test/xml/test_builder.rb +17 -0
  42. data/test/xml/test_document.rb +22 -0
  43. data/test/xml/test_node.rb +19 -1
  44. data/test/xml/test_node_reparenting.rb +16 -3
  45. data/test/xml/test_node_set.rb +34 -0
  46. data/test/xml/test_schema.rb +5 -0
  47. data/test/xslt/test_exception_handling.rb +37 -0
  48. metadata +185 -157
  49. data/deps.rip +0 -5
  50. data/ext/java/nokogiri/EncodingHandler.java +0 -124
  51. data/ext/java/nokogiri/HtmlDocument.java +0 -149
  52. data/ext/java/nokogiri/HtmlElementDescription.java +0 -145
  53. data/ext/java/nokogiri/HtmlEntityLookup.java +0 -79
  54. data/ext/java/nokogiri/HtmlSaxParserContext.java +0 -259
  55. data/ext/java/nokogiri/NokogiriService.java +0 -535
  56. data/ext/java/nokogiri/XmlAttr.java +0 -191
  57. data/ext/java/nokogiri/XmlAttributeDecl.java +0 -130
  58. data/ext/java/nokogiri/XmlCdata.java +0 -91
  59. data/ext/java/nokogiri/XmlComment.java +0 -86
  60. data/ext/java/nokogiri/XmlDocument.java +0 -529
  61. data/ext/java/nokogiri/XmlDocumentFragment.java +0 -217
  62. data/ext/java/nokogiri/XmlDtd.java +0 -467
  63. data/ext/java/nokogiri/XmlElement.java +0 -222
  64. data/ext/java/nokogiri/XmlElementContent.java +0 -382
  65. data/ext/java/nokogiri/XmlElementDecl.java +0 -148
  66. data/ext/java/nokogiri/XmlEntityDecl.java +0 -162
  67. data/ext/java/nokogiri/XmlEntityReference.java +0 -75
  68. data/ext/java/nokogiri/XmlNamespace.java +0 -128
  69. data/ext/java/nokogiri/XmlNode.java +0 -1399
  70. data/ext/java/nokogiri/XmlNodeSet.java +0 -311
  71. data/ext/java/nokogiri/XmlProcessingInstruction.java +0 -103
  72. data/ext/java/nokogiri/XmlReader.java +0 -411
  73. data/ext/java/nokogiri/XmlRelaxng.java +0 -144
  74. data/ext/java/nokogiri/XmlSaxParserContext.java +0 -367
  75. data/ext/java/nokogiri/XmlSaxPushParser.java +0 -184
  76. data/ext/java/nokogiri/XmlSchema.java +0 -319
  77. data/ext/java/nokogiri/XmlSyntaxError.java +0 -119
  78. data/ext/java/nokogiri/XmlText.java +0 -136
  79. data/ext/java/nokogiri/XmlXpathContext.java +0 -179
  80. data/ext/java/nokogiri/XsltStylesheet.java +0 -183
  81. data/ext/java/nokogiri/internals/HtmlDomParserContext.java +0 -206
  82. data/ext/java/nokogiri/internals/NokogiriDocumentCache.java +0 -73
  83. data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +0 -86
  84. data/ext/java/nokogiri/internals/NokogiriHandler.java +0 -327
  85. data/ext/java/nokogiri/internals/NokogiriHelpers.java +0 -582
  86. data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +0 -171
  87. data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +0 -118
  88. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +0 -74
  89. data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +0 -121
  90. data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +0 -79
  91. data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +0 -126
  92. data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +0 -56
  93. data/ext/java/nokogiri/internals/ParserContext.java +0 -278
  94. data/ext/java/nokogiri/internals/PushInputStream.java +0 -411
  95. data/ext/java/nokogiri/internals/ReaderNode.java +0 -474
  96. data/ext/java/nokogiri/internals/SaveContext.java +0 -288
  97. data/ext/java/nokogiri/internals/SchemaErrorHandler.java +0 -76
  98. data/ext/java/nokogiri/internals/XmlDeclHandler.java +0 -42
  99. data/ext/java/nokogiri/internals/XmlDomParser.java +0 -77
  100. data/ext/java/nokogiri/internals/XmlDomParserContext.java +0 -238
  101. data/ext/java/nokogiri/internals/XmlSaxParser.java +0 -65
  102. data/ext/java/nokogiri/internals/XsltExtensionFunction.java +0 -72
  103. data/lib/isorelax.jar +0 -0
  104. data/lib/jing.jar +0 -0
  105. data/lib/nekodtd.jar +0 -0
  106. data/lib/nekohtml.jar +0 -0
  107. data/lib/xercesImpl.jar +0 -0
@@ -1,311 +0,0 @@
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
- import static nokogiri.internals.NokogiriHelpers.nodeListToRubyArray;
37
-
38
- import java.util.List;
39
-
40
- import org.jruby.Ruby;
41
- import org.jruby.RubyArray;
42
- import org.jruby.RubyClass;
43
- import org.jruby.RubyObject;
44
- import org.jruby.anno.JRubyClass;
45
- import org.jruby.anno.JRubyMethod;
46
- import org.jruby.javasupport.util.RuntimeHelpers;
47
- import org.jruby.runtime.Block;
48
- import org.jruby.runtime.ThreadContext;
49
- import org.jruby.runtime.builtin.IRubyObject;
50
- import org.w3c.dom.Node;
51
- import org.w3c.dom.NodeList;
52
-
53
- /**
54
- * Class for Nokogiri::XML::NodeSet
55
- *
56
- * @author sergio
57
- * @author Yoko Harada <yokolet@gmail.com>
58
- */
59
- @JRubyClass(name="Nokogiri::XML::NodeSet")
60
- public class XmlNodeSet extends RubyObject {
61
- private NodeList nodeList;
62
- private RubyArray nodes;
63
- private IRubyObject doc;
64
-
65
- public XmlNodeSet(Ruby ruby, RubyClass klazz) {
66
- super(ruby, klazz);
67
- }
68
-
69
- /**
70
- * Create and return a copy of this object.
71
- *
72
- * @return a clone of this object
73
- */
74
- @Override
75
- public Object clone() throws CloneNotSupportedException {
76
- return super.clone();
77
- }
78
-
79
- public XmlNodeSet(Ruby ruby, NodeList nodes) {
80
- this(ruby, getNokogiriClass(ruby, "Nokogiri::XML::NodeSet"), nodes);
81
- }
82
-
83
- public XmlNodeSet(Ruby ruby, RubyArray nodes) {
84
- this(ruby, getNokogiriClass(ruby, "Nokogiri::XML::NodeSet"), nodes);
85
- }
86
-
87
- public XmlNodeSet(Ruby ruby, RubyClass rubyClass, NodeList nodes) {
88
- this(ruby, rubyClass, nodeListToRubyArray(ruby, nodes));
89
- nodeList = nodes;
90
- }
91
-
92
- public XmlNodeSet(Ruby ruby, RubyClass rubyClass, RubyArray nodes){
93
- super(ruby, rubyClass);
94
- this.nodes = nodes;
95
-
96
- IRubyObject first = nodes.first();
97
- initialize(ruby, first);
98
- }
99
-
100
- public XmlNodeSet(Ruby ruby, XmlNodeSet reference){
101
- super(ruby, getNokogiriClass(ruby, "Nokogiri::XML::NodeSet"));
102
- this.nodes = null;
103
-
104
- IRubyObject first = reference.nodes.first();
105
- initialize(ruby, first);
106
- }
107
-
108
- void setNodes(RubyArray nodes) {
109
- this.nodes = nodes;
110
- nodeList = null;
111
- }
112
-
113
- public void setInitialNodes(RubyArray nodes) {
114
- this.nodes = nodes;
115
- IRubyObject first = nodes.first();
116
- initialize(getRuntime(), first);
117
- }
118
-
119
- public void setNodeList(NodeList nodeList) {
120
- setInitialNodes(nodeListToRubyArray(getRuntime(), nodeList));
121
- this.nodeList = nodeList;
122
- }
123
-
124
- private void initialize(Ruby ruby, IRubyObject refNode) {
125
- if (refNode instanceof XmlNode) {
126
- XmlNode n = (XmlNode)refNode;
127
- doc = n.document(ruby.getCurrentContext());
128
- setInstanceVariable("@document", doc);
129
- if (doc != null) {
130
- RuntimeHelpers.invoke(ruby.getCurrentContext(), doc, "decorate", this);
131
- }
132
- }
133
- }
134
-
135
- public static IRubyObject newEmptyNodeSet(ThreadContext context) {
136
- return (XmlNodeSet)NokogiriService.XML_NODESET_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::NodeSet"));
137
- }
138
-
139
- public long length() {
140
- if (nodes == null) return 0L;
141
- return nodes.length().getLongValue();
142
- }
143
-
144
- public void relink_namespace(ThreadContext context) {
145
- nodeList = null;
146
- List<?> n = nodes.getList();
147
-
148
- for (int i = 0; i < n.size(); i++) {
149
- if (n.get(i) instanceof XmlNode) {
150
- ((XmlNode) n.get(i)).relink_namespace(context);
151
- }
152
- }
153
- }
154
-
155
- public void setDocument(IRubyObject document) {
156
- setInstanceVariable("@document", document);
157
- this.doc = document;
158
- }
159
-
160
- public NodeList toNodeList(Ruby ruby) {
161
- if (nodeList != null) return nodeList;
162
- return new NokogiriNodeList(ruby, this.nodes);
163
- }
164
-
165
- @JRubyMethod(name="&")
166
- public IRubyObject and(ThreadContext context, IRubyObject nodeSet){
167
- nodeList = null;
168
- return newXmlNodeSet(context, (RubyArray) nodes.op_and(asXmlNodeSet(context, nodeSet).nodes));
169
- }
170
-
171
- @JRubyMethod
172
- public IRubyObject delete(ThreadContext context, IRubyObject node_or_namespace){
173
- nodeList = null;
174
- return nodes.delete(context, asXmlNodeOrNamespace(context, node_or_namespace), Block.NULL_BLOCK);
175
- }
176
-
177
- @JRubyMethod
178
- public IRubyObject dup(ThreadContext context){
179
- return newXmlNodeSet(context, nodes.aryDup());
180
- }
181
-
182
- @JRubyMethod(name = "include?")
183
- public IRubyObject include_p(ThreadContext context, IRubyObject node_or_namespace){
184
- return nodes.include_p(context, asXmlNodeOrNamespace(context, node_or_namespace));
185
- }
186
-
187
- @JRubyMethod(name = {"length", "size"})
188
- public IRubyObject length(ThreadContext context) {
189
- if (nodes == null) setInitialNodes(RubyArray.newEmptyArray(context.getRuntime()));
190
- return nodes.length();
191
- }
192
-
193
- @JRubyMethod(name="-")
194
- public IRubyObject op_diff(ThreadContext context, IRubyObject nodeSet){
195
- nodeList = null;
196
- XmlNodeSet xmlNodeSet = newXmlNodeSet(context, this);
197
- xmlNodeSet.setNodes((RubyArray) nodes.op_diff(asXmlNodeSet(context, nodeSet).nodes));
198
- return xmlNodeSet;
199
- }
200
-
201
- @JRubyMethod(name={"|", "+"})
202
- public IRubyObject op_or(ThreadContext context, IRubyObject nodeSet){
203
- nodeList = null;
204
- if (nodes == null) setInitialNodes(RubyArray.newEmptyArray(context.getRuntime()));
205
- return newXmlNodeSet(context, (RubyArray) nodes.op_or(asXmlNodeSet(context, nodeSet).nodes));
206
- }
207
-
208
- @JRubyMethod(name = {"push", "<<"})
209
- public IRubyObject push(ThreadContext context, IRubyObject node_or_namespace) {
210
- nodeList = null;
211
- if (nodes == null) setInitialNodes(RubyArray.newEmptyArray(context.getRuntime()));
212
- nodes.append(asXmlNodeOrNamespace(context, node_or_namespace));
213
- return this;
214
- }
215
-
216
- @JRubyMethod(name={"[]", "slice"})
217
- public IRubyObject slice(ThreadContext context, IRubyObject indexOrRange){
218
- IRubyObject result;
219
- if (context.getRuntime().is1_9()) {
220
- result = nodes.aref19(indexOrRange);
221
- } else {
222
- result = nodes.aref(indexOrRange);
223
- }
224
- if (result instanceof RubyArray) {
225
- return newXmlNodeSet(context, (RubyArray)result);
226
- } else {
227
- return result;
228
- }
229
- }
230
-
231
- @JRubyMethod(name={"[]", "slice"})
232
- public IRubyObject slice(ThreadContext context, IRubyObject start, IRubyObject length){
233
- IRubyObject result;
234
- if (context.getRuntime().is1_9()) {
235
- result = nodes.aref19(start, length);
236
- } else {
237
- result = nodes.aref(start, length);
238
- }
239
- if (result instanceof RubyArray) return newXmlNodeSet(context, (RubyArray)result);
240
- else return context.getRuntime().getNil();
241
- }
242
-
243
- @JRubyMethod(name = {"to_a", "to_ary"})
244
- public IRubyObject to_a(ThreadContext context){
245
- return nodes;
246
- }
247
-
248
- @JRubyMethod(name = {"unlink", "remove"})
249
- public IRubyObject unlink(ThreadContext context){
250
- nodeList = null;
251
- IRubyObject[] arr = this.nodes.toJavaArrayUnsafe();
252
- long length = arr.length;
253
- for (int i = 0; i < length; i++) {
254
- if (arr[i] instanceof XmlNode) {
255
- ((XmlNode) arr[i] ).unlink(context);
256
- }
257
- }
258
- return this;
259
- }
260
-
261
- private XmlNodeSet newXmlNodeSet(ThreadContext context, RubyArray array) {
262
- XmlNodeSet xmlNodeSet = (XmlNodeSet)NokogiriService.XML_NODESET_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::NodeSet"));
263
- xmlNodeSet.setInitialNodes(array);
264
- return xmlNodeSet;
265
- }
266
-
267
- private XmlNodeSet newXmlNodeSet(ThreadContext context, XmlNodeSet reference) {
268
- XmlNodeSet result = new XmlNodeSet(context.getRuntime(), reference);
269
- return result;
270
- }
271
-
272
- private IRubyObject asXmlNodeOrNamespace(ThreadContext context, IRubyObject possibleNode) {
273
- if (possibleNode instanceof XmlNode || possibleNode instanceof XmlNamespace) {
274
- return possibleNode;
275
- } else {
276
- throw context.getRuntime().newArgumentError("node must be a Nokogiri::XML::Node or Nokogiri::XML::Namespace");
277
- }
278
- }
279
-
280
- private XmlNodeSet asXmlNodeSet(ThreadContext context, IRubyObject possibleNodeSet) {
281
- // if(!(possibleNodeSet instanceof XmlNodeSet)) {
282
- if(!RuntimeHelpers.invoke(context, possibleNodeSet, "is_a?",
283
- getNokogiriClass(context.getRuntime(), "Nokogiri::XML::NodeSet")).isTrue()) {
284
- throw context.getRuntime().newArgumentError("node must be a Nokogiri::XML::NodeSet");
285
- }
286
- XmlNodeSet xmlNodeSet = (XmlNodeSet)possibleNodeSet;
287
- if (xmlNodeSet.nodes == null) xmlNodeSet.setInitialNodes(RubyArray.newEmptyArray(context.getRuntime()));
288
- return xmlNodeSet;
289
- }
290
-
291
- class NokogiriNodeList implements NodeList{
292
-
293
- private final RubyArray nodes;
294
- private final Ruby ruby;
295
-
296
- public NokogiriNodeList(Ruby ruby, RubyArray nodes) {
297
- this.nodes = nodes;
298
- this.ruby = ruby;
299
- }
300
-
301
- public Node item(int i) {
302
- return XmlNode.getNodeFromXmlNode(ruby.getCurrentContext(),
303
- this.nodes.aref(ruby.newFixnum(i)));
304
- }
305
-
306
- public int getLength() {
307
- return this.nodes.getLength();
308
- }
309
-
310
- }
311
- }
@@ -1,103 +0,0 @@
1
- /**
2
- * (The MIT License)
3
- *
4
- * Copyright (c) 2008 - 2011:
5
- *
6
- * * {Aaron Patterson}[http://tenderlovemaking.com]
7
- * * {Mike Dalessio}[http://mike.daless.io]
8
- * * {Charles Nutter}[http://blog.headius.com]
9
- * * {Sergio Arbeo}[http://www.serabe.com]
10
- * * {Patrick Mahoney}[http://polycrystal.org]
11
- * * {Yoko Harada}[http://yokolet.blogspot.com]
12
- *
13
- * Permission is hereby granted, free of charge, to any person obtaining
14
- * a copy of this software and associated documentation files (the
15
- * 'Software'), to deal in the Software without restriction, including
16
- * without limitation the rights to use, copy, modify, merge, publish,
17
- * distribute, sublicense, and/or sell copies of the Software, and to
18
- * permit persons to whom the Software is furnished to do so, subject to
19
- * the following conditions:
20
- *
21
- * The above copyright notice and this permission notice shall be
22
- * included in all copies or substantial portions of the Software.
23
- *
24
- * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
25
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
28
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
- */
32
-
33
- package nokogiri;
34
-
35
- import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
36
- import nokogiri.internals.SaveContext;
37
-
38
- import org.jruby.Ruby;
39
- import org.jruby.RubyClass;
40
- import org.jruby.anno.JRubyClass;
41
- import org.jruby.anno.JRubyMethod;
42
- import org.jruby.javasupport.util.RuntimeHelpers;
43
- import org.jruby.runtime.ThreadContext;
44
- import org.jruby.runtime.builtin.IRubyObject;
45
- import org.w3c.dom.Document;
46
- import org.w3c.dom.Node;
47
-
48
- /**
49
- * Class for Nokogiri::XML::ProcessingInstruction
50
- *
51
- * @author sergio
52
- */
53
- @JRubyClass(name="Nokogiri::XML::ProcessingInstruction", parent="Nokogiri::XML::Node")
54
- public class XmlProcessingInstruction extends XmlNode {
55
-
56
- public XmlProcessingInstruction(Ruby ruby, RubyClass klass, Node node) {
57
- super(ruby, klass, node);
58
- }
59
-
60
- @JRubyMethod(name="new", meta=true, rest=true, required=3)
61
- public static IRubyObject rbNew(ThreadContext context,
62
- IRubyObject klass,
63
- IRubyObject[] args) {
64
-
65
- IRubyObject doc = args[0];
66
- IRubyObject target = args[1];
67
- IRubyObject data = args[2];
68
-
69
- Document document = ((XmlNode) doc).getOwnerDocument();
70
- Node node =
71
- document.createProcessingInstruction(rubyStringToString(target),
72
- rubyStringToString(data));
73
- XmlProcessingInstruction self =
74
- new XmlProcessingInstruction(context.getRuntime(),
75
- (RubyClass) klass,
76
- node);
77
-
78
- RuntimeHelpers.invoke(context, self, "initialize", args);
79
-
80
- // TODO: if_block_given.
81
-
82
- return self;
83
- }
84
-
85
- @Override
86
- public boolean isProcessingInstruction() { return true; }
87
-
88
- @Override
89
- public void saveContent(ThreadContext context, SaveContext ctx) {
90
- ctx.append("<?");
91
- ctx.append(node_name(context).convertToString().asJavaString());
92
- IRubyObject content = content(context);
93
- if(!content.isNil()) {
94
- if (ctx.asHtml()) ctx.append(" ");
95
- ctx.append(content.convertToString().asJavaString());
96
- }
97
- if (ctx.asHtml())
98
- ctx.append(">");
99
- else
100
- ctx.append("?>");
101
- }
102
-
103
- }
@@ -1,411 +0,0 @@
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.stringOrBlank;
36
-
37
- import java.io.ByteArrayInputStream;
38
- import java.io.IOException;
39
- import java.util.ArrayDeque;
40
- import java.util.Stack;
41
-
42
- import nokogiri.internals.ReaderNode;
43
- import nokogiri.internals.ReaderNode.ElementNode;
44
-
45
- import org.jruby.Ruby;
46
- import org.jruby.RubyArray;
47
- import org.jruby.RubyBoolean;
48
- import org.jruby.RubyClass;
49
- import org.jruby.RubyFixnum;
50
- import org.jruby.RubyModule;
51
- import org.jruby.RubyObject;
52
- import org.jruby.RubyString;
53
- import org.jruby.anno.JRubyClass;
54
- import org.jruby.anno.JRubyMethod;
55
- import org.jruby.exceptions.RaiseException;
56
- import org.jruby.javasupport.util.RuntimeHelpers;
57
- import org.jruby.runtime.Block;
58
- import org.jruby.runtime.ThreadContext;
59
- import org.jruby.runtime.builtin.IRubyObject;
60
- import org.jruby.util.ByteList;
61
- import org.xml.sax.Attributes;
62
- import org.xml.sax.InputSource;
63
- import org.xml.sax.SAXException;
64
- import org.xml.sax.SAXParseException;
65
- import org.xml.sax.XMLReader;
66
- import org.xml.sax.ext.DefaultHandler2;
67
- import org.xml.sax.helpers.XMLReaderFactory;
68
-
69
- /**
70
- * Class for Nokogiri:XML::Reader
71
- *
72
- * @author sergio
73
- * @author Yoko Harada <yokolet@gmail.com>
74
- */
75
- @JRubyClass(name="Nokogiri::XML::Reader")
76
- public class XmlReader extends RubyObject {
77
-
78
- private static final int XML_TEXTREADER_MODE_INITIAL = 0;
79
- private static final int XML_TEXTREADER_MODE_INTERACTIVE = 1;
80
- private static final int XML_TEXTREADER_MODE_ERROR = 2;
81
- private static final int XML_TEXTREADER_MODE_EOF = 3;
82
- private static final int XML_TEXTREADER_MODE_CLOSED = 4;
83
- private static final int XML_TEXTREADER_MODE_READING = 5;
84
-
85
- final ArrayDeque<ReaderNode> nodeQueue = new ArrayDeque<ReaderNode>();
86
- private int state;
87
-
88
- public XmlReader(Ruby ruby, RubyClass rubyClass) {
89
- super(ruby, rubyClass);
90
- nodeQueue.add(new ReaderNode.EmptyNode(ruby));
91
- }
92
-
93
- private static IRubyObject[] getArgs(IRubyObject[] args) {
94
- int size = Math.min(args.length, 3);
95
- IRubyObject[] newArgs = new IRubyObject[size];
96
- for(int i = 0; i < size; i++)
97
- newArgs[i] = args[i];
98
- return newArgs;
99
- }
100
-
101
- private void parseRubyString(ThreadContext context, RubyString content){
102
- Ruby ruby = context.getRuntime();
103
- try {
104
- this.setState(XML_TEXTREADER_MODE_READING);
105
- XMLReader reader = this.createReader(ruby);
106
- ByteList byteList = content.getByteList();
107
- ByteArrayInputStream bais = new ByteArrayInputStream(byteList.unsafeBytes(), byteList.begin(), byteList.length());
108
- reader.parse(new InputSource(bais));
109
- this.setState(XML_TEXTREADER_MODE_CLOSED);
110
- } catch (SAXParseException spe) {
111
- this.setState(XML_TEXTREADER_MODE_ERROR);
112
- this.nodeQueue.add(new ReaderNode.ExceptionNode(ruby, spe));
113
- } catch (IOException ioe) {
114
- throw RaiseException.createNativeRaiseException(ruby, ioe);
115
- } catch (SAXException saxe) {
116
- throw RaiseException.createNativeRaiseException(ruby, saxe);
117
- }
118
- }
119
-
120
- private void setSource(IRubyObject source){
121
- this.setInstanceVariable("@source", source);
122
- }
123
-
124
- private void setState(int state) { this.state = state; }
125
-
126
- @JRubyMethod
127
- public IRubyObject attribute(ThreadContext context, IRubyObject name) {
128
- return nodeQueue.peek().getAttributeByName(name);
129
- }
130
-
131
- @JRubyMethod
132
- public IRubyObject attribute_at(ThreadContext context, IRubyObject index) {
133
- return nodeQueue.peek().getAttributeByIndex(index);
134
- }
135
-
136
- @JRubyMethod
137
- public IRubyObject attribute_count(ThreadContext context) {
138
- return nodeQueue.peek().getAttributeCount();
139
- }
140
-
141
- @JRubyMethod
142
- public IRubyObject attribute_nodes(ThreadContext context) {
143
- return nodeQueue.peek().getAttributesNodes();
144
- }
145
-
146
- @JRubyMethod
147
- public IRubyObject attr_nodes(ThreadContext context) {
148
- return nodeQueue.peek().getAttributesNodes();
149
- }
150
-
151
- @JRubyMethod(name = "attributes?")
152
- public IRubyObject attributes_p(ThreadContext context) {
153
- return nodeQueue.peek().hasAttributes();
154
- }
155
-
156
- @JRubyMethod
157
- public IRubyObject base_uri(ThreadContext context) {
158
- return nodeQueue.peek().getXmlBase();
159
- }
160
-
161
- @JRubyMethod(name="default?")
162
- public IRubyObject default_p(ThreadContext context){
163
- return nodeQueue.peek().isDefault();
164
- }
165
-
166
- @JRubyMethod
167
- public IRubyObject depth(ThreadContext context) {
168
- return nodeQueue.peek().getDepth();
169
- }
170
-
171
- @JRubyMethod(name = {"empty_element?", "self_closing?"})
172
- public IRubyObject empty_element_p(ThreadContext context) {
173
- ReaderNode readerNode = nodeQueue.peek();
174
- if (readerNode == null) return context.getRuntime().getNil();
175
- if (!(readerNode instanceof ElementNode)) context.getRuntime().getFalse();
176
- return RubyBoolean.newBoolean(context.getRuntime(), !readerNode.hasChildren);
177
- }
178
-
179
- @JRubyMethod(meta = true, rest = true)
180
- public static IRubyObject from_io(ThreadContext context, IRubyObject cls, IRubyObject args[]) {
181
-
182
- // Only to pass the source test.
183
- Ruby ruby = context.getRuntime();
184
-
185
- // Not nil allowed!
186
- if(args[0].isNil()) throw ruby.newArgumentError("io cannot be nil");
187
-
188
- XmlReader r = new XmlReader(ruby, ((RubyModule) ruby.getModule("Nokogiri").getConstant("XML")).getClass("Reader"));
189
-
190
- r.callInit(getArgs(args), Block.NULL_BLOCK);
191
-
192
- r.setSource(args[0]);
193
-
194
- RubyString content = RuntimeHelpers.invoke(context, args[0], "read").convertToString();
195
-
196
- r.parseRubyString(context, content);
197
- return r;
198
- }
199
-
200
- @JRubyMethod(meta = true, rest = true)
201
- public static IRubyObject from_memory(ThreadContext context, IRubyObject cls, IRubyObject args[]) {
202
- Ruby ruby = context.getRuntime();
203
-
204
- // Not nil allowed!
205
- if(args[0].isNil()) throw ruby.newArgumentError("string cannot be nil");
206
-
207
- XmlReader r = new XmlReader(ruby, ((RubyModule) ruby.getModule("Nokogiri").getConstant("XML")).getClass("Reader"));
208
-
209
- r.callInit(getArgs(args), Block.NULL_BLOCK);
210
-
211
- r.setSource(args[0]);
212
-
213
- r.parseRubyString(context, args[0].convertToString());
214
-
215
- return r;
216
- }
217
-
218
- @JRubyMethod
219
- public IRubyObject node_type(ThreadContext context) {
220
- IRubyObject node_type = nodeQueue.peek().getNodeType();
221
- return node_type == null ? RubyFixnum.zero(context.getRuntime()) : node_type;
222
- }
223
-
224
- @JRubyMethod
225
- public IRubyObject inner_xml(ThreadContext context) {
226
- return stringOrBlank(context.getRuntime(), getInnerXml(nodeQueue, nodeQueue.peek()));
227
- }
228
-
229
- private String getInnerXml(ArrayDeque<ReaderNode> nodeQueue, ReaderNode current) {
230
- if (current.depth < 0) return null;
231
- if (!current.hasChildren) return null;
232
- StringBuffer sb = new StringBuffer();
233
- int currentDepth = (Integer)current.depth;
234
- for (ReaderNode node : nodeQueue) {
235
- if (((Integer)node.depth) > currentDepth) sb.append(node.getString());
236
- }
237
- return new String(sb);
238
- }
239
-
240
- @JRubyMethod
241
- public IRubyObject outer_xml(ThreadContext context) {
242
- return stringOrBlank(context.getRuntime(), getOuterXml(nodeQueue, nodeQueue.peek()));
243
- }
244
-
245
- private String getOuterXml(ArrayDeque<ReaderNode> nodeQueue, ReaderNode current) {
246
- if (current.depth < 0) return null;
247
- StringBuffer sb = new StringBuffer();
248
- int initialDepth = (Integer)current.depth - 1;
249
- for (ReaderNode node : nodeQueue) {
250
- if (((Integer)node.depth) > initialDepth) sb.append(node.getString());
251
- }
252
- return new String(sb);
253
- }
254
-
255
- @JRubyMethod
256
- public IRubyObject lang(ThreadContext context) {
257
- return nodeQueue.peek().getLang();
258
- }
259
-
260
- @JRubyMethod
261
- public IRubyObject local_name(ThreadContext context) {
262
- return nodeQueue.peek().getLocalName();
263
- }
264
-
265
- @JRubyMethod
266
- public IRubyObject name(ThreadContext context) {
267
- return nodeQueue.peek().getName();
268
- }
269
-
270
- @JRubyMethod
271
- public IRubyObject namespace_uri(ThreadContext context) {
272
- return nodeQueue.peek().getUri();
273
- }
274
-
275
- @JRubyMethod
276
- public IRubyObject namespaces(ThreadContext context) {
277
- return nodeQueue.peek().getNamespaces(context);
278
- }
279
-
280
- @JRubyMethod
281
- public IRubyObject prefix(ThreadContext context) {
282
- return nodeQueue.peek().getPrefix();
283
- }
284
-
285
- @JRubyMethod
286
- public IRubyObject read(ThreadContext context) {
287
- this.nodeQueue.poll();
288
- if(nodeQueue.peek() == null) {
289
- return context.getRuntime().getNil();
290
- } else if(nodeQueue.peek().isError()) {
291
- RubyArray errors = (RubyArray) this.getInstanceVariable("@errors");
292
- errors.append(nodeQueue.peek().toSyntaxError());
293
-
294
- this.setInstanceVariable("@errors", errors);
295
-
296
- throw new RaiseException((XmlSyntaxError) nodeQueue.peek().toSyntaxError());
297
- } else {
298
- return this;
299
- }
300
- }
301
-
302
- @JRubyMethod
303
- public IRubyObject state(ThreadContext context) {
304
- return context.getRuntime().newFixnum(this.state);
305
- }
306
-
307
- @JRubyMethod
308
- public IRubyObject value(ThreadContext context) {
309
- return nodeQueue.peek().getValue();
310
- }
311
-
312
- @JRubyMethod(name = "value?")
313
- public IRubyObject value_p(ThreadContext context) {
314
- return nodeQueue.peek().hasValue();
315
- }
316
-
317
- @JRubyMethod
318
- public IRubyObject xml_version(ThreadContext context) {
319
- return nodeQueue.peek().getXmlVersion();
320
- }
321
-
322
- protected XMLReader createReader(final Ruby ruby) {
323
- DefaultHandler2 handler = new DefaultHandler2() {
324
-
325
- Stack<String> langStack;
326
- int depth;
327
- Stack<String> xmlBaseStack;
328
- Stack<ReaderNode.ElementNode> elementStack;
329
-
330
- @Override
331
- public void characters(char[] chars, int start, int length) {
332
- ReaderNode.TextNode node = new ReaderNode.TextNode(ruby, new String(chars, start, length), depth, langStack, xmlBaseStack);
333
- nodeQueue.add(node);
334
- }
335
-
336
- @Override
337
- public void endDocument() throws SAXException {
338
- langStack = null;
339
- xmlBaseStack = null;
340
- elementStack = null;
341
- }
342
-
343
- @Override
344
- public void endElement(String uri, String localName, String qName) {
345
- depth--;
346
- ReaderNode previous = nodeQueue.getLast();
347
- ElementNode startElementNode = elementStack.pop();
348
- if (previous instanceof ReaderNode.ElementNode && qName.equals(previous.name)) {
349
- previous.hasChildren = false;
350
- } else {
351
- ReaderNode node = new ReaderNode.ClosingNode(ruby, uri, localName, qName, depth, langStack, xmlBaseStack);
352
- if (startElementNode != null) {
353
- node.attributeList = startElementNode.attributeList;
354
- node.namespaces = startElementNode.namespaces;
355
- }
356
- nodeQueue.add(node);
357
- }
358
- if (!langStack.isEmpty()) langStack.pop();
359
- if (!xmlBaseStack.isEmpty()) xmlBaseStack.pop();
360
- }
361
-
362
- @Override
363
- public void error(SAXParseException ex) throws SAXParseException {
364
- nodeQueue.add(new ReaderNode.ExceptionNode(ruby, ex));
365
- throw ex;
366
- }
367
-
368
- @Override
369
- public void fatalError(SAXParseException ex) throws SAXParseException {
370
- nodeQueue.add(new ReaderNode.ExceptionNode(ruby, ex));
371
- throw ex;
372
- }
373
-
374
- @Override
375
- public void startDocument() {
376
- depth = 0;
377
- langStack = new Stack<String>();
378
- xmlBaseStack = new Stack<String>();
379
- elementStack = new Stack<ReaderNode.ElementNode>();
380
- }
381
-
382
- @Override
383
- public void startElement(String uri, String localName, String qName, Attributes attrs) {
384
- ReaderNode readerNode = new ReaderNode.ElementNode(ruby, uri, localName, qName, attrs, depth, langStack, xmlBaseStack);
385
- nodeQueue.add(readerNode);
386
- depth++;
387
- if (readerNode.lang != null) langStack.push(readerNode.lang);
388
- if (readerNode.xmlBase != null) xmlBaseStack.push(readerNode.xmlBase);
389
- elementStack.push((ReaderNode.ElementNode)readerNode);
390
- }
391
-
392
- @Override
393
- public void warning(SAXParseException ex) throws SAXParseException {
394
- nodeQueue.add(new ReaderNode.ExceptionNode(ruby, ex));
395
- throw ex;
396
- }
397
- };
398
- try {
399
- XMLReader reader = XMLReaderFactory.createXMLReader();
400
- reader.setContentHandler(handler);
401
- reader.setDTDHandler(handler);
402
- reader.setErrorHandler(handler);
403
- reader.setFeature("http://xml.org/sax/features/xmlns-uris", true);
404
- reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
405
- reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
406
- return reader;
407
- } catch (SAXException saxe) {
408
- throw RaiseException.createNativeRaiseException(ruby, saxe);
409
- }
410
- }
411
- }