marți, 28 decembrie 2010

Rhino, EnvJS and Java

This is my first sample application involving Rhino and EnvJS. I'm very pleased with the power given by Rhino for using Javascript code inside Java.


The sample application takes an URL (either http:// or file:///) and retrieves the source code after page rendering.



package org.html;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.Script;
import org.mozilla.javascript.tools.shell.Global;
import org.mozilla.javascript.tools.shell.Main;
public class HTMLDownloader {
private Context cx = null;
private Global global = null;
HTMLDownloader() {
setEnvironment();
}
private void setEnvironment() {
cx = ContextFactory.getGlobal().enterContext();
cx.setOptimizationLevel(-1);
cx.setLanguageVersion(Context.VERSION_1_6);
global = Main.getGlobal();
global.init(cx);
Main.processSource(cx, "env.rhino.1.2.js");
}
public byte[] downloadPage(String url) {
Context.toString(loadJavascript("window.location='" + url + "'"));
byte[] data = Context.toString(loadJavascript("content=document.getElementsByTagName('html')[0].innerHTML;")).getBytes();
return data;
}

private Object loadJavascript(String js) {
Script script = Main.loadScriptFromSource(cx, js, "<stdin>", 1, null);
Object result = null;
if (script != null) {
result = Main.evaluateScript(script, cx, global);
if (result == Context.getUndefinedValue()) {
result = null;
}
}
return result;
}
protected void finalize() throws Throwable {
   try {
       Context.exit();
   } finally {
       super.finalize();
   }
}

}


   JUnit testcase:

package org.html;
import org.junit.Test;
public class HTMLDownloaderTests {
private String toString(byte[] data) {
String sstream = null;
if (data.length > 0) {
sstream = new String(data);
}
return sstream;
}

@Test
public void downloadPages() {
HTMLDownloader hd = new HTMLDownloader();
byte[] data = hd.downloadPage("file:///d:/x.html");
System.out.println(toString(data));
data = hd.downloadPage("http://www.google.com");
System.out.println(toString(data));
}