javascript - parse js file for Java Application -
i'm using jsoup
parse html website. there 2 option lists dynamically created multidimensional array in javascript file. since dynamically created jsoup
can't parse results in html. however, data need located in js file. ideally i'd able periodically load file , persist / refresh array data file local database on android application. js file in question here , website showing lists here there way download selected aspects of file manipulate in java, dom in html?
you use webview javascriptinterface or use rhino engine. latter approach described below.
first download rhino (current version 1.7.7.1)
copy rhino jar (e.g. rhino-1.7.7.1.jar
) lib folder libs folder in android project.
add build.gradle (module: app) file dependency:
dependencies { [other dependencies] compile files('libs/rhino-1.7.7.1.jar') }
the following example activity loads script file, modifies script (see comments) , executes functions populate arrays. arrays retrieved , stored arrays in java:
mainactivity.java
import android.os.asynctask; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import org.mozilla.javascript.context; import org.mozilla.javascript.function; import org.mozilla.javascript.nativearray; import org.mozilla.javascript.scriptable; import org.mozilla.javascript.scriptableobject; import java.io.bufferedreader; import java.io.inputstreamreader; import java.net.url; public class mainactivity extends appcompatactivity { string[][] deptarray; string[][] zonearray; string[][] roomarray; string[][] studsetarraybycode; string[][] studsetarraybytitle; string[][] staffarray; string[][] roombydeptarray; string[][] modulearraybycode; string[][] modulearraybytitle; string[][] progarraybycode; string[][] progarraybytitle; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); new retrievejsfiletask().execute("http://timetables.itsligo.ie:81/js/filter.js"); } class retrievejsfiletask extends asynctask<string, void, string> { protected string doinbackground(string... urls) { try { url url = new url(urls[0]); bufferedreader in = new bufferedreader(new inputstreamreader(url.openstream())); stringbuilder builder = new stringbuilder(); string line=""; boolean store = false; builder.append("var deptarray;"); builder.append("var zonearray;"); while ((line = in.readline()) != null) { if(line.startswith("var roomarray = ")||line.startswith("var studsetarray =")||line.startswith("var staffarray =")||line.startswith("var roombydeptarray =")||line.startswith("var modulearray =")||line.startswith("var progarray =")){ builder.append(line); continue; }else if(line.contains("function populatefilter(strzoneordept, cbxfilter)")){ // populates dept , zone store = true; } else if(line.contains("function filtermodulesbycode(form) {") || line.contains("function filtermodulesbytitle(form) {") ){ // populates module store = true; } else if(line.contains("function filterstudentsetsbycode(form) {") || line.contains("function filterstudentsetsbytitle(form) {") ){ // populates studset store = true; } else if(line.contains("function filterrooms(form) {") ){ // populates room store = true; } else if(line.contains("function filterroomsbydept(form) {") ){ // populates roombydept store = true; } else if( line.contains("function filterprogrammesbycode(form) {") || line.contains("function filterprogrammesbytitle(form) {") ){ // populates prog store = true; } else if(line.contains("function filterstaff(form) {") ){ // populates staff store = true; } else if(line.contains("var zonearray") || line.contains("var deptarray") ){ // make zone , dept global line = line.replace("var ", ""); } if(store){ builder.append(line); } if( line.contains("zonearray.sort()") || line.contains("modulearray.sort();") || line.contains("studsetarray.sort();") || line.contains("roomarray.sort();") || line.contains(" roombydeptarray.sort();") || line.contains(" progarray.sort();") || line.contains("staffarray.sort();") ){ builder.append("return;}"); // stop function execution after population of arrays store = false; continue; } } in.close(); return builder.tostring(); } catch (exception e) { e.printstacktrace(); return null; } } protected void onpostexecute(string jssource) { if(jssource==null) return; context rhino = context.enter(); // turn off optimization make rhino android compatible rhino.setoptimizationlevel(-1); scriptable scope = rhino.initstandardobjects(); scriptableobject.putproperty(scope, "javacontext", context.javatojs(this, scope)); rhino.evaluatestring(scope, jssource, "scriptapi", 1, null); ((function)scope.get("populatefilter", scope)).call(rhino, scope, scope, new object[]{null,null}); deptarray = getarray((nativearray)scope.get("deptarray", scope)); zonearray = getarray((nativearray)scope.get("zonearray", scope)); ((function)scope.get("filtermodulesbycode", scope)).call(rhino, scope, scope, new object[]{null}); modulearraybycode = getarray((nativearray)scope.get("modulearray", scope)); ((function)scope.get("filtermodulesbytitle", scope)).call(rhino, scope, scope, new object[]{null}); modulearraybytitle = getarray((nativearray)scope.get("modulearray", scope)); ((function)scope.get("filterstudentsetsbycode", scope)).call(rhino, scope, scope, new object[]{null}); studsetarraybycode = getarray((nativearray)scope.get("studsetarray", scope)); ((function)scope.get("filterstudentsetsbytitle", scope)).call(rhino, scope, scope, new object[]{null}); studsetarraybytitle = getarray((nativearray)scope.get("studsetarray", scope)); ((function)scope.get("filterrooms", scope)).call(rhino, scope, scope, new object[]{null}); roomarray = getarray((nativearray)scope.get("roomarray", scope)); ((function)scope.get("filterroomsbydept", scope)).call(rhino, scope, scope, new object[]{null}); roombydeptarray = getarray((nativearray)scope.get("roombydeptarray", scope)); ((function)scope.get("filterprogrammesbycode", scope)).call(rhino, scope, scope, new object[]{null}); progarraybycode = getarray((nativearray)scope.get("progarray", scope)); ((function)scope.get("filterprogrammesbytitle", scope)).call(rhino, scope, scope, new object[]{null}); progarraybytitle = getarray((nativearray)scope.get("progarray", scope)); ((function)scope.get("filterstaff", scope)).call(rhino, scope, scope, new object[]{null}); staffarray = getarray((nativearray)scope.get("staffarray", scope)); printarray(deptarray, "deptarray"); printarray(zonearray, "zonearray"); printarray(modulearraybycode, "modulearraybycode"); printarray(modulearraybytitle, "modulearraybytitle"); printarray(studsetarraybycode, "studsetarraybycode"); printarray(studsetarraybytitle, "studsetarraybytitle"); printarray(roomarray, "roomarray"); printarray(roombydeptarray, "roombydeptarray"); printarray(progarraybycode, "progarraybycode"); printarray(progarraybytitle, "progarraybytitle"); printarray(staffarray, "staffarray"); context.exit(); } private void printarray(string[][] array, string tag){ string output = ""; for(int row=0;row<array.length;row++){ for(int column=0;column<array[row].length;column++){ output+=array[row][column]+"\n"; } } log.e( (tag==null?"javaarray":tag),output); } private string[][] getarray(nativearray nativearray){ string[][] javaarray = new string[nativearray.size()][((nativearray)nativearray.get(0)).size()]; (integer row : nativearray.getindexids()) { for(int column=0;column<((nativearray)nativearray.get(row)).size();column++){ javaarray[row][column] = (string)((nativearray) nativearray.get(row)).get(column); } } return javaarray; } } }
Comments
Post a Comment