From 366a463a631cbc0754931ed9d5d6da9ce9e6c168 Mon Sep 17 00:00:00 2001 From: Jan-Pascal van Best Date: Wed, 4 Apr 2012 16:58:15 +0200 Subject: [PATCH] Configure epg sources in proerties file (experimental) --- .../org/vanbest/xmltv/AbstractEPGSource.java | 9 ++ src/main/java/org/vanbest/xmltv/Channel.java | 2 +- src/main/java/org/vanbest/xmltv/Config.java | 9 +- .../java/org/vanbest/xmltv/EPGSource.java | 1 + .../org/vanbest/xmltv/EPGSourceFactory.java | 113 +++++++++++++----- src/main/java/org/vanbest/xmltv/RTL.java | 93 ++------------ src/main/java/org/vanbest/xmltv/TvGids.java | 12 +- src/main/resources/tv_grab_nl_java.properties | 4 +- 8 files changed, 119 insertions(+), 124 deletions(-) diff --git a/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java b/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java index 6dcf076..2eb200e 100644 --- a/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java +++ b/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java @@ -13,6 +13,7 @@ import org.vanbest.xmltv.EPGSource.Stats; public abstract class AbstractEPGSource implements EPGSource { + private int sourceId; protected Config config; protected ProgrammeCache cache; protected Stats stats = new Stats(); @@ -24,6 +25,14 @@ public abstract class AbstractEPGSource implements EPGSource { cache = new ProgrammeCache(config); } + public int getId() { + return sourceId; + } + + public void setId(int id) { + sourceId = id; + } + public List getProgrammes(Channel channel, int day) throws Exception { ArrayList list = new ArrayList(2); diff --git a/src/main/java/org/vanbest/xmltv/Channel.java b/src/main/java/org/vanbest/xmltv/Channel.java index e1725b8..5b8f84e 100644 --- a/src/main/java/org/vanbest/xmltv/Channel.java +++ b/src/main/java/org/vanbest/xmltv/Channel.java @@ -46,7 +46,7 @@ public class Channel { } public String getSourceName() { - return EPGSourceFactory.getChannelSourceName(source); + return EPGSourceFactory.newInstance().getChannelSourceName(source); } public void serialize(XMLStreamWriter writer) throws XMLStreamException { diff --git a/src/main/java/org/vanbest/xmltv/Config.java b/src/main/java/org/vanbest/xmltv/Config.java index 0707120..835627b 100644 --- a/src/main/java/org/vanbest/xmltv/Config.java +++ b/src/main/java/org/vanbest/xmltv/Config.java @@ -64,7 +64,8 @@ public class Config { private Config() { Properties configProp = new Properties(); - InputStream in = this.getClass().getResourceAsStream("/tv_grab_nl_java.properties"); + InputStream in = ClassLoader.getSystemResourceAsStream("/tv_grab_nl_java.properties"); + System.out.println(in); try { configProp.load(in); } catch (IOException e) { @@ -214,13 +215,13 @@ public class Config { // System.out.println("Adding channel " + parts + " in file format " + fileformat); switch(fileformat) { case 0: - c = Channel.getChannel(EPGSourceFactory.CHANNEL_SOURCE_TVGIDS, parts.get(1), parts.get(2)); + c = Channel.getChannel(EPGSourceFactory.newInstance().getChannelSourceId("tvgids.nl"), parts.get(1), parts.get(2)); if (parts.size()>3) { c.addIcon(parts.get(3)); } break; case 1: - c = Channel.getChannel(EPGSourceFactory.CHANNEL_SOURCE_TVGIDS, parts.get(1), parts.get(3)); + c = Channel.getChannel(EPGSourceFactory.newInstance().getChannelSourceId("tvgids.nl"), parts.get(1), parts.get(3)); if (parts.size()>4) { c.addIcon(parts.get(4)); } @@ -240,7 +241,7 @@ public class Config { if (fileformat==2) { source = Integer.parseInt(parts.get(1)); } else { - source = EPGSourceFactory.getChannelSourceId(parts.get(1)); + source = EPGSourceFactory.newInstance().getChannelSourceId(parts.get(1)); } c = Channel.getChannel(source, parts.get(2), parts.get(4)); if (parts.size()>5) { diff --git a/src/main/java/org/vanbest/xmltv/EPGSource.java b/src/main/java/org/vanbest/xmltv/EPGSource.java index fd5a497..d3211b7 100644 --- a/src/main/java/org/vanbest/xmltv/EPGSource.java +++ b/src/main/java/org/vanbest/xmltv/EPGSource.java @@ -13,6 +13,7 @@ public interface EPGSource { } public int getId(); + public void setId(int id); public String getName(); public List getChannels(); diff --git a/src/main/java/org/vanbest/xmltv/EPGSourceFactory.java b/src/main/java/org/vanbest/xmltv/EPGSourceFactory.java index 9634b17..7a78bc8 100644 --- a/src/main/java/org/vanbest/xmltv/EPGSourceFactory.java +++ b/src/main/java/org/vanbest/xmltv/EPGSourceFactory.java @@ -1,18 +1,58 @@ package org.vanbest.xmltv; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Constructor; +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.Map; +import java.util.Properties; public class EPGSourceFactory { - - public final static int CHANNEL_SOURCE_TVGIDS=1; - public final static int CHANNEL_SOURCE_RTL=2; - - private final static int[] CHANNEL_IDS={CHANNEL_SOURCE_TVGIDS, CHANNEL_SOURCE_RTL}; - private final static String[] CHANNEL_SOURCE_NAMES={"tvgids.nl", "rtl.nl"}; - private static Map channelSourceNameMap = new HashMap(); - + private static Map ids = new HashMap(); + private static Map> classes = new HashMap>(); + private static Map names = new HashMap(); + private static boolean initialised=false; + + 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"); + try { + configProp.load(in); + } catch (IOException e) { + e.printStackTrace(); + } + for(int source=1; ; source++) { + String name = configProp.getProperty("org.vanbest.xmltv.epgsource.impl."+source); + if (name==null) break; + try { + Class clazz = (Class) loader.loadClass(name); + classes.put(source, clazz); + Method getName=clazz.getMethod("getName"); + String sourceName=(String)getName.invoke(null); + names.put(source,sourceName); + ids.put(sourceName,source); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ReflectiveOperationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + initialised=true; + } + private EPGSourceFactory() { + init(); } public static EPGSourceFactory newInstance() { @@ -20,37 +60,52 @@ public class EPGSourceFactory { } public EPGSource createEPGSource(int source, Config config) { - switch(source) { - case EPGSourceFactory.CHANNEL_SOURCE_RTL: - return new RTL(config, false); - case EPGSourceFactory.CHANNEL_SOURCE_TVGIDS: - return new TvGids(config); - default: - return null; + Constructor constructor; + try { + constructor = classes.get(source).getConstructor(Config.class); + return constructor.newInstance(source, config); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } + return null; } public EPGSource createEPGSource(String source, Config config) { - int sourceId = EPGSourceFactory.getChannelSourceId(source); + int sourceId = getChannelSourceId(source); return createEPGSource(sourceId, config); } - public static String getChannelSourceName(int id) { - return CHANNEL_SOURCE_NAMES[id-1]; + public String getChannelSourceName(int id) { + return names.get(id); } - public static int getChannelSourceId(String name) { - if (channelSourceNameMap.isEmpty()) { - int i=1; - for (String s: EPGSourceFactory.CHANNEL_SOURCE_NAMES) { - EPGSourceFactory.channelSourceNameMap.put(s, i); - i++; - } - } - return EPGSourceFactory.channelSourceNameMap.get(name); + public int getChannelSourceId(String name) { + return ids.get(name); } - + public int[] getAll() { - return CHANNEL_IDS; + Integer[] list = (Integer[]) classes.keySet().toArray(); + Arrays.sort(list); + int[] result = new int[list.length]; + for(int i=0; i xmlKeyMap = new HashMap(); - private ProgrammeCache cache; + //Map xmlKeyMap = new HashMap(); class RTLException extends Exception { public RTLException(String s) { @@ -76,53 +72,14 @@ public class RTL extends AbstractEPGSource implements EPGSource { } } - public RTL(Config config, boolean useDB) { + public RTL(Config config) { super(config); - cache = new ProgrammeCache(config); - try { - if (useDB) { - Properties dbProp = new Properties(); - try { - InputStream in = new FileInputStream("tv_grab_nl_java.db.properties"); - dbProp.load(in); - } catch (IOException e) { - e.printStackTrace(); - } - db = DriverManager.getConnection(dbProp.getProperty("db_url"), dbProp.getProperty("db_user"), dbProp.getProperty("db_passwd")); - Statement stat = db.createStatement(); - StringBuilder s = new StringBuilder(); - s.append("CREATE TABLE IF NOT EXISTS prog (id VARCHAR(32) primary key, "); - int i=0; - for( String key: xmlKeys) { - if(i>0) s.append(", "); - xmlKeyMap.put(key, i+1); - s.append(key); - s.append(" TEXT"); - i++; - } - s.append(");"); - System.out.println(s); - stat.execute(s.toString()); - stat.execute("TRUNCATE TABLE prog"); - } else { - db = null; - } - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - System.exit(1); - db = null; - } } - - public int getId() { - return EPGSourceFactory.CHANNEL_SOURCE_RTL; - } - public String getName() { - return EPGSourceFactory.getChannelSourceName(getId()); - } - + public String getName() { + return "rtl.nl"; + } + public List getChannels() { List result = new ArrayList(10); @@ -149,7 +106,7 @@ public class RTL extends AbstractEPGSource implements EPGSource { String name = (String) j.get(0); String icon = icon_url+id+".gif"; - Channel c = Channel.getChannel(EPGSourceFactory.CHANNEL_SOURCE_RTL, id, name, icon); + Channel c = Channel.getChannel(getId(), id, name, icon); result.add(c); } @@ -204,25 +161,6 @@ public class RTL extends AbstractEPGSource implements EPGSource { if (root.hasAttributes()) { System.out.println("Unknown attributes for RTL detail root node"); } - PreparedStatement stat = null; - if (db != null) { - StringBuilder sql = new StringBuilder("INSERT INTO prog (id"); - StringBuilder sql2= new StringBuilder(") values (?"); - for(String key:xmlKeys) { - sql.append(","); - sql.append(key); - sql2.append(","); - sql2.append("?"); - } - sql.append(sql2); - sql.append(");"); - // System.out.println(sql.toString()); - stat = db.prepareStatement(sql.toString()); - stat.setString(1, id); - for(String key:xmlKeys) { - - } - } NodeList nodes = root.getChildNodes(); for( int i=0; i result = new LinkedList(); Map channelMap = new HashMap(); for(Channel c: channels) { - if (c.enabled && c.source==EPGSourceFactory.CHANNEL_SOURCE_RTL) channelMap.put(c.id, c); + if (c.enabled && c.source==getId()) channelMap.put(c.id, c); } URL url = programmeUrl(day); //String xmltext = fetchURL(url); @@ -373,8 +304,8 @@ public class RTL extends AbstractEPGSource implements EPGSource { public void close() throws FileNotFoundException, IOException { super.close(); - cache.close(); } + private Date parseTime(Date date, String time) { Calendar result = Calendar.getInstance(); result.setTime(date); @@ -404,7 +335,7 @@ public class RTL extends AbstractEPGSource implements EPGSource { */ public static void main(String[] args) { Config config = Config.getDefaultConfig(); - RTL rtl = new RTL(config, false); + RTL rtl = new RTL(config); try { List 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 3668db2..92d2239 100644 --- a/src/main/java/org/vanbest/xmltv/TvGids.java +++ b/src/main/java/org/vanbest/xmltv/TvGids.java @@ -62,14 +62,10 @@ public class TvGids extends AbstractEPGSource implements EPGSource { super(config); } - public int getId() { - return EPGSourceFactory.CHANNEL_SOURCE_TVGIDS; - } + public String getName() { + return "tvgids.nl"; + } - public String getName() { - return EPGSourceFactory.getChannelSourceName(getId()); - } - public static URL programmeUrl(List channels, int day) throws Exception { StringBuilder s = new StringBuilder(programme_base_url); if (channels.size() < 1) { @@ -166,7 +162,7 @@ public class TvGids extends AbstractEPGSource implements EPGSource { int id = zender.getInt("id"); String name = org.apache.commons.lang.StringEscapeUtils.unescapeHtml(zender.getString("name")); String icon = "http://tvgidsassets.nl/img/channels/53x27/" + id + ".png"; - Channel c = Channel.getChannel(EPGSourceFactory.CHANNEL_SOURCE_TVGIDS, Integer.toString(id), name, icon); + Channel c = Channel.getChannel(getId(), Integer.toString(id), name, icon); result.add(c); } diff --git a/src/main/resources/tv_grab_nl_java.properties b/src/main/resources/tv_grab_nl_java.properties index fb70c7b..2efb1d9 100644 --- a/src/main/resources/tv_grab_nl_java.properties +++ b/src/main/resources/tv_grab_nl_java.properties @@ -1,4 +1,6 @@ project.version=${project.version} project.name=${project.name} project.description=${project.description} -build.time=${build.time} \ No newline at end of file +build.time=${build.time} +org.vanbest.xmltv.epgsource.impl.1=org.vanbest.xmltv.TvGids +org.vanbest.xmltv.epgsource.impl.2=org.vanbest.xmltv.RTL \ No newline at end of file -- 2.39.5