Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Required fields Google News #35

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.dfabulich</groupId>
<groupId>com.github.TomWalbers</groupId>
<artifactId>sitemapgen4j</artifactId>
<packaging>jar</packaging>
<version>1.0.7-SNAPSHOT</version>
<name>SitemapGen4J</name>
<url>https://github.com/dfabulich/sitemapgen4j/</url>
<url>https://github.com/TomWalbers/sitemapgen4j/</url>
<description>SitemapGen4j is an XML sitemap generator written in Java.</description>
<licenses>
<license>
Expand All @@ -16,9 +16,9 @@
</license>
</licenses>
<scm>
<connection>scm:git:git://github.com:dfabulich/sitemapgen4j.git</connection>
<developerConnection>scm:git:git@github.com:dfabulich/sitemapgen4j.git</developerConnection>
<url>https://github.com/dfabulich/sitemapgen4j/</url>
<connection>scm:git:git://github.com:TomWalbers/sitemapgen4j.git</connection>
<developerConnection>scm:git:git@github.com:TomWalbers/sitemapgen4j.git</developerConnection>
<url>https://github.com/TomWalbers/sitemapgen4j/</url>
</scm>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ public void renderTag(StringBuilder sb, String namespace, String tagName, Object
sb.append(">\n");
}

public void renderSubTag(StringBuilder sb, String namespace, String tagName, Object value) {
sb.append(" ");
renderTag(sb, namespace, tagName, value);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.redfin.sitemapgenerator;

/**
* @author Tom
* @since 23/05/2017
*/
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 @@ -6,106 +6,121 @@

/**
* Builds a sitemap for Google News. To configure options, use {@link #builder(URL, File)}
*
* @author Dan Fabulich
* @see <a href="http://www.google.com/support/news_pub/bin/answer.py?answer=74288">Creating a News Sitemap</a>
*/
public class GoogleNewsSitemapGenerator extends SitemapGenerator<GoogleNewsSitemapUrl,GoogleNewsSitemapGenerator> {

/** 1000 URLs max in a Google News sitemap. */
public static final int MAX_URLS_PER_SITEMAP = 1000;

/** Configures a builder so you can specify sitemap generator options
*
* @param baseUrl All URLs in the generated sitemap(s) should appear under this base URL
* @param baseDir Sitemap files will be generated in this directory as either "sitemap.xml" or "sitemap1.xml" "sitemap2.xml" and so on.
* @return a builder; call .build() on it to make a sitemap generator
*/
public static SitemapGeneratorBuilder<GoogleNewsSitemapGenerator> builder(URL baseUrl, File baseDir) {
SitemapGeneratorBuilder<GoogleNewsSitemapGenerator> builder =
new SitemapGeneratorBuilder<GoogleNewsSitemapGenerator>(baseUrl, baseDir, GoogleNewsSitemapGenerator.class);
builder.maxUrls = 1000;
return builder;
}

/** Configures a builder so you can specify sitemap generator options
*
* @param baseUrl All URLs in the generated sitemap(s) should appear under this base URL
* @param baseDir Sitemap files will be generated in this directory as either "sitemap.xml" or "sitemap1.xml" "sitemap2.xml" and so on.
* @return a builder; call .build() on it to make a sitemap generator
*/
public static SitemapGeneratorBuilder<GoogleNewsSitemapGenerator> builder(String baseUrl, File baseDir) throws MalformedURLException {
SitemapGeneratorBuilder<GoogleNewsSitemapGenerator> builder =
new SitemapGeneratorBuilder<GoogleNewsSitemapGenerator>(baseUrl, baseDir, GoogleNewsSitemapGenerator.class);
builder.maxUrls = GoogleNewsSitemapGenerator.MAX_URLS_PER_SITEMAP;
return builder;
}

GoogleNewsSitemapGenerator(AbstractSitemapGeneratorOptions<?> options) {
super(options, new Renderer());
if (options.maxUrls > GoogleNewsSitemapGenerator.MAX_URLS_PER_SITEMAP) {
throw new RuntimeException("Google News sitemaps can have only 1000 URLs per sitemap: " + options.maxUrls);
}
}

/** Configures the generator with a base URL and directory to write the sitemap files.
*
* @param baseUrl All URLs in the generated sitemap(s) should appear under this base URL
* @param baseDir Sitemap files will be generated in this directory as either "sitemap.xml" or "sitemap1.xml" "sitemap2.xml" and so on.
* @throws MalformedURLException
*/
public GoogleNewsSitemapGenerator(String baseUrl, File baseDir)
throws MalformedURLException {
this(new SitemapGeneratorOptions(baseUrl, baseDir));
}

/** Configures the generator with a base URL and directory to write the sitemap files.
*
* @param baseUrl All URLs in the generated sitemap(s) should appear under this base URL
* @param baseDir Sitemap files will be generated in this directory as either "sitemap.xml" or "sitemap1.xml" "sitemap2.xml" and so on.
*/
public GoogleNewsSitemapGenerator(URL baseUrl, File baseDir) {
this(new SitemapGeneratorOptions(baseUrl, baseDir));
}

/**Configures the generator with a base URL and a null directory. The object constructed
* is not intended to be used to write to files. Rather, it is intended to be used to obtain
* XML-formatted strings that represent sitemaps.
*
* @param baseUrl All URLs in the generated sitemap(s) should appear under this base URL
*/
public GoogleNewsSitemapGenerator(String baseUrl) throws MalformedURLException {
this(new SitemapGeneratorOptions(new URL(baseUrl)));
}

/**Configures the generator with a base URL and a null directory. The object constructed
* is not intended to be used to write to files. Rather, it is intended to be used to obtain
* XML-formatted strings that represent sitemaps.
*
* @param baseUrl All URLs in the generated sitemap(s) should appear under this base URL
*/
public GoogleNewsSitemapGenerator(URL baseUrl) {
this(new SitemapGeneratorOptions(baseUrl));
}

private static class Renderer extends AbstractSitemapUrlRenderer<GoogleNewsSitemapUrl> implements ISitemapUrlRenderer<GoogleNewsSitemapUrl> {

public Class<GoogleNewsSitemapUrl> getUrlClass() {
return GoogleNewsSitemapUrl.class;
}

public String getXmlNamespaces() {
return "xmlns:news=\"http://www.google.com/schemas/sitemap-news/0.9\"";
}

public void render(GoogleNewsSitemapUrl url, StringBuilder sb, W3CDateFormat dateFormat) {
StringBuilder tagSb = new StringBuilder();
tagSb.append(" <news:news>\n");
renderTag(tagSb, "news", "publication_date", dateFormat.format(url.getPublicationDate()));
renderTag(tagSb, "news", "keywords", url.getKeywords());
tagSb.append(" </news:news>\n");
super.render(url, sb, dateFormat, tagSb.toString());
}

}
public class GoogleNewsSitemapGenerator extends SitemapGenerator<GoogleNewsSitemapUrl, GoogleNewsSitemapGenerator> {

/**
* 1000 URLs max in a Google News sitemap.
*/
public static final int MAX_URLS_PER_SITEMAP = 1000;

GoogleNewsSitemapGenerator(AbstractSitemapGeneratorOptions<?> options) {
super(options, new Renderer());
if (options.maxUrls > GoogleNewsSitemapGenerator.MAX_URLS_PER_SITEMAP) {
throw new RuntimeException("Google News sitemaps can have only 1000 URLs per sitemap: " + options.maxUrls);
}
}

/**
* Configures the generator with a base URL and directory to write the sitemap files.
*
* @param baseUrl All URLs in the generated sitemap(s) should appear under this base URL
* @param baseDir Sitemap files will be generated in this directory as either "sitemap.xml" or "sitemap1.xml" "sitemap2.xml" and so on.
* @throws MalformedURLException
*/
public GoogleNewsSitemapGenerator(String baseUrl, File baseDir)
throws MalformedURLException {
this(new SitemapGeneratorOptions(baseUrl, baseDir));
}

/**
* Configures the generator with a base URL and directory to write the sitemap files.
*
* @param baseUrl All URLs in the generated sitemap(s) should appear under this base URL
* @param baseDir Sitemap files will be generated in this directory as either "sitemap.xml" or "sitemap1.xml" "sitemap2.xml" and so on.
*/
public GoogleNewsSitemapGenerator(URL baseUrl, File baseDir) {
this(new SitemapGeneratorOptions(baseUrl, baseDir));
}

/**
* Configures the generator with a base URL and a null directory. The object constructed
* is not intended to be used to write to files. Rather, it is intended to be used to obtain
* XML-formatted strings that represent sitemaps.
*
* @param baseUrl All URLs in the generated sitemap(s) should appear under this base URL
*/
public GoogleNewsSitemapGenerator(String baseUrl) throws MalformedURLException {
this(new SitemapGeneratorOptions(new URL(baseUrl)));
}

/**
* Configures the generator with a base URL and a null directory. The object constructed
* is not intended to be used to write to files. Rather, it is intended to be used to obtain
* XML-formatted strings that represent sitemaps.
*
* @param baseUrl All URLs in the generated sitemap(s) should appear under this base URL
*/
public GoogleNewsSitemapGenerator(URL baseUrl) {
this(new SitemapGeneratorOptions(baseUrl));
}

/**
* Configures a builder so you can specify sitemap generator options
*
* @param baseUrl All URLs in the generated sitemap(s) should appear under this base URL
* @param baseDir Sitemap files will be generated in this directory as either "sitemap.xml" or "sitemap1.xml" "sitemap2.xml" and so on.
* @return a builder; call .build() on it to make a sitemap generator
*/
public static SitemapGeneratorBuilder<GoogleNewsSitemapGenerator> builder(URL baseUrl, File baseDir) {
SitemapGeneratorBuilder<GoogleNewsSitemapGenerator> builder =
new SitemapGeneratorBuilder<GoogleNewsSitemapGenerator>(baseUrl, baseDir, GoogleNewsSitemapGenerator.class);
builder.maxUrls = 1000;
return builder;
}

/**
* Configures a builder so you can specify sitemap generator options
*
* @param baseUrl All URLs in the generated sitemap(s) should appear under this base URL
* @param baseDir Sitemap files will be generated in this directory as either "sitemap.xml" or "sitemap1.xml" "sitemap2.xml" and so on.
* @return a builder; call .build() on it to make a sitemap generator
*/
public static SitemapGeneratorBuilder<GoogleNewsSitemapGenerator> builder(String baseUrl, File baseDir) throws MalformedURLException {
SitemapGeneratorBuilder<GoogleNewsSitemapGenerator> builder =
new SitemapGeneratorBuilder<GoogleNewsSitemapGenerator>(baseUrl, baseDir, GoogleNewsSitemapGenerator.class);
builder.maxUrls = GoogleNewsSitemapGenerator.MAX_URLS_PER_SITEMAP;
return builder;
}

private static class Renderer extends AbstractSitemapUrlRenderer<GoogleNewsSitemapUrl> implements ISitemapUrlRenderer<GoogleNewsSitemapUrl> {

public Class<GoogleNewsSitemapUrl> getUrlClass() {
return GoogleNewsSitemapUrl.class;
}

public String getXmlNamespaces() {
return "xmlns:news=\"http://www.google.com/schemas/sitemap-news/0.9\"";
}

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());
}

}

}