Search in sources :

Example 11 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)

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