Search in sources :

Example 1 with PyDictionary

use of org.python.core.PyDictionary 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)

Aggregations

Iterator (java.util.Iterator)1 Map (java.util.Map)1 ScriptContext (javax.script.ScriptContext)1 ScriptEngine (javax.script.ScriptEngine)1 ScriptException (javax.script.ScriptException)1 RubyHash (org.jruby.RubyHash)1 PyDictionary (org.python.core.PyDictionary)1