remove test codes
parent
bc5c30de17
commit
7f26b84439
@ -1,82 +0,0 @@
|
||||
package us.codecraft.webmagic.javascript;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import us.codecraft.webmagic.Page;
|
||||
import us.codecraft.webmagic.Site;
|
||||
import us.codecraft.webmagic.Spider;
|
||||
import us.codecraft.webmagic.processor.PageProcessor;
|
||||
import us.codecraft.webmagic.scripts.ScriptProcessor;
|
||||
import us.codecraft.webmagic.scripts.ScriptProcessorBuilder;
|
||||
|
||||
import javax.script.ScriptContext;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author code4crafter@gmail.com
|
||||
* @since 0.4.1
|
||||
*/
|
||||
public class JsScriptProcessor implements PageProcessor {
|
||||
|
||||
private ScriptEngine engine;
|
||||
|
||||
private String defines;
|
||||
|
||||
private String script;
|
||||
|
||||
JsScriptProcessor(String script) throws IOException {
|
||||
ScriptEngineManager manager = new ScriptEngineManager();
|
||||
engine = manager.getEngineByName("javascript");
|
||||
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("js/defines.js");
|
||||
defines = IOUtils.toString(resourceAsStream);
|
||||
this.script = script;
|
||||
}
|
||||
|
||||
public static JsScriptProcessor fromFile(String fileName) {
|
||||
try {
|
||||
InputStream resourceAsStream = new FileInputStream(fileName);
|
||||
String script = IOUtils.toString(resourceAsStream);
|
||||
return new JsScriptProcessor(script);
|
||||
} catch (IOException e) {
|
||||
//wrap IOException because I prefer a runtime exception...
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static JsScriptProcessor fromClassPathFile(String fileName) {
|
||||
try {
|
||||
InputStream resourceAsStream = JsScriptProcessor.class.getClassLoader().getResourceAsStream(fileName);
|
||||
String script = IOUtils.toString(resourceAsStream);
|
||||
return new JsScriptProcessor(script);
|
||||
} catch (IOException e) {
|
||||
//wrap IOException because I prefer a runtime exception...
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(Page page) {
|
||||
ScriptContext context = engine.getContext();
|
||||
context.setAttribute("page", page, ScriptContext.ENGINE_SCOPE);
|
||||
try {
|
||||
engine.eval(defines + script, context);
|
||||
} catch (ScriptException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Site getSite() {
|
||||
return Site.me();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ScriptProcessor pageProcessor = ScriptProcessorBuilder.custom().scriptFromClassPathFile("js/oschina.js").build();
|
||||
Spider.create(pageProcessor).addUrl("http://my.oschina.net/flashsword/blog").run();
|
||||
}
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
package us.codecraft.webmagic.jruby;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import us.codecraft.webmagic.Page;
|
||||
import us.codecraft.webmagic.Site;
|
||||
import us.codecraft.webmagic.Spider;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author code4crafter@gmail.com
|
||||
*/
|
||||
public class RubyScriptProcessor implements PageProcessor {
|
||||
|
||||
private ScriptEngine rubyEngine;
|
||||
|
||||
private String defines;
|
||||
|
||||
private String script;
|
||||
|
||||
public RubyScriptProcessor(String filename){
|
||||
ScriptEngineManager manager = new ScriptEngineManager();
|
||||
rubyEngine = manager.getEngineByName("jruby");
|
||||
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("ruby/defines.rb");
|
||||
try {
|
||||
defines = IOUtils.toString(resourceAsStream);
|
||||
resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(filename);
|
||||
script = IOUtils.toString(resourceAsStream);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void process(Page page) {
|
||||
ScriptContext context = rubyEngine.getContext();
|
||||
context.setAttribute("page", page, ScriptContext.ENGINE_SCOPE);
|
||||
try {
|
||||
rubyEngine.eval(defines+script, context);
|
||||
} catch (ScriptException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Site getSite() {
|
||||
return Site.me();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Spider.create(new RubyScriptProcessor("ruby/oschina.rb")).addUrl("http://my.oschina.net/flashsword/blog").run();
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package us.codecraft.webmagic.jruby;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.script.ScriptContext;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
/**
|
||||
* @author code4crafter@gmail.com
|
||||
*/
|
||||
public class TestJRubyCall {
|
||||
|
||||
@Test
|
||||
public void test() throws ScriptException {
|
||||
ScriptEngineManager manager = new ScriptEngineManager();
|
||||
ScriptEngine rubyEngine = manager.getEngineByName("jruby");
|
||||
ScriptContext context = rubyEngine.getContext();
|
||||
|
||||
context.setAttribute("a", "sad", ScriptContext.ENGINE_SCOPE);
|
||||
// rubyEngine.eval("", context);
|
||||
rubyEngine.eval("b=1; puts b", context);
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package us.codecraft.webmagic.js;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.script.ScriptContext;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
/**
|
||||
* @author code4crafter@gmail.com
|
||||
*/
|
||||
public class TestJsCall {
|
||||
|
||||
@Test
|
||||
public void test() throws ScriptException {
|
||||
ScriptEngineManager manager = new ScriptEngineManager();
|
||||
ScriptEngine rubyEngine = manager.getEngineByName("javascript");
|
||||
ScriptContext context = rubyEngine.getContext();
|
||||
|
||||
context.setAttribute("a", "sad", ScriptContext.ENGINE_SCOPE);
|
||||
// rubyEngine.eval("", context);
|
||||
rubyEngine.eval("print(a)", context);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue