nokogiri 1.11.0.rc3-java → 1.11.4-java
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of nokogiri might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/Gemfile +3 -0
- data/LICENSE-DEPENDENCIES.md +1015 -947
- data/LICENSE.md +1 -1
- data/README.md +168 -91
- data/dependencies.yml +12 -12
- data/ext/java/nokogiri/EncodingHandler.java +76 -89
- data/ext/java/nokogiri/HtmlDocument.java +135 -144
- data/ext/java/nokogiri/HtmlElementDescription.java +102 -117
- data/ext/java/nokogiri/HtmlEntityLookup.java +33 -60
- data/ext/java/nokogiri/HtmlSaxParserContext.java +218 -222
- data/ext/java/nokogiri/HtmlSaxPushParser.java +162 -169
- data/ext/java/nokogiri/NokogiriService.java +595 -556
- data/ext/java/nokogiri/XmlAttr.java +118 -126
- data/ext/java/nokogiri/XmlAttributeDecl.java +95 -106
- data/ext/java/nokogiri/XmlCdata.java +35 -58
- data/ext/java/nokogiri/XmlComment.java +46 -67
- data/ext/java/nokogiri/XmlDocument.java +645 -572
- data/ext/java/nokogiri/XmlDocumentFragment.java +125 -137
- data/ext/java/nokogiri/XmlDtd.java +448 -414
- data/ext/java/nokogiri/XmlElement.java +23 -48
- data/ext/java/nokogiri/XmlElementContent.java +343 -316
- data/ext/java/nokogiri/XmlElementDecl.java +124 -125
- data/ext/java/nokogiri/XmlEntityDecl.java +119 -127
- data/ext/java/nokogiri/XmlEntityReference.java +49 -72
- data/ext/java/nokogiri/XmlNamespace.java +175 -175
- data/ext/java/nokogiri/XmlNode.java +1843 -1622
- data/ext/java/nokogiri/XmlNodeSet.java +361 -331
- data/ext/java/nokogiri/XmlProcessingInstruction.java +47 -69
- data/ext/java/nokogiri/XmlReader.java +513 -450
- data/ext/java/nokogiri/XmlRelaxng.java +89 -101
- data/ext/java/nokogiri/XmlSaxParserContext.java +328 -310
- data/ext/java/nokogiri/XmlSaxPushParser.java +227 -220
- data/ext/java/nokogiri/XmlSchema.java +335 -242
- data/ext/java/nokogiri/XmlSyntaxError.java +113 -119
- data/ext/java/nokogiri/XmlText.java +55 -76
- data/ext/java/nokogiri/XmlXpathContext.java +242 -210
- data/ext/java/nokogiri/XsltStylesheet.java +280 -269
- data/ext/java/nokogiri/internals/ClosedStreamException.java +5 -2
- data/ext/java/nokogiri/internals/HtmlDomParserContext.java +201 -190
- data/ext/java/nokogiri/internals/IgnoreSchemaErrorsErrorHandler.java +17 -10
- data/ext/java/nokogiri/internals/NokogiriBlockingQueueInputStream.java +43 -16
- data/ext/java/nokogiri/internals/NokogiriDomParser.java +63 -80
- data/ext/java/nokogiri/internals/NokogiriEntityResolver.java +107 -88
- data/ext/java/nokogiri/internals/NokogiriErrorHandler.java +27 -52
- data/ext/java/nokogiri/internals/NokogiriHandler.java +316 -286
- data/ext/java/nokogiri/internals/NokogiriHelpers.java +736 -652
- data/ext/java/nokogiri/internals/NokogiriNamespaceCache.java +184 -173
- data/ext/java/nokogiri/internals/NokogiriNamespaceContext.java +79 -89
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler.java +64 -79
- data/ext/java/nokogiri/internals/NokogiriNonStrictErrorHandler4NekoHtml.java +84 -99
- data/ext/java/nokogiri/internals/NokogiriStrictErrorHandler.java +48 -65
- data/ext/java/nokogiri/internals/NokogiriXPathFunction.java +119 -78
- data/ext/java/nokogiri/internals/NokogiriXPathFunctionResolver.java +34 -54
- data/ext/java/nokogiri/internals/NokogiriXPathVariableResolver.java +23 -46
- data/ext/java/nokogiri/internals/NokogiriXsltErrorListener.java +55 -72
- data/ext/java/nokogiri/internals/ParserContext.java +206 -211
- data/ext/java/nokogiri/internals/ReaderNode.java +478 -403
- data/ext/java/nokogiri/internals/SaveContextVisitor.java +822 -739
- data/ext/java/nokogiri/internals/SchemaErrorHandler.java +31 -54
- data/ext/java/nokogiri/internals/XalanDTMManagerPatch.java +129 -123
- data/ext/java/nokogiri/internals/XmlDeclHandler.java +3 -34
- data/ext/java/nokogiri/internals/XmlDomParserContext.java +206 -207
- data/ext/java/nokogiri/internals/XmlSaxParser.java +22 -47
- data/ext/java/nokogiri/internals/c14n/AttrCompare.java +71 -68
- data/ext/java/nokogiri/internals/c14n/C14nHelper.java +137 -118
- data/ext/java/nokogiri/internals/c14n/CanonicalFilter.java +27 -21
- data/ext/java/nokogiri/internals/c14n/CanonicalizationException.java +74 -61
- data/ext/java/nokogiri/internals/c14n/Canonicalizer.java +230 -205
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11.java +572 -547
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_OmitComments.java +17 -10
- data/ext/java/nokogiri/internals/c14n/Canonicalizer11_WithComments.java +17 -10
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315.java +323 -302
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315Excl.java +232 -219
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclOmitComments.java +22 -15
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315ExclWithComments.java +23 -16
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315OmitComments.java +23 -16
- data/ext/java/nokogiri/internals/c14n/Canonicalizer20010315WithComments.java +22 -15
- data/ext/java/nokogiri/internals/c14n/CanonicalizerBase.java +575 -545
- data/ext/java/nokogiri/internals/c14n/CanonicalizerPhysical.java +141 -120
- data/ext/java/nokogiri/internals/c14n/CanonicalizerSpi.java +39 -38
- data/ext/java/nokogiri/internals/c14n/Constants.java +13 -10
- data/ext/java/nokogiri/internals/c14n/ElementProxy.java +279 -247
- data/ext/java/nokogiri/internals/c14n/HelperNodeList.java +66 -53
- data/ext/java/nokogiri/internals/c14n/IgnoreAllErrorHandler.java +44 -37
- data/ext/java/nokogiri/internals/c14n/InclusiveNamespaces.java +135 -120
- data/ext/java/nokogiri/internals/c14n/InvalidCanonicalizerException.java +59 -48
- data/ext/java/nokogiri/internals/c14n/NameSpaceSymbTable.java +384 -334
- data/ext/java/nokogiri/internals/c14n/NodeFilter.java +25 -24
- data/ext/java/nokogiri/internals/c14n/UtfHelpper.java +151 -140
- data/ext/java/nokogiri/internals/c14n/XMLUtils.java +456 -423
- data/ext/java/nokogiri/internals/dom2dtm/DOM2DTM.java +1466 -1500
- data/ext/java/nokogiri/internals/dom2dtm/DOM2DTMdefaultNamespaceDeclarationNode.java +626 -574
- data/ext/nokogiri/depend +37 -358
- data/ext/nokogiri/extconf.rb +581 -374
- data/ext/nokogiri/html_document.c +78 -82
- data/ext/nokogiri/html_element_description.c +84 -71
- data/ext/nokogiri/html_entity_lookup.c +21 -16
- data/ext/nokogiri/html_sax_parser_context.c +69 -66
- data/ext/nokogiri/html_sax_push_parser.c +42 -34
- data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
- data/ext/nokogiri/nokogiri.c +192 -93
- data/ext/nokogiri/test_global_handlers.c +40 -0
- data/ext/nokogiri/xml_attr.c +15 -15
- data/ext/nokogiri/xml_attribute_decl.c +18 -18
- data/ext/nokogiri/xml_cdata.c +13 -18
- data/ext/nokogiri/xml_comment.c +19 -26
- data/ext/nokogiri/xml_document.c +246 -188
- data/ext/nokogiri/xml_document_fragment.c +13 -15
- data/ext/nokogiri/xml_dtd.c +54 -48
- data/ext/nokogiri/xml_element_content.c +30 -27
- data/ext/nokogiri/xml_element_decl.c +22 -22
- data/ext/nokogiri/xml_encoding_handler.c +17 -11
- data/ext/nokogiri/xml_entity_decl.c +32 -30
- data/ext/nokogiri/xml_entity_reference.c +16 -18
- data/ext/nokogiri/xml_namespace.c +56 -49
- data/ext/nokogiri/xml_node.c +371 -320
- data/ext/nokogiri/xml_node_set.c +168 -156
- data/ext/nokogiri/xml_processing_instruction.c +17 -19
- data/ext/nokogiri/xml_reader.c +191 -157
- data/ext/nokogiri/xml_relax_ng.c +52 -28
- data/ext/nokogiri/xml_sax_parser.c +118 -118
- data/ext/nokogiri/xml_sax_parser_context.c +103 -86
- data/ext/nokogiri/xml_sax_push_parser.c +36 -27
- data/ext/nokogiri/xml_schema.c +95 -47
- data/ext/nokogiri/xml_syntax_error.c +42 -21
- data/ext/nokogiri/xml_text.c +13 -17
- data/ext/nokogiri/xml_xpath_context.c +206 -123
- data/ext/nokogiri/xslt_stylesheet.c +158 -161
- data/lib/nokogiri.rb +3 -7
- data/lib/nokogiri/css/parser.rb +3 -3
- data/lib/nokogiri/css/parser.y +2 -2
- data/lib/nokogiri/css/xpath_visitor.rb +70 -42
- data/lib/nokogiri/extension.rb +26 -0
- data/lib/nokogiri/html/document.rb +12 -26
- data/lib/nokogiri/html/document_fragment.rb +15 -15
- data/lib/nokogiri/nokogiri.jar +0 -0
- data/lib/nokogiri/version.rb +2 -149
- data/lib/nokogiri/version/constant.rb +5 -0
- data/lib/nokogiri/version/info.rb +205 -0
- data/lib/nokogiri/xml/document.rb +91 -35
- data/lib/nokogiri/xml/document_fragment.rb +4 -6
- data/lib/nokogiri/xml/node.rb +89 -69
- data/lib/nokogiri/xml/parse_options.rb +6 -0
- data/lib/nokogiri/xml/reader.rb +2 -9
- data/lib/nokogiri/xml/relax_ng.rb +6 -2
- data/lib/nokogiri/xml/schema.rb +12 -4
- data/lib/nokogiri/xml/searchable.rb +3 -1
- data/lib/nokogiri/xml/xpath.rb +1 -3
- data/lib/nokogiri/xml/xpath/syntax_error.rb +1 -1
- metadata +86 -177
- data/ext/nokogiri/html_document.h +0 -10
- data/ext/nokogiri/html_element_description.h +0 -10
- data/ext/nokogiri/html_entity_lookup.h +0 -8
- data/ext/nokogiri/html_sax_parser_context.h +0 -11
- data/ext/nokogiri/html_sax_push_parser.h +0 -9
- data/ext/nokogiri/nokogiri.h +0 -134
- data/ext/nokogiri/xml_attr.h +0 -9
- data/ext/nokogiri/xml_attribute_decl.h +0 -9
- data/ext/nokogiri/xml_cdata.h +0 -9
- data/ext/nokogiri/xml_comment.h +0 -9
- data/ext/nokogiri/xml_document.h +0 -23
- data/ext/nokogiri/xml_document_fragment.h +0 -10
- data/ext/nokogiri/xml_dtd.h +0 -10
- data/ext/nokogiri/xml_element_content.h +0 -10
- data/ext/nokogiri/xml_element_decl.h +0 -9
- data/ext/nokogiri/xml_encoding_handler.h +0 -8
- data/ext/nokogiri/xml_entity_decl.h +0 -10
- data/ext/nokogiri/xml_entity_reference.h +0 -9
- data/ext/nokogiri/xml_io.c +0 -63
- data/ext/nokogiri/xml_io.h +0 -11
- data/ext/nokogiri/xml_libxml2_hacks.c +0 -112
- data/ext/nokogiri/xml_libxml2_hacks.h +0 -12
- data/ext/nokogiri/xml_namespace.h +0 -14
- data/ext/nokogiri/xml_node.h +0 -13
- data/ext/nokogiri/xml_node_set.h +0 -12
- data/ext/nokogiri/xml_processing_instruction.h +0 -9
- data/ext/nokogiri/xml_reader.h +0 -10
- data/ext/nokogiri/xml_relax_ng.h +0 -9
- data/ext/nokogiri/xml_sax_parser.h +0 -39
- data/ext/nokogiri/xml_sax_parser_context.h +0 -10
- data/ext/nokogiri/xml_sax_push_parser.h +0 -9
- data/ext/nokogiri/xml_schema.h +0 -9
- data/ext/nokogiri/xml_syntax_error.h +0 -13
- data/ext/nokogiri/xml_text.h +0 -9
- data/ext/nokogiri/xml_xpath_context.h +0 -10
- data/ext/nokogiri/xslt_stylesheet.h +0 -14
@@ -1,35 +1,3 @@
|
|
1
|
-
/**
|
2
|
-
* (The MIT License)
|
3
|
-
*
|
4
|
-
* Copyright (c) 2008 - 2012:
|
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
1
|
package nokogiri;
|
34
2
|
|
35
3
|
import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
|
@@ -66,221 +34,260 @@ import nokogiri.internals.ParserContext;
|
|
66
34
|
* @author Patrick Mahoney <pat@polycrystal.org>
|
67
35
|
* @author Yoko Harada <yokolet@gmail.com>
|
68
36
|
*/
|
69
|
-
@JRubyClass(name="Nokogiri::XML::SAX::PushParser")
|
70
|
-
public class XmlSaxPushParser extends RubyObject
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
37
|
+
@JRubyClass(name = "Nokogiri::XML::SAX::PushParser")
|
38
|
+
public class XmlSaxPushParser extends RubyObject
|
39
|
+
{
|
40
|
+
ParserContext.Options options;
|
41
|
+
IRubyObject saxParser;
|
42
|
+
|
43
|
+
NokogiriBlockingQueueInputStream stream;
|
44
|
+
|
45
|
+
private ParserTask parserTask = null;
|
46
|
+
private FutureTask<XmlSaxParserContext> futureTask = null;
|
47
|
+
private ExecutorService executor = null;
|
48
|
+
RaiseException ex = null;
|
49
|
+
|
50
|
+
public
|
51
|
+
XmlSaxPushParser(Ruby ruby, RubyClass rubyClass)
|
52
|
+
{
|
53
|
+
super(ruby, rubyClass);
|
54
|
+
}
|
55
|
+
|
56
|
+
@Override
|
57
|
+
public void
|
58
|
+
finalize()
|
59
|
+
{
|
60
|
+
try {
|
61
|
+
terminateImpl();
|
62
|
+
} catch (Exception e) { /* ignored */ }
|
63
|
+
}
|
64
|
+
|
65
|
+
@JRubyMethod
|
66
|
+
public IRubyObject
|
67
|
+
initialize_native(final ThreadContext context, IRubyObject saxParser, IRubyObject fileName)
|
68
|
+
{
|
69
|
+
options = new ParserContext.Options(0);
|
70
|
+
this.saxParser = saxParser;
|
71
|
+
return this;
|
72
|
+
}
|
73
|
+
|
74
|
+
private transient IRubyObject parse_options;
|
75
|
+
|
76
|
+
private IRubyObject
|
77
|
+
parse_options(final ThreadContext context)
|
78
|
+
{
|
79
|
+
if (parse_options == null) {
|
80
|
+
parse_options = invoke(context, context.runtime.getClassFromPath("Nokogiri::XML::ParseOptions"), "new");
|
83
81
|
}
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
82
|
+
return parse_options;
|
83
|
+
}
|
84
|
+
|
85
|
+
@JRubyMethod(name = "options")
|
86
|
+
public IRubyObject
|
87
|
+
getOptions(ThreadContext context)
|
88
|
+
{
|
89
|
+
return invoke(context, parse_options(context), "options");
|
90
|
+
}
|
91
|
+
|
92
|
+
@JRubyMethod(name = "options=")
|
93
|
+
public IRubyObject
|
94
|
+
setOptions(ThreadContext context, IRubyObject opts)
|
95
|
+
{
|
96
|
+
invoke(context, parse_options(context), "options=", opts);
|
97
|
+
options = new ParserContext.Options(opts.convertToInteger().getLongValue());
|
98
|
+
return getOptions(context);
|
99
|
+
}
|
100
|
+
|
101
|
+
/**
|
102
|
+
* Can take a boolean assignment.
|
103
|
+
*
|
104
|
+
* @param context
|
105
|
+
* @param value
|
106
|
+
* @return
|
107
|
+
*/
|
108
|
+
@JRubyMethod(name = "replace_entities=")
|
109
|
+
public IRubyObject
|
110
|
+
setReplaceEntities(ThreadContext context, IRubyObject value)
|
111
|
+
{
|
112
|
+
// Ignore the value.
|
113
|
+
return this;
|
114
|
+
}
|
115
|
+
|
116
|
+
@JRubyMethod(name = "replace_entities")
|
117
|
+
public IRubyObject
|
118
|
+
getReplaceEntities(ThreadContext context)
|
119
|
+
{
|
120
|
+
// The java parser always replaces entities.
|
121
|
+
return context.getRuntime().getTrue();
|
122
|
+
}
|
123
|
+
|
124
|
+
@JRubyMethod
|
125
|
+
public IRubyObject
|
126
|
+
native_write(ThreadContext context, IRubyObject chunk,
|
127
|
+
IRubyObject isLast)
|
128
|
+
{
|
129
|
+
if (ex != null) {
|
130
|
+
// parser has already errored, rethrow the exception
|
131
|
+
throw ex;
|
91
132
|
}
|
92
133
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
return this;
|
134
|
+
try {
|
135
|
+
initialize_task(context);
|
136
|
+
} catch (IOException e) {
|
137
|
+
throw context.runtime.newRuntimeError(e.getMessage());
|
98
138
|
}
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
private IRubyObject parse_options(final ThreadContext context) {
|
103
|
-
if (parse_options == null) {
|
104
|
-
parse_options = invoke(context, context.runtime.getClassFromPath("Nokogiri::XML::ParseOptions"), "new");
|
105
|
-
}
|
106
|
-
return parse_options;
|
139
|
+
final ByteArrayInputStream data = NokogiriHelpers.stringBytesToStream(chunk);
|
140
|
+
if (data == null) {
|
141
|
+
return this;
|
107
142
|
}
|
108
143
|
|
109
|
-
|
110
|
-
public IRubyObject getOptions(ThreadContext context) {
|
111
|
-
return invoke(context, parse_options(context), "options");
|
112
|
-
}
|
144
|
+
int errorCount0 = parserTask.getErrorCount();
|
113
145
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
146
|
+
try {
|
147
|
+
Future<Void> task = stream.addChunk(data);
|
148
|
+
task.get();
|
149
|
+
} catch (ClosedStreamException ex) {
|
150
|
+
// this means the stream is closed, ignore this exception
|
151
|
+
} catch (Exception e) {
|
152
|
+
throw context.runtime.newRuntimeError(e.toString());
|
119
153
|
}
|
120
154
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
* @param context
|
125
|
-
* @param value
|
126
|
-
* @return
|
127
|
-
*/
|
128
|
-
@JRubyMethod(name = "replace_entities=")
|
129
|
-
public IRubyObject setReplaceEntities(ThreadContext context, IRubyObject value) {
|
130
|
-
// Ignore the value.
|
131
|
-
return this;
|
155
|
+
if (isLast.isTrue()) {
|
156
|
+
parserTask.getNokogiriHandler().endDocument();
|
157
|
+
terminateTask(context.runtime);
|
132
158
|
}
|
133
159
|
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
return context.getRuntime().getTrue();
|
160
|
+
if (!options.recover && parserTask.getErrorCount() > errorCount0) {
|
161
|
+
terminateTask(context.runtime);
|
162
|
+
throw ex = parserTask.getLastError();
|
138
163
|
}
|
139
164
|
|
140
|
-
|
141
|
-
|
142
|
-
IRubyObject isLast) {
|
143
|
-
if (ex != null) {
|
144
|
-
// parser has already errored, rethrow the exception
|
145
|
-
throw ex;
|
146
|
-
}
|
147
|
-
|
148
|
-
try {
|
149
|
-
initialize_task(context);
|
150
|
-
} catch (IOException e) {
|
151
|
-
throw context.runtime.newRuntimeError(e.getMessage());
|
152
|
-
}
|
153
|
-
final ByteArrayInputStream data = NokogiriHelpers.stringBytesToStream(chunk);
|
154
|
-
if (data == null) {
|
155
|
-
return this;
|
156
|
-
}
|
157
|
-
|
158
|
-
int errorCount0 = parserTask.getErrorCount();
|
165
|
+
return this;
|
166
|
+
}
|
159
167
|
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
} catch (Exception e) {
|
166
|
-
throw context.runtime.newRuntimeError(e.toString());
|
167
|
-
}
|
168
|
-
|
169
|
-
if (isLast.isTrue()) {
|
170
|
-
parserTask.getNokogiriHandler().endDocument();
|
171
|
-
terminateTask(context.runtime);
|
172
|
-
}
|
168
|
+
private void
|
169
|
+
initialize_task(ThreadContext context) throws IOException
|
170
|
+
{
|
171
|
+
if (futureTask == null || stream == null) {
|
172
|
+
stream = new NokogiriBlockingQueueInputStream();
|
173
173
|
|
174
|
-
|
175
|
-
|
176
|
-
|
174
|
+
assert saxParser != null : "saxParser null";
|
175
|
+
parserTask = new ParserTask(context, saxParser, stream);
|
176
|
+
futureTask = new FutureTask<XmlSaxParserContext>(parserTask);
|
177
|
+
executor = Executors.newSingleThreadExecutor(new ThreadFactory() {
|
178
|
+
@Override
|
179
|
+
public Thread newThread(Runnable r) {
|
180
|
+
Thread t = new Thread(r);
|
181
|
+
t.setName("XmlSaxPushParser");
|
182
|
+
t.setDaemon(true);
|
183
|
+
return t;
|
177
184
|
}
|
178
|
-
|
179
|
-
|
185
|
+
});
|
186
|
+
executor.submit(futureTask);
|
180
187
|
}
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
t.setDaemon(true);
|
195
|
-
return t;
|
196
|
-
}
|
197
|
-
});
|
198
|
-
executor.submit(futureTask);
|
199
|
-
}
|
188
|
+
}
|
189
|
+
|
190
|
+
private void
|
191
|
+
terminateTask(final Ruby runtime)
|
192
|
+
{
|
193
|
+
if (executor == null) { return; }
|
194
|
+
|
195
|
+
try {
|
196
|
+
terminateImpl();
|
197
|
+
} catch (InterruptedException e) {
|
198
|
+
throw runtime.newRuntimeError(e.toString());
|
199
|
+
} catch (Exception e) {
|
200
|
+
throw runtime.newRuntimeError(e.toString());
|
200
201
|
}
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
202
|
+
}
|
203
|
+
|
204
|
+
private synchronized void
|
205
|
+
terminateImpl() throws InterruptedException, ExecutionException
|
206
|
+
{
|
207
|
+
terminateExecution(executor, stream, futureTask);
|
208
|
+
|
209
|
+
executor = null;
|
210
|
+
stream = null;
|
211
|
+
futureTask = null;
|
212
|
+
}
|
213
|
+
|
214
|
+
// SHARED for HtmlSaxPushParser
|
215
|
+
static void
|
216
|
+
terminateExecution(final ExecutorService executor, final NokogiriBlockingQueueInputStream stream,
|
217
|
+
final FutureTask<?> futureTask)
|
218
|
+
throws InterruptedException, ExecutionException
|
219
|
+
{
|
220
|
+
|
221
|
+
if (executor == null) { return; }
|
222
|
+
|
223
|
+
try {
|
224
|
+
Future<Void> task = stream.addChunk(NokogiriBlockingQueueInputStream.END);
|
225
|
+
task.get();
|
226
|
+
} catch (ClosedStreamException ex) {
|
227
|
+
// ignore this exception, it means the stream was closed
|
214
228
|
}
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
229
|
+
futureTask.cancel(true);
|
230
|
+
executor.shutdown();
|
231
|
+
}
|
232
|
+
|
233
|
+
private static XmlSaxParserContext
|
234
|
+
parse(final Ruby runtime, final InputStream stream)
|
235
|
+
{
|
236
|
+
RubyClass klazz = getNokogiriClass(runtime, "Nokogiri::XML::SAX::ParserContext");
|
237
|
+
return XmlSaxParserContext.parse_stream(runtime, klazz, stream);
|
238
|
+
}
|
239
|
+
|
240
|
+
static class ParserTask extends ParserContext.ParserTask<XmlSaxParserContext>
|
241
|
+
{
|
242
|
+
|
243
|
+
final InputStream stream;
|
244
|
+
|
245
|
+
private
|
246
|
+
ParserTask(ThreadContext context, IRubyObject handler, InputStream stream)
|
247
|
+
{
|
248
|
+
this(context, handler, parse(context.runtime, stream), stream);
|
220
249
|
}
|
221
250
|
|
222
|
-
//
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
try {
|
230
|
-
Future<Void> task = stream.addChunk(NokogiriBlockingQueueInputStream.END);
|
231
|
-
task.get();
|
232
|
-
}
|
233
|
-
catch (ClosedStreamException ex) {
|
234
|
-
// ignore this exception, it means the stream was closed
|
235
|
-
}
|
236
|
-
futureTask.cancel(true);
|
237
|
-
executor.shutdown();
|
251
|
+
// IMPL with HtmlSaxPushParser
|
252
|
+
protected
|
253
|
+
ParserTask(ThreadContext context, IRubyObject handler, XmlSaxParserContext parser, InputStream stream)
|
254
|
+
{
|
255
|
+
super(context, handler, parser);
|
256
|
+
this.stream = stream;
|
238
257
|
}
|
239
258
|
|
240
|
-
|
241
|
-
|
242
|
-
|
259
|
+
@Override
|
260
|
+
public XmlSaxParserContext
|
261
|
+
call() throws Exception
|
262
|
+
{
|
263
|
+
try {
|
264
|
+
parser.parse_with(context, handler);
|
265
|
+
} finally { stream.close(); }
|
266
|
+
// we have to close the stream before exiting, otherwise someone
|
267
|
+
// can add a chunk and block on task.get() forever.
|
268
|
+
return parser;
|
243
269
|
}
|
244
270
|
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
this(context, handler, parse(context.runtime, stream), stream);
|
251
|
-
}
|
252
|
-
|
253
|
-
// IMPL with HtmlSaxPushParser
|
254
|
-
protected ParserTask(ThreadContext context, IRubyObject handler, XmlSaxParserContext parser, InputStream stream) {
|
255
|
-
super(context, handler, parser);
|
256
|
-
this.stream = stream;
|
257
|
-
}
|
258
|
-
|
259
|
-
@Override
|
260
|
-
public XmlSaxParserContext call() throws Exception {
|
261
|
-
try {
|
262
|
-
parser.parse_with(context, handler);
|
263
|
-
}
|
264
|
-
finally { stream.close(); }
|
265
|
-
// we have to close the stream before exiting, otherwise someone
|
266
|
-
// can add a chunk and block on task.get() forever.
|
267
|
-
return parser;
|
268
|
-
}
|
269
|
-
|
270
|
-
final NokogiriHandler getNokogiriHandler() {
|
271
|
-
return parser.getNokogiriHandler();
|
272
|
-
}
|
273
|
-
|
274
|
-
synchronized final int getErrorCount() {
|
275
|
-
// check for null because thread may not have started yet
|
276
|
-
if (parser.getNokogiriHandler() == null) return 0;
|
277
|
-
return parser.getNokogiriHandler().getErrorCount();
|
278
|
-
}
|
271
|
+
final NokogiriHandler
|
272
|
+
getNokogiriHandler()
|
273
|
+
{
|
274
|
+
return parser.getNokogiriHandler();
|
275
|
+
}
|
279
276
|
|
280
|
-
|
281
|
-
|
282
|
-
|
277
|
+
synchronized final int
|
278
|
+
getErrorCount()
|
279
|
+
{
|
280
|
+
// check for null because thread may not have started yet
|
281
|
+
if (parser.getNokogiriHandler() == null) { return 0; }
|
282
|
+
return parser.getNokogiriHandler().getErrorCount();
|
283
|
+
}
|
283
284
|
|
285
|
+
synchronized final RaiseException
|
286
|
+
getLastError()
|
287
|
+
{
|
288
|
+
return parser.getNokogiriHandler().getLastError();
|
284
289
|
}
|
285
290
|
|
291
|
+
}
|
292
|
+
|
286
293
|
}
|