From 4cb2d7bfc89da6db42877d8e5dfbc67640bcdbb0 Mon Sep 17 00:00:00 2001
From: Jan-Pascal van Best <janpascal@vanbest.org>
Date: Wed, 4 Apr 2012 21:11:53 +0200
Subject: [PATCH] Work in progress, doesn't work

---
 .../org/vanbest/xmltv/AbstractEPGSource.java  |  3 ++-
 src/main/java/org/vanbest/xmltv/Config.java   |  3 +--
 .../java/org/vanbest/xmltv/EPGSource.java     |  2 +-
 .../org/vanbest/xmltv/EPGSourceFactory.java   | 20 +++++++++++--------
 src/main/java/org/vanbest/xmltv/RTL.java      |  9 +++++----
 src/main/java/org/vanbest/xmltv/TvGids.java   |  9 +++++----
 6 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java b/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java
index 2eb200e..d9e2684 100644
--- a/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java
+++ b/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java
@@ -20,8 +20,9 @@ public abstract class AbstractEPGSource implements EPGSource {
 	
 	public static final int MAX_FETCH_TRIES=5;
 
-	public AbstractEPGSource(Config config) {
+	public AbstractEPGSource(int sourceId, Config config) {
 		this.config = config;
+		this.sourceId = sourceId;
 		cache = new ProgrammeCache(config);
 	}
 
diff --git a/src/main/java/org/vanbest/xmltv/Config.java b/src/main/java/org/vanbest/xmltv/Config.java
index 835627b..60ef333 100644
--- a/src/main/java/org/vanbest/xmltv/Config.java
+++ b/src/main/java/org/vanbest/xmltv/Config.java
@@ -64,8 +64,7 @@ public class Config {
 	
 	private Config() {
 		Properties configProp = new Properties();
-        InputStream in = ClassLoader.getSystemResourceAsStream("/tv_grab_nl_java.properties");
-        System.out.println(in);
+        InputStream in = ClassLoader.getSystemResourceAsStream("tv_grab_nl_java.properties");
         try {
             configProp.load(in);
         } catch (IOException e) {
diff --git a/src/main/java/org/vanbest/xmltv/EPGSource.java b/src/main/java/org/vanbest/xmltv/EPGSource.java
index d3211b7..2c53e55 100644
--- a/src/main/java/org/vanbest/xmltv/EPGSource.java
+++ b/src/main/java/org/vanbest/xmltv/EPGSource.java
@@ -14,7 +14,7 @@ public interface EPGSource {
 
 	public int getId();
 	public void setId(int id);
-    public String getName();
+    public String getName(); // must be static
 
 	public List<Channel> getChannels();
 	// Convenience method
diff --git a/src/main/java/org/vanbest/xmltv/EPGSourceFactory.java b/src/main/java/org/vanbest/xmltv/EPGSourceFactory.java
index 7a78bc8..1263e9c 100644
--- a/src/main/java/org/vanbest/xmltv/EPGSourceFactory.java
+++ b/src/main/java/org/vanbest/xmltv/EPGSourceFactory.java
@@ -3,11 +3,13 @@ package org.vanbest.xmltv;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
@@ -16,12 +18,13 @@ public class EPGSourceFactory {
 	private static Map<Integer,Class<EPGSource>> classes = new HashMap<Integer,Class<EPGSource>>();
 	private static Map<Integer,String> names = new HashMap<Integer,String>();
 	private static boolean initialised=false;
+	private static List<Integer> sources=new ArrayList<Integer>();
 
 	static void init() {
 		if(initialised) return;
 		Properties configProp = new Properties();
         ClassLoader loader = ClassLoader.getSystemClassLoader();
-        InputStream in = loader.getResourceAsStream("/org/vanbest/xmltv/tv_grab_nl_java.properties");
+        InputStream in = loader.getResourceAsStream("tv_grab_nl_java.properties");
         try {
             configProp.load(in);
         } catch (IOException e) {
@@ -33,10 +36,13 @@ public class EPGSourceFactory {
 			try {
 				Class<EPGSource> clazz = (Class<EPGSource>) loader.loadClass(name);
 				classes.put(source,  clazz);
-				Method getName=clazz.getMethod("getName");
-				String sourceName=(String)getName.invoke(null);
+				System.out.println("clazz: " + clazz.toString());
+				Field NAME=clazz.getField("NAME");
+				System.out.println("NAME: " + NAME.toString());
+				String sourceName=(String)NAME.get(null);
 				names.put(source,sourceName);
 				ids.put(sourceName,source);
+				sources.add(source);
 			} catch (ClassNotFoundException e) {
 				// TODO Auto-generated catch block
 				e.printStackTrace();
@@ -62,7 +68,7 @@ public class EPGSourceFactory {
 	public EPGSource createEPGSource(int source, Config config) {
 		Constructor<EPGSource> constructor;
 		try {
-			constructor = classes.get(source).getConstructor(Config.class);
+			constructor = classes.get(source).getConstructor(Integer.class,Config.class);
 			return constructor.newInstance(source, config);
 		} catch (NoSuchMethodException e) {
 			// TODO Auto-generated catch block
@@ -100,11 +106,9 @@ public class EPGSourceFactory {
 	}
 
 	public int[] getAll() {
-		Integer[] list = (Integer[]) classes.keySet().toArray();
-		Arrays.sort(list);
-		int[] result = new int[list.length];
+		int[] result = new int[sources.size()];
 		for(int i=0; i<result.length; i++) {
-			result[i]=list[i];
+			result[i]=sources.get(i);
 		}
 		return result;
 	}
diff --git a/src/main/java/org/vanbest/xmltv/RTL.java b/src/main/java/org/vanbest/xmltv/RTL.java
index 038f9bc..fcf2583 100644
--- a/src/main/java/org/vanbest/xmltv/RTL.java
+++ b/src/main/java/org/vanbest/xmltv/RTL.java
@@ -60,6 +60,7 @@ public class RTL extends AbstractEPGSource implements EPGSource  {
 	private static final String detail_url="http://www.rtl.nl/active/epg_data/uitzending_data/";
 	private static final String icon_url="http://www.rtl.nl/service/gids/components/vaste_componenten/";
 	private static final int MAX_PROGRAMMES_PER_DAY = 9999;
+	public static final String NAME="rtl.nl";
 	
 	String[] xmlKeys = {"zendernr", "pgmsoort", "genre", "bijvnwlanden", "ondertiteling", "begintijd", "titel", 
 			"site_path", "wwwadres", "presentatie", "omroep", "eindtijd", "inhoud", "tt_inhoud", "alginhoud", "afl_titel", "kijkwijzer" };
@@ -72,12 +73,12 @@ public class RTL extends AbstractEPGSource implements EPGSource  {
 		}
 	}
 	
-	public RTL(Config config) {
-		super(config);
+	public RTL(int sourceId, Config config) {
+		super(sourceId, config);
 	}
 	
 	public String getName() {
-		return "rtl.nl";
+		return NAME;
 	}
 	
 	public List<Channel> getChannels() {
@@ -335,7 +336,7 @@ public class RTL extends AbstractEPGSource implements EPGSource  {
 	 */
 	public static void main(String[] args) {
 		Config config = Config.getDefaultConfig();
-		RTL rtl = new RTL(config);
+		RTL rtl = new RTL(2, config);
 		try {
 			List<Channel> channels = rtl.getChannels();
 			System.out.println("Channels: " + channels);
diff --git a/src/main/java/org/vanbest/xmltv/TvGids.java b/src/main/java/org/vanbest/xmltv/TvGids.java
index 92d2239..91582f3 100644
--- a/src/main/java/org/vanbest/xmltv/TvGids.java
+++ b/src/main/java/org/vanbest/xmltv/TvGids.java
@@ -57,13 +57,14 @@ public class TvGids extends AbstractEPGSource implements EPGSource {
 	static String html_detail_base_url = "http://www.tvgids.nl/programma/";
 	
 	private static final int MAX_PROGRAMMES_PER_DAY = 9999;
+	public static String NAME="tvgids.nl";
 
-	public TvGids(Config config) {
-		super(config);
+	public TvGids(int sourceId, Config config) {
+		super(sourceId, config);
 	}
 	
 	public String getName() {
-		return "tvgids.nl";
+		return NAME;
 	}
 	
 	public static URL programmeUrl(List<Channel> channels, int day) throws Exception {
@@ -439,7 +440,7 @@ public class TvGids extends AbstractEPGSource implements EPGSource {
 	 */
 	public static void main(String[] args) {
 		Config config = Config.getDefaultConfig();
-		TvGids gids = new TvGids(config);
+		TvGids gids = new TvGids(1, config);
 		try {
 			List<Channel> channels = gids.getChannels();
 			System.out.println("Channels: " + channels);
-- 
2.39.5