diff --git a/src/main/java/com/redfin/sitemapgenerator/AbstractSitemapUrlRenderer.java b/src/main/java/com/redfin/sitemapgenerator/AbstractSitemapUrlRenderer.java index a8ec3b2..99be3cb 100644 --- a/src/main/java/com/redfin/sitemapgenerator/AbstractSitemapUrlRenderer.java +++ b/src/main/java/com/redfin/sitemapgenerator/AbstractSitemapUrlRenderer.java @@ -43,4 +43,10 @@ public void renderTag(StringBuilder sb, String namespace, String tagName, Object sb.append(">\n"); } + public void renderSubTag(StringBuilder sb, String namespace, String tagName, Object value) { + if (value == null) return; + sb.append(" "); + renderTag(sb, namespace, tagName, value); + } + } diff --git a/src/main/java/com/redfin/sitemapgenerator/GoogleNewsPublication.java b/src/main/java/com/redfin/sitemapgenerator/GoogleNewsPublication.java new file mode 100644 index 0000000..2418617 --- /dev/null +++ b/src/main/java/com/redfin/sitemapgenerator/GoogleNewsPublication.java @@ -0,0 +1,28 @@ +package com.redfin.sitemapgenerator; + +public class GoogleNewsPublication { + + private String name; + private String language; + + public GoogleNewsPublication(String name, String language) { + this.name = name; + this.language = language; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLanguage() { + return language; + } + + public void setLanguage(String language) { + this.language = language; + } +} diff --git a/src/main/java/com/redfin/sitemapgenerator/GoogleNewsSitemapGenerator.java b/src/main/java/com/redfin/sitemapgenerator/GoogleNewsSitemapGenerator.java index 6e88f3b..7e36a54 100644 --- a/src/main/java/com/redfin/sitemapgenerator/GoogleNewsSitemapGenerator.java +++ b/src/main/java/com/redfin/sitemapgenerator/GoogleNewsSitemapGenerator.java @@ -100,7 +100,13 @@ public String getXmlNamespaces() { public void render(GoogleNewsSitemapUrl url, StringBuilder sb, W3CDateFormat dateFormat) { StringBuilder tagSb = new StringBuilder(); tagSb.append(" \n"); + tagSb.append(" \n"); + renderSubTag(tagSb, "news", "name", url.getPublication().getName()); + renderSubTag(tagSb, "news", "language", url.getPublication().getLanguage()); + tagSb.append(" \n"); + renderTag(tagSb, "news", "genres", url.getGenres()); renderTag(tagSb, "news", "publication_date", dateFormat.format(url.getPublicationDate())); + renderTag(tagSb, "news", "title", url.getTitle()); renderTag(tagSb, "news", "keywords", url.getKeywords()); tagSb.append(" \n"); super.render(url, sb, dateFormat, tagSb.toString()); diff --git a/src/main/java/com/redfin/sitemapgenerator/GoogleNewsSitemapUrl.java b/src/main/java/com/redfin/sitemapgenerator/GoogleNewsSitemapUrl.java index f3f733f..2f73df8 100644 --- a/src/main/java/com/redfin/sitemapgenerator/GoogleNewsSitemapUrl.java +++ b/src/main/java/com/redfin/sitemapgenerator/GoogleNewsSitemapUrl.java @@ -15,22 +15,42 @@ public class GoogleNewsSitemapUrl extends WebSitemapUrl { private final Date publicationDate; private final String keywords; + private final String genres; + private final String title; + private final GoogleNewsPublication publication; /** Options to configure Google News URLs */ public static class Options extends AbstractSitemapUrlOptions { private Date publicationDate; private String keywords; + private String genres; + private String title; + private GoogleNewsPublication publication; /** Specifies an URL and publication date (which is mandatory for Google News) */ - public Options(String url, Date publicationDate) throws MalformedURLException { - this(new URL(url), publicationDate); + public Options(String url, Date publicationDate, String title, GoogleNewsPublication publication) throws MalformedURLException { + this(new URL(url), publicationDate, title, publication); } + public Options(String url, Date publicationDate, String title, String name, String language) throws MalformedURLException { + this(new URL(url), publicationDate, title, new GoogleNewsPublication(name, language)); + } + + public Options(URL url, Date publicationDate, String title, String name, String language) { + this(url, publicationDate, title, new GoogleNewsPublication(name, language)); + } + /** Specifies an URL and publication date (which is mandatory for Google News) */ - public Options(URL url, Date publicationDate) { + public Options(URL url, Date publicationDate, String title, GoogleNewsPublication publication) { super(url, GoogleNewsSitemapUrl.class); if (publicationDate == null) throw new NullPointerException("publicationDate must not be null"); this.publicationDate = publicationDate; + if (title == null) throw new NullPointerException("title must not be null"); + this.title = title; + if (publication == null) throw new NullPointerException("publication must not be null"); + if (publication.getName() == null) throw new NullPointerException("publication name must not be null"); + if (publication.getLanguage() == null) throw new NullPointerException("publication language must not be null"); + this.publication = publication; } /** Specifies a list of comma-delimited keywords */ @@ -41,18 +61,32 @@ public Options keywords(String keywords) { /** Specifies a list of comma-delimited keywords */ public Options keywords(Iterable keywords) { + this.keywords = getListAsCommaSeparatedString(keywords); + return this; + } + + public Options genres(String genres) { + this.genres = genres; + return this; + } + + public Options genres(Iterable genres) { + this.genres = getListAsCommaSeparatedString(genres); + return this; + } + + private String getListAsCommaSeparatedString(Iterable values) { StringBuilder sb = new StringBuilder(); boolean first = true; - for (String keyword : keywords) { + for (String value : values) { if (first) { first = false; } else { sb.append(", "); } - sb.append(keyword); + sb.append(value); } - this.keywords = sb.toString(); - return this; + return sb.toString(); } /** Specifies a list of comma-delimited keywords */ @@ -60,16 +94,30 @@ public Options keywords(String... keywords) { return keywords(Arrays.asList(keywords)); } + public Options genres(String... genres) { + return genres(Arrays.asList(genres)); + } + } - /** Specifies an URL and publication date (which is mandatory for Google News) */ - public GoogleNewsSitemapUrl(URL url, Date publicationDate) { - this(new Options(url, publicationDate)); + /** Specifies an URL and publication date, title and publication (which are mandatory for Google News) */ + public GoogleNewsSitemapUrl(URL url, Date publicationDate, String title, String name, String language) { + this(new Options(url, publicationDate, title, name, language)); } - /** Specifies an URL and publication date (which is mandatory for Google News) */ - public GoogleNewsSitemapUrl(String url, Date publicationDate) throws MalformedURLException { - this(new Options(url, publicationDate)); + /** Specifies an URL and publication date, title and publication (which are mandatory for Google News) */ + public GoogleNewsSitemapUrl(URL url, Date publicationDate, String title, GoogleNewsPublication publication) { + this(new Options(url, publicationDate, title, publication)); + } + + /** Specifies an URL and publication date, title and publication (which are mandatory for Google News) */ + public GoogleNewsSitemapUrl(String url, Date publicationDate, String title, String name, String language) throws MalformedURLException { + this(new Options(url, publicationDate, title, name, language)); + } + + /** Specifies an URL and publication date, title and publication (which are mandatory for Google News) */ + public GoogleNewsSitemapUrl(String url, Date publicationDate, String title, GoogleNewsPublication publication) throws MalformedURLException { + this(new Options(url, publicationDate, title, publication)); } /** Configures an URL with options */ @@ -77,6 +125,9 @@ public GoogleNewsSitemapUrl(Options options) { super(options); publicationDate = options.publicationDate; keywords = options.keywords; + genres = options.genres; + title = options.title; + publication = options.publication; } /** Retrieves the publication date */ @@ -89,7 +140,26 @@ public String getKeywords() { return keywords; } + /** + * Retrieves the Genres + */ + public String getGenres() { + return genres; + } + + /** + * Retrieves the title + */ + public String getTitle() { + return title; + } + /** + * Retrieves the publication with name and language + */ + public GoogleNewsPublication getPublication() { + return publication; + } } diff --git a/src/test/java/com/redfin/sitemapgenerator/GoogleNewsSitemapUrlTest.java b/src/test/java/com/redfin/sitemapgenerator/GoogleNewsSitemapUrlTest.java index e147f69..5ecc9e6 100644 --- a/src/test/java/com/redfin/sitemapgenerator/GoogleNewsSitemapUrlTest.java +++ b/src/test/java/com/redfin/sitemapgenerator/GoogleNewsSitemapUrlTest.java @@ -6,9 +6,6 @@ import junit.framework.TestCase; -import com.redfin.sitemapgenerator.GoogleNewsSitemapGenerator; -import com.redfin.sitemapgenerator.GoogleNewsSitemapUrl; -import com.redfin.sitemapgenerator.W3CDateFormat; import com.redfin.sitemapgenerator.W3CDateFormat.Pattern; public class GoogleNewsSitemapUrlTest extends TestCase { @@ -38,14 +35,19 @@ public void testSimpleUrl() throws Exception { dateFormat.setTimeZone(W3CDateFormat.ZULU); wsg = GoogleNewsSitemapGenerator.builder("http://www.example.com", dir) .dateFormat(dateFormat).build(); - GoogleNewsSitemapUrl url = new GoogleNewsSitemapUrl("http://www.example.com/index.html", new Date(0)); + GoogleNewsSitemapUrl url = new GoogleNewsSitemapUrl("http://www.example.com/index.html", new Date(0), "Example Title", "The Example Times", "en"); wsg.addUrl(url); String expected = "\n" + "\n" + " \n" + " http://www.example.com/index.html\n" + " \n" + - " 1970-01-01T00:00:00Z\n" + + " \n" + + " The Example Times\n" + + " en\n" + + " \n" + + " 1970-01-01T00:00:00Z\n" + + " Example Title\n" + " \n" + " \n" + ""; @@ -58,7 +60,7 @@ public void testKeywords() throws Exception { dateFormat.setTimeZone(W3CDateFormat.ZULU); wsg = GoogleNewsSitemapGenerator.builder("http://www.example.com", dir) .dateFormat(dateFormat).build(); - GoogleNewsSitemapUrl url = new GoogleNewsSitemapUrl.Options("http://www.example.com/index.html", new Date(0)) + GoogleNewsSitemapUrl url = new GoogleNewsSitemapUrl.Options("http://www.example.com/index.html", new Date(0), "Example Title", "The Example Times", "en") .keywords("Klaatu", "Barrata", "Nicto") .build(); wsg.addUrl(url); @@ -67,7 +69,12 @@ public void testKeywords() throws Exception { " \n" + " http://www.example.com/index.html\n" + " \n" + - " 1970-01-01T00:00:00Z\n" + + " \n" + + " The Example Times\n" + + " en\n" + + " \n" + + " 1970-01-01T00:00:00Z\n" + + " Example Title\n" + " Klaatu, Barrata, Nicto\n" + " \n" + " \n" + @@ -75,6 +82,34 @@ public void testKeywords() throws Exception { String sitemap = writeSingleSiteMap(wsg); assertEquals(expected, sitemap); } + + public void testGenres() throws Exception { + W3CDateFormat dateFormat = new W3CDateFormat(Pattern.SECOND); + dateFormat.setTimeZone(W3CDateFormat.ZULU); + wsg = GoogleNewsSitemapGenerator.builder("http://www.example.com", dir) + .dateFormat(dateFormat).build(); + GoogleNewsSitemapUrl url = new GoogleNewsSitemapUrl.Options("http://www.example.com/index.html", new Date(0), "Example Title", "The Example Times", "en") + .genres("persbericht") + .build(); + wsg.addUrl(url); + String expected = "\n" + + "\n" + + " \n" + + " http://www.example.com/index.html\n" + + " \n" + + " \n" + + " The Example Times\n" + + " en\n" + + " \n" + + " persbericht\n" + + " 1970-01-01T00:00:00Z\n" + + " Example Title\n" + + " \n" + + " \n" + + ""; + String sitemap = writeSingleSiteMap(wsg); + assertEquals(expected, sitemap); + } private String writeSingleSiteMap(GoogleNewsSitemapGenerator wsg) { List files = wsg.write();