Search in sources :

Example 6 with RubyString

use of org.jruby.RubyString 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 RubyString

use of org.jruby.RubyString in project cucumber-jvm by cucumber.

the class JRubyStepDefinition method matchedArguments.

@Override
public List<Argument> matchedArguments(Step step) {
    RubyString stepName = stepdefRunner.getRuntime().newString(step.getName());
    IRubyObject arguments = stepdefRunner.callMethod("matched_arguments", stepName);
    return toJava(arguments);
}
Also used : RubyString(org.jruby.RubyString) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 8 with RubyString

use of org.jruby.RubyString 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 9 with RubyString

use of org.jruby.RubyString in project gocd by gocd.

the class XmlNode method native_write_to.

/**
     * @param args {IRubyObject io,
     *              IRubyObject encoding,
     *              IRubyObject indentString,
     *              IRubyObject options}
     */
@JRubyMethod(required = 4, visibility = Visibility.PRIVATE)
public IRubyObject native_write_to(ThreadContext context, IRubyObject[] args) {
    IRubyObject io = args[0];
    IRubyObject encoding = args[1];
    IRubyObject indentString = args[2];
    IRubyObject options = args[3];
    String encString = encoding.isNil() ? null : rubyStringToString(encoding);
    SaveContextVisitor visitor = new SaveContextVisitor((Integer) options.toJava(Integer.class), rubyStringToString(indentString), encString, isHtmlDoc(context), isFragment(), 0);
    accept(context, visitor);
    IRubyObject rubyString = null;
    if (NokogiriHelpers.isUTF8(encString)) {
        rubyString = stringOrNil(context.getRuntime(), visitor.toString());
    } else {
        try {
            byte[] bytes = NokogiriHelpers.convertEncoding(Charset.forName(encString), visitor.toString());
            rubyString = stringOrNil(context.getRuntime(), bytes);
        } catch (CharacterCodingException e) {
            throw context.getRuntime().newRuntimeError(e.getMessage());
        }
    }
    RuntimeHelpers.invoke(context, io, "write", rubyString);
    return io;
}
Also used : SaveContextVisitor(nokogiri.internals.SaveContextVisitor) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) CharacterCodingException(java.nio.charset.CharacterCodingException) IRubyObject(org.jruby.runtime.builtin.IRubyObject) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 10 with RubyString

use of org.jruby.RubyString in project nokogiri by sparklemotion.

the class XsltStylesheet method getTemplatesFromStreamSource.

private Templates getTemplatesFromStreamSource() throws TransformerConfigurationException {
    if (stylesheet instanceof RubyString) {
        StringReader reader = new StringReader(stylesheet.asJavaString());
        StreamSource xsltStreamSource = new StreamSource(reader);
        return factory.newTemplates(xsltStreamSource);
    }
    return null;
}
Also used : RubyString(org.jruby.RubyString) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader)

Aggregations

RubyString (org.jruby.RubyString)11 IRubyObject (org.jruby.runtime.builtin.IRubyObject)7 ByteArrayInputStream (java.io.ByteArrayInputStream)4 StringReader (java.io.StringReader)4 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)4 JRubyMethod (org.jruby.anno.JRubyMethod)4 ByteList (org.jruby.util.ByteList)4 Charset (java.nio.charset.Charset)3 Ruby (org.jruby.Ruby)3 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)2 StreamSource (javax.xml.transform.stream.StreamSource)2 ClosedStreamException (nokogiri.internals.ClosedStreamException)2 SaveContextVisitor (nokogiri.internals.SaveContextVisitor)2 RubyClass (org.jruby.RubyClass)2 RubyException (org.jruby.RubyException)2 RubyIO (org.jruby.RubyIO)2 RaiseException (org.jruby.exceptions.RaiseException)2