Search in sources :

Example 1 with ClosedStreamException

use of nokogiri.internals.ClosedStreamException in project nokogiri by sparklemotion.

the class HtmlSaxPushParser method native_write.

@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);
        // Nokogiri::HTML::SyntaxError
        throw new RaiseException(XmlSyntaxError.createHTMLSyntaxError(context.runtime));
    }
    int errorCount0 = parserTask.getErrorCount();
    if (isLast.isTrue()) {
        IRubyObject document = invoke(context, this, "document");
        invoke(context, document, "end_document");
        terminateTask(context);
    } 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.getRuntime().newRuntimeError(e.getMessage());
        }
    }
    if (!options.recover && parserTask.getErrorCount() > errorCount0) {
        terminateTask(context);
        throw new RaiseException(parserTask.getLastError(), true);
    }
    return this;
}
Also used : ClosedStreamException(nokogiri.internals.ClosedStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) RaiseException(org.jruby.exceptions.RaiseException) IOException(java.io.IOException) IRubyObject(org.jruby.runtime.builtin.IRubyObject) RaiseException(org.jruby.exceptions.RaiseException) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) RubyException(org.jruby.RubyException) IOException(java.io.IOException) ClosedStreamException(nokogiri.internals.ClosedStreamException) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 2 with ClosedStreamException

use of nokogiri.internals.ClosedStreamException in project gocd by gocd.

the class HtmlSaxPushParser method native_write.

@JRubyMethod
public IRubyObject native_write(ThreadContext context, IRubyObject chunk, IRubyObject isLast) {
    try {
        initialize_task(context);
    } catch (IOException e) {
        throw context.getRuntime().newRuntimeError(e.getMessage());
    }
    byte[] data = null;
    if (chunk instanceof RubyString || chunk.respondsTo("to_str")) {
        data = chunk.convertToString().getBytes();
    } else {
        terminateTask(context);
        XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::HTML::SyntaxError"));
        throw new RaiseException(xmlSyntaxError);
    }
    int errorCount0 = parserTask.getErrorCount();
    ;
    if (isLast.isTrue()) {
        IRubyObject document = invoke(context, this, "document");
        invoke(context, document, "end_document");
        terminateTask(context);
    } else {
        try {
            Future<Void> task = stream.addChunk(new ByteArrayInputStream(data));
            task.get();
        } catch (ClosedStreamException ex) {
        // this means the stream is closed, ignore this exception
        } catch (Exception e) {
            throw context.getRuntime().newRuntimeError(e.getMessage());
        }
    }
    if (!options.recover && parserTask.getErrorCount() > errorCount0) {
        terminateTask(context);
        throw new RaiseException(parserTask.getLastError(), true);
    }
    return this;
}
Also used : ClosedStreamException(nokogiri.internals.ClosedStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) RubyString(org.jruby.RubyString) RaiseException(org.jruby.exceptions.RaiseException) IOException(java.io.IOException) IRubyObject(org.jruby.runtime.builtin.IRubyObject) RaiseException(org.jruby.exceptions.RaiseException) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) RubyException(org.jruby.RubyException) IOException(java.io.IOException) ClosedStreamException(nokogiri.internals.ClosedStreamException) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 3 with ClosedStreamException

use of nokogiri.internals.ClosedStreamException in project nokogiri by sparklemotion.

the class Html4SaxPushParser method native_write.

@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);
        // Nokogiri::HTML4::SyntaxError
        throw XmlSyntaxError.createHTMLSyntaxError(context.runtime).toThrowable();
    }
    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().toThrowable();
    }
    return this;
}
Also used : ClosedStreamException(nokogiri.internals.ClosedStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) IRubyObject(org.jruby.runtime.builtin.IRubyObject) IOException(java.io.IOException) ClosedStreamException(nokogiri.internals.ClosedStreamException) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 4 with ClosedStreamException

use of nokogiri.internals.ClosedStreamException in project gocd by gocd.

the class XmlSaxPushParser method native_write.

@JRubyMethod
public IRubyObject native_write(ThreadContext context, IRubyObject chunk, IRubyObject isLast) {
    try {
        initialize_task(context);
    } catch (IOException e) {
        throw context.getRuntime().newRuntimeError(e.getMessage());
    }
    byte[] data = null;
    if (chunk instanceof RubyString || chunk.respondsTo("to_str")) {
        data = chunk.convertToString().getBytes();
    } else {
        terminateTask(context);
        XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::SyntaxError"));
        throw new RaiseException(xmlSyntaxError);
    }
    int errorCount0 = parserTask.getErrorCount();
    ;
    if (isLast.isTrue()) {
        IRubyObject document = invoke(context, this, "document");
        invoke(context, document, "end_document");
        terminateTask(context);
    } else {
        try {
            Future<Void> task = stream.addChunk(new ByteArrayInputStream(data));
            task.get();
        } catch (ClosedStreamException ex) {
        // this means the stream is closed, ignore this exception
        } catch (Exception e) {
            throw context.getRuntime().newRuntimeError(e.getMessage());
        }
    }
    if (!options.recover && parserTask.getErrorCount() > errorCount0) {
        terminateTask(context);
        throw new RaiseException(parserTask.getLastError(), true);
    }
    return this;
}
Also used : ClosedStreamException(nokogiri.internals.ClosedStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) RubyString(org.jruby.RubyString) RaiseException(org.jruby.exceptions.RaiseException) IOException(java.io.IOException) IRubyObject(org.jruby.runtime.builtin.IRubyObject) RaiseException(org.jruby.exceptions.RaiseException) RubyException(org.jruby.RubyException) IOException(java.io.IOException) ClosedStreamException(nokogiri.internals.ClosedStreamException) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 ClosedStreamException (nokogiri.internals.ClosedStreamException)4 JRubyMethod (org.jruby.anno.JRubyMethod)4 IRubyObject (org.jruby.runtime.builtin.IRubyObject)4 RubyException (org.jruby.RubyException)3 RaiseException (org.jruby.exceptions.RaiseException)3 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)2 RubyString (org.jruby.RubyString)2