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.
- data/.gemtest +0 -0
- data/CHANGELOG.ja.rdoc +113 -0
- data/CHANGELOG.rdoc +113 -1
- data/Manifest.txt +72 -68
- data/README.ja.rdoc +4 -4
- data/README.rdoc +31 -7
- data/Rakefile +110 -132
- data/bin/nokogiri +7 -3
- 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 -32
- data/ext/nokogiri/extconf.rb +10 -6
- data/ext/nokogiri/nokogiri.c +23 -3
- data/ext/nokogiri/nokogiri.h +7 -2
- data/ext/nokogiri/xml_document.c +9 -0
- data/ext/nokogiri/xml_dtd.c +2 -2
- data/ext/nokogiri/xml_io.c +32 -7
- data/ext/nokogiri/xml_node.c +45 -25
- data/ext/nokogiri/xml_node_set.c +1 -1
- data/ext/nokogiri/xml_relax_ng.c +0 -7
- data/ext/nokogiri/xml_sax_parser.c +14 -7
- data/ext/nokogiri/xml_sax_parser_context.c +40 -0
- data/ext/nokogiri/xml_xpath_context.c +33 -2
- data/ext/nokogiri/xslt_stylesheet.c +128 -6
- data/lib/isorelax.jar +0 -0
- data/lib/jing.jar +0 -0
- data/lib/nekodtd.jar +0 -0
- data/lib/nekohtml.jar +0 -0
- data/lib/nokogiri/css/parser.rb +665 -70
- data/lib/nokogiri/css/parser.y +3 -1
- data/lib/nokogiri/css/parser_extras.rb +91 -0
- data/lib/nokogiri/css/tokenizer.rb +148 -3
- data/lib/nokogiri/css/tokenizer.rex +1 -1
- data/lib/nokogiri/css/xpath_visitor.rb +15 -7
- data/lib/nokogiri/css.rb +5 -3
- data/lib/nokogiri/decorators/slop.rb +5 -3
- data/lib/nokogiri/html/document.rb +142 -19
- data/lib/nokogiri/html/document_fragment.rb +19 -17
- data/lib/nokogiri/html/element_description_defaults.rb +671 -0
- data/lib/nokogiri/html/sax/parser.rb +6 -2
- data/lib/nokogiri/html.rb +1 -0
- data/lib/nokogiri/nokogiri.jar +0 -0
- data/lib/nokogiri/version.rb +76 -26
- data/lib/nokogiri/xml/attribute_decl.rb +1 -1
- data/lib/nokogiri/xml/builder.rb +7 -0
- data/lib/nokogiri/xml/document.rb +43 -2
- data/lib/nokogiri/xml/document_fragment.rb +16 -2
- data/lib/nokogiri/xml/dtd.rb +12 -1
- data/lib/nokogiri/xml/element_decl.rb +1 -1
- data/lib/nokogiri/xml/entity_decl.rb +1 -1
- data/lib/nokogiri/xml/node/save_options.rb +20 -1
- data/lib/nokogiri/xml/node.rb +198 -78
- data/lib/nokogiri/xml/node_set.rb +10 -3
- data/lib/nokogiri/xml/parse_options.rb +8 -0
- data/lib/nokogiri/xml/reader.rb +42 -6
- data/lib/nokogiri/xml/sax/document.rb +4 -2
- data/lib/nokogiri/xml/schema.rb +7 -1
- data/lib/nokogiri/xslt/stylesheet.rb +1 -1
- data/lib/nokogiri/xslt.rb +9 -5
- data/lib/nokogiri.rb +19 -25
- data/lib/xercesImpl.jar +0 -0
- data/nokogiri_help_responses.md +40 -0
- data/tasks/cross_compile.rb +130 -136
- data/tasks/nokogiri.org.rb +18 -0
- data/tasks/test.rb +2 -2
- data/test/css/test_parser.rb +29 -18
- data/test/css/test_tokenizer.rb +8 -0
- data/test/decorators/test_slop.rb +16 -0
- data/test/files/encoding.html +82 -0
- data/test/files/encoding.xhtml +84 -0
- data/test/files/metacharset.html +10 -0
- data/test/files/noencoding.html +47 -0
- data/test/helper.rb +5 -1
- data/test/html/sax/test_parser.rb +65 -3
- data/test/html/test_document.rb +75 -1
- data/test/html/test_document_encoding.rb +61 -0
- data/test/html/test_document_fragment.rb +50 -5
- data/test/html/test_element_description.rb +4 -2
- data/test/html/test_node.rb +9 -0
- data/test/test_memory_leak.rb +20 -35
- data/test/test_nokogiri.rb +14 -20
- data/test/test_reader.rb +23 -6
- data/test/test_xslt_transforms.rb +6 -2
- data/test/xml/node/test_save_options.rb +10 -2
- data/test/xml/sax/test_parser.rb +39 -8
- data/test/xml/sax/test_parser_context.rb +50 -0
- data/test/xml/sax/test_push_parser.rb +18 -1
- data/test/xml/test_attribute_decl.rb +7 -3
- data/test/xml/test_builder.rb +17 -0
- data/test/xml/test_document.rb +34 -5
- data/test/xml/test_document_fragment.rb +14 -2
- data/test/xml/test_dtd.rb +28 -3
- data/test/xml/test_element_content.rb +1 -1
- data/test/xml/test_element_decl.rb +1 -1
- data/test/xml/test_entity_decl.rb +12 -10
- data/test/xml/test_namespace.rb +7 -5
- data/test/xml/test_node.rb +65 -13
- data/test/xml/test_node_reparenting.rb +72 -31
- data/test/xml/test_node_set.rb +57 -0
- data/test/xml/test_schema.rb +5 -0
- data/test/xml/test_xpath.rb +32 -0
- data/test/xslt/test_custom_functions.rb +94 -0
- data/test/xslt/test_exception_handling.rb +37 -0
- metadata +512 -517
- data/deps.rip +0 -5
- data/ext/nokogiri/libcharset-1.dll +0 -0
- data/ext/nokogiri/libexslt.dll +0 -0
- data/ext/nokogiri/libiconv-2.dll +0 -0
- data/ext/nokogiri/libxml2.dll +0 -0
- data/ext/nokogiri/libxslt.dll +0 -0
- data/ext/nokogiri/zlib1.dll +0 -0
- data/lib/nokogiri/css/generated_parser.rb +0 -676
- data/lib/nokogiri/css/generated_tokenizer.rb +0 -146
- data/lib/nokogiri/ffi/encoding_handler.rb +0 -42
- data/lib/nokogiri/ffi/html/document.rb +0 -28
- data/lib/nokogiri/ffi/html/element_description.rb +0 -81
- data/lib/nokogiri/ffi/html/entity_lookup.rb +0 -16
- data/lib/nokogiri/ffi/html/sax/parser_context.rb +0 -38
- data/lib/nokogiri/ffi/io_callbacks.rb +0 -42
- data/lib/nokogiri/ffi/libxml.rb +0 -411
- data/lib/nokogiri/ffi/structs/common_node.rb +0 -38
- data/lib/nokogiri/ffi/structs/html_elem_desc.rb +0 -24
- data/lib/nokogiri/ffi/structs/html_entity_desc.rb +0 -13
- data/lib/nokogiri/ffi/structs/xml_alloc.rb +0 -16
- data/lib/nokogiri/ffi/structs/xml_attr.rb +0 -19
- data/lib/nokogiri/ffi/structs/xml_attribute.rb +0 -27
- data/lib/nokogiri/ffi/structs/xml_buffer.rb +0 -16
- data/lib/nokogiri/ffi/structs/xml_char_encoding_handler.rb +0 -11
- data/lib/nokogiri/ffi/structs/xml_document.rb +0 -117
- data/lib/nokogiri/ffi/structs/xml_dtd.rb +0 -28
- data/lib/nokogiri/ffi/structs/xml_element.rb +0 -26
- data/lib/nokogiri/ffi/structs/xml_element_content.rb +0 -17
- data/lib/nokogiri/ffi/structs/xml_entity.rb +0 -32
- data/lib/nokogiri/ffi/structs/xml_enumeration.rb +0 -12
- data/lib/nokogiri/ffi/structs/xml_node.rb +0 -28
- data/lib/nokogiri/ffi/structs/xml_node_set.rb +0 -53
- data/lib/nokogiri/ffi/structs/xml_notation.rb +0 -11
- data/lib/nokogiri/ffi/structs/xml_ns.rb +0 -15
- data/lib/nokogiri/ffi/structs/xml_parser_context.rb +0 -19
- data/lib/nokogiri/ffi/structs/xml_relax_ng.rb +0 -14
- data/lib/nokogiri/ffi/structs/xml_sax_handler.rb +0 -51
- data/lib/nokogiri/ffi/structs/xml_sax_push_parser_context.rb +0 -124
- data/lib/nokogiri/ffi/structs/xml_schema.rb +0 -13
- data/lib/nokogiri/ffi/structs/xml_syntax_error.rb +0 -31
- data/lib/nokogiri/ffi/structs/xml_text_reader.rb +0 -12
- data/lib/nokogiri/ffi/structs/xml_xpath_context.rb +0 -38
- data/lib/nokogiri/ffi/structs/xml_xpath_object.rb +0 -35
- data/lib/nokogiri/ffi/structs/xml_xpath_parser_context.rb +0 -20
- data/lib/nokogiri/ffi/structs/xslt_stylesheet.rb +0 -13
- data/lib/nokogiri/ffi/weak_bucket.rb +0 -40
- data/lib/nokogiri/ffi/xml/attr.rb +0 -41
- data/lib/nokogiri/ffi/xml/attribute_decl.rb +0 -27
- data/lib/nokogiri/ffi/xml/cdata.rb +0 -19
- data/lib/nokogiri/ffi/xml/comment.rb +0 -18
- data/lib/nokogiri/ffi/xml/document.rb +0 -166
- data/lib/nokogiri/ffi/xml/document_fragment.rb +0 -21
- data/lib/nokogiri/ffi/xml/dtd.rb +0 -67
- data/lib/nokogiri/ffi/xml/element_content.rb +0 -43
- data/lib/nokogiri/ffi/xml/element_decl.rb +0 -19
- data/lib/nokogiri/ffi/xml/entity_decl.rb +0 -36
- data/lib/nokogiri/ffi/xml/entity_reference.rb +0 -19
- data/lib/nokogiri/ffi/xml/namespace.rb +0 -44
- data/lib/nokogiri/ffi/xml/node.rb +0 -554
- data/lib/nokogiri/ffi/xml/node_set.rb +0 -149
- data/lib/nokogiri/ffi/xml/processing_instruction.rb +0 -20
- data/lib/nokogiri/ffi/xml/reader.rb +0 -236
- data/lib/nokogiri/ffi/xml/relax_ng.rb +0 -85
- data/lib/nokogiri/ffi/xml/sax/parser.rb +0 -135
- data/lib/nokogiri/ffi/xml/sax/parser_context.rb +0 -67
- data/lib/nokogiri/ffi/xml/sax/push_parser.rb +0 -51
- data/lib/nokogiri/ffi/xml/schema.rb +0 -109
- data/lib/nokogiri/ffi/xml/syntax_error.rb +0 -98
- data/lib/nokogiri/ffi/xml/text.rb +0 -18
- data/lib/nokogiri/ffi/xml/xpath.rb +0 -9
- data/lib/nokogiri/ffi/xml/xpath_context.rb +0 -148
- data/lib/nokogiri/ffi/xslt/stylesheet.rb +0 -53
- data/lib/nokogiri/version_warning.rb +0 -14
- data/test/ffi/test_document.rb +0 -35
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
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.io.ByteArrayInputStream;
|
|
38
|
+
import java.io.IOException;
|
|
39
|
+
import java.io.InputStream;
|
|
40
|
+
import java.io.StringWriter;
|
|
41
|
+
import java.io.UnsupportedEncodingException;
|
|
42
|
+
|
|
43
|
+
import javax.xml.transform.Source;
|
|
44
|
+
import javax.xml.transform.TransformerConfigurationException;
|
|
45
|
+
import javax.xml.transform.TransformerException;
|
|
46
|
+
import javax.xml.transform.TransformerFactory;
|
|
47
|
+
import javax.xml.transform.dom.DOMSource;
|
|
48
|
+
import javax.xml.transform.stream.StreamResult;
|
|
49
|
+
import javax.xml.transform.stream.StreamSource;
|
|
50
|
+
|
|
51
|
+
import org.iso_relax.verifier.Schema;
|
|
52
|
+
import org.iso_relax.verifier.Verifier;
|
|
53
|
+
import org.iso_relax.verifier.VerifierConfigurationException;
|
|
54
|
+
import org.iso_relax.verifier.VerifierFactory;
|
|
55
|
+
import org.jruby.Ruby;
|
|
56
|
+
import org.jruby.RubyClass;
|
|
57
|
+
import org.jruby.anno.JRubyClass;
|
|
58
|
+
import org.jruby.runtime.ThreadContext;
|
|
59
|
+
import org.w3c.dom.Document;
|
|
60
|
+
import org.xml.sax.ErrorHandler;
|
|
61
|
+
import org.xml.sax.SAXException;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Class for Nokogiri::XML::RelaxNG
|
|
65
|
+
*
|
|
66
|
+
* @author sergio
|
|
67
|
+
* @author Yoko Harada <yokolet@gmail.com>
|
|
68
|
+
*/
|
|
69
|
+
@JRubyClass(name="Nokogiri::XML::RelaxNG", parent="Nokogiri::XML::Schema")
|
|
70
|
+
public class XmlRelaxng extends XmlSchema {
|
|
71
|
+
private Verifier verifier;
|
|
72
|
+
|
|
73
|
+
public XmlRelaxng(Ruby ruby, RubyClass klazz) {
|
|
74
|
+
super(ruby, klazz);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private void setVerifier(Verifier verifier) {
|
|
78
|
+
this.verifier = verifier;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
static XmlSchema createSchemaInstance(ThreadContext context, RubyClass klazz, Source source) {
|
|
82
|
+
Ruby runtime = context.getRuntime();
|
|
83
|
+
XmlRelaxng xmlRelaxng = (XmlRelaxng) NokogiriService.XML_RELAXNG_ALLOCATOR.allocate(runtime, klazz);
|
|
84
|
+
xmlRelaxng.setInstanceVariable("@errors", runtime.newEmptyArray());
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
Schema schema = xmlRelaxng.getSchema(source, context);
|
|
88
|
+
xmlRelaxng.setVerifier(schema.newVerifier());
|
|
89
|
+
return xmlRelaxng;
|
|
90
|
+
} catch (VerifierConfigurationException ex) {
|
|
91
|
+
throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private Schema getSchema(Source source, ThreadContext context) {
|
|
96
|
+
InputStream is = null;
|
|
97
|
+
VerifierFactory factory = new com.thaiopensource.relaxng.jarv.VerifierFactoryImpl();
|
|
98
|
+
if (source instanceof StreamSource) {
|
|
99
|
+
StreamSource ss = (StreamSource)source;
|
|
100
|
+
is = ss.getInputStream();
|
|
101
|
+
} else { //if (this.source instanceof DOMSource)
|
|
102
|
+
DOMSource ds = (DOMSource)source;
|
|
103
|
+
StringWriter xmlAsWriter = new StringWriter();
|
|
104
|
+
StreamResult result = new StreamResult(xmlAsWriter);
|
|
105
|
+
try {
|
|
106
|
+
TransformerFactory.newInstance().newTransformer().transform(ds, result);
|
|
107
|
+
} catch (TransformerConfigurationException ex) {
|
|
108
|
+
throw context.getRuntime()
|
|
109
|
+
.newRuntimeError("Could not parse document: "+ex.getMessage());
|
|
110
|
+
} catch (TransformerException ex) {
|
|
111
|
+
throw context.getRuntime()
|
|
112
|
+
.newRuntimeError("Could not parse document: "+ex.getMessage());
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
is = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8"));
|
|
116
|
+
} catch (UnsupportedEncodingException ex) {
|
|
117
|
+
throw context.getRuntime()
|
|
118
|
+
.newRuntimeError("Could not parse document: "+ex.getMessage());
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
try {
|
|
123
|
+
return factory.compileSchema(is);
|
|
124
|
+
} catch (VerifierConfigurationException ex) {
|
|
125
|
+
throw context.getRuntime()
|
|
126
|
+
.newRuntimeError("Could not parse document: "+ex.getMessage());
|
|
127
|
+
} catch (SAXException ex) {
|
|
128
|
+
throw context.getRuntime()
|
|
129
|
+
.newRuntimeError("Could not parse document: "+ex.getMessage());
|
|
130
|
+
} catch (IOException ex) {
|
|
131
|
+
throw context.getRuntime().newIOError(ex.getMessage());
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
@Override
|
|
136
|
+
protected void setErrorHandler(ErrorHandler errorHandler) {
|
|
137
|
+
verifier.setErrorHandler(errorHandler);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
@Override
|
|
141
|
+
protected void validate(Document document) throws SAXException, IOException {
|
|
142
|
+
verifier.verify(document);
|
|
143
|
+
}
|
|
144
|
+
}
|