package us.codecraft.webmagic.scripts;

import org.apache.commons.io.IOUtils;
import sun.org.mozilla.javascript.internal.NativeObject;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * @author code4crafter@gmail.com
 * @since 0.4.1
 */
public class ScriptProcessor implements PageProcessor {

    private ScriptEngine engine;

    private String defines;

    private String script;

    private final Language language;

    private Site site = Site.me();

    public ScriptProcessor(Language language, String script) {
        if (language == null || script == null) {
            throw new IllegalArgumentException("language and script must not be null!");
        }
        this.language = language;
        ScriptEngineManager manager = new ScriptEngineManager();
        engine = manager.getEngineByName(language.getEngineName());
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(language.getDefineFile());
        try {
            defines = IOUtils.toString(resourceAsStream);
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
        this.script = script;
    }

    @Override
    public void process(Page page) {
        ScriptContext context = engine.getContext();
        context.setAttribute("page", page, ScriptContext.ENGINE_SCOPE);
        context.setAttribute("config", site, ScriptContext.ENGINE_SCOPE);
        try {
            engine.eval(defines + "\n" + script, context);
            switch (language) {
                case JavaScript:
                    NativeObject o = (NativeObject) engine.get("result");
                    if (o != null) {
                        for (Map.Entry<Object, Object> objectObjectEntry : o.entrySet()) {
                            page.getResultItems().put(objectObjectEntry.getKey().toString(), objectObjectEntry.getValue());
                        }
                    }
                    break;
                case JRuby:
                    Object o1 = engine.get("result");
                    break;
            }
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

}

@@ -75,65 +75,8 @@
 ls;%0A
-import sun.org.mozilla.javascript.internal.NativeObject;%0A
 impo
@@ -402,30 +402,8 @@
 eam;
-%0Aimport java.util.Map;
 %0A%0A/*
@@ -1705,16 +1705,18 @@
 ntext);%0A
+//
         
@@ -1735,24 +1735,26 @@
 language) %7B%0A
+//
             
@@ -1774,16 +1774,18 @@
 Script:%0A
+//
         
@@ -1842,32 +1842,34 @@
 .get(%22result%22);%0A
+//
                 
@@ -1881,32 +1881,34 @@
 f (o != null) %7B%0A
+//
                 
@@ -1978,24 +1978,26 @@
 trySet()) %7B%0A
+//
             
@@ -2104,24 +2104,26 @@
 etValue());%0A
+//
             
@@ -2128,32 +2128,34 @@
               %7D%0A
+//
                 
@@ -2152,32 +2152,34 @@
               %7D%0A
+//
                 
@@ -2181,32 +2181,34 @@
          break;%0A
+//
                 
@@ -2219,16 +2219,18 @@
  JRuby:%0A
+//
         
@@ -2271,24 +2271,26 @@
 (%22result%22);%0A
+//
             
@@ -2304,16 +2304,18 @@
  break;%0A
+//
         

remove nativeobject for rhino