Skip to content

Commit

Permalink
Extend google news
Browse files Browse the repository at this point in the history
  • Loading branch information
Twb-foreach authored and mkurz committed Jul 5, 2018
1 parent d52043d commit 1ceac7f
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ public String getXmlNamespaces() {
public void render(GoogleNewsSitemapUrl url, StringBuilder sb, W3CDateFormat dateFormat) {
StringBuilder tagSb = new StringBuilder();
tagSb.append(" <news:news>\n");
tagSb.append(" <news:publication>\n");
renderSubTag(tagSb, "news", "name", url.getPublication().getName());
renderSubTag(tagSb, "news", "language", url.getPublication().getLanguage());
tagSb.append(" </news:publication>\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(" </news:news>\n");
super.render(url, sb, dateFormat, tagSb.toString());
Expand Down
96 changes: 83 additions & 13 deletions src/main/java/com/redfin/sitemapgenerator/GoogleNewsSitemapUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<GoogleNewsSitemapUrl, Options> {
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 */
Expand All @@ -41,42 +61,73 @@ public Options keywords(String keywords) {

/** Specifies a list of comma-delimited keywords */
public Options keywords(Iterable<String> keywords) {
this.keywords = getListAsCommaSeparatedString(keywords);
return this;
}

public Options genres(String genres) {
this.genres = genres;
return this;
}

public Options genres(Iterable<String> genres) {
this.genres = getListAsCommaSeparatedString(genres);
return this;
}

private String getListAsCommaSeparatedString(Iterable<String> 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 */
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 */
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 */
Expand All @@ -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;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:news=\"http://www.google.com/schemas/sitemap-news/0.9\" >\n" +
" <url>\n" +
" <loc>http://www.example.com/index.html</loc>\n" +
" <news:news>\n" +
" <news:publication_date>1970-01-01T00:00:00Z</news:publication_date>\n" +
" <news:publication>\n" +
" <news:name>The Example Times</news:name>\n" +
" <news:language>en</news:language>\n" +
" </news:publication>\n" +
" <news:publication_date>1970-01-01T00:00:00Z</news:publication_date>\n" +
" <news:title>Example Title</news:title>\n" +
" </news:news>\n" +
" </url>\n" +
"</urlset>";
Expand All @@ -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);
Expand All @@ -67,14 +69,47 @@ public void testKeywords() throws Exception {
" <url>\n" +
" <loc>http://www.example.com/index.html</loc>\n" +
" <news:news>\n" +
" <news:publication_date>1970-01-01T00:00:00Z</news:publication_date>\n" +
" <news:publication>\n" +
" <news:name>The Example Times</news:name>\n" +
" <news:language>en</news:language>\n" +
" </news:publication>\n" +
" <news:publication_date>1970-01-01T00:00:00Z</news:publication_date>\n" +
" <news:title>Example Title</news:title>\n" +
" <news:keywords>Klaatu, Barrata, Nicto</news:keywords>\n" +
" </news:news>\n" +
" </url>\n" +
"</urlset>";
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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:news=\"http://www.google.com/schemas/sitemap-news/0.9\" >\n" +
" <url>\n" +
" <loc>http://www.example.com/index.html</loc>\n" +
" <news:news>\n" +
" <news:publication>\n" +
" <news:name>The Example Times</news:name>\n" +
" <news:language>en</news:language>\n" +
" </news:publication>\n" +
" <news:genres>persbericht</news:genres>\n" +
" <news:publication_date>1970-01-01T00:00:00Z</news:publication_date>\n" +
" <news:title>Example Title</news:title>\n" +
" </news:news>\n" +
" </url>\n" +
"</urlset>";
String sitemap = writeSingleSiteMap(wsg);
assertEquals(expected, sitemap);
}

private String writeSingleSiteMap(GoogleNewsSitemapGenerator wsg) {
List<File> files = wsg.write();
Expand Down

0 comments on commit 1ceac7f

Please sign in to comment.