]> www.vanbest.org Git - tv_grab_nl_java/commitdiff
Use Mozilla Rhino for JavaScript evaluation instead of internal parser, which has...
authorJan-Pascal van Best <janpascal@vanbest.org>
Sun, 10 Jul 2016 13:56:38 +0000 (15:56 +0200)
committerJan-Pascal van Best <janpascal@vanbest.org>
Sun, 10 Jul 2016 13:56:38 +0000 (15:56 +0200)
pom.xml
src/main/java/org/vanbest/xmltv/Horizon.java

diff --git a/pom.xml b/pom.xml
index 685e0e329831ca0dd71ef2353ee768a231c2f99b..a840c3422b39c9076a618d31e086ac4b43062e19 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -2,7 +2,7 @@
        <modelVersion>4.0.0</modelVersion>
        <groupId>org.vanbest.xmltv.tv_grab_nl_java</groupId>
        <artifactId>tv_grab_nl_java</artifactId>
-       <version>1.7.1</version>
+       <version>1.7.2-dev</version>
        <scm>
                <connection>scm:git:git://github.com/janpascal/tv_grab_nl_java.git</connection>
                <developerConnection>scm:git:git://github.com/janpascal/tv_grab_nl_java.git</developerConnection>
                      <artifactId>reflections</artifactId>
                      <version>0.9.9-RC1</version>
                  </dependency>
+                <!-- https://mvnrepository.com/artifact/org.mozilla/rhino -->
+                <dependency>
+                    <groupId>org.mozilla</groupId>
+                    <artifactId>rhino</artifactId>
+                    <version>1.7.7.1</version>
+                </dependency>
        </dependencies>
        <build>
                <plugins>
index d9b9e37a1349a38e8d052b903db5e452a477292b..9937883d2c4276f66a25c3575c69467a95ec6c95 100644 (file)
@@ -24,12 +24,10 @@ import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
 
-import javax.script.*;
-
 import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamWriter;
 
-import sun.org.mozilla.javascript.NativeObject;
+import org.mozilla.javascript.*;
 
 import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
@@ -90,59 +88,59 @@ public class Horizon extends AbstractEPGSource implements EPGSource {
         //System.out.println("\ntest config javascript: ");
         //System.out.println(config);
 
-        // Get the JavaScript engine
-        ScriptEngineManager manager = new ScriptEngineManager();
-        ScriptEngine engine = manager.getEngineByName("JavaScript");
-
-        // Set JavaScript variables
-        Bindings vars = new SimpleBindings();
-        
-        // Evaluate script
+        Context cx = Context.enter();
         try {
-            engine.eval(config);
-        } catch (Exception e) {
-            logger.error("Exception parsing Horizon javascript config", e);
-            return;
-        }
+            // Initialize the standard objects (Object, Function, etc.)
+            // This must be done before scripts can be executed. Returns
+            // a scope object that we use in later calls.
+            Scriptable scope = cx.initStandardObjects();
 
-        Object settings = engine.get("BBVSettingsObject");
-
-        String base_url = null;
-
-        // Parse URLs from settings object
-        if (settings instanceof NativeObject) {
-            NativeObject nObj = (NativeObject)settings;
-            Object api = nObj.get("api", nObj);
-            if (api instanceof NativeObject) {
-                NativeObject nApi = (NativeObject) api;
-                Object urls = nApi.get("urls", nApi);
-                if (urls instanceof NativeObject) {
-                    NativeObject nUrls = (NativeObject) urls;
-                    Object base = nUrls.get("base", nUrls);
-                    try {
-                        URL url_base = new URL((String) base);
-                        URL root_base = new URL(url_base.getProtocol(), url_base.getHost(), url_base.getPort(), "");
-                        base_url = root_base.toString();
-                    } catch (MalformedURLException e) {
-                        logger.error("Malformed URL trying to calculate Horizon base URL", e);
+            // Now evaluate the string we've colected.
+            Object result = cx.evaluateString(scope, config, "<cmd>", 1, null);
+
+            Object settings = scope.get("BBVSettingsObject", scope);
+            if (settings == Scriptable.NOT_FOUND) {
+                logger.error("settings is not defined.");
+            } else {
+                //System.out.println("Settings: = " + Context.toString(settings));
+                String base_url = null;
+                if (settings instanceof Scriptable) {
+                    Scriptable s = (Scriptable) settings;
+                    Scriptable api = (Scriptable) s.get("api", s);
+                    logger.debug("api: " + Context.toString(api));
+                    Scriptable urls =  (Scriptable) api.get("urls", api);
+                    logger.debug("urls: " + Context.toString(urls));
+                    Object base = urls.get("base", urls);
+                    if (settings== Scriptable.NOT_FOUND) {
+                        logger.error("Horizon config: base is not defined.");
+                    } else {
+                        String baseString = Context.toString(base);
+                        try {
+                            URL baseURL = new URL(baseString);
+                            URL root_base = new URL(baseURL.getProtocol(), baseURL.getHost(), baseURL.getPort(), "");
+                            base_url = root_base.toString();
+                        } catch (MalformedURLException e) {
+                            logger.error("Malformed URL trying to calculate Horizon base URL", e);
+                        }
                     }
-                }
-            }
-            Object routes = nObj.get("oespRoutes", nObj);
-            if (routes instanceof NativeObject) {
-                NativeObject nRoutes = (NativeObject) routes;
 
-                Object channels = nRoutes.get("channels", nRoutes);
-                this.channels_url = base_url + (String) channels;
-                //System.out.println("channels_url: " + channels_url);
+                    Scriptable routes = (Scriptable) s.get("oespRoutes", s);
+                    logger.debug("routes: " + Context.toString(routes));
 
-                Object listings = nRoutes.get("listings", nRoutes);
-                this.listings_url = base_url + (String) listings;
-                //System.out.println("listings_url: " + listings_url);
-            }
+                    Object channels = routes.get("channels", routes);
+                    this.channels_url = base_url + Context.toString(channels);
+                    logger.debug("channels_url: " + channels_url);
 
+                    Object listings = routes.get("listings", routes);
+                    this.listings_url = base_url + Context.toString(listings);
+                    logger.debug("listings_url: " + listings_url);
+                }
+            }
+        } catch (Exception e) {
+            logger.error("Exception parsing Horizon javascript config", e);
+        } finally {
+            Context.exit();
         }
-                
     }
 
     public String getName() {