Extend google news
This commit is contained in:
@@ -43,4 +43,10 @@ abstract class AbstractSitemapUrlRenderer<T extends WebSitemapUrl> implements IS
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -100,7 +100,13 @@ public class GoogleNewsSitemapGenerator extends SitemapGenerator<GoogleNewsSitem
|
||||
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());
|
||||
|
||||
@@ -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 */
|
||||
@@ -41,18 +61,32 @@ public class GoogleNewsSitemapUrl extends WebSitemapUrl {
|
||||
|
||||
/** 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 */
|
||||
@@ -60,16 +94,30 @@ public class GoogleNewsSitemapUrl extends WebSitemapUrl {
|
||||
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 class GoogleNewsSitemapUrl extends WebSitemapUrl {
|
||||
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 class GoogleNewsSitemapUrl extends WebSitemapUrl {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -6,9 +6,6 @@ import java.util.List;
|
||||
|
||||
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 class GoogleNewsSitemapUrlTest extends TestCase {
|
||||
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>";
|
||||
@@ -58,7 +60,7 @@ public class GoogleNewsSitemapUrlTest extends TestCase {
|
||||
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 class GoogleNewsSitemapUrlTest extends TestCase {
|
||||
" <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" +
|
||||
@@ -75,6 +82,34 @@ public class GoogleNewsSitemapUrlTest extends TestCase {
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user