nokogiri-maven 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.
- data/CHANGELOG.ja.rdoc +544 -0
- data/CHANGELOG.rdoc +532 -0
- data/Manifest.txt +283 -0
- data/README.ja.rdoc +106 -0
- data/README.rdoc +174 -0
- data/Rakefile +164 -0
- data/bin/nokogiri +53 -0
- data/ext/java/nokogiri/EncodingHandler.java +124 -0
- data/ext/java/nokogiri/HtmlDocument.java +119 -0
- data/ext/java/nokogiri/HtmlElementDescription.java +145 -0
- data/ext/java/nokogiri/HtmlEntityLookup.java +79 -0
- data/ext/java/nokogiri/HtmlSaxParserContext.java +259 -0
- data/ext/java/nokogiri/NokogiriService.java +590 -0
- data/ext/java/nokogiri/XmlAttr.java +180 -0
- data/ext/java/nokogiri/XmlAttributeDecl.java +130 -0
- data/ext/java/nokogiri/XmlCdata.java +84 -0
- data/ext/java/nokogiri/XmlComment.java +86 -0
- data/ext/java/nokogiri/XmlDocument.java +519 -0
- data/ext/java/nokogiri/XmlDocumentFragment.java +223 -0
- data/ext/java/nokogiri/XmlDtd.java +469 -0
- data/ext/java/nokogiri/XmlElement.java +195 -0
- data/ext/java/nokogiri/XmlElementContent.java +382 -0
- data/ext/java/nokogiri/XmlElementDecl.java +152 -0
- data/ext/java/nokogiri/XmlEntityDecl.java +162 -0
- data/ext/java/nokogiri/XmlEntityReference.java +97 -0
- data/ext/java/nokogiri/XmlNamespace.java +183 -0
- data/ext/java/nokogiri/XmlNode.java +1378 -0
- data/ext/java/nokogiri/XmlNodeSet.java +267 -0
- data/ext/java/nokogiri/XmlProcessingInstruction.java +99 -0
- data/ext/java/nokogiri/XmlReader.java +408 -0
- data/ext/java/nokogiri/XmlRelaxng.java +144 -0
- data/ext/java/nokogiri/XmlSaxParserContext.java +367 -0
- data/ext/java/nokogiri/XmlSaxPushParser.java +184 -0
- data/ext/java/nokogiri/XmlSchema.java +324 -0
- data/ext/java/nokogiri/XmlSyntaxError.java +119 -0
- data/ext/java/nokogiri/XmlText.java +119 -0
- data/ext/java/nokogiri/XmlXpathContext.java +199 -0
- data/ext/java/nokogiri/XsltStylesheet.java +197 -0
- data/ext/java/nokogiri/internals/HtmlDomParserContext.java +204 -0
- data/ext/java/nokogiri/internals/NokogiriDocumentCache.java +73 -0
- data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +86 -0
- data/ext/java/nokogiri/internals/NokogiriHandler.java +327 -0
- data/ext/java/nokogiri/internals/NokogiriHelpers.java +639 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +167 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +130 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +74 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +121 -0
- data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +79 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +141 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +73 -0
- data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +67 -0
- data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +86 -0
- data/ext/java/nokogiri/internals/ParserContext.java +276 -0
- data/ext/java/nokogiri/internals/PushInputStream.java +411 -0
- data/ext/java/nokogiri/internals/ReaderNode.java +531 -0
- data/ext/java/nokogiri/internals/SaveContextVisitor.java +567 -0
- data/ext/java/nokogiri/internals/SchemaErrorHandler.java +76 -0
- data/ext/java/nokogiri/internals/XmlDeclHandler.java +42 -0
- data/ext/java/nokogiri/internals/XmlDomParser.java +76 -0
- data/ext/java/nokogiri/internals/XmlDomParserContext.java +244 -0
- data/ext/java/nokogiri/internals/XmlSaxParser.java +65 -0
- data/ext/java/nokogiri/internals/XsltExtensionFunction.java +72 -0
- data/ext/nokogiri/depend +358 -0
- data/ext/nokogiri/extconf.rb +124 -0
- data/ext/nokogiri/html_document.c +154 -0
- data/ext/nokogiri/html_document.h +10 -0
- data/ext/nokogiri/html_element_description.c +276 -0
- data/ext/nokogiri/html_element_description.h +10 -0
- data/ext/nokogiri/html_entity_lookup.c +32 -0
- data/ext/nokogiri/html_entity_lookup.h +8 -0
- data/ext/nokogiri/html_sax_parser_context.c +94 -0
- data/ext/nokogiri/html_sax_parser_context.h +11 -0
- data/ext/nokogiri/nokogiri.c +115 -0
- data/ext/nokogiri/nokogiri.h +160 -0
- data/ext/nokogiri/xml_attr.c +94 -0
- data/ext/nokogiri/xml_attr.h +9 -0
- data/ext/nokogiri/xml_attribute_decl.c +70 -0
- data/ext/nokogiri/xml_attribute_decl.h +9 -0
- data/ext/nokogiri/xml_cdata.c +56 -0
- data/ext/nokogiri/xml_cdata.h +9 -0
- data/ext/nokogiri/xml_comment.c +54 -0
- data/ext/nokogiri/xml_comment.h +9 -0
- data/ext/nokogiri/xml_document.c +478 -0
- data/ext/nokogiri/xml_document.h +23 -0
- data/ext/nokogiri/xml_document_fragment.c +48 -0
- data/ext/nokogiri/xml_document_fragment.h +10 -0
- data/ext/nokogiri/xml_dtd.c +202 -0
- data/ext/nokogiri/xml_dtd.h +10 -0
- data/ext/nokogiri/xml_element_content.c +123 -0
- data/ext/nokogiri/xml_element_content.h +10 -0
- data/ext/nokogiri/xml_element_decl.c +69 -0
- data/ext/nokogiri/xml_element_decl.h +9 -0
- data/ext/nokogiri/xml_encoding_handler.c +79 -0
- data/ext/nokogiri/xml_encoding_handler.h +8 -0
- data/ext/nokogiri/xml_entity_decl.c +110 -0
- data/ext/nokogiri/xml_entity_decl.h +10 -0
- data/ext/nokogiri/xml_entity_reference.c +52 -0
- data/ext/nokogiri/xml_entity_reference.h +9 -0
- data/ext/nokogiri/xml_io.c +56 -0
- data/ext/nokogiri/xml_io.h +11 -0
- data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
- data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
- data/ext/nokogiri/xml_namespace.c +84 -0
- data/ext/nokogiri/xml_namespace.h +13 -0
- data/ext/nokogiri/xml_node.c +1385 -0
- data/ext/nokogiri/xml_node.h +13 -0
- data/ext/nokogiri/xml_node_set.c +418 -0
- data/ext/nokogiri/xml_node_set.h +9 -0
- data/ext/nokogiri/xml_processing_instruction.c +56 -0
- data/ext/nokogiri/xml_processing_instruction.h +9 -0
- data/ext/nokogiri/xml_reader.c +684 -0
- data/ext/nokogiri/xml_reader.h +10 -0
- data/ext/nokogiri/xml_relax_ng.c +161 -0
- data/ext/nokogiri/xml_relax_ng.h +9 -0
- data/ext/nokogiri/xml_sax_parser.c +293 -0
- data/ext/nokogiri/xml_sax_parser.h +39 -0
- data/ext/nokogiri/xml_sax_parser_context.c +199 -0
- data/ext/nokogiri/xml_sax_parser_context.h +10 -0
- data/ext/nokogiri/xml_sax_push_parser.c +115 -0
- data/ext/nokogiri/xml_sax_push_parser.h +9 -0
- data/ext/nokogiri/xml_schema.c +205 -0
- data/ext/nokogiri/xml_schema.h +9 -0
- data/ext/nokogiri/xml_syntax_error.c +58 -0
- data/ext/nokogiri/xml_syntax_error.h +13 -0
- data/ext/nokogiri/xml_text.c +50 -0
- data/ext/nokogiri/xml_text.h +9 -0
- data/ext/nokogiri/xml_xpath_context.c +309 -0
- data/ext/nokogiri/xml_xpath_context.h +9 -0
- data/ext/nokogiri/xslt_stylesheet.c +264 -0
- data/ext/nokogiri/xslt_stylesheet.h +9 -0
- data/lib/nokogiri.rb +127 -0
- data/lib/nokogiri/css.rb +27 -0
- data/lib/nokogiri/css/node.rb +99 -0
- data/lib/nokogiri/css/parser.rb +677 -0
- data/lib/nokogiri/css/parser.y +237 -0
- data/lib/nokogiri/css/parser_extras.rb +91 -0
- data/lib/nokogiri/css/syntax_error.rb +7 -0
- data/lib/nokogiri/css/tokenizer.rb +152 -0
- data/lib/nokogiri/css/tokenizer.rex +55 -0
- data/lib/nokogiri/css/xpath_visitor.rb +171 -0
- data/lib/nokogiri/decorators/slop.rb +35 -0
- data/lib/nokogiri/html.rb +36 -0
- data/lib/nokogiri/html/builder.rb +35 -0
- data/lib/nokogiri/html/document.rb +213 -0
- data/lib/nokogiri/html/document_fragment.rb +41 -0
- data/lib/nokogiri/html/element_description.rb +23 -0
- data/lib/nokogiri/html/element_description_defaults.rb +671 -0
- data/lib/nokogiri/html/entity_lookup.rb +13 -0
- data/lib/nokogiri/html/sax/parser.rb +52 -0
- data/lib/nokogiri/html/sax/parser_context.rb +16 -0
- data/lib/nokogiri/nokogiri.jar +0 -0
- data/lib/nokogiri/syntax_error.rb +4 -0
- data/lib/nokogiri/version.rb +88 -0
- data/lib/nokogiri/xml.rb +67 -0
- data/lib/nokogiri/xml/attr.rb +14 -0
- data/lib/nokogiri/xml/attribute_decl.rb +18 -0
- data/lib/nokogiri/xml/builder.rb +425 -0
- data/lib/nokogiri/xml/cdata.rb +11 -0
- data/lib/nokogiri/xml/character_data.rb +7 -0
- data/lib/nokogiri/xml/document.rb +234 -0
- data/lib/nokogiri/xml/document_fragment.rb +98 -0
- data/lib/nokogiri/xml/dtd.rb +22 -0
- data/lib/nokogiri/xml/element_content.rb +36 -0
- data/lib/nokogiri/xml/element_decl.rb +13 -0
- data/lib/nokogiri/xml/entity_decl.rb +19 -0
- data/lib/nokogiri/xml/namespace.rb +13 -0
- data/lib/nokogiri/xml/node.rb +915 -0
- data/lib/nokogiri/xml/node/save_options.rb +61 -0
- data/lib/nokogiri/xml/node_set.rb +357 -0
- data/lib/nokogiri/xml/notation.rb +6 -0
- data/lib/nokogiri/xml/parse_options.rb +93 -0
- data/lib/nokogiri/xml/pp.rb +2 -0
- data/lib/nokogiri/xml/pp/character_data.rb +18 -0
- data/lib/nokogiri/xml/pp/node.rb +56 -0
- data/lib/nokogiri/xml/processing_instruction.rb +8 -0
- data/lib/nokogiri/xml/reader.rb +112 -0
- data/lib/nokogiri/xml/relax_ng.rb +32 -0
- data/lib/nokogiri/xml/sax.rb +4 -0
- data/lib/nokogiri/xml/sax/document.rb +164 -0
- data/lib/nokogiri/xml/sax/parser.rb +115 -0
- data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
- data/lib/nokogiri/xml/schema.rb +63 -0
- data/lib/nokogiri/xml/syntax_error.rb +47 -0
- data/lib/nokogiri/xml/text.rb +9 -0
- data/lib/nokogiri/xml/xpath.rb +10 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +11 -0
- data/lib/nokogiri/xml/xpath_context.rb +16 -0
- data/lib/nokogiri/xslt.rb +52 -0
- data/lib/nokogiri/xslt/stylesheet.rb +25 -0
- data/lib/xsd/xmlparser/nokogiri.rb +90 -0
- data/nokogiri_help_responses.md +40 -0
- data/tasks/cross_compile.rb +152 -0
- data/tasks/nokogiri.org.rb +18 -0
- data/tasks/test.rb +94 -0
- data/test/css/test_nthiness.rb +159 -0
- data/test/css/test_parser.rb +303 -0
- data/test/css/test_tokenizer.rb +198 -0
- data/test/css/test_xpath_visitor.rb +85 -0
- data/test/decorators/test_slop.rb +16 -0
- data/test/files/2ch.html +108 -0
- data/test/files/address_book.rlx +12 -0
- data/test/files/address_book.xml +10 -0
- data/test/files/bar/bar.xsd +4 -0
- data/test/files/dont_hurt_em_why.xml +422 -0
- data/test/files/encoding.html +82 -0
- data/test/files/encoding.xhtml +84 -0
- data/test/files/exslt.xml +8 -0
- data/test/files/exslt.xslt +35 -0
- data/test/files/foo/foo.xsd +4 -0
- data/test/files/metacharset.html +10 -0
- data/test/files/noencoding.html +47 -0
- data/test/files/po.xml +32 -0
- data/test/files/po.xsd +66 -0
- data/test/files/shift_jis.html +10 -0
- data/test/files/shift_jis.xml +5 -0
- data/test/files/snuggles.xml +3 -0
- data/test/files/staff.dtd +10 -0
- data/test/files/staff.xml +59 -0
- data/test/files/staff.xslt +32 -0
- data/test/files/tlm.html +850 -0
- data/test/files/valid_bar.xml +2 -0
- data/test/helper.rb +173 -0
- data/test/html/sax/test_parser.rb +136 -0
- data/test/html/sax/test_parser_context.rb +48 -0
- data/test/html/test_builder.rb +164 -0
- data/test/html/test_document.rb +472 -0
- data/test/html/test_document_encoding.rb +138 -0
- data/test/html/test_document_fragment.rb +255 -0
- data/test/html/test_element_description.rb +100 -0
- data/test/html/test_named_characters.rb +14 -0
- data/test/html/test_node.rb +190 -0
- data/test/html/test_node_encoding.rb +27 -0
- data/test/test_convert_xpath.rb +135 -0
- data/test/test_css_cache.rb +45 -0
- data/test/test_encoding_handler.rb +46 -0
- data/test/test_memory_leak.rb +72 -0
- data/test/test_nokogiri.rb +132 -0
- data/test/test_reader.rb +425 -0
- data/test/test_soap4r_sax.rb +52 -0
- data/test/test_xslt_transforms.rb +193 -0
- data/test/xml/node/test_save_options.rb +28 -0
- data/test/xml/node/test_subclass.rb +44 -0
- data/test/xml/sax/test_parser.rb +338 -0
- data/test/xml/sax/test_parser_context.rb +113 -0
- data/test/xml/sax/test_push_parser.rb +156 -0
- data/test/xml/test_attr.rb +65 -0
- data/test/xml/test_attribute_decl.rb +86 -0
- data/test/xml/test_builder.rb +227 -0
- data/test/xml/test_cdata.rb +50 -0
- data/test/xml/test_comment.rb +29 -0
- data/test/xml/test_document.rb +697 -0
- data/test/xml/test_document_encoding.rb +26 -0
- data/test/xml/test_document_fragment.rb +192 -0
- data/test/xml/test_dtd.rb +107 -0
- data/test/xml/test_dtd_encoding.rb +33 -0
- data/test/xml/test_element_content.rb +56 -0
- data/test/xml/test_element_decl.rb +73 -0
- data/test/xml/test_entity_decl.rb +122 -0
- data/test/xml/test_entity_reference.rb +21 -0
- data/test/xml/test_namespace.rb +70 -0
- data/test/xml/test_node.rb +917 -0
- data/test/xml/test_node_attributes.rb +34 -0
- data/test/xml/test_node_encoding.rb +107 -0
- data/test/xml/test_node_reparenting.rb +334 -0
- data/test/xml/test_node_set.rb +742 -0
- data/test/xml/test_parse_options.rb +52 -0
- data/test/xml/test_processing_instruction.rb +30 -0
- data/test/xml/test_reader_encoding.rb +126 -0
- data/test/xml/test_relax_ng.rb +60 -0
- data/test/xml/test_schema.rb +94 -0
- data/test/xml/test_syntax_error.rb +12 -0
- data/test/xml/test_text.rb +47 -0
- data/test/xml/test_unparented_node.rb +381 -0
- data/test/xml/test_xpath.rb +237 -0
- data/test/xslt/test_custom_functions.rb +94 -0
- data/test/xslt/test_exception_handling.rb +37 -0
- metadata +552 -0
|
@@ -0,0 +1,267 @@
|
|
|
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 implements NodeList {
|
|
61
|
+
private List<?> list;
|
|
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
|
+
void setNodes(RubyArray array) {
|
|
80
|
+
this.nodes = array;
|
|
81
|
+
|
|
82
|
+
IRubyObject first = array.first();
|
|
83
|
+
initialize(array.getRuntime(), first);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private void setReference(XmlNodeSet reference) {
|
|
87
|
+
this.nodes = null;
|
|
88
|
+
IRubyObject first = reference.nodes.first();
|
|
89
|
+
initialize(reference.getRuntime(), first);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
public void setNodeList(NodeList nodeList) {
|
|
93
|
+
setNodes(nodeListToRubyArray(getRuntime(), nodeList));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private void initialize(Ruby ruby, IRubyObject refNode) {
|
|
97
|
+
if (refNode instanceof XmlNode) {
|
|
98
|
+
XmlNode n = (XmlNode)refNode;
|
|
99
|
+
doc = n.document(ruby.getCurrentContext());
|
|
100
|
+
setInstanceVariable("@document", doc);
|
|
101
|
+
if (doc != null) {
|
|
102
|
+
RuntimeHelpers.invoke(ruby.getCurrentContext(), doc, "decorate", this);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public static IRubyObject newEmptyNodeSet(ThreadContext context) {
|
|
108
|
+
return (XmlNodeSet)NokogiriService.XML_NODESET_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::NodeSet"));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public long length() {
|
|
112
|
+
if (nodes == null) return 0L;
|
|
113
|
+
return nodes.length().getLongValue();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public void relink_namespace(ThreadContext context) {
|
|
117
|
+
List<?> n = nodes.getList();
|
|
118
|
+
|
|
119
|
+
for (int i = 0; i < n.size(); i++) {
|
|
120
|
+
if (n.get(i) instanceof XmlNode) {
|
|
121
|
+
((XmlNode) n.get(i)).relink_namespace(context);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
@JRubyMethod(name="&")
|
|
127
|
+
public IRubyObject and(ThreadContext context, IRubyObject nodeSet){
|
|
128
|
+
if (nodes == null) setNodes(RubyArray.newEmptyArray(context.getRuntime()));
|
|
129
|
+
return newXmlNodeSet(context, (RubyArray) nodes.op_and(asXmlNodeSet(context, nodeSet).nodes));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
@JRubyMethod
|
|
133
|
+
public IRubyObject delete(ThreadContext context, IRubyObject node_or_namespace){
|
|
134
|
+
if (nodes == null) return context.getRuntime().getNil();
|
|
135
|
+
return nodes.delete(context, asXmlNodeOrNamespace(context, node_or_namespace), Block.NULL_BLOCK);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
@JRubyMethod
|
|
139
|
+
public IRubyObject dup(ThreadContext context){
|
|
140
|
+
if (nodes == null) setNodes(RubyArray.newEmptyArray(context.getRuntime()));
|
|
141
|
+
return newXmlNodeSet(context, nodes.aryDup());
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
@JRubyMethod(name = "include?")
|
|
145
|
+
public IRubyObject include_p(ThreadContext context, IRubyObject node_or_namespace){
|
|
146
|
+
if (nodes == null) setNodes(RubyArray.newEmptyArray(context.getRuntime()));
|
|
147
|
+
return nodes.include_p(context, asXmlNodeOrNamespace(context, node_or_namespace));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
@JRubyMethod(name = {"length", "size"})
|
|
151
|
+
public IRubyObject length(ThreadContext context) {
|
|
152
|
+
if (nodes != null) return nodes.length();
|
|
153
|
+
else return context.getRuntime().newFixnum(0);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
@JRubyMethod(name="-")
|
|
157
|
+
public IRubyObject op_diff(ThreadContext context, IRubyObject nodeSet){
|
|
158
|
+
XmlNodeSet xmlNodeSet = newXmlNodeSet(context, this);
|
|
159
|
+
if (nodes == null) setNodes(RubyArray.newEmptyArray(context.getRuntime()));
|
|
160
|
+
xmlNodeSet.setNodes((RubyArray) nodes.op_diff(asXmlNodeSet(context, nodeSet).nodes));
|
|
161
|
+
return xmlNodeSet;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
@JRubyMethod(name={"|", "+"})
|
|
165
|
+
public IRubyObject op_or(ThreadContext context, IRubyObject nodeSet){
|
|
166
|
+
if (nodes == null) setNodes(RubyArray.newEmptyArray(context.getRuntime()));
|
|
167
|
+
return newXmlNodeSet(context, (RubyArray) nodes.op_or(asXmlNodeSet(context, nodeSet).nodes));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
@JRubyMethod(name = {"push", "<<"})
|
|
171
|
+
public IRubyObject push(ThreadContext context, IRubyObject node_or_namespace) {
|
|
172
|
+
if (nodes == null) setNodes(RubyArray.newEmptyArray(context.getRuntime()));
|
|
173
|
+
nodes.append(asXmlNodeOrNamespace(context, node_or_namespace));
|
|
174
|
+
return this;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
@JRubyMethod(name={"[]", "slice"})
|
|
178
|
+
public IRubyObject slice(ThreadContext context, IRubyObject indexOrRange){
|
|
179
|
+
IRubyObject result;
|
|
180
|
+
if (nodes == null) return context.getRuntime().getNil();
|
|
181
|
+
if (context.getRuntime().is1_9()) {
|
|
182
|
+
result = nodes.aref19(indexOrRange);
|
|
183
|
+
} else {
|
|
184
|
+
result = nodes.aref(indexOrRange);
|
|
185
|
+
}
|
|
186
|
+
if (result instanceof RubyArray) {
|
|
187
|
+
return newXmlNodeSet(context, (RubyArray)result);
|
|
188
|
+
} else {
|
|
189
|
+
return result;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
@JRubyMethod(name={"[]", "slice"})
|
|
194
|
+
public IRubyObject slice(ThreadContext context, IRubyObject start, IRubyObject length){
|
|
195
|
+
IRubyObject result;
|
|
196
|
+
if (nodes == null) return context.getRuntime().getNil();
|
|
197
|
+
if (context.getRuntime().is1_9()) {
|
|
198
|
+
result = nodes.aref19(start, length);
|
|
199
|
+
} else {
|
|
200
|
+
result = nodes.aref(start, length);
|
|
201
|
+
}
|
|
202
|
+
if (result instanceof RubyArray) return newXmlNodeSet(context, (RubyArray)result);
|
|
203
|
+
else return context.getRuntime().getNil();
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
@JRubyMethod(name = {"to_a", "to_ary"})
|
|
207
|
+
public IRubyObject to_a(ThreadContext context) {
|
|
208
|
+
return nodes;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
@JRubyMethod(name = {"unlink", "remove"})
|
|
212
|
+
public IRubyObject unlink(ThreadContext context){
|
|
213
|
+
if (nodes == null) setNodes(RubyArray.newEmptyArray(context.getRuntime()));
|
|
214
|
+
IRubyObject[] arr = this.nodes.toJavaArrayUnsafe();
|
|
215
|
+
long length = arr.length;
|
|
216
|
+
for (int i = 0; i < length; i++) {
|
|
217
|
+
if (arr[i] instanceof XmlNode) {
|
|
218
|
+
((XmlNode) arr[i] ).unlink(context);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return this;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
public static XmlNodeSet newXmlNodeSet(ThreadContext context, RubyArray array) {
|
|
225
|
+
XmlNodeSet xmlNodeSet = (XmlNodeSet)NokogiriService.XML_NODESET_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::NodeSet"));
|
|
226
|
+
xmlNodeSet.setNodes(array);
|
|
227
|
+
return xmlNodeSet;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private XmlNodeSet newXmlNodeSet(ThreadContext context, XmlNodeSet reference) {
|
|
231
|
+
XmlNodeSet xmlNodeSet = (XmlNodeSet)NokogiriService.XML_NODESET_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::NodeSet"));
|
|
232
|
+
xmlNodeSet.setReference(reference);
|
|
233
|
+
return xmlNodeSet;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
private IRubyObject asXmlNodeOrNamespace(ThreadContext context, IRubyObject possibleNode) {
|
|
237
|
+
if (possibleNode instanceof XmlNode || possibleNode instanceof XmlNamespace) {
|
|
238
|
+
return possibleNode;
|
|
239
|
+
} else {
|
|
240
|
+
throw context.getRuntime().newArgumentError("node must be a Nokogiri::XML::Node or Nokogiri::XML::Namespace");
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
private XmlNodeSet asXmlNodeSet(ThreadContext context, IRubyObject possibleNodeSet) {
|
|
245
|
+
// if(!(possibleNodeSet instanceof XmlNodeSet)) {
|
|
246
|
+
if(!RuntimeHelpers.invoke(context, possibleNodeSet, "is_a?",
|
|
247
|
+
getNokogiriClass(context.getRuntime(), "Nokogiri::XML::NodeSet")).isTrue()) {
|
|
248
|
+
throw context.getRuntime().newArgumentError("node must be a Nokogiri::XML::NodeSet");
|
|
249
|
+
}
|
|
250
|
+
XmlNodeSet xmlNodeSet = (XmlNodeSet)possibleNodeSet;
|
|
251
|
+
if (xmlNodeSet.nodes == null) xmlNodeSet.setNodes(RubyArray.newEmptyArray(context.getRuntime()));
|
|
252
|
+
return xmlNodeSet;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
public int getLength() {
|
|
256
|
+
if (nodes == null) return 0 ;
|
|
257
|
+
return nodes.size();
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
public Node item(int index) {
|
|
261
|
+
if (nodes == null) return null ;
|
|
262
|
+
Object n = nodes.get(index);
|
|
263
|
+
if (n instanceof XmlNode) return ((XmlNode)n).node;
|
|
264
|
+
if (n instanceof XmlNamespace) return ((XmlNamespace)n).getNode();
|
|
265
|
+
return null;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
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.SaveContextVisitor;
|
|
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
|
+
import org.w3c.dom.ProcessingInstruction;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Class for Nokogiri::XML::ProcessingInstruction
|
|
51
|
+
*
|
|
52
|
+
* @author sergio
|
|
53
|
+
* @author Yoko Harada <yokolet@gmail.com>
|
|
54
|
+
*/
|
|
55
|
+
@JRubyClass(name="Nokogiri::XML::ProcessingInstruction", parent="Nokogiri::XML::Node")
|
|
56
|
+
public class XmlProcessingInstruction extends XmlNode {
|
|
57
|
+
|
|
58
|
+
public XmlProcessingInstruction(Ruby ruby, RubyClass klazz) {
|
|
59
|
+
super(ruby, klazz);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public XmlProcessingInstruction(Ruby ruby, RubyClass klazz, Node node) {
|
|
63
|
+
super(ruby, klazz, node);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
@JRubyMethod(name="new", meta=true, rest=true, required=3)
|
|
67
|
+
public static IRubyObject rbNew(ThreadContext context,
|
|
68
|
+
IRubyObject klazz,
|
|
69
|
+
IRubyObject[] args) {
|
|
70
|
+
|
|
71
|
+
IRubyObject doc = args[0];
|
|
72
|
+
IRubyObject target = args[1];
|
|
73
|
+
IRubyObject data = args[2];
|
|
74
|
+
|
|
75
|
+
Document document = ((XmlNode) doc).getOwnerDocument();
|
|
76
|
+
Node node =
|
|
77
|
+
document.createProcessingInstruction(rubyStringToString(target),
|
|
78
|
+
rubyStringToString(data));
|
|
79
|
+
XmlProcessingInstruction self =
|
|
80
|
+
new XmlProcessingInstruction(context.getRuntime(),
|
|
81
|
+
(RubyClass) klazz,
|
|
82
|
+
node);
|
|
83
|
+
|
|
84
|
+
RuntimeHelpers.invoke(context, self, "initialize", args);
|
|
85
|
+
|
|
86
|
+
// TODO: if_block_given.
|
|
87
|
+
|
|
88
|
+
return self;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
@Override
|
|
92
|
+
public boolean isProcessingInstruction() { return true; }
|
|
93
|
+
|
|
94
|
+
@Override
|
|
95
|
+
public void accept(ThreadContext context, SaveContextVisitor visitor) {
|
|
96
|
+
visitor.enter((ProcessingInstruction)node);
|
|
97
|
+
visitor.leave((ProcessingInstruction)node);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,408 @@
|
|
|
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.stringOrBlank;
|
|
37
|
+
|
|
38
|
+
import java.io.ByteArrayInputStream;
|
|
39
|
+
import java.io.IOException;
|
|
40
|
+
import java.util.ArrayDeque;
|
|
41
|
+
import java.util.Stack;
|
|
42
|
+
|
|
43
|
+
import nokogiri.internals.ReaderNode;
|
|
44
|
+
import nokogiri.internals.ReaderNode.ElementNode;
|
|
45
|
+
|
|
46
|
+
import org.jruby.Ruby;
|
|
47
|
+
import org.jruby.RubyArray;
|
|
48
|
+
import org.jruby.RubyBoolean;
|
|
49
|
+
import org.jruby.RubyClass;
|
|
50
|
+
import org.jruby.RubyFixnum;
|
|
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.ThreadContext;
|
|
58
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
|
59
|
+
import org.jruby.util.ByteList;
|
|
60
|
+
import org.xml.sax.Attributes;
|
|
61
|
+
import org.xml.sax.InputSource;
|
|
62
|
+
import org.xml.sax.SAXException;
|
|
63
|
+
import org.xml.sax.SAXParseException;
|
|
64
|
+
import org.xml.sax.XMLReader;
|
|
65
|
+
import org.xml.sax.ext.DefaultHandler2;
|
|
66
|
+
import org.xml.sax.helpers.XMLReaderFactory;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Class for Nokogiri:XML::Reader
|
|
70
|
+
*
|
|
71
|
+
* @author sergio
|
|
72
|
+
* @author Yoko Harada <yokolet@gmail.com>
|
|
73
|
+
*/
|
|
74
|
+
@JRubyClass(name="Nokogiri::XML::Reader")
|
|
75
|
+
public class XmlReader extends RubyObject {
|
|
76
|
+
|
|
77
|
+
private static final int XML_TEXTREADER_MODE_INITIAL = 0;
|
|
78
|
+
private static final int XML_TEXTREADER_MODE_INTERACTIVE = 1;
|
|
79
|
+
private static final int XML_TEXTREADER_MODE_ERROR = 2;
|
|
80
|
+
private static final int XML_TEXTREADER_MODE_EOF = 3;
|
|
81
|
+
private static final int XML_TEXTREADER_MODE_CLOSED = 4;
|
|
82
|
+
private static final int XML_TEXTREADER_MODE_READING = 5;
|
|
83
|
+
|
|
84
|
+
ArrayDeque<ReaderNode> nodeQueue;
|
|
85
|
+
private int state;
|
|
86
|
+
|
|
87
|
+
public XmlReader(Ruby runtime, RubyClass klazz) {
|
|
88
|
+
super(runtime, klazz);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Create and return a copy of this object.
|
|
93
|
+
*
|
|
94
|
+
* @return a clone of this object
|
|
95
|
+
*/
|
|
96
|
+
@Override
|
|
97
|
+
public Object clone() throws CloneNotSupportedException {
|
|
98
|
+
return super.clone();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
public void init(Ruby runtime) {
|
|
102
|
+
nodeQueue = new ArrayDeque<ReaderNode>();
|
|
103
|
+
nodeQueue.add(new ReaderNode.EmptyNode(runtime));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private void parseRubyString(ThreadContext context, RubyString content){
|
|
107
|
+
Ruby ruby = context.getRuntime();
|
|
108
|
+
try {
|
|
109
|
+
this.setState(XML_TEXTREADER_MODE_READING);
|
|
110
|
+
XMLReader reader = this.createReader(ruby);
|
|
111
|
+
ByteList byteList = content.getByteList();
|
|
112
|
+
ByteArrayInputStream bais = new ByteArrayInputStream(byteList.unsafeBytes(), byteList.begin(), byteList.length());
|
|
113
|
+
reader.parse(new InputSource(bais));
|
|
114
|
+
this.setState(XML_TEXTREADER_MODE_CLOSED);
|
|
115
|
+
} catch (SAXParseException spe) {
|
|
116
|
+
this.setState(XML_TEXTREADER_MODE_ERROR);
|
|
117
|
+
this.nodeQueue.add(new ReaderNode.ExceptionNode(ruby, spe));
|
|
118
|
+
} catch (IOException ioe) {
|
|
119
|
+
throw RaiseException.createNativeRaiseException(ruby, ioe);
|
|
120
|
+
} catch (SAXException saxe) {
|
|
121
|
+
throw RaiseException.createNativeRaiseException(ruby, saxe);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private void setState(int state) { this.state = state; }
|
|
126
|
+
|
|
127
|
+
@JRubyMethod
|
|
128
|
+
public IRubyObject attribute(ThreadContext context, IRubyObject name) {
|
|
129
|
+
return nodeQueue.peek().getAttributeByName(name);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
@JRubyMethod
|
|
133
|
+
public IRubyObject attribute_at(ThreadContext context, IRubyObject index) {
|
|
134
|
+
return nodeQueue.peek().getAttributeByIndex(index);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
@JRubyMethod
|
|
138
|
+
public IRubyObject attribute_count(ThreadContext context) {
|
|
139
|
+
return nodeQueue.peek().getAttributeCount();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
@JRubyMethod
|
|
143
|
+
public IRubyObject attribute_nodes(ThreadContext context) {
|
|
144
|
+
return nodeQueue.peek().getAttributesNodes();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
@JRubyMethod
|
|
148
|
+
public IRubyObject attr_nodes(ThreadContext context) {
|
|
149
|
+
return nodeQueue.peek().getAttributesNodes();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
@JRubyMethod(name = "attributes?")
|
|
153
|
+
public IRubyObject attributes_p(ThreadContext context) {
|
|
154
|
+
return nodeQueue.peek().hasAttributes();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
@JRubyMethod
|
|
158
|
+
public IRubyObject base_uri(ThreadContext context) {
|
|
159
|
+
return nodeQueue.peek().getXmlBase();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
@JRubyMethod(name="default?")
|
|
163
|
+
public IRubyObject default_p(ThreadContext context){
|
|
164
|
+
return nodeQueue.peek().isDefault();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
@JRubyMethod
|
|
168
|
+
public IRubyObject depth(ThreadContext context) {
|
|
169
|
+
return nodeQueue.peek().getDepth();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
@JRubyMethod(name = {"empty_element?", "self_closing?"})
|
|
173
|
+
public IRubyObject empty_element_p(ThreadContext context) {
|
|
174
|
+
ReaderNode readerNode = nodeQueue.peek();
|
|
175
|
+
if (readerNode == null) return context.getRuntime().getNil();
|
|
176
|
+
if (!(readerNode instanceof ElementNode)) context.getRuntime().getFalse();
|
|
177
|
+
return RubyBoolean.newBoolean(context.getRuntime(), !readerNode.hasChildren);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
@JRubyMethod(meta = true, rest = true)
|
|
181
|
+
public static IRubyObject from_io(ThreadContext context, IRubyObject cls, IRubyObject args[]) {
|
|
182
|
+
// Only to pass the source test.
|
|
183
|
+
Ruby runtime = context.getRuntime();
|
|
184
|
+
// Not nil allowed!
|
|
185
|
+
if(args[0].isNil()) throw runtime.newArgumentError("io cannot be nil");
|
|
186
|
+
|
|
187
|
+
XmlReader reader = (XmlReader) NokogiriService.XML_READER_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Reader"));
|
|
188
|
+
reader.init(runtime);
|
|
189
|
+
reader.setInstanceVariable("@source", args[0]);
|
|
190
|
+
reader.setInstanceVariable("@errors", runtime.newArray());
|
|
191
|
+
if (args.length > 2) reader.setInstanceVariable("@encoding", args[2]);
|
|
192
|
+
|
|
193
|
+
RubyString content = RuntimeHelpers.invoke(context, args[0], "read").convertToString();
|
|
194
|
+
reader.parseRubyString(context, content);
|
|
195
|
+
return reader;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
@JRubyMethod(meta = true, rest = true)
|
|
199
|
+
public static IRubyObject from_memory(ThreadContext context, IRubyObject cls, IRubyObject args[]) {
|
|
200
|
+
// args[0]: string, args[1]: url, args[2]: encoding, args[3]: options
|
|
201
|
+
Ruby runtime = context.getRuntime();
|
|
202
|
+
// Not nil allowed!
|
|
203
|
+
if(args[0].isNil()) throw runtime.newArgumentError("string cannot be nil");
|
|
204
|
+
|
|
205
|
+
XmlReader reader = (XmlReader) NokogiriService.XML_READER_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Reader"));
|
|
206
|
+
reader.init(runtime);
|
|
207
|
+
reader.setInstanceVariable("@source", args[0]);
|
|
208
|
+
reader.setInstanceVariable("@errors", runtime.newArray());
|
|
209
|
+
if (args.length > 2) reader.setInstanceVariable("@encoding", args[2]);
|
|
210
|
+
|
|
211
|
+
reader.parseRubyString(context, args[0].convertToString());
|
|
212
|
+
return reader;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
@JRubyMethod
|
|
216
|
+
public IRubyObject node_type(ThreadContext context) {
|
|
217
|
+
IRubyObject node_type = nodeQueue.peek().getNodeType();
|
|
218
|
+
return node_type == null ? RubyFixnum.zero(context.getRuntime()) : node_type;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
@JRubyMethod
|
|
222
|
+
public IRubyObject inner_xml(ThreadContext context) {
|
|
223
|
+
return stringOrBlank(context.getRuntime(), getInnerXml(nodeQueue, nodeQueue.peek()));
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
private String getInnerXml(ArrayDeque<ReaderNode> nodeQueue, ReaderNode current) {
|
|
227
|
+
if (current.depth < 0) return null;
|
|
228
|
+
if (!current.hasChildren) return null;
|
|
229
|
+
StringBuffer sb = new StringBuffer();
|
|
230
|
+
int currentDepth = (Integer)current.depth;
|
|
231
|
+
for (ReaderNode node : nodeQueue) {
|
|
232
|
+
if (((Integer)node.depth) > currentDepth) sb.append(node.getString());
|
|
233
|
+
}
|
|
234
|
+
return new String(sb);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
@JRubyMethod
|
|
238
|
+
public IRubyObject outer_xml(ThreadContext context) {
|
|
239
|
+
return stringOrBlank(context.getRuntime(), getOuterXml(nodeQueue, nodeQueue.peek()));
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
private String getOuterXml(ArrayDeque<ReaderNode> nodeQueue, ReaderNode current) {
|
|
243
|
+
if (current.depth < 0) return null;
|
|
244
|
+
StringBuffer sb = new StringBuffer();
|
|
245
|
+
int initialDepth = (Integer)current.depth - 1;
|
|
246
|
+
for (ReaderNode node : nodeQueue) {
|
|
247
|
+
if (((Integer)node.depth) > initialDepth) sb.append(node.getString());
|
|
248
|
+
}
|
|
249
|
+
return new String(sb);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
@JRubyMethod
|
|
253
|
+
public IRubyObject lang(ThreadContext context) {
|
|
254
|
+
return nodeQueue.peek().getLang();
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
@JRubyMethod
|
|
258
|
+
public IRubyObject local_name(ThreadContext context) {
|
|
259
|
+
return nodeQueue.peek().getLocalName();
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
@JRubyMethod
|
|
263
|
+
public IRubyObject name(ThreadContext context) {
|
|
264
|
+
return nodeQueue.peek().getName();
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
@JRubyMethod
|
|
268
|
+
public IRubyObject namespace_uri(ThreadContext context) {
|
|
269
|
+
return nodeQueue.peek().getUri();
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
@JRubyMethod
|
|
273
|
+
public IRubyObject namespaces(ThreadContext context) {
|
|
274
|
+
return nodeQueue.peek().getNamespaces(context);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
@JRubyMethod
|
|
278
|
+
public IRubyObject prefix(ThreadContext context) {
|
|
279
|
+
return nodeQueue.peek().getPrefix();
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
@JRubyMethod
|
|
283
|
+
public IRubyObject read(ThreadContext context) {
|
|
284
|
+
this.nodeQueue.poll();
|
|
285
|
+
if(nodeQueue.peek() == null) {
|
|
286
|
+
return context.getRuntime().getNil();
|
|
287
|
+
} else if(nodeQueue.peek().isError()) {
|
|
288
|
+
RubyArray errors = (RubyArray) this.getInstanceVariable("@errors");
|
|
289
|
+
errors.append(nodeQueue.peek().toSyntaxError());
|
|
290
|
+
|
|
291
|
+
this.setInstanceVariable("@errors", errors);
|
|
292
|
+
|
|
293
|
+
throw new RaiseException((XmlSyntaxError) nodeQueue.peek().toSyntaxError());
|
|
294
|
+
} else {
|
|
295
|
+
return this;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
@JRubyMethod
|
|
300
|
+
public IRubyObject state(ThreadContext context) {
|
|
301
|
+
return context.getRuntime().newFixnum(this.state);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
@JRubyMethod
|
|
305
|
+
public IRubyObject value(ThreadContext context) {
|
|
306
|
+
return nodeQueue.peek().getValue();
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
@JRubyMethod(name = "value?")
|
|
310
|
+
public IRubyObject value_p(ThreadContext context) {
|
|
311
|
+
return nodeQueue.peek().hasValue();
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
@JRubyMethod
|
|
315
|
+
public IRubyObject xml_version(ThreadContext context) {
|
|
316
|
+
return nodeQueue.peek().getXmlVersion();
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
protected XMLReader createReader(final Ruby ruby) {
|
|
320
|
+
DefaultHandler2 handler = new DefaultHandler2() {
|
|
321
|
+
|
|
322
|
+
Stack<String> langStack;
|
|
323
|
+
int depth;
|
|
324
|
+
Stack<String> xmlBaseStack;
|
|
325
|
+
Stack<ReaderNode.ElementNode> elementStack;
|
|
326
|
+
|
|
327
|
+
@Override
|
|
328
|
+
public void characters(char[] chars, int start, int length) {
|
|
329
|
+
ReaderNode.TextNode node = ReaderNode.createTextNode(ruby, new String(chars, start, length), depth, langStack, xmlBaseStack);
|
|
330
|
+
nodeQueue.add(node);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
@Override
|
|
334
|
+
public void endDocument() throws SAXException {
|
|
335
|
+
langStack = null;
|
|
336
|
+
xmlBaseStack = null;
|
|
337
|
+
elementStack = null;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
@Override
|
|
341
|
+
public void endElement(String uri, String localName, String qName) {
|
|
342
|
+
depth--;
|
|
343
|
+
ReaderNode previous = nodeQueue.getLast();
|
|
344
|
+
ElementNode startElementNode = elementStack.pop();
|
|
345
|
+
if (previous instanceof ReaderNode.ElementNode && qName.equals(previous.name)) {
|
|
346
|
+
previous.hasChildren = false;
|
|
347
|
+
} else {
|
|
348
|
+
ReaderNode node = ReaderNode.createClosingNode(ruby, uri, localName, qName, depth, langStack, xmlBaseStack);
|
|
349
|
+
if (startElementNode != null) {
|
|
350
|
+
node.attributeList = startElementNode.attributeList;
|
|
351
|
+
node.namespaces = startElementNode.namespaces;
|
|
352
|
+
}
|
|
353
|
+
nodeQueue.add(node);
|
|
354
|
+
}
|
|
355
|
+
if (!langStack.isEmpty()) langStack.pop();
|
|
356
|
+
if (!xmlBaseStack.isEmpty()) xmlBaseStack.pop();
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
@Override
|
|
360
|
+
public void error(SAXParseException ex) throws SAXParseException {
|
|
361
|
+
nodeQueue.add(new ReaderNode.ExceptionNode(ruby, ex));
|
|
362
|
+
throw ex;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
@Override
|
|
366
|
+
public void fatalError(SAXParseException ex) throws SAXParseException {
|
|
367
|
+
nodeQueue.add(new ReaderNode.ExceptionNode(ruby, ex));
|
|
368
|
+
throw ex;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
@Override
|
|
372
|
+
public void startDocument() {
|
|
373
|
+
depth = 0;
|
|
374
|
+
langStack = new Stack<String>();
|
|
375
|
+
xmlBaseStack = new Stack<String>();
|
|
376
|
+
elementStack = new Stack<ReaderNode.ElementNode>();
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
@Override
|
|
380
|
+
public void startElement(String uri, String localName, String qName, Attributes attrs) {
|
|
381
|
+
ReaderNode readerNode = ReaderNode.createElementNode(ruby, uri, localName, qName, attrs, depth, langStack, xmlBaseStack);
|
|
382
|
+
nodeQueue.add(readerNode);
|
|
383
|
+
depth++;
|
|
384
|
+
if (readerNode.lang != null) langStack.push(readerNode.lang);
|
|
385
|
+
if (readerNode.xmlBase != null) xmlBaseStack.push(readerNode.xmlBase);
|
|
386
|
+
elementStack.push((ReaderNode.ElementNode)readerNode);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
@Override
|
|
390
|
+
public void warning(SAXParseException ex) throws SAXParseException {
|
|
391
|
+
nodeQueue.add(new ReaderNode.ExceptionNode(ruby, ex));
|
|
392
|
+
throw ex;
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
try {
|
|
396
|
+
XMLReader reader = XMLReaderFactory.createXMLReader();
|
|
397
|
+
reader.setContentHandler(handler);
|
|
398
|
+
reader.setDTDHandler(handler);
|
|
399
|
+
reader.setErrorHandler(handler);
|
|
400
|
+
reader.setFeature("http://xml.org/sax/features/xmlns-uris", true);
|
|
401
|
+
reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
|
|
402
|
+
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
|
403
|
+
return reader;
|
|
404
|
+
} catch (SAXException saxe) {
|
|
405
|
+
throw RaiseException.createNativeRaiseException(ruby, saxe);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|