Nokogiri_precompiled_aarch64_dedshit 1.14.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +44 -0
- data/LICENSE-DEPENDENCIES.md +2224 -0
- data/LICENSE.md +9 -0
- data/README.md +287 -0
- data/bin/nokogiri +131 -0
- data/dependencies.yml +41 -0
- data/ext/java/nokogiri/Html4Document.java +157 -0
- data/ext/java/nokogiri/Html4ElementDescription.java +133 -0
- data/ext/java/nokogiri/Html4EntityLookup.java +63 -0
- data/ext/java/nokogiri/Html4SaxParserContext.java +289 -0
- data/ext/java/nokogiri/Html4SaxPushParser.java +213 -0
- data/ext/java/nokogiri/NokogiriService.java +613 -0
- data/ext/java/nokogiri/XmlAttr.java +154 -0
- data/ext/java/nokogiri/XmlAttributeDecl.java +119 -0
- data/ext/java/nokogiri/XmlCdata.java +60 -0
- data/ext/java/nokogiri/XmlComment.java +77 -0
- data/ext/java/nokogiri/XmlDocument.java +705 -0
- data/ext/java/nokogiri/XmlDocumentFragment.java +163 -0
- data/ext/java/nokogiri/XmlDtd.java +516 -0
- data/ext/java/nokogiri/XmlElement.java +44 -0
- data/ext/java/nokogiri/XmlElementContent.java +412 -0
- data/ext/java/nokogiri/XmlElementDecl.java +148 -0
- data/ext/java/nokogiri/XmlEntityDecl.java +151 -0
- data/ext/java/nokogiri/XmlEntityReference.java +79 -0
- data/ext/java/nokogiri/XmlNamespace.java +193 -0
- data/ext/java/nokogiri/XmlNode.java +1938 -0
- data/ext/java/nokogiri/XmlNodeSet.java +463 -0
- data/ext/java/nokogiri/XmlProcessingInstruction.java +79 -0
- data/ext/java/nokogiri/XmlReader.java +615 -0
- data/ext/java/nokogiri/XmlRelaxng.java +133 -0
- data/ext/java/nokogiri/XmlSaxParserContext.java +329 -0
- data/ext/java/nokogiri/XmlSaxPushParser.java +288 -0
- data/ext/java/nokogiri/XmlSchema.java +423 -0
- data/ext/java/nokogiri/XmlSyntaxError.java +137 -0
- data/ext/java/nokogiri/XmlText.java +90 -0
- data/ext/java/nokogiri/XmlXpathContext.java +305 -0
- data/ext/java/nokogiri/XsltStylesheet.java +368 -0
- data/ext/java/nokogiri/internals/ClosedStreamException.java +13 -0
- data/ext/java/nokogiri/internals/HtmlDomParserContext.java +252 -0
- data/ext/java/nokogiri/internals/IgnoreSchemaErrorsErrorHandler.java +27 -0
- data/ext/java/nokogiri/internals/NokogiriBlockingQueueInputStream.java +178 -0
- data/ext/java/nokogiri/internals/NokogiriDomParser.java +99 -0
- data/ext/java/nokogiri/internals/NokogiriEntityResolver.java +140 -0
- data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +65 -0
- data/ext/java/nokogiri/internals/NokogiriHandler.java +339 -0
- data/ext/java/nokogiri/internals/NokogiriHelpers.java +817 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +228 -0
- data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +110 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +86 -0
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +107 -0
- data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +62 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +165 -0
- data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +50 -0
- data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +37 -0
- data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +70 -0
- data/ext/java/nokogiri/internals/ParserContext.java +262 -0
- data/ext/java/nokogiri/internals/ReaderNode.java +564 -0
- data/ext/java/nokogiri/internals/SaveContextVisitor.java +865 -0
- data/ext/java/nokogiri/internals/SchemaErrorHandler.java +50 -0
- data/ext/java/nokogiri/internals/XalanDTMManagerPatch.java +174 -0
- data/ext/java/nokogiri/internals/XmlDeclHandler.java +11 -0
- data/ext/java/nokogiri/internals/XmlDomParserContext.java +265 -0
- data/ext/java/nokogiri/internals/XmlSaxParser.java +40 -0
- data/ext/java/nokogiri/internals/c14n/AttrCompare.java +122 -0
- data/ext/java/nokogiri/internals/c14n/C14nHelper.java +178 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalFilter.java +43 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizationException.java +106 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer.java +278 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11.java +664 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_OmitComments.java +45 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_WithComments.java +45 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315.java +388 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315Excl.java +308 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclOmitComments.java +47 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclWithComments.java +51 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315OmitComments.java +51 -0
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315WithComments.java +50 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizerBase.java +660 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizerPhysical.java +194 -0
- data/ext/java/nokogiri/internals/c14n/CanonicalizerSpi.java +77 -0
- data/ext/java/nokogiri/internals/c14n/Constants.java +45 -0
- data/ext/java/nokogiri/internals/c14n/ElementProxy.java +325 -0
- data/ext/java/nokogiri/internals/c14n/HelperNodeList.java +106 -0
- data/ext/java/nokogiri/internals/c14n/IgnoreAllErrorHandler.java +86 -0
- data/ext/java/nokogiri/internals/c14n/InclusiveNamespaces.java +181 -0
- data/ext/java/nokogiri/internals/c14n/InvalidCanonicalizerException.java +87 -0
- data/ext/java/nokogiri/internals/c14n/NameSpaceSymbTable.java +452 -0
- data/ext/java/nokogiri/internals/c14n/NodeFilter.java +52 -0
- data/ext/java/nokogiri/internals/c14n/UtfHelpper.java +190 -0
- data/ext/java/nokogiri/internals/c14n/XMLUtils.java +540 -0
- data/ext/java/nokogiri/internals/dom2dtm/DOM2DTM.java +1712 -0
- data/ext/java/nokogiri/internals/dom2dtm/DOM2DTMdefaultNamespaceDeclarationNode.java +737 -0
- data/ext/nokogiri/depend +38 -0
- data/ext/nokogiri/extconf.rb +1086 -0
- data/ext/nokogiri/gumbo.c +594 -0
- data/ext/nokogiri/html4_document.c +167 -0
- data/ext/nokogiri/html4_element_description.c +294 -0
- data/ext/nokogiri/html4_entity_lookup.c +37 -0
- data/ext/nokogiri/html4_sax_parser_context.c +116 -0
- data/ext/nokogiri/html4_sax_push_parser.c +95 -0
- data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
- data/ext/nokogiri/nokogiri.c +265 -0
- data/ext/nokogiri/nokogiri.h +235 -0
- data/ext/nokogiri/test_global_handlers.c +42 -0
- data/ext/nokogiri/xml_attr.c +103 -0
- data/ext/nokogiri/xml_attribute_decl.c +70 -0
- data/ext/nokogiri/xml_cdata.c +57 -0
- data/ext/nokogiri/xml_comment.c +62 -0
- data/ext/nokogiri/xml_document.c +689 -0
- data/ext/nokogiri/xml_document_fragment.c +44 -0
- data/ext/nokogiri/xml_dtd.c +210 -0
- data/ext/nokogiri/xml_element_content.c +128 -0
- data/ext/nokogiri/xml_element_decl.c +69 -0
- data/ext/nokogiri/xml_encoding_handler.c +104 -0
- data/ext/nokogiri/xml_entity_decl.c +112 -0
- data/ext/nokogiri/xml_entity_reference.c +50 -0
- data/ext/nokogiri/xml_namespace.c +186 -0
- data/ext/nokogiri/xml_node.c +2426 -0
- data/ext/nokogiri/xml_node_set.c +496 -0
- data/ext/nokogiri/xml_processing_instruction.c +54 -0
- data/ext/nokogiri/xml_reader.c +794 -0
- data/ext/nokogiri/xml_relax_ng.c +164 -0
- data/ext/nokogiri/xml_sax_parser.c +316 -0
- data/ext/nokogiri/xml_sax_parser_context.c +283 -0
- data/ext/nokogiri/xml_sax_push_parser.c +166 -0
- data/ext/nokogiri/xml_schema.c +260 -0
- data/ext/nokogiri/xml_syntax_error.c +85 -0
- data/ext/nokogiri/xml_text.c +48 -0
- data/ext/nokogiri/xml_xpath_context.c +415 -0
- data/ext/nokogiri/xslt_stylesheet.c +363 -0
- data/gumbo-parser/CHANGES.md +63 -0
- data/gumbo-parser/Makefile +111 -0
- data/gumbo-parser/THANKS +27 -0
- data/gumbo-parser/src/Makefile +34 -0
- data/gumbo-parser/src/README.md +41 -0
- data/gumbo-parser/src/ascii.c +75 -0
- data/gumbo-parser/src/ascii.h +115 -0
- data/gumbo-parser/src/attribute.c +42 -0
- data/gumbo-parser/src/attribute.h +17 -0
- data/gumbo-parser/src/char_ref.c +22225 -0
- data/gumbo-parser/src/char_ref.h +29 -0
- data/gumbo-parser/src/char_ref.rl +2154 -0
- data/gumbo-parser/src/error.c +626 -0
- data/gumbo-parser/src/error.h +148 -0
- data/gumbo-parser/src/foreign_attrs.c +104 -0
- data/gumbo-parser/src/foreign_attrs.gperf +27 -0
- data/gumbo-parser/src/insertion_mode.h +33 -0
- data/gumbo-parser/src/macros.h +91 -0
- data/gumbo-parser/src/nokogiri_gumbo.h +944 -0
- data/gumbo-parser/src/parser.c +4878 -0
- data/gumbo-parser/src/parser.h +41 -0
- data/gumbo-parser/src/replacement.h +33 -0
- data/gumbo-parser/src/string_buffer.c +103 -0
- data/gumbo-parser/src/string_buffer.h +68 -0
- data/gumbo-parser/src/string_piece.c +48 -0
- data/gumbo-parser/src/svg_attrs.c +174 -0
- data/gumbo-parser/src/svg_attrs.gperf +77 -0
- data/gumbo-parser/src/svg_tags.c +137 -0
- data/gumbo-parser/src/svg_tags.gperf +55 -0
- data/gumbo-parser/src/tag.c +223 -0
- data/gumbo-parser/src/tag_lookup.c +382 -0
- data/gumbo-parser/src/tag_lookup.gperf +170 -0
- data/gumbo-parser/src/tag_lookup.h +13 -0
- data/gumbo-parser/src/token_buffer.c +79 -0
- data/gumbo-parser/src/token_buffer.h +71 -0
- data/gumbo-parser/src/token_type.h +17 -0
- data/gumbo-parser/src/tokenizer.c +3463 -0
- data/gumbo-parser/src/tokenizer.h +112 -0
- data/gumbo-parser/src/tokenizer_states.h +339 -0
- data/gumbo-parser/src/utf8.c +245 -0
- data/gumbo-parser/src/utf8.h +164 -0
- data/gumbo-parser/src/util.c +66 -0
- data/gumbo-parser/src/util.h +34 -0
- data/gumbo-parser/src/vector.c +111 -0
- data/gumbo-parser/src/vector.h +45 -0
- data/lib/nokogiri/class_resolver.rb +67 -0
- data/lib/nokogiri/css/node.rb +54 -0
- data/lib/nokogiri/css/parser.rb +770 -0
- data/lib/nokogiri/css/parser.y +277 -0
- data/lib/nokogiri/css/parser_extras.rb +96 -0
- data/lib/nokogiri/css/syntax_error.rb +9 -0
- data/lib/nokogiri/css/tokenizer.rb +155 -0
- data/lib/nokogiri/css/tokenizer.rex +56 -0
- data/lib/nokogiri/css/xpath_visitor.rb +359 -0
- data/lib/nokogiri/css.rb +66 -0
- data/lib/nokogiri/decorators/slop.rb +44 -0
- data/lib/nokogiri/encoding_handler.rb +57 -0
- data/lib/nokogiri/extension.rb +32 -0
- data/lib/nokogiri/gumbo.rb +15 -0
- data/lib/nokogiri/html.rb +48 -0
- data/lib/nokogiri/html4/builder.rb +37 -0
- data/lib/nokogiri/html4/document.rb +214 -0
- data/lib/nokogiri/html4/document_fragment.rb +54 -0
- data/lib/nokogiri/html4/element_description.rb +25 -0
- data/lib/nokogiri/html4/element_description_defaults.rb +572 -0
- data/lib/nokogiri/html4/encoding_reader.rb +121 -0
- data/lib/nokogiri/html4/entity_lookup.rb +15 -0
- data/lib/nokogiri/html4/sax/parser.rb +63 -0
- data/lib/nokogiri/html4/sax/parser_context.rb +20 -0
- data/lib/nokogiri/html4/sax/push_parser.rb +37 -0
- data/lib/nokogiri/html4.rb +47 -0
- data/lib/nokogiri/html5/document.rb +168 -0
- data/lib/nokogiri/html5/document_fragment.rb +90 -0
- data/lib/nokogiri/html5/node.rb +98 -0
- data/lib/nokogiri/html5.rb +389 -0
- data/lib/nokogiri/jruby/dependencies.rb +3 -0
- data/lib/nokogiri/jruby/isorelax/isorelax/20030108/isorelax-20030108.jar +0 -0
- data/lib/nokogiri/jruby/net/sf/saxon/Saxon-HE/9.6.0-4/Saxon-HE-9.6.0-4.jar +0 -0
- data/lib/nokogiri/jruby/net/sourceforge/htmlunit/neko-htmlunit/2.63.0/neko-htmlunit-2.63.0.jar +0 -0
- data/lib/nokogiri/jruby/nokogiri_jars.rb +43 -0
- data/lib/nokogiri/jruby/nu/validator/jing/20200702VNU/jing-20200702VNU.jar +0 -0
- data/lib/nokogiri/jruby/org/nokogiri/nekodtd/0.1.11.noko2/nekodtd-0.1.11.noko2.jar +0 -0
- data/lib/nokogiri/jruby/xalan/serializer/2.7.3/serializer-2.7.3.jar +0 -0
- data/lib/nokogiri/jruby/xalan/xalan/2.7.3/xalan-2.7.3.jar +0 -0
- data/lib/nokogiri/jruby/xerces/xercesImpl/2.12.2/xercesImpl-2.12.2.jar +0 -0
- data/lib/nokogiri/jruby/xml-apis/xml-apis/1.4.01/xml-apis-1.4.01.jar +0 -0
- data/lib/nokogiri/syntax_error.rb +6 -0
- data/lib/nokogiri/version/constant.rb +6 -0
- data/lib/nokogiri/version/info.rb +223 -0
- data/lib/nokogiri/version.rb +4 -0
- data/lib/nokogiri/xml/attr.rb +66 -0
- data/lib/nokogiri/xml/attribute_decl.rb +20 -0
- data/lib/nokogiri/xml/builder.rb +487 -0
- data/lib/nokogiri/xml/cdata.rb +13 -0
- data/lib/nokogiri/xml/character_data.rb +9 -0
- data/lib/nokogiri/xml/document.rb +471 -0
- data/lib/nokogiri/xml/document_fragment.rb +205 -0
- data/lib/nokogiri/xml/dtd.rb +34 -0
- data/lib/nokogiri/xml/element_content.rb +38 -0
- data/lib/nokogiri/xml/element_decl.rb +15 -0
- data/lib/nokogiri/xml/entity_decl.rb +21 -0
- data/lib/nokogiri/xml/entity_reference.rb +20 -0
- data/lib/nokogiri/xml/namespace.rb +58 -0
- data/lib/nokogiri/xml/node/save_options.rb +68 -0
- data/lib/nokogiri/xml/node.rb +1563 -0
- data/lib/nokogiri/xml/node_set.rb +447 -0
- data/lib/nokogiri/xml/notation.rb +19 -0
- data/lib/nokogiri/xml/parse_options.rb +213 -0
- data/lib/nokogiri/xml/pp/character_data.rb +21 -0
- data/lib/nokogiri/xml/pp/node.rb +57 -0
- data/lib/nokogiri/xml/pp.rb +4 -0
- data/lib/nokogiri/xml/processing_instruction.rb +11 -0
- data/lib/nokogiri/xml/reader.rb +105 -0
- data/lib/nokogiri/xml/relax_ng.rb +38 -0
- data/lib/nokogiri/xml/sax/document.rb +167 -0
- data/lib/nokogiri/xml/sax/parser.rb +125 -0
- data/lib/nokogiri/xml/sax/parser_context.rb +21 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +61 -0
- data/lib/nokogiri/xml/sax.rb +6 -0
- data/lib/nokogiri/xml/schema.rb +73 -0
- data/lib/nokogiri/xml/searchable.rb +270 -0
- data/lib/nokogiri/xml/syntax_error.rb +72 -0
- data/lib/nokogiri/xml/text.rb +11 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +13 -0
- data/lib/nokogiri/xml/xpath.rb +21 -0
- data/lib/nokogiri/xml/xpath_context.rb +16 -0
- data/lib/nokogiri/xml.rb +76 -0
- data/lib/nokogiri/xslt/stylesheet.rb +27 -0
- data/lib/nokogiri/xslt.rb +65 -0
- data/lib/nokogiri.rb +120 -0
- data/lib/xsd/xmlparser/nokogiri.rb +106 -0
- metadata +391 -0
@@ -0,0 +1,133 @@
|
|
1
|
+
package nokogiri;
|
2
|
+
|
3
|
+
import java.util.ArrayList;
|
4
|
+
import java.util.Collections;
|
5
|
+
import java.util.HashMap;
|
6
|
+
import java.util.List;
|
7
|
+
import java.util.Map;
|
8
|
+
|
9
|
+
import net.sourceforge.htmlunit.cyberneko.HTMLElements;
|
10
|
+
import org.jruby.Ruby;
|
11
|
+
import org.jruby.RubyClass;
|
12
|
+
import org.jruby.RubyObject;
|
13
|
+
import org.jruby.anno.JRubyClass;
|
14
|
+
import org.jruby.anno.JRubyMethod;
|
15
|
+
import org.jruby.runtime.ThreadContext;
|
16
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
17
|
+
|
18
|
+
/**
|
19
|
+
* Class for Nokogiri::HTML4::ElementDescription.
|
20
|
+
*
|
21
|
+
* @author Patrick Mahoney <pat@polycrystal.org>
|
22
|
+
*/
|
23
|
+
@JRubyClass(name = "Nokogiri::HTML4::ElementDescription")
|
24
|
+
public class Html4ElementDescription extends RubyObject
|
25
|
+
{
|
26
|
+
private static final long serialVersionUID = 1L;
|
27
|
+
private static final HTMLElements htmlElements_ = new HTMLElements();
|
28
|
+
|
29
|
+
/**
|
30
|
+
* Stores memoized hash of element -> list of valid subelements.
|
31
|
+
*/
|
32
|
+
static protected Map<Short, List<String>> subElements;
|
33
|
+
static
|
34
|
+
{
|
35
|
+
Map<Short, List<String>> _subElements =
|
36
|
+
new HashMap<Short, List<String>>();
|
37
|
+
subElements = Collections.synchronizedMap(_subElements);
|
38
|
+
}
|
39
|
+
|
40
|
+
protected HTMLElements.Element element;
|
41
|
+
|
42
|
+
public
|
43
|
+
Html4ElementDescription(Ruby runtime, RubyClass rubyClass)
|
44
|
+
{
|
45
|
+
super(runtime, rubyClass);
|
46
|
+
}
|
47
|
+
|
48
|
+
/**
|
49
|
+
* Lookup the list of sub elements of <code>code</code>. If not
|
50
|
+
* already stored, iterate through all elements to find valid
|
51
|
+
* subelements; save this list and return it.
|
52
|
+
*/
|
53
|
+
protected static List<String>
|
54
|
+
findSubElements(HTMLElements.Element elem)
|
55
|
+
{
|
56
|
+
List<String> subs = subElements.get(elem.code);
|
57
|
+
|
58
|
+
if (subs == null) {
|
59
|
+
subs = new ArrayList<String>();
|
60
|
+
|
61
|
+
/*
|
62
|
+
* A bit of a hack. NekoHtml source code shows that
|
63
|
+
* UNKNOWN is the highest value element. We cannot access
|
64
|
+
* the list of elements directly because it's protected.
|
65
|
+
*/
|
66
|
+
for (short c = 0; c < HTMLElements.UNKNOWN; c++) {
|
67
|
+
HTMLElements.Element maybe_sub = htmlElements_.getElement(c);
|
68
|
+
if (maybe_sub != null && maybe_sub.isParent(elem)) {
|
69
|
+
subs.add(maybe_sub.name);
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
subElements.put(elem.code, subs);
|
74
|
+
}
|
75
|
+
|
76
|
+
return subs;
|
77
|
+
}
|
78
|
+
|
79
|
+
@JRubyMethod(name = "[]", meta = true)
|
80
|
+
public static IRubyObject
|
81
|
+
get(ThreadContext context,
|
82
|
+
IRubyObject klazz, IRubyObject name)
|
83
|
+
{
|
84
|
+
|
85
|
+
// nekohtml will return an element even for invalid names, which breaks `test_fetch_nonexistent'
|
86
|
+
// see getElement() in HTMLElements.java
|
87
|
+
HTMLElements.Element elem = htmlElements_.getElement(name.asJavaString(), htmlElements_.NO_SUCH_ELEMENT);
|
88
|
+
if (elem == htmlElements_.NO_SUCH_ELEMENT) {
|
89
|
+
return context.nil;
|
90
|
+
}
|
91
|
+
|
92
|
+
Html4ElementDescription desc =
|
93
|
+
new Html4ElementDescription(context.getRuntime(), (RubyClass)klazz);
|
94
|
+
desc.element = elem;
|
95
|
+
return desc;
|
96
|
+
}
|
97
|
+
|
98
|
+
@JRubyMethod()
|
99
|
+
public IRubyObject
|
100
|
+
name(ThreadContext context)
|
101
|
+
{
|
102
|
+
return context.getRuntime().newString(element.name.toLowerCase());
|
103
|
+
}
|
104
|
+
|
105
|
+
@JRubyMethod(name = "inline?")
|
106
|
+
public IRubyObject
|
107
|
+
inline_eh(ThreadContext context)
|
108
|
+
{
|
109
|
+
return context.getRuntime().newBoolean(element.isInline());
|
110
|
+
}
|
111
|
+
|
112
|
+
@JRubyMethod(name = "empty?")
|
113
|
+
public IRubyObject
|
114
|
+
empty_eh(ThreadContext context)
|
115
|
+
{
|
116
|
+
return context.getRuntime().newBoolean(element.isEmpty());
|
117
|
+
}
|
118
|
+
|
119
|
+
@JRubyMethod()
|
120
|
+
public IRubyObject
|
121
|
+
sub_elements(ThreadContext context)
|
122
|
+
{
|
123
|
+
Ruby ruby = context.getRuntime();
|
124
|
+
List<String> subs = findSubElements(element);
|
125
|
+
IRubyObject[] ary = new IRubyObject[subs.size()];
|
126
|
+
for (int i = 0; i < subs.size(); ++i) {
|
127
|
+
ary[i] = ruby.newString(subs.get(i));
|
128
|
+
}
|
129
|
+
|
130
|
+
return ruby.newArray(ary);
|
131
|
+
}
|
132
|
+
|
133
|
+
}
|
@@ -0,0 +1,63 @@
|
|
1
|
+
package nokogiri;
|
2
|
+
|
3
|
+
import static org.jruby.runtime.Helpers.invoke;
|
4
|
+
|
5
|
+
import net.sourceforge.htmlunit.cyberneko.HTMLEntitiesParser;
|
6
|
+
import org.jruby.Ruby;
|
7
|
+
import org.jruby.RubyClass;
|
8
|
+
import org.jruby.RubyObject;
|
9
|
+
import org.jruby.anno.JRubyClass;
|
10
|
+
import org.jruby.anno.JRubyMethod;
|
11
|
+
import org.jruby.runtime.ThreadContext;
|
12
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
13
|
+
|
14
|
+
/**
|
15
|
+
* Class for Nokogiri::HTML4::EntityLookup.
|
16
|
+
*
|
17
|
+
* @author Patrick Mahoney <pat@polycrystal.org>
|
18
|
+
*/
|
19
|
+
@JRubyClass(name = "Nokogiri::HTML4::EntityLookup")
|
20
|
+
public class Html4EntityLookup extends RubyObject
|
21
|
+
{
|
22
|
+
private static final long serialVersionUID = 1L;
|
23
|
+
|
24
|
+
public
|
25
|
+
Html4EntityLookup(Ruby runtime, RubyClass rubyClass)
|
26
|
+
{
|
27
|
+
super(runtime, rubyClass);
|
28
|
+
}
|
29
|
+
|
30
|
+
/**
|
31
|
+
* Looks up an HTML entity <code>key</code>.
|
32
|
+
*
|
33
|
+
* The description is a bit lacking.
|
34
|
+
*/
|
35
|
+
@JRubyMethod()
|
36
|
+
public IRubyObject
|
37
|
+
get(ThreadContext context, IRubyObject key)
|
38
|
+
{
|
39
|
+
Ruby ruby = context.getRuntime();
|
40
|
+
String name = key.toString();
|
41
|
+
|
42
|
+
HTMLEntitiesParser parser = new HTMLEntitiesParser();
|
43
|
+
for (int j = 0 ; j < name.length() ; j++) {
|
44
|
+
if (!parser.parse(name.charAt(j))) {
|
45
|
+
break;
|
46
|
+
}
|
47
|
+
}
|
48
|
+
String match = parser.getMatch();
|
49
|
+
|
50
|
+
if (match == null) { return ruby.getNil(); }
|
51
|
+
|
52
|
+
int val = match.charAt(0);
|
53
|
+
|
54
|
+
IRubyObject edClass =
|
55
|
+
ruby.getClassFromPath("Nokogiri::HTML4::EntityDescription");
|
56
|
+
IRubyObject edObj = invoke(context, edClass, "new",
|
57
|
+
ruby.newFixnum(val), ruby.newString(name),
|
58
|
+
ruby.newString(name + " entity"));
|
59
|
+
|
60
|
+
return edObj;
|
61
|
+
}
|
62
|
+
|
63
|
+
}
|
@@ -0,0 +1,289 @@
|
|
1
|
+
package nokogiri;
|
2
|
+
|
3
|
+
import java.io.ByteArrayInputStream;
|
4
|
+
import java.io.InputStream;
|
5
|
+
import java.nio.charset.Charset;
|
6
|
+
import java.nio.charset.IllegalCharsetNameException;
|
7
|
+
import java.nio.charset.UnsupportedCharsetException;
|
8
|
+
import java.util.regex.Matcher;
|
9
|
+
import java.util.regex.Pattern;
|
10
|
+
|
11
|
+
import org.apache.xerces.parsers.AbstractSAXParser;
|
12
|
+
import net.sourceforge.htmlunit.cyberneko.parsers.SAXParser;
|
13
|
+
import org.jruby.Ruby;
|
14
|
+
import org.jruby.RubyClass;
|
15
|
+
import org.jruby.RubyFixnum;
|
16
|
+
import org.jruby.RubyString;
|
17
|
+
import org.jruby.anno.JRubyClass;
|
18
|
+
import org.jruby.anno.JRubyMethod;
|
19
|
+
import org.jruby.runtime.ThreadContext;
|
20
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
21
|
+
import org.xml.sax.SAXException;
|
22
|
+
|
23
|
+
import nokogiri.internals.NokogiriHandler;
|
24
|
+
import static nokogiri.internals.NokogiriHelpers.rubyStringToString;
|
25
|
+
|
26
|
+
/**
|
27
|
+
* Class for Nokogiri::HTML4::SAX::ParserContext.
|
28
|
+
*
|
29
|
+
* @author serabe
|
30
|
+
* @author Patrick Mahoney <pat@polycrystal.org>
|
31
|
+
* @author Yoko Harada <yokolet@gmail.com>
|
32
|
+
*/
|
33
|
+
@JRubyClass(name = "Nokogiri::HTML4::SAX::ParserContext", parent = "Nokogiri::XML::SAX::ParserContext")
|
34
|
+
public class Html4SaxParserContext extends XmlSaxParserContext
|
35
|
+
{
|
36
|
+
private static final long serialVersionUID = 1L;
|
37
|
+
|
38
|
+
static Html4SaxParserContext
|
39
|
+
newInstance(final Ruby runtime, final RubyClass klazz)
|
40
|
+
{
|
41
|
+
Html4SaxParserContext instance = new Html4SaxParserContext(runtime, klazz);
|
42
|
+
instance.initialize(runtime);
|
43
|
+
return instance;
|
44
|
+
}
|
45
|
+
|
46
|
+
public
|
47
|
+
Html4SaxParserContext(Ruby ruby, RubyClass rubyClass)
|
48
|
+
{
|
49
|
+
super(ruby, rubyClass);
|
50
|
+
}
|
51
|
+
|
52
|
+
@Override
|
53
|
+
protected AbstractSAXParser
|
54
|
+
createParser() throws SAXException
|
55
|
+
{
|
56
|
+
SAXParser parser = new SAXParser();
|
57
|
+
|
58
|
+
try {
|
59
|
+
parser.setProperty(
|
60
|
+
"http://cyberneko.org/html/properties/names/elems", "lower");
|
61
|
+
parser.setProperty(
|
62
|
+
"http://cyberneko.org/html/properties/names/attrs", "lower");
|
63
|
+
|
64
|
+
// NekoHTML should not try to guess the encoding based on the meta
|
65
|
+
// tags or other information in the document. This is already
|
66
|
+
// handled by the EncodingReader.
|
67
|
+
parser.setFeature("http://cyberneko.org/html/features/scanner/ignore-specified-charset", true);
|
68
|
+
return parser;
|
69
|
+
} catch (SAXException ex) {
|
70
|
+
throw new SAXException(
|
71
|
+
"Problem while creating HTML4 SAX Parser: " + ex.toString());
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
@JRubyMethod(name = "memory", meta = true)
|
76
|
+
public static IRubyObject
|
77
|
+
parse_memory(ThreadContext context,
|
78
|
+
IRubyObject klazz,
|
79
|
+
IRubyObject data,
|
80
|
+
IRubyObject encoding)
|
81
|
+
{
|
82
|
+
Html4SaxParserContext ctx = Html4SaxParserContext.newInstance(context.runtime, (RubyClass) klazz);
|
83
|
+
String javaEncoding = findEncodingName(context, encoding);
|
84
|
+
if (javaEncoding != null) {
|
85
|
+
CharSequence input = applyEncoding(rubyStringToString(data.convertToString()), javaEncoding);
|
86
|
+
ByteArrayInputStream istream = new ByteArrayInputStream(input.toString().getBytes());
|
87
|
+
ctx.setInputSource(istream);
|
88
|
+
ctx.getInputSource().setEncoding(javaEncoding);
|
89
|
+
}
|
90
|
+
return ctx;
|
91
|
+
}
|
92
|
+
|
93
|
+
public enum EncodingType {
|
94
|
+
NONE(0, "NONE"),
|
95
|
+
UTF_8(1, "UTF-8"),
|
96
|
+
UTF16LE(2, "UTF16LE"),
|
97
|
+
UTF16BE(3, "UTF16BE"),
|
98
|
+
UCS4LE(4, "UCS4LE"),
|
99
|
+
UCS4BE(5, "UCS4BE"),
|
100
|
+
EBCDIC(6, "EBCDIC"),
|
101
|
+
UCS4_2143(7, "ICS4-2143"),
|
102
|
+
UCS4_3412(8, "UCS4-3412"),
|
103
|
+
UCS2(9, "UCS2"),
|
104
|
+
ISO_8859_1(10, "ISO-8859-1"),
|
105
|
+
ISO_8859_2(11, "ISO-8859-2"),
|
106
|
+
ISO_8859_3(12, "ISO-8859-3"),
|
107
|
+
ISO_8859_4(13, "ISO-8859-4"),
|
108
|
+
ISO_8859_5(14, "ISO-8859-5"),
|
109
|
+
ISO_8859_6(15, "ISO-8859-6"),
|
110
|
+
ISO_8859_7(16, "ISO-8859-7"),
|
111
|
+
ISO_8859_8(17, "ISO-8859-8"),
|
112
|
+
ISO_8859_9(18, "ISO-8859-9"),
|
113
|
+
ISO_2022_JP(19, "ISO-2022-JP"),
|
114
|
+
SHIFT_JIS(20, "SHIFT-JIS"),
|
115
|
+
EUC_JP(21, "EUC-JP"),
|
116
|
+
ASCII(22, "ASCII");
|
117
|
+
|
118
|
+
private final int value;
|
119
|
+
private final String name;
|
120
|
+
|
121
|
+
EncodingType(int value, String name)
|
122
|
+
{
|
123
|
+
this.value = value;
|
124
|
+
this.name = name;
|
125
|
+
}
|
126
|
+
|
127
|
+
public int getValue()
|
128
|
+
{
|
129
|
+
return value;
|
130
|
+
}
|
131
|
+
|
132
|
+
public String toString()
|
133
|
+
{
|
134
|
+
return name;
|
135
|
+
}
|
136
|
+
|
137
|
+
private static transient EncodingType[] values;
|
138
|
+
|
139
|
+
// NOTE: assuming ordinal == value
|
140
|
+
static EncodingType get(final int ordinal)
|
141
|
+
{
|
142
|
+
EncodingType[] values = EncodingType.values;
|
143
|
+
if (values == null) {
|
144
|
+
values = EncodingType.values();
|
145
|
+
EncodingType.values = values;
|
146
|
+
}
|
147
|
+
if (ordinal >= 0 && ordinal < values.length) {
|
148
|
+
return values[ordinal];
|
149
|
+
}
|
150
|
+
return null;
|
151
|
+
}
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
private static String
|
156
|
+
findEncodingName(final int value)
|
157
|
+
{
|
158
|
+
EncodingType type = EncodingType.get(value);
|
159
|
+
if (type == null) { return null; }
|
160
|
+
assert type.value == value;
|
161
|
+
return type.name;
|
162
|
+
}
|
163
|
+
|
164
|
+
private static String
|
165
|
+
findEncodingName(ThreadContext context, IRubyObject encoding)
|
166
|
+
{
|
167
|
+
String rubyEncoding = null;
|
168
|
+
if (encoding instanceof RubyString) {
|
169
|
+
rubyEncoding = rubyStringToString((RubyString) encoding);
|
170
|
+
} else if (encoding instanceof RubyFixnum) {
|
171
|
+
rubyEncoding = findEncodingName(RubyFixnum.fix2int((RubyFixnum) encoding));
|
172
|
+
}
|
173
|
+
if (rubyEncoding == null) { return null; }
|
174
|
+
try {
|
175
|
+
return Charset.forName(rubyEncoding).displayName();
|
176
|
+
} catch (UnsupportedCharsetException e) {
|
177
|
+
throw context.getRuntime().newEncodingCompatibilityError(rubyEncoding + "is not supported");
|
178
|
+
} catch (IllegalCharsetNameException e) {
|
179
|
+
throw context.getRuntime().newEncodingError(e.getMessage());
|
180
|
+
}
|
181
|
+
}
|
182
|
+
|
183
|
+
private static final Pattern CHARSET_PATTERN = Pattern.compile("charset(()|\\s)=(()|\\s)([a-z]|-|_|\\d)+",
|
184
|
+
Pattern.CASE_INSENSITIVE);
|
185
|
+
|
186
|
+
private static CharSequence
|
187
|
+
applyEncoding(final String input, final String enc)
|
188
|
+
{
|
189
|
+
int start_pos = 0;
|
190
|
+
int end_pos = 0;
|
191
|
+
if (containsIgnoreCase(input, "charset")) {
|
192
|
+
Matcher m = CHARSET_PATTERN.matcher(input);
|
193
|
+
while (m.find()) {
|
194
|
+
start_pos = m.start();
|
195
|
+
end_pos = m.end();
|
196
|
+
}
|
197
|
+
}
|
198
|
+
if (start_pos != end_pos) {
|
199
|
+
return new StringBuilder(input).replace(start_pos, end_pos, "charset=" + enc);
|
200
|
+
}
|
201
|
+
return input;
|
202
|
+
}
|
203
|
+
|
204
|
+
private static boolean
|
205
|
+
containsIgnoreCase(final String str, final String sub)
|
206
|
+
{
|
207
|
+
final int len = sub.length();
|
208
|
+
final int max = str.length() - len;
|
209
|
+
|
210
|
+
if (len == 0) { return true; }
|
211
|
+
final char c0Lower = Character.toLowerCase(sub.charAt(0));
|
212
|
+
final char c0Upper = Character.toUpperCase(sub.charAt(0));
|
213
|
+
|
214
|
+
for (int i = 0; i <= max; i++) {
|
215
|
+
final char ch = str.charAt(i);
|
216
|
+
if (ch != c0Lower && Character.toLowerCase(ch) != c0Lower && Character.toUpperCase(ch) != c0Upper) {
|
217
|
+
continue; // first char doesn't match
|
218
|
+
}
|
219
|
+
|
220
|
+
if (str.regionMatches(true, i + 1, sub, 0 + 1, len - 1)) {
|
221
|
+
return true;
|
222
|
+
}
|
223
|
+
}
|
224
|
+
return false;
|
225
|
+
}
|
226
|
+
|
227
|
+
@JRubyMethod(name = "file", meta = true)
|
228
|
+
public static IRubyObject
|
229
|
+
parse_file(ThreadContext context,
|
230
|
+
IRubyObject klass,
|
231
|
+
IRubyObject data,
|
232
|
+
IRubyObject encoding)
|
233
|
+
{
|
234
|
+
if (!(data instanceof RubyString)) {
|
235
|
+
throw context.getRuntime().newTypeError("data must be kind_of String");
|
236
|
+
}
|
237
|
+
if (!(encoding instanceof RubyString)) {
|
238
|
+
throw context.getRuntime().newTypeError("data must be kind_of String");
|
239
|
+
}
|
240
|
+
|
241
|
+
Html4SaxParserContext ctx = Html4SaxParserContext.newInstance(context.runtime, (RubyClass) klass);
|
242
|
+
ctx.setInputSourceFile(context, data);
|
243
|
+
String javaEncoding = findEncodingName(context, encoding);
|
244
|
+
if (javaEncoding != null) {
|
245
|
+
ctx.getInputSource().setEncoding(javaEncoding);
|
246
|
+
}
|
247
|
+
return ctx;
|
248
|
+
}
|
249
|
+
|
250
|
+
@JRubyMethod(name = "io", meta = true)
|
251
|
+
public static IRubyObject
|
252
|
+
parse_io(ThreadContext context,
|
253
|
+
IRubyObject klass,
|
254
|
+
IRubyObject data,
|
255
|
+
IRubyObject encoding)
|
256
|
+
{
|
257
|
+
if (!(encoding instanceof RubyFixnum)) {
|
258
|
+
throw context.getRuntime().newTypeError("encoding must be kind_of String");
|
259
|
+
}
|
260
|
+
|
261
|
+
Html4SaxParserContext ctx = Html4SaxParserContext.newInstance(context.runtime, (RubyClass) klass);
|
262
|
+
ctx.setIOInputSource(context, data, context.nil);
|
263
|
+
String javaEncoding = findEncodingName(context, encoding);
|
264
|
+
if (javaEncoding != null) {
|
265
|
+
ctx.getInputSource().setEncoding(javaEncoding);
|
266
|
+
}
|
267
|
+
return ctx;
|
268
|
+
}
|
269
|
+
|
270
|
+
/**
|
271
|
+
* Create a new parser context that will read from a raw input stream.
|
272
|
+
* Meant to be run in a separate thread by Html4SaxPushParser.
|
273
|
+
*/
|
274
|
+
static Html4SaxParserContext
|
275
|
+
parse_stream(final Ruby runtime, RubyClass klass, InputStream stream)
|
276
|
+
{
|
277
|
+
Html4SaxParserContext ctx = Html4SaxParserContext.newInstance(runtime, klass);
|
278
|
+
ctx.setInputSource(stream);
|
279
|
+
return ctx;
|
280
|
+
}
|
281
|
+
|
282
|
+
@Override
|
283
|
+
protected void
|
284
|
+
preParse(final Ruby runtime, IRubyObject handlerRuby, NokogiriHandler handler)
|
285
|
+
{
|
286
|
+
// this function is meant to be empty. It overrides the one in XmlSaxParserContext
|
287
|
+
}
|
288
|
+
|
289
|
+
}
|
@@ -0,0 +1,213 @@
|
|
1
|
+
package nokogiri;
|
2
|
+
|
3
|
+
import nokogiri.internals.ClosedStreamException;
|
4
|
+
import nokogiri.internals.NokogiriBlockingQueueInputStream;
|
5
|
+
import nokogiri.internals.NokogiriHelpers;
|
6
|
+
import nokogiri.internals.ParserContext;
|
7
|
+
import org.jruby.Ruby;
|
8
|
+
import org.jruby.RubyClass;
|
9
|
+
import org.jruby.RubyObject;
|
10
|
+
import org.jruby.anno.JRubyClass;
|
11
|
+
import org.jruby.anno.JRubyMethod;
|
12
|
+
import org.jruby.runtime.ThreadContext;
|
13
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
14
|
+
|
15
|
+
import java.io.ByteArrayInputStream;
|
16
|
+
import java.io.IOException;
|
17
|
+
import java.io.InputStream;
|
18
|
+
import java.util.concurrent.*;
|
19
|
+
|
20
|
+
import static nokogiri.XmlSaxPushParser.terminateExecution;
|
21
|
+
import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
|
22
|
+
import static org.jruby.runtime.Helpers.invoke;
|
23
|
+
|
24
|
+
/**
|
25
|
+
* Class for Nokogiri::HTML4::SAX::PushParser
|
26
|
+
*
|
27
|
+
* @author
|
28
|
+
* @author Piotr Szmielew <p.szmielew@ava.waw.pl> - based on Nokogiri::XML::SAX::PushParser
|
29
|
+
*/
|
30
|
+
@JRubyClass(name = "Nokogiri::HTML4::SAX::PushParser")
|
31
|
+
public class Html4SaxPushParser extends RubyObject
|
32
|
+
{
|
33
|
+
private static final long serialVersionUID = 1L;
|
34
|
+
|
35
|
+
ParserContext.Options options;
|
36
|
+
IRubyObject saxParser;
|
37
|
+
|
38
|
+
NokogiriBlockingQueueInputStream stream;
|
39
|
+
|
40
|
+
private ParserTask parserTask = null;
|
41
|
+
private FutureTask<Html4SaxParserContext> futureTask = null;
|
42
|
+
private ExecutorService executor = null;
|
43
|
+
|
44
|
+
public
|
45
|
+
Html4SaxPushParser(Ruby ruby, RubyClass rubyClass)
|
46
|
+
{
|
47
|
+
super(ruby, rubyClass);
|
48
|
+
}
|
49
|
+
|
50
|
+
@SuppressWarnings("deprecation")
|
51
|
+
@Override
|
52
|
+
public void
|
53
|
+
finalize()
|
54
|
+
{
|
55
|
+
try {
|
56
|
+
terminateImpl();
|
57
|
+
} catch (Exception e) { /* ignored */ }
|
58
|
+
}
|
59
|
+
|
60
|
+
@JRubyMethod
|
61
|
+
public IRubyObject
|
62
|
+
initialize_native(final ThreadContext context,
|
63
|
+
IRubyObject saxParser,
|
64
|
+
IRubyObject fileName,
|
65
|
+
IRubyObject encoding)
|
66
|
+
{
|
67
|
+
// NOTE: Silently skips provided encoding
|
68
|
+
options = new ParserContext.Options(0);
|
69
|
+
this.saxParser = saxParser;
|
70
|
+
return this;
|
71
|
+
}
|
72
|
+
|
73
|
+
private transient IRubyObject parse_options;
|
74
|
+
|
75
|
+
private IRubyObject
|
76
|
+
parse_options(final ThreadContext context)
|
77
|
+
{
|
78
|
+
if (parse_options == null) {
|
79
|
+
parse_options = invoke(context, context.runtime.getClassFromPath("Nokogiri::XML::ParseOptions"), "new");
|
80
|
+
}
|
81
|
+
return parse_options;
|
82
|
+
}
|
83
|
+
|
84
|
+
@JRubyMethod(name = "options")
|
85
|
+
public IRubyObject
|
86
|
+
getOptions(ThreadContext context)
|
87
|
+
{
|
88
|
+
return invoke(context, parse_options(context), "options");
|
89
|
+
}
|
90
|
+
|
91
|
+
@JRubyMethod(name = "options=")
|
92
|
+
public IRubyObject
|
93
|
+
setOptions(ThreadContext context, IRubyObject opts)
|
94
|
+
{
|
95
|
+
invoke(context, parse_options(context), "options=", opts);
|
96
|
+
options = new ParserContext.Options(opts.convertToInteger().getLongValue());
|
97
|
+
return getOptions(context);
|
98
|
+
}
|
99
|
+
|
100
|
+
@JRubyMethod
|
101
|
+
public IRubyObject
|
102
|
+
native_write(ThreadContext context, IRubyObject chunk, IRubyObject isLast)
|
103
|
+
{
|
104
|
+
try {
|
105
|
+
initialize_task(context);
|
106
|
+
} catch (IOException e) {
|
107
|
+
throw context.getRuntime().newRuntimeError(e.getMessage());
|
108
|
+
}
|
109
|
+
final ByteArrayInputStream data = NokogiriHelpers.stringBytesToStream(chunk);
|
110
|
+
if (data == null) {
|
111
|
+
terminateTask(context.runtime);
|
112
|
+
throw XmlSyntaxError.createHTMLSyntaxError(context.runtime).toThrowable(); // Nokogiri::HTML4::SyntaxError
|
113
|
+
}
|
114
|
+
|
115
|
+
int errorCount0 = parserTask.getErrorCount();
|
116
|
+
|
117
|
+
if (isLast.isTrue()) {
|
118
|
+
IRubyObject document = invoke(context, this, "document");
|
119
|
+
invoke(context, document, "end_document");
|
120
|
+
terminateTask(context.runtime);
|
121
|
+
} else {
|
122
|
+
try {
|
123
|
+
Future<Void> task = stream.addChunk(data);
|
124
|
+
task.get();
|
125
|
+
} catch (ClosedStreamException ex) {
|
126
|
+
// this means the stream is closed, ignore this exception
|
127
|
+
} catch (Exception e) {
|
128
|
+
throw context.runtime.newRuntimeError(e.getMessage());
|
129
|
+
}
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
if (!options.recover && parserTask.getErrorCount() > errorCount0) {
|
134
|
+
terminateTask(context.runtime);
|
135
|
+
throw parserTask.getLastError().toThrowable();
|
136
|
+
}
|
137
|
+
|
138
|
+
return this;
|
139
|
+
}
|
140
|
+
|
141
|
+
@SuppressWarnings("unchecked")
|
142
|
+
private void
|
143
|
+
initialize_task(ThreadContext context) throws IOException
|
144
|
+
{
|
145
|
+
if (futureTask == null || stream == null) {
|
146
|
+
stream = new NokogiriBlockingQueueInputStream();
|
147
|
+
|
148
|
+
assert saxParser != null : "saxParser null";
|
149
|
+
parserTask = new ParserTask(context, saxParser, stream);
|
150
|
+
futureTask = new FutureTask<Html4SaxParserContext>((Callable) parserTask);
|
151
|
+
executor = Executors.newSingleThreadExecutor(new ThreadFactory() {
|
152
|
+
@Override
|
153
|
+
public Thread newThread(Runnable r) {
|
154
|
+
Thread t = new Thread(r);
|
155
|
+
t.setName("Html4SaxPushParser");
|
156
|
+
t.setDaemon(true);
|
157
|
+
return t;
|
158
|
+
}
|
159
|
+
});
|
160
|
+
executor.submit(futureTask);
|
161
|
+
}
|
162
|
+
}
|
163
|
+
|
164
|
+
private void
|
165
|
+
terminateTask(final Ruby runtime)
|
166
|
+
{
|
167
|
+
if (executor == null) { return; }
|
168
|
+
|
169
|
+
try {
|
170
|
+
terminateImpl();
|
171
|
+
} catch (InterruptedException e) {
|
172
|
+
throw runtime.newRuntimeError(e.toString());
|
173
|
+
} catch (Exception e) {
|
174
|
+
throw runtime.newRuntimeError(e.toString());
|
175
|
+
}
|
176
|
+
}
|
177
|
+
|
178
|
+
private synchronized void
|
179
|
+
terminateImpl() throws InterruptedException, ExecutionException
|
180
|
+
{
|
181
|
+
terminateExecution(executor, stream, futureTask);
|
182
|
+
|
183
|
+
executor = null;
|
184
|
+
stream = null;
|
185
|
+
futureTask = null;
|
186
|
+
}
|
187
|
+
|
188
|
+
private static Html4SaxParserContext
|
189
|
+
parse(final Ruby runtime, final InputStream stream)
|
190
|
+
{
|
191
|
+
RubyClass klazz = getNokogiriClass(runtime, "Nokogiri::HTML4::SAX::ParserContext");
|
192
|
+
return Html4SaxParserContext.parse_stream(runtime, klazz, stream);
|
193
|
+
}
|
194
|
+
|
195
|
+
static class ParserTask extends XmlSaxPushParser.ParserTask /* <Html4SaxPushParser> */
|
196
|
+
{
|
197
|
+
|
198
|
+
private
|
199
|
+
ParserTask(ThreadContext context, IRubyObject handler, InputStream stream)
|
200
|
+
{
|
201
|
+
super(context, handler, parse(context.runtime, stream), stream);
|
202
|
+
}
|
203
|
+
|
204
|
+
@Override
|
205
|
+
public Html4SaxParserContext
|
206
|
+
call() throws Exception
|
207
|
+
{
|
208
|
+
return (Html4SaxParserContext) super.call();
|
209
|
+
}
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
}
|