Search in sources :

Example 6 with RaiseException

use of org.jruby.exceptions.RaiseException 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 7 with RaiseException

use of org.jruby.exceptions.RaiseException 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)

Example 8 with RaiseException

use of org.jruby.exceptions.RaiseException in project gocd by gocd.

the class XmlSchema method from_document.

/*
     * call-seq:
     *  from_document(doc)
     *
     * Create a new Schema from the Nokogiri::XML::Document +doc+
     */
@JRubyMethod(meta = true)
public static IRubyObject from_document(ThreadContext context, IRubyObject klazz, IRubyObject document) {
    XmlDocument doc = ((XmlDocument) ((XmlNode) document).document(context));
    RubyArray errors = (RubyArray) doc.getInstanceVariable("@errors");
    if (!errors.isEmpty()) {
        throw new RaiseException((XmlSyntaxError) errors.first());
    }
    DOMSource source = new DOMSource(doc.getDocument());
    IRubyObject uri = doc.url(context);
    if (!uri.isNil()) {
        source.setSystemId(uri.convertToString().asJavaString());
    }
    return getSchema(context, (RubyClass) klazz, source);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) RubyArray(org.jruby.RubyArray) RaiseException(org.jruby.exceptions.RaiseException) IRubyObject(org.jruby.runtime.builtin.IRubyObject) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 9 with RaiseException

use of org.jruby.exceptions.RaiseException in project gocd by gocd.

the class XmlNode method process_xincludes.

/**
     * call-seq:
     *   process_xincludes(options)
     *
     * Loads and substitutes all xinclude elements below the node. The
     * parser context will be initialized with +options+.
     * 
     */
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject process_xincludes(ThreadContext context, IRubyObject options) {
    XmlDocument xmlDocument = (XmlDocument) document(context);
    RubyArray errors = (RubyArray) xmlDocument.getInstanceVariable("@errors");
    while (errors.getLength() > 0) {
        XmlSyntaxError error = (XmlSyntaxError) errors.shift(context);
        if (error.toString().contains("Include operation failed")) {
            throw new RaiseException(error);
        }
    }
    return this;
}
Also used : RubyArray(org.jruby.RubyArray) NokogiriHelpers.nodeArrayToRubyArray(nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray) RaiseException(org.jruby.exceptions.RaiseException) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 10 with RaiseException

use of org.jruby.exceptions.RaiseException in project enumerable by hraberg.

the class RubySpecTestBase method mspec.

void mspec(List<String> files) throws Exception {
    StringWriter stdout = new StringWriter();
    StringWriter stderr = new StringWriter();
    Writer originalOut = rb.getContext().getWriter();
    Writer originalErr = rb.getContext().getErrorWriter();
    if (!specdoc)
        rb.getContext().setWriter(stdout);
    if (!debug)
        rb.getContext().setErrorWriter(stderr);
    try {
        // We need to trick MSpec into thinking we're running a real ruby
        eval("RUBY_EXE = '/usr/bin/jruby'");
        // While telling it we're not, to skip specs for our "platform"
        eval("RUBY_PLATFORM = 'enumerable_java'");
        // We support Enumerable from 1.8.8
        eval("RUBY_VERSION = '1.8.8'");
        require("mspec");
        require("mspec/utils/script");
        // Identity won't work as JRuby will turn Ruby objects into Java
        // and then back again.
        eval("class EqualMatcher; def matches?(actual); @actual = actual; @actual == @expected; end; end");
        eval("formatter = SpecdocFormatter.new; formatter.register;");
        eval("MSpec.store :formatter, formatter");
        eval("MSpec.register_files " + files);
        eval("MSpec.process");
        try {
            eval("raise formatter.exceptions[0] unless MSpec.exit_code == 0");
        } catch (RaiseException e) {
            try {
                fail(e.getException().message.asJavaString());
            } catch (AssertionError error) {
                error.setStackTrace(e.getStackTrace());
                throw error;
            }
        }
    } catch (ScriptException e) {
        out.println(stdout.toString());
        err.println(stderr.toString());
        throw uncheck(e);
    } finally {
        rb.getContext().setWriter(originalOut);
        rb.getContext().setErrorWriter(originalErr);
        eval("MSpec.unregister :exception, formatter; MSpec.unregister :before, formatter; " + "MSpec.unregister :after, formatter; MSpec.unregister :finish, formatter; " + "MSpec.unregister :enter, formatter; MSpec.register_exit(nil); " + "MSpec.clear_current; MSpec.clear_modes; MSpec.clear_expectations");
    }
}
Also used : ScriptException(javax.script.ScriptException) StringWriter(java.io.StringWriter) RaiseException(org.jruby.exceptions.RaiseException) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Aggregations

RaiseException (org.jruby.exceptions.RaiseException)16 JRubyMethod (org.jruby.anno.JRubyMethod)8 RubyArray (org.jruby.RubyArray)7 IRubyObject (org.jruby.runtime.builtin.IRubyObject)7 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 ClosedStreamException (nokogiri.internals.ClosedStreamException)4 RubyException (org.jruby.RubyException)4 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)2 DOMSource (javax.xml.transform.dom.DOMSource)2 XmlDocument (nokogiri.XmlDocument)2 XmlSyntaxError (nokogiri.XmlSyntaxError)2 NokogiriHelpers.nodeArrayToRubyArray (nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray)2 ClojureTest (org.enumerable.lambda.support.clojure.ClojureTest)2 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)2 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)2 LambdaJRuby (org.enumerable.lambda.support.jruby.LambdaJRuby)2 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)2 Ruby (org.jruby.Ruby)2 RubyProc (org.jruby.RubyProc)2