diff options
author | Michael Bien <[email protected]> | 2010-05-17 00:20:35 +0200 |
---|---|---|
committer | Michael Bien <[email protected]> | 2010-05-17 00:20:35 +0200 |
commit | 2dbf7cb31149942d95b523ba7fc6dc5407ca48cb (patch) | |
tree | a382b16584c13967e72baeae9759d87ac05589fb /src/main/java/com | |
parent | 94c3621743fa49b05a5a68d931d6d8b3b6398cd9 (diff) |
switched to freemarker template engine.
Diffstat (limited to 'src/main/java/com')
-rw-r--r-- | src/main/java/com/jogamp/hungryharry/Config.java | 34 | ||||
-rw-r--r-- | src/main/java/com/jogamp/hungryharry/FeedAggregator.java | 62 | ||||
-rw-r--r-- | src/main/java/com/jogamp/hungryharry/config.xml | 6 |
3 files changed, 65 insertions, 37 deletions
diff --git a/src/main/java/com/jogamp/hungryharry/Config.java b/src/main/java/com/jogamp/hungryharry/Config.java index 9b69510..33fcf52 100644 --- a/src/main/java/com/jogamp/hungryharry/Config.java +++ b/src/main/java/com/jogamp/hungryharry/Config.java @@ -4,8 +4,6 @@ package com.jogamp.hungryharry; import java.util.List; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @@ -13,7 +11,7 @@ import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.XmlValue; /** - * Hungry Harrie's configuration. + * Hungry Harry's configuration. * @author Michael Bien */ @XmlType(name = "") @@ -84,7 +82,7 @@ public class Config { public final String link; @XmlElement(name="feed") - public final List<String> feeds; + public final List<PlanetFeed> feeds; @XmlElement(name="template") public final String templatePath; @@ -97,5 +95,33 @@ public class Config { feeds = null; templatePath = null; } + + public String getLink() { + return link; + } + + @XmlType + public static class PlanetFeed { + + @XmlValue + public String type; + + public String getSpecificType() { + return type; + } + + public String getFeedType() { + if(type.contains("atom")) + return "atom"; + if(type.contains("rss")) + return "rss"; + return ""; + } + + public String getFileName() { + return type+".xml"; + } + + } } } diff --git a/src/main/java/com/jogamp/hungryharry/FeedAggregator.java b/src/main/java/com/jogamp/hungryharry/FeedAggregator.java index 8bfc4eb..872a598 100644 --- a/src/main/java/com/jogamp/hungryharry/FeedAggregator.java +++ b/src/main/java/com/jogamp/hungryharry/FeedAggregator.java @@ -5,7 +5,6 @@ package com.jogamp.hungryharry; import com.jogamp.hungryharry.Config.Planet; import com.sun.syndication.io.SyndFeedOutput; -import java.io.PrintWriter; import com.sun.syndication.feed.synd.SyndEntry; import com.sun.syndication.fetcher.FetcherException; import com.sun.syndication.io.FeedException; @@ -21,11 +20,15 @@ import com.sun.syndication.fetcher.FeedFetcher; import com.sun.syndication.fetcher.impl.FeedFetcherCache; import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache; import com.sun.syndication.fetcher.impl.HttpURLFeedFetcher; -import java.io.BufferedReader; +import freemarker.template.Configuration; +import freemarker.template.DefaultObjectWrapper; +import freemarker.template.Template; +import freemarker.template.TemplateException; import java.io.File; -import java.io.FileOutputStream; -import java.io.FileReader; +import java.io.FileWriter; +import java.io.Writer; import java.util.Comparator; +import java.util.HashMap; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -96,21 +99,21 @@ public class FeedAggregator { Planet planet = config.planet; String path = cutoffTail(planet.templatePath, separatorChar); - for (String feedType : planet.feeds) { + for (Planet.PlanetFeed planetFeed : planet.feeds) { try { SyndFeed feed = new SyndFeedImpl(); - feed.setFeedType(feedType); + feed.setFeedType(planetFeed.type); feed.setTitle(planet.title); feed.setDescription(planet.description); feed.setAuthor(planet.author); - feed.setLink(planet.link); + feed.setLink(planet.link+separatorChar+planetFeed.getFileName()); feed.setEntries(entries); SyndFeedOutput output = new SyndFeedOutput(); - output.output(feed, new File(path+separatorChar+feedType+".xml")); + output.output(feed, new File(path+separatorChar+planetFeed.getFileName())); } catch (IOException ex) { LOG.log(SEVERE, null, ex); @@ -120,7 +123,7 @@ public class FeedAggregator { } StringBuilder content = new StringBuilder(); - int max = 20; + int max = 10; int n = 0; for (SyndEntry entry : entries) { if(n++>max) { @@ -139,18 +142,32 @@ public class FeedAggregator { } + HashMap<String, Object> root = new HashMap<String, Object>(); + root.put("content", content.toString()); + root.put("planet", planet); + root.put("feeds", planet.feeds); + try { - StringBuilder template = readFileAsString(planet.templatePath); - replace(template, "@atom@", planet.link); - replace(template, "@rss@", planet.link); - replace(template, "@content@", content.toString()); + Configuration cfg = new Configuration(); + // Specify the data source where the template files come from. + // Here I set a file directory for it: + cfg.setDirectoryForTemplateLoading(new File("/home/mbien/streams")); + // 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("planet-template.html"); + + Writer writer = new FileWriter(new File(path+separator+"planet.html")); - FileOutputStream fos = new FileOutputStream(new File(path+separator+"planet.html")); - fos.write(template.toString().getBytes()); + + temp.process(root, writer); + writer.close(); } catch (IOException ex) { LOG.log(SEVERE, null, ex); + } catch (TemplateException ex) { + LOG.log(SEVERE, null, ex); } } @@ -164,21 +181,6 @@ public class FeedAggregator { return sb; } - private static StringBuilder readFileAsString(String filePath) throws java.io.IOException{ - StringBuilder fileData = new StringBuilder(1000); - BufferedReader reader = new BufferedReader(new FileReader(filePath)); - char[] buf = new char[1024]; - int numRead=0; - while((numRead=reader.read(buf)) != -1){ - String readData = String.valueOf(buf, 0, numRead); - fileData.append(readData); - buf = new char[1024]; - } - reader.close(); - return fileData; - } - - public static void main(String[] args) throws MalformedURLException { new FeedAggregator().aggregate(); diff --git a/src/main/java/com/jogamp/hungryharry/config.xml b/src/main/java/com/jogamp/hungryharry/config.xml index 2fddf55..3b5294c 100644 --- a/src/main/java/com/jogamp/hungryharry/config.xml +++ b/src/main/java/com/jogamp/hungryharry/config.xml @@ -2,9 +2,9 @@ <config> <planet title="JogAmp Streams" - description="JogAmp Aggregated Feeds" - author="Hungry Harry" - link="http://jogamp.org/stream"> + description="JogAmp Aggregated Feeds" + author="Hungry Harry" + link="/home/mbien/streams"> <feed>atom_0.3</feed> <feed>rss_2.0</feed> <template>/home/mbien/streams/planet-template.html</template> |