diff --git a/src/fr/devinsy/util/rss/RSSCache.java b/src/fr/devinsy/util/rss/RSSCache.java new file mode 100644 index 0000000..cd6ef51 --- /dev/null +++ b/src/fr/devinsy/util/rss/RSSCache.java @@ -0,0 +1,173 @@ +package fr.devinsy.util.rss; + +import java.util.HashMap; +import java.util.Locale; + +/** + * @author christian.momon@devinsy.fr, 2013, copyright. + * + * This file is free software under the terms of the GNU Library General + * Public License as published by the Free Software Foundation version 3 + * or any later version. + */ +public class RSSCache +{ + private static RSSCache instance = new RSSCache(); + private HashMap cache; + + /** + * + */ + private RSSCache() + { + this.cache = new HashMap(); + } + + /** + * + * @param key + * @param locale + * @return + */ + public String get(final String name) + { + return get(name, Locale.ROOT); + } + + /** + * + * @param name + * @param locale + * @return + */ + public String get(final String name, final Locale locale) + { + String result; + + if (name == null) + { + throw new NullPointerException("name is null."); + } + else if (locale == null) + { + result = get(name, Locale.ROOT); + } + else + { + result = this.cache.get(key(name, locale)); + + if (result == null) + { + result = this.cache.get(name); + } + } + + return result; + } + + /** + * + * @param name + * @param locale + * @return + */ + private String key(final String name, final Locale locale) + { + String result; + + if (locale.getLanguage().length() == 0) + { + result = name; + } + else + { + result = name + "_" + locale.getLanguage(); + } + + // + return result; + } + + /** + * + * @param key + * @param locale + * @param rss + */ + public void put(final String name, final Locale locale, final String rss) + { + if (name == null) + { + throw new NullPointerException("name is null."); + } + else if (locale == null) + { + put(name, Locale.ROOT, rss); + } + else + { + this.cache.put(key(name, locale), rss); + } + } + + /** + * + * @param name + * @param locale + * @param rss + */ + public void put(final String name, final String rss) + { + put(name, Locale.ROOT, rss); + } + + /** + * + * @param name + */ + public void remove(final String name) + { + remove(name, Locale.ROOT); + } + + /** + * + * @param name + */ + public void remove(final String name, final Locale locale) + { + if (name == null) + { + throw new NullPointerException("key is null."); + } + else + { + this.cache.remove(name + "-" + locale.getLanguage()); + } + } + + /** + * + * @param key + */ + public void setOudated(final String name) + { + for (String subkey : this.cache.keySet()) + { + if (subkey.startsWith(name)) + { + this.cache.remove(subkey); + } + } + } + + /** + * + * @return + */ + public static RSSCache instance() + { + return instance; + } + +} diff --git a/src/fr/devinsy/util/rss/RSSElement.java b/src/fr/devinsy/util/rss/RSSElement.java new file mode 100644 index 0000000..7b8b73f --- /dev/null +++ b/src/fr/devinsy/util/rss/RSSElement.java @@ -0,0 +1,139 @@ +package fr.devinsy.util.rss; + +import org.joda.time.DateTime; + +/** + * @author christian.momon@devinsy.fr, 2013, copyright. + * + * This file is free software under the terms of the GNU Library General + * Public License as published by the Free Software Foundation version 3 + * or any later version. + */ +public class RSSElement +{ + public static final String DATE_PATTERN = "dd MMM YYYY hh:mm:ss Z"; + private String name; + private String value; + private String[] attributes; + + /** + * + * @param name + * @param value + */ + public RSSElement(final String name, final DateTime value) + { + setName(name); + if (value == null) + { + this.value = null; + } + else + { + this.value = value.toString(DATE_PATTERN); + } + this.attributes = null; + } + + /** + * + * @param name + * @param value + */ + public RSSElement(final String name, final long value) + { + setName(name); + this.value = String.valueOf(value); + this.attributes = null; + } + + /** + * + * @param name + * @param value + */ + public RSSElement(final String name, final long value, final String... attributes) + { + setName(name); + this.value = String.valueOf(value); + this.attributes = attributes; + } + + /** + * + * @param name + * @param value + */ + public RSSElement(final String name, final String value) + { + setName(name); + this.value = value; + this.attributes = null; + } + + /** + * + * @param name + * @param value + */ + public RSSElement(final String name, final String value, final String... attributes) + { + setName(name); + this.value = value; + this.attributes = attributes; + } + + public String[] getAttributes() + { + return attributes; + } + + /** + * + * @return + */ + public String getName() + { + return name; + } + + /** + * + * @return + */ + public String getValue() + { + return value; + } + + public void setAttributes(final String[] attributes) + { + this.attributes = attributes; + } + + /** + * + * @param name + */ + public void setName(final String name) + { + if (name == null) + { + throw new NullPointerException("name is null"); + } + else + { + this.name = name; + } + } + + /** + * + * @param value + */ + public void setValue(final String value) + { + this.value = value; + } + +} diff --git a/src/fr/devinsy/util/rss/RSSWriter.java b/src/fr/devinsy/util/rss/RSSWriter.java new file mode 100644 index 0000000..8c60ffb --- /dev/null +++ b/src/fr/devinsy/util/rss/RSSWriter.java @@ -0,0 +1,157 @@ +package fr.devinsy.util.rss; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.io.Writer; + +import fr.devinsy.util.xml.XMLWriter; + +/** + * @author christian.momon@devinsy.fr, 2013, copyright. + * + * This file is free software under the terms of the GNU Library General + * Public License as published by the Free Software Foundation version 3 + * or any later version. + */ +public class RSSWriter +{ + private XMLWriter out; + + /** + * + * @param file + * @throws FileNotFoundException + * @throws UnsupportedEncodingException + */ + public RSSWriter(final File file) throws UnsupportedEncodingException, FileNotFoundException + { + this.out = new XMLWriter(file); + writeRSSHeader(); + } + + /** + * + * @param target + * @throws UnsupportedEncodingException + */ + public RSSWriter(final OutputStream target) throws UnsupportedEncodingException + { + this.out = new XMLWriter(target); + writeRSSHeader(); + } + + /** + * + * @param target + * @throws UnsupportedEncodingException + */ + public RSSWriter(final Writer target) throws UnsupportedEncodingException + { + this.out = new XMLWriter(target); + writeRSSHeader(); + } + + /** + * + */ + public void close() throws IOException + { + if (this.out != null) + { + this.out.writeEndTag("channel"); + this.out.writeEndTag("rss"); + this.out.close(); + } + } + + /** + * + */ + public void flush() throws IOException + { + if (this.out != null) + { + this.out.flush(); + } + } + + /** + * + */ + public void writeChannel(final String title, final String link, final String description, final RSSElement... elements) + { + // + this.out.writeStartTag("channel"); + + // + this.out.writeTag("title", title); + this.out.writeTag("link", link); + this.out.writeTag("description", description); + + // + if ((elements != null) && (elements.length > 0)) + { + for (RSSElement element : elements) + { + out.writeTag(element.getName(), element.getValue(), element.getAttributes()); + } + } + } + + /** + * + * @param comment + */ + public void writeComment(final String comment) + { + this.out.writeComment(comment); + } + + /** + * + */ + public void writeItem(final String title, final String link, final String description, final RSSElement... elements) + { + // + this.out.writeStartTag("item"); + + // + this.out.writeTag("title", title); + if (link != null) + { + this.out.writeTag("link", link); + } + if (description == null) + { + this.out.writeTag("description", "n/a"); + } + else + { + this.out.writeTag("description", description); + } + + // + if ((elements != null) && (elements.length > 0)) + { + for (RSSElement element : elements) + { + out.writeTag(element.getName(), element.getValue(), element.getAttributes()); + } + } + + // + this.out.writeEndTag("item"); + } + + /** + * + */ + private void writeRSSHeader() + { + this.out.writeXMLHeader(); + this.out.writeStartTag("rss", "version", "2.0"); + } +}