Search in sources :

Example 1 with RubyHash

use of org.jruby.RubyHash in project webmagic by code4craft.

the class ScriptProcessor method process.

@Override
public void process(Page page) {
    ScriptEngine engine = enginePool.getEngine();
    try {
        ScriptContext context = engine.getContext();
        context.setAttribute("page", page, ScriptContext.ENGINE_SCOPE);
        context.setAttribute("config", site, ScriptContext.ENGINE_SCOPE);
        try {
            switch(language) {
                case JavaScript:
                    engine.eval(defines + "\n" + script, context);
                    //                        }
                    break;
                case JRuby:
                    RubyHash oRuby = (RubyHash) engine.eval(defines + "\n" + script, context);
                    Iterator itruby = oRuby.entrySet().iterator();
                    while (itruby.hasNext()) {
                        Map.Entry pairs = (Map.Entry) itruby.next();
                        page.getResultItems().put(pairs.getKey().toString(), pairs.getValue());
                    }
                    break;
                case Jython:
                    engine.eval(defines + "\n" + script, context);
                    PyDictionary oJython = (PyDictionary) engine.get("result");
                    Iterator it = oJython.entrySet().iterator();
                    while (it.hasNext()) {
                        Map.Entry pairs = (Map.Entry) it.next();
                        page.getResultItems().put(pairs.getKey().toString(), pairs.getValue());
                    }
                    break;
            }
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    } finally {
        enginePool.release(engine);
    }
}
Also used : ScriptException(javax.script.ScriptException) RubyHash(org.jruby.RubyHash) Iterator(java.util.Iterator) ScriptContext(javax.script.ScriptContext) PyDictionary(org.python.core.PyDictionary) Map(java.util.Map) ScriptEngine(javax.script.ScriptEngine)

Example 2 with RubyHash

use of org.jruby.RubyHash in project enumerable by hraberg.

the class JRubyTestBase method javaInRubyToRuby.

protected static IRubyObject javaInRubyToRuby(Ruby ruby, Object value) {
    if (value instanceof List<?> && !(value instanceof RubyArray)) {
        RubyArray array = RubyArray.newArray(ruby);
        array.addAll((Collection<?>) value);
        return array;
    }
    if (value instanceof Map<?, ?> && !(value instanceof RubyHash)) {
        RubyHash hash = RubyHash.newHash(ruby);
        hash.putAll((Map<?, ?>) value);
        return hash;
    }
    return javaToRuby(ruby, value);
}
Also used : RubyArray(org.jruby.RubyArray) RubyHash(org.jruby.RubyHash)

Example 3 with RubyHash

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

the class ReaderNode method getAttributes.

public IRubyObject getAttributes(ThreadContext context) {
    final Ruby runtime = context.runtime;
    if (attributeList == null)
        return runtime.getNil();
    RubyHash hash = RubyHash.newHash(runtime);
    for (int i = 0; i < attributeList.length; i++) {
        IRubyObject k = stringOrBlank(runtime, attributeList.names.get(i));
        IRubyObject v = stringOrBlank(runtime, attributeList.values.get(i));
        // hash.op_aset(context, k, v)
        hash.fastASetCheckString(runtime, k, v);
    }
    return hash;
}
Also used : RubyHash(org.jruby.RubyHash) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby)

Example 4 with RubyHash

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

the class ReaderNode method getAttributes.

public IRubyObject getAttributes(ThreadContext context) {
    if (attributeList == null)
        return context.getRuntime().getNil();
    RubyHash hash = RubyHash.newHash(context.getRuntime());
    for (int i = 0; i < attributeList.length; i++) {
        IRubyObject k = stringOrBlank(context.getRuntime(), attributeList.names.get(i));
        IRubyObject v = stringOrBlank(context.getRuntime(), attributeList.values.get(i));
        if (context.getRuntime().is1_9())
            hash.op_aset19(context, k, v);
        else
            hash.op_aset(context, k, v);
    }
    return hash;
}
Also used : RubyHash(org.jruby.RubyHash) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 5 with RubyHash

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

the class ReaderNode method getNamespaces.

public IRubyObject getNamespaces(ThreadContext context) {
    if (namespaces == null)
        return ruby.getNil();
    RubyHash hash = RubyHash.newHash(ruby);
    Set<String> keys = namespaces.keySet();
    for (String key : keys) {
        String stringValue = namespaces.get(key);
        IRubyObject k = stringOrBlank(context.getRuntime(), key);
        IRubyObject v = stringOrBlank(context.getRuntime(), stringValue);
        if (context.getRuntime().is1_9())
            hash.op_aset19(context, k, v);
        else
            hash.op_aset(context, k, v);
    }
    return hash;
}
Also used : RubyHash(org.jruby.RubyHash) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Aggregations

RubyHash (org.jruby.RubyHash)6 IRubyObject (org.jruby.runtime.builtin.IRubyObject)4 Map (java.util.Map)2 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)2 Ruby (org.jruby.Ruby)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 ScriptContext (javax.script.ScriptContext)1 ScriptEngine (javax.script.ScriptEngine)1 ScriptException (javax.script.ScriptException)1 RubyArray (org.jruby.RubyArray)1 PyDictionary (org.python.core.PyDictionary)1