Search in sources :

Example 1 with RubyString

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

the class ParserContext method setInputSource.

/**
     * Set the InputSource from <code>url</code> or <code>data</code>,
     * which may be an IO object, a String, or a StringIO.
     */
public void setInputSource(ThreadContext context, IRubyObject data, IRubyObject url) {
    source = new InputSource();
    Ruby ruby = context.getRuntime();
    ParserContext.setUrl(context, source, url);
    // to the EncodingReaderInputStream
    if (setEncoding(context, data))
        return;
    RubyString stringData = null;
    if (invoke(context, data, "respond_to?", ruby.newSymbol("to_io")).isTrue()) {
        RubyIO io = (RubyIO) TypeConverter.convertToType(data, ruby.getIO(), "to_io");
        // use unclosedable input stream to fix #495
        source.setByteStream(new UncloseableInputStream(io.getInStream()));
    } else if (invoke(context, data, "respond_to?", ruby.newSymbol("read")).isTrue()) {
        stringData = invoke(context, data, "read").convertToString();
    } else if (invoke(context, data, "respond_to?", ruby.newSymbol("string")).isTrue()) {
        stringData = invoke(context, data, "string").convertToString();
    } else if (data instanceof RubyString) {
        stringData = (RubyString) data;
    } else {
        throw ruby.newArgumentError("must be kind_of String or respond to :to_io, :read, or :string");
    }
    if (stringData != null) {
        String encName = null;
        if (stringData.encoding(context) != null) {
            encName = stringData.encoding(context).toString();
        }
        Charset charset = null;
        if (encName != null) {
            try {
                charset = Charset.forName(encName);
            } catch (UnsupportedCharsetException e) {
            // do nothing;
            }
        }
        ByteList bytes = stringData.getByteList();
        if (charset != null) {
            StringReader reader = new StringReader(new String(bytes.unsafeBytes(), bytes.begin(), bytes.length(), charset));
            source.setCharacterStream(reader);
            source.setEncoding(charset.name());
        } else {
            stringDataSize = bytes.length() - bytes.begin();
            ByteArrayInputStream stream = new ByteArrayInputStream(bytes.unsafeBytes(), bytes.begin(), bytes.length());
            source.setByteStream(stream);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) ByteList(org.jruby.util.ByteList) ByteArrayInputStream(java.io.ByteArrayInputStream) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) RubyString(org.jruby.RubyString) StringReader(java.io.StringReader) Charset(java.nio.charset.Charset) RubyString(org.jruby.RubyString) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyIO(org.jruby.RubyIO) Ruby(org.jruby.Ruby)

Example 2 with RubyString

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

the class XsltStylesheet method getTemplatesFromStreamSource.

private Templates getTemplatesFromStreamSource() throws TransformerConfigurationException {
    if (stylesheet instanceof RubyString) {
        StringReader reader = new StringReader((String) stylesheet.toJava(String.class));
        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)

Example 3 with RubyString

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

the class NokogiriHelpers method nkf.

// This method is used from HTML documents. HTML meta tag with encoding specification
// might appear after non-ascii characters are used. For example, a title tag before
// a meta tag. In such a case, Xerces encodes characters in UTF-8 without seeing meta tag.
// Nokogiri uses NKF library to convert characters correct encoding. This means the method
// works only for JIS/Shift_JIS/EUC-JP.
public static String nkf(Ruby runtime, String ruby_encoding, String thing) {
    StringBuffer sb = new StringBuffer("-");
    Charset that = Charset.forName(ruby_encoding);
    if (NokogiriHelpers.shift_jis.compareTo(that) == 0) {
        sb.append("S");
    } else if (NokogiriHelpers.jis.compareTo(that) == 0) {
        sb.append("J");
    } else if (NokogiriHelpers.euc_jp.compareTo(that) == 0) {
        sb.append("E");
    } else {
        // should not come here. should be treated before this method.
        sb.append("W");
    }
    sb.append("w");
    Class nkfClass = null;
    try {
        // JRuby 1.7 and later
        nkfClass = runtime.getClassLoader().loadClass("org.jruby.ext.nkf.RubyNKF");
    } catch (ClassNotFoundException e1) {
        try {
            // Before JRuby 1.7
            nkfClass = runtime.getClassLoader().loadClass("org.jruby.RubyNKF");
        } catch (ClassNotFoundException e2) {
            return thing;
        }
    }
    Method nkf_method;
    try {
        nkf_method = nkfClass.getMethod("nkf", ThreadContext.class, IRubyObject.class, IRubyObject.class, IRubyObject.class);
        RubyString r_str = (RubyString) nkf_method.invoke(null, runtime.getCurrentContext(), null, runtime.newString(new String(sb)), runtime.newString(thing));
        return NokogiriHelpers.rubyStringToString(r_str);
    } catch (SecurityException e) {
        return thing;
    } catch (NoSuchMethodException e) {
        return thing;
    } catch (IllegalArgumentException e) {
        return thing;
    } catch (IllegalAccessException e) {
        return thing;
    } catch (InvocationTargetException e) {
        return thing;
    }
}
Also used : RubyString(org.jruby.RubyString) ThreadContext(org.jruby.runtime.ThreadContext) Charset(java.nio.charset.Charset) Method(java.lang.reflect.Method) RubyString(org.jruby.RubyString) IRubyObject(org.jruby.runtime.builtin.IRubyObject) InvocationTargetException(java.lang.reflect.InvocationTargetException) RubyClass(org.jruby.RubyClass)

Example 4 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 5 with RubyString

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

the class ParserContext method setInputSource.

/**
 * Set the InputSource from <code>url</code> or <code>data</code>,
 * which may be an IO object, a String, or a StringIO.
 */
public void setInputSource(ThreadContext context, IRubyObject data, IRubyObject url) {
    source = new InputSource();
    Ruby ruby = context.getRuntime();
    ParserContext.setUrl(context, source, url);
    // to the EncodingReaderInputStream
    if (setEncoding(context, data))
        return;
    RubyString stringData = null;
    if (invoke(context, data, "respond_to?", ruby.newSymbol("to_io").to_sym()).isTrue()) {
        /* IO or other object that responds to :to_io */
        RubyIO io = (RubyIO) TypeConverter.convertToType(data, ruby.getIO(), "to_io");
        // use unclosedable input stream to fix #495
        source.setByteStream(new UncloseableInputStream(io.getInStream()));
    } else {
        if (invoke(context, data, "respond_to?", ruby.newSymbol("string").to_sym()).isTrue()) {
            /* StringIO or other object that responds to :string */
            stringData = invoke(context, data, "string").convertToString();
        } else if (data instanceof RubyString) {
            stringData = (RubyString) data;
        } else {
            throw ruby.newArgumentError("must be kind_of String or respond to :to_io or :string");
        }
    }
    if (stringData != null) {
        String encName = null;
        if (stringData.encoding(context) != null) {
            encName = stringData.encoding(context).toString();
        }
        Charset charset = null;
        if (encName != null) {
            try {
                charset = Charset.forName(encName);
            } catch (UnsupportedCharsetException e) {
            // do nothing;
            }
        }
        ByteList bytes = stringData.getByteList();
        if (charset != null) {
            StringReader reader = new StringReader(new String(bytes.unsafeBytes(), bytes.begin(), bytes.length(), charset));
            source.setCharacterStream(reader);
            source.setEncoding(charset.name());
        } else {
            stringDataSize = bytes.length() - bytes.begin();
            ByteArrayInputStream stream = new ByteArrayInputStream(bytes.unsafeBytes(), bytes.begin(), bytes.length());
            source.setByteStream(stream);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) ByteList(org.jruby.util.ByteList) ByteArrayInputStream(java.io.ByteArrayInputStream) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) RubyString(org.jruby.RubyString) StringReader(java.io.StringReader) Charset(java.nio.charset.Charset) RubyString(org.jruby.RubyString) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyIO(org.jruby.RubyIO) Ruby(org.jruby.Ruby)

Aggregations

RubyString (org.jruby.RubyString)49 JRubyMethod (org.jruby.anno.JRubyMethod)32 Ruby (org.jruby.Ruby)28 IRubyObject (org.jruby.runtime.builtin.IRubyObject)18 IOException (java.io.IOException)15 ByteList (org.jruby.util.ByteList)12 StringReader (java.io.StringReader)8 ByteArrayInputStream (java.io.ByteArrayInputStream)5 GeneralSecurityException (java.security.GeneralSecurityException)5 RaiseException (org.jruby.exceptions.RaiseException)5 BigInteger (java.math.BigInteger)4 PublicKey (java.security.PublicKey)4 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)4 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)4 RubyArray (org.jruby.RubyArray)4 Charset (java.nio.charset.Charset)3 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)3 RubyInteger (org.jruby.RubyInteger)3 ThreadContext (org.jruby.runtime.ThreadContext)3 InputSource (org.xml.sax.InputSource)3