File: HtmlSaxPushParser.java

package info (click to toggle)
ruby-nokogiri 1.11.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,576 kB
  • sloc: xml: 28,086; ruby: 18,456; java: 13,067; ansic: 5,138; yacc: 265; sh: 208; makefile: 27
file content (222 lines) | stat: -rw-r--r-- 7,865 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
 * (The MIT License)
 *
 * Copyright (c) 2008 - 2012:
 *
 * * {Aaron Patterson}[http://tenderlovemaking.com]
 * * {Mike Dalessio}[http://mike.daless.io]
 * * {Charles Nutter}[http://blog.headius.com]
 * * {Sergio Arbeo}[http://www.serabe.com]
 * * {Patrick Mahoney}[http://polycrystal.org]
 * * {Yoko Harada}[http://yokolet.blogspot.com]
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * 'Software'), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package nokogiri;

import static nokogiri.XmlSaxPushParser.terminateExecution;
import static nokogiri.internals.NokogiriHelpers.getNokogiriClass;
import static org.jruby.runtime.Helpers.invoke;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ThreadFactory;

import nokogiri.internals.*;

import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyObject;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;

/**
 * Class for Nokogiri::HTML::SAX::PushParser
 *
 * @author 
 * @author Piotr Szmielew <p.szmielew@ava.waw.pl> - based on Nokogiri::XML::SAX::PushParser
 */
@JRubyClass(name="Nokogiri::HTML::SAX::PushParser")
public class HtmlSaxPushParser extends RubyObject {
    ParserContext.Options options;
    IRubyObject saxParser;

    NokogiriBlockingQueueInputStream stream;

    private ParserTask parserTask = null;
    private FutureTask<HtmlSaxParserContext> futureTask = null;
    private ExecutorService executor = null;

    public HtmlSaxPushParser(Ruby ruby, RubyClass rubyClass) {
        super(ruby, rubyClass);
    }

    @Override
    public void finalize() {
        try {
            terminateImpl();
        }
        catch (Exception e) { /* ignored */ }
    }

    @JRubyMethod
    public IRubyObject initialize_native(final ThreadContext context,
                                         IRubyObject saxParser,
                                         IRubyObject fileName,
                                         IRubyObject encoding) {
        // NOTE: Silently skips provided encoding
        options = new ParserContext.Options(0);
        this.saxParser = saxParser;
        return this;
    }

    private transient IRubyObject parse_options;

    private IRubyObject parse_options(final ThreadContext context) {
        if (parse_options == null) {
            parse_options = invoke(context, context.runtime.getClassFromPath("Nokogiri::XML::ParseOptions"), "new");
        }
        return parse_options;
    }

    @JRubyMethod(name="options")
    public IRubyObject getOptions(ThreadContext context) {
        return invoke(context, parse_options(context), "options");
    }

    @JRubyMethod(name="options=")
    public IRubyObject setOptions(ThreadContext context, IRubyObject opts) {
        invoke(context, parse_options(context), "options=", opts);
        options = new ParserContext.Options(opts.convertToInteger().getLongValue());
        return getOptions(context);
    }

    @JRubyMethod
    public IRubyObject native_write(ThreadContext context, IRubyObject chunk, IRubyObject isLast) {
        try {
            initialize_task(context);
        } catch (IOException e) {
            throw context.getRuntime().newRuntimeError(e.getMessage());
        }
        final ByteArrayInputStream data = NokogiriHelpers.stringBytesToStream(chunk);
        if (data == null) {
            terminateTask(context.runtime);
            throw XmlSyntaxError.createHTMLSyntaxError(context.runtime).toThrowable(); // Nokogiri::HTML::SyntaxError
        }

        int errorCount0 = parserTask.getErrorCount();

        if (isLast.isTrue()) {
            IRubyObject document = invoke(context, this, "document");
            invoke(context, document, "end_document");
            terminateTask(context.runtime);
        } else {
            try {
                Future<Void> task = stream.addChunk(data);
                task.get();
            }
            catch (ClosedStreamException ex) {
                // this means the stream is closed, ignore this exception
            }
            catch (Exception e) {
                throw context.runtime.newRuntimeError(e.getMessage());
            }

        }

        if (!options.recover && parserTask.getErrorCount() > errorCount0) {
            terminateTask(context.runtime);
            throw parserTask.getLastError();
        }

        return this;
    }

    @SuppressWarnings("unchecked")
    private void initialize_task(ThreadContext context) throws IOException {
        if (futureTask == null || stream == null) {
            stream = new NokogiriBlockingQueueInputStream();

            assert saxParser != null : "saxParser null";
            parserTask = new ParserTask(context, saxParser, stream);
            futureTask = new FutureTask<HtmlSaxParserContext>((Callable) parserTask);
            executor = Executors.newSingleThreadExecutor(new ThreadFactory() {
              @Override
              public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName("HtmlSaxPushParser");
                t.setDaemon(true);
                return t;
              }
            });
            executor.submit(futureTask);
        }
    }

    private void terminateTask(final Ruby runtime) {
        if (executor == null) return;

        try {
            terminateImpl();
        }
        catch (InterruptedException e) {
            throw runtime.newRuntimeError(e.toString());
        }
        catch (Exception e) {
            throw runtime.newRuntimeError(e.toString());
        }
    }

    private synchronized void terminateImpl() throws InterruptedException, ExecutionException {
        terminateExecution(executor, stream, futureTask);

        executor = null; stream = null; futureTask = null;
    }

    private static HtmlSaxParserContext parse(final Ruby runtime, final InputStream stream) {
        RubyClass klazz = getNokogiriClass(runtime, "Nokogiri::HTML::SAX::ParserContext");
        return HtmlSaxParserContext.parse_stream(runtime, klazz, stream);
    }

    static class ParserTask extends XmlSaxPushParser.ParserTask /* <HtmlSaxPushParser> */ {

        private ParserTask(ThreadContext context, IRubyObject handler, InputStream stream) {
            super(context, handler, parse(context.runtime, stream), stream);
        }

        @Override
        public HtmlSaxParserContext call() throws Exception {
            return (HtmlSaxParserContext) super.call();
        }

    }

}