From 307b85dc58eaed06e74472ce0f5d0d2026193053 Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Mon, 23 Aug 2010 04:13:08 +0200 Subject: continued with freemarker model. description filter. updated freemarker dependency. --- src/main/java/com/jogamp/hungryharry/Config.java | 15 +++++++ .../com/jogamp/hungryharry/FeedAggregator.java | 52 ++++++++++++++-------- src/main/java/com/jogamp/hungryharry/config.xml | 8 ++-- 3 files changed, 53 insertions(+), 22 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/jogamp/hungryharry/Config.java b/src/main/java/com/jogamp/hungryharry/Config.java index 99a8363..b80a093 100644 --- a/src/main/java/com/jogamp/hungryharry/Config.java +++ b/src/main/java/com/jogamp/hungryharry/Config.java @@ -45,6 +45,12 @@ public class Config { name = null; url = null; } + + @Override + public String toString() { + return getClass().getName() + "[" + name + ", " + url+"]"; + } + } @XmlType @@ -56,6 +62,9 @@ public class Config { @XmlAttribute(name="idpattern") public final String idpattern; + @XmlAttribute + public final String descriptionfilter; + @XmlValue public final String text; @@ -63,6 +72,12 @@ public class Config { keyword = null; text = null; idpattern = null; + descriptionfilter = null; + } + + @Override + public String toString() { + return getClass().getName() + "["+ keyword +"]"; } } diff --git a/src/main/java/com/jogamp/hungryharry/FeedAggregator.java b/src/main/java/com/jogamp/hungryharry/FeedAggregator.java index 2f5ec27..0d076d0 100644 --- a/src/main/java/com/jogamp/hungryharry/FeedAggregator.java +++ b/src/main/java/com/jogamp/hungryharry/FeedAggregator.java @@ -12,7 +12,6 @@ import com.sun.syndication.io.FeedException; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; -import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -24,7 +23,7 @@ import com.sun.syndication.fetcher.impl.FeedFetcherCache; import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache; import com.sun.syndication.fetcher.impl.HttpURLFeedFetcher; import freemarker.template.Configuration; -import freemarker.template.DefaultObjectWrapper; +import freemarker.template.ObjectWrapper; import freemarker.template.Template; import freemarker.template.TemplateException; import java.io.File; @@ -52,7 +51,7 @@ import static java.io.File.*; * */ public class FeedAggregator { - + private static final Logger LOG = Logger.getLogger(FeedAggregator.class.getName()); private final String configFile; @@ -60,7 +59,7 @@ public class FeedAggregator { this.configFile = configFile; } - private void aggregate() throws MalformedURLException { + private void aggregate() { Config config = null; try { @@ -72,15 +71,17 @@ public class FeedAggregator { } List feeds = config.feed; - List entries = loadFeeds(feeds); + List entries = downloadFeeds(feeds); Planet planet = config.planet; new File(planet.outputFolder).mkdirs(); createAggregatedFeed(planet, entries); - StringBuilder content = new StringBuilder(); + List> aggregatedEntries = new ArrayList>(entries.size()); int n = 0; + + for (SyndEntry entry : entries) { if(n++ >= planet.maxEntries) { break; @@ -88,22 +89,35 @@ public class FeedAggregator { String link = entry.getLink(); for (Config.Template template : config.template) { if(link.contains(template.keyword)) { - Pattern pattern = Pattern.compile(template.idpattern); - Matcher matcher = pattern.matcher(link); + Matcher matcher = Pattern.compile(template.idpattern).matcher(link); matcher.find(); - content.append(template.text.replaceAll("#id#", matcher.group(1))); + String playercode = template.text.replaceAll("#id#", matcher.group(1)); + + Map map = new HashMap(); + map.put("player", playercode); + + String filteredDescription = entry.getDescription().getValue(); + if(template.descriptionfilter != null) { + Pattern descPattern = Pattern.compile(template.descriptionfilter); + Matcher filter = descPattern.matcher(filteredDescription); + filter.find(); + filteredDescription = filter.group(1); + } + map.put("description", filteredDescription); + + aggregatedEntries.add(map); break; } } } - generatePage(content.toString(), planet); + generatePage(aggregatedEntries, planet); } - private void generatePage(String content, Planet planet) { + private void generatePage(List> entries, Planet planet) { Map root = new HashMap(); - root.put("content", content); + root.put("entries", entries); root.put("planet", planet); root.put("feeds", planet.feeds); @@ -117,10 +131,11 @@ public class FeedAggregator { cfg.setDirectoryForTemplateLoading(new File(templateFolder)); // Specify how templates will see the data-model. This is an advanced topic... // but just use this: - cfg.setObjectWrapper(new DefaultObjectWrapper()); - Template temp = cfg.getTemplate(templateName); + cfg.setObjectWrapper(ObjectWrapper.DEFAULT_WRAPPER); + + Template template = cfg.getTemplate(templateName); Writer writer = new FileWriter(new File(planet.outputFolder + separator + "planet.html")); - temp.process(root, writer); + template.process(root, writer); writer.close(); } catch (IOException ex) { LOG.log(SEVERE, null, ex); @@ -153,13 +168,14 @@ public class FeedAggregator { } } - private List loadFeeds(List feeds) throws IllegalArgumentException { + private List downloadFeeds(List feeds) throws IllegalArgumentException { FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance(); FeedFetcher feedFetcher = new HttpURLFeedFetcher(feedInfoCache); List entries = new ArrayList(); for (Config.Feed feed : feeds) { + LOG.info("downloading "+feed); try { SyndFeed inFeed = feedFetcher.retrieveFeed(new URL(feed.url)); entries.addAll(inFeed.getEntries()); @@ -171,7 +187,7 @@ public class FeedAggregator { LOG.log(WARNING, "skipping feed", ex); } } - + sort(entries, new Comparator() { @Override public int compare(SyndEntry o1, SyndEntry o2) { @@ -203,7 +219,7 @@ public class FeedAggregator { return sb; } - public static void main(String... args) throws MalformedURLException { + public static void main(String... args) { if(args.length < 1) { System.out.println("args must contain a path to the configuration file"); diff --git a/src/main/java/com/jogamp/hungryharry/config.xml b/src/main/java/com/jogamp/hungryharry/config.xml index f34377e..311c574 100644 --- a/src/main/java/com/jogamp/hungryharry/config.xml +++ b/src/main/java/com/jogamp/hungryharry/config.xml @@ -5,11 +5,11 @@ description="JogAmp Aggregated Feeds" author="Hungry Harry" maxEntries="6" - link="/home/mbien/streams/out"> + link="/home/mbien/NetBeansProjects/JOGAMP/stream"> atom_0.3 rss_2.0 - - /home/mbien/streams/out + + /home/mbien/NetBeansProjects/JOGAMP/stream