added write to string functionality for sitemap generators
This commit is contained in:
parent
c24db4203b
commit
54138163ce
|
@ -16,12 +16,15 @@ abstract class AbstractSitemapGeneratorOptions<THIS extends AbstractSitemapGener
|
|||
boolean gzip = false;
|
||||
|
||||
public AbstractSitemapGeneratorOptions(URL baseUrl, File baseDir) {
|
||||
if (baseDir == null) throw new NullPointerException("baseDir may not be null");
|
||||
if (baseUrl == null) throw new NullPointerException("baseUrl may not be null");
|
||||
this.baseDir = baseDir;
|
||||
this.baseUrl = baseUrl.toString();
|
||||
}
|
||||
|
||||
public AbstractSitemapGeneratorOptions(URL baseUrl) {
|
||||
this(baseUrl, null);
|
||||
}
|
||||
|
||||
/** The prefix of the name of the sitemaps we'll create; by default this is "sitemap" */
|
||||
public THIS fileNamePrefix(String fileNamePrefix) {
|
||||
if (fileNamePrefix == null) throw new NullPointerException("fileNamePrefix may not be null");
|
||||
|
|
|
@ -1,33 +1,31 @@
|
|||
package com.redfin.sitemapgenerator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
|
||||
abstract class AbstractSitemapUrlRenderer<T extends WebSitemapUrl> implements ISitemapUrlRenderer<T> {
|
||||
|
||||
public void render(WebSitemapUrl url, OutputStreamWriter out, W3CDateFormat dateFormat, String additionalData)
|
||||
throws IOException {
|
||||
out.write(" <url>\n");
|
||||
out.write(" <loc>");
|
||||
out.write(url.getUrl().toString());
|
||||
out.write("</loc>\n");
|
||||
|
||||
public void render(WebSitemapUrl url, StringBuilder sb, W3CDateFormat dateFormat, String additionalData) {
|
||||
sb.append(" <url>\n");
|
||||
sb.append(" <loc>");
|
||||
sb.append(url.getUrl().toString());
|
||||
sb.append("</loc>\n");
|
||||
if (url.getLastMod() != null) {
|
||||
out.write(" <lastmod>");
|
||||
out.write(dateFormat.format(url.getLastMod()));
|
||||
out.write("</lastmod>\n");
|
||||
sb.append(" <lastmod>");
|
||||
sb.append(dateFormat.format(url.getLastMod()));
|
||||
sb.append("</lastmod>\n");
|
||||
}
|
||||
if (url.getChangeFreq() != null) {
|
||||
out.write(" <changefreq>");
|
||||
out.write(url.getChangeFreq().toString());
|
||||
out.write("</changefreq>\n");
|
||||
sb.append(" <changefreq>");
|
||||
sb.append(url.getChangeFreq().toString());
|
||||
sb.append("</changefreq>\n");
|
||||
}
|
||||
if (url.getPriority() != null) {
|
||||
out.write(" <priority>");
|
||||
out.write(url.getPriority().toString());
|
||||
out.write("</priority>\n");
|
||||
sb.append(" <priority>");
|
||||
sb.append(url.getPriority().toString());
|
||||
sb.append("</priority>\n");
|
||||
}
|
||||
if (additionalData != null) out.write(additionalData);
|
||||
out.write(" </url>\n");
|
||||
if (additionalData != null) {
|
||||
sb.append(additionalData);
|
||||
}
|
||||
sb.append(" </url>\n");
|
||||
}
|
||||
|
||||
public void renderTag(StringBuilder sb, String namespace, String tagName, Object value) {
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package com.redfin.sitemapgenerator;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
|
@ -37,6 +35,26 @@ public class GoogleCodeSitemapGenerator extends SitemapGenerator<GoogleCodeSitem
|
|||
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 GoogleCodeSitemapGenerator(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 GoogleCodeSitemapGenerator(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
|
||||
|
@ -63,23 +81,23 @@ public class GoogleCodeSitemapGenerator extends SitemapGenerator<GoogleCodeSitem
|
|||
public Class<GoogleCodeSitemapUrl> getUrlClass() {
|
||||
return GoogleCodeSitemapUrl.class;
|
||||
}
|
||||
|
||||
public void render(GoogleCodeSitemapUrl url, OutputStreamWriter out,
|
||||
W3CDateFormat dateFormat) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(" <codesearch:codesearch>\n");
|
||||
renderTag(sb, "codesearch", "filetype", url.getFileType());
|
||||
renderTag(sb, "codesearch", "license", url.getLicense());
|
||||
renderTag(sb, "codesearch", "filename", url.getFileName());
|
||||
renderTag(sb, "codesearch", "packageurl", url.getPackageUrl());
|
||||
renderTag(sb, "codesearch", "packagemap", url.getPackageMap());
|
||||
sb.append(" </codesearch:codesearch>\n");
|
||||
super.render(url, out, dateFormat, sb.toString());
|
||||
}
|
||||
|
||||
public String getXmlNamespaces() {
|
||||
return "xmlns:codesearch=\"http://www.google.com/codesearch/schemas/sitemap/1.0\"";
|
||||
}
|
||||
|
||||
public void render(GoogleCodeSitemapUrl url, StringBuilder sb,
|
||||
W3CDateFormat dateFormat) {
|
||||
StringBuilder tagSb = new StringBuilder();
|
||||
tagSb.append(" <codesearch:codesearch>\n");
|
||||
renderTag(tagSb, "codesearch", "filetype", url.getFileType());
|
||||
renderTag(tagSb, "codesearch", "license", url.getLicense());
|
||||
renderTag(tagSb, "codesearch", "filename", url.getFileName());
|
||||
renderTag(tagSb, "codesearch", "packageurl", url.getPackageUrl());
|
||||
renderTag(tagSb, "codesearch", "packagemap", url.getPackageMap());
|
||||
tagSb.append(" </codesearch:codesearch>\n");
|
||||
super.render(url, sb, dateFormat, tagSb.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package com.redfin.sitemapgenerator;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
|
@ -57,6 +55,27 @@ public class GoogleGeoSitemapGenerator extends SitemapGenerator<GoogleGeoSitemap
|
|||
public GoogleGeoSitemapGenerator(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 GoogleGeoSitemapGenerator(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 GoogleGeoSitemapGenerator(URL baseUrl) {
|
||||
this(new SitemapGeneratorOptions(baseUrl));
|
||||
}
|
||||
|
||||
private static class Renderer extends AbstractSitemapUrlRenderer<GoogleGeoSitemapUrl> implements ISitemapUrlRenderer<GoogleGeoSitemapUrl> {
|
||||
|
||||
|
@ -64,19 +83,17 @@ public class GoogleGeoSitemapGenerator extends SitemapGenerator<GoogleGeoSitemap
|
|||
return GoogleGeoSitemapUrl.class;
|
||||
}
|
||||
|
||||
public void render(GoogleGeoSitemapUrl url, OutputStreamWriter out,
|
||||
W3CDateFormat dateFormat) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(" <geo:geo>\n");
|
||||
sb.append(" <geo:format>"+url.getFormat()+"</geo:format>\n");
|
||||
sb.append(" </geo:geo>\n");
|
||||
super.render(url, out, dateFormat, sb.toString());
|
||||
|
||||
}
|
||||
|
||||
public String getXmlNamespaces() {
|
||||
return "xmlns:geo=\"http://www.google.com/geo/schemas/sitemap/1.0\"";
|
||||
}
|
||||
|
||||
public void render(GoogleGeoSitemapUrl url, StringBuilder sb, W3CDateFormat dateFormat) {
|
||||
StringBuilder tagSb = new StringBuilder();
|
||||
tagSb.append(" <geo:geo>\n");
|
||||
tagSb.append(" <geo:format>"+url.getFormat()+"</geo:format>\n");
|
||||
tagSb.append(" </geo:geo>\n");
|
||||
super.render(url, sb, dateFormat, tagSb.toString());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package com.redfin.sitemapgenerator;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
|
@ -56,6 +54,26 @@ public class GoogleMobileSitemapGenerator extends SitemapGenerator<GoogleMobileS
|
|||
public GoogleMobileSitemapGenerator(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 GoogleMobileSitemapGenerator(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 GoogleMobileSitemapGenerator(URL baseUrl) {
|
||||
this(new SitemapGeneratorOptions(baseUrl));
|
||||
}
|
||||
|
||||
private static class Renderer extends AbstractSitemapUrlRenderer<GoogleMobileSitemapUrl> implements ISitemapUrlRenderer<GoogleMobileSitemapUrl> {
|
||||
|
||||
|
@ -63,16 +81,14 @@ public class GoogleMobileSitemapGenerator extends SitemapGenerator<GoogleMobileS
|
|||
return GoogleMobileSitemapUrl.class;
|
||||
}
|
||||
|
||||
public void render(GoogleMobileSitemapUrl url, OutputStreamWriter out,
|
||||
W3CDateFormat dateFormat) throws IOException {
|
||||
String additionalData = " <mobile:mobile/>\n";
|
||||
super.render(url, out, dateFormat, additionalData);
|
||||
|
||||
}
|
||||
|
||||
public String getXmlNamespaces() {
|
||||
return "xmlns:mobile=\"http://www.google.com/schemas/sitemap-mobile/1.0\"";
|
||||
}
|
||||
|
||||
public void render(GoogleMobileSitemapUrl url, StringBuilder sb, W3CDateFormat dateFormat) {
|
||||
String additionalData = " <mobile:mobile/>\n";
|
||||
super.render(url, sb, dateFormat, additionalData);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package com.redfin.sitemapgenerator;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
|
@ -69,26 +67,44 @@ public class GoogleNewsSitemapGenerator extends SitemapGenerator<GoogleNewsSitem
|
|||
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 void render(GoogleNewsSitemapUrl url, OutputStreamWriter out,
|
||||
W3CDateFormat dateFormat) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(" <news:news>\n");
|
||||
renderTag(sb, "news", "publication_date", dateFormat.format(url.getPublicationDate()));
|
||||
renderTag(sb, "news", "keywords", url.getKeywords());
|
||||
sb.append(" </news:news>\n");
|
||||
super.render(url, out, dateFormat, sb.toString());
|
||||
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package com.redfin.sitemapgenerator;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
|
@ -56,49 +54,67 @@ public class GoogleVideoSitemapGenerator extends SitemapGenerator<GoogleVideoSit
|
|||
public GoogleVideoSitemapGenerator(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 GoogleVideoSitemapGenerator(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 GoogleVideoSitemapGenerator(URL baseUrl) {
|
||||
this(new SitemapGeneratorOptions(baseUrl));
|
||||
}
|
||||
|
||||
private static class Renderer extends AbstractSitemapUrlRenderer<GoogleVideoSitemapUrl> implements ISitemapUrlRenderer<GoogleVideoSitemapUrl> {
|
||||
|
||||
public Class<GoogleVideoSitemapUrl> getUrlClass() {
|
||||
return GoogleVideoSitemapUrl.class;
|
||||
}
|
||||
|
||||
public void render(GoogleVideoSitemapUrl url, OutputStreamWriter out,
|
||||
W3CDateFormat dateFormat) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(" <video:video>\n");
|
||||
renderTag(sb, "video", "content_loc", url.getContentUrl());
|
||||
if (url.getPlayerUrl() != null) {
|
||||
sb.append(" <video:player_loc allow_embed=\"");
|
||||
sb.append(url.getAllowEmbed());
|
||||
sb.append("\">");
|
||||
sb.append(url.getPlayerUrl());
|
||||
sb.append("</video:player_loc>\n");
|
||||
}
|
||||
renderTag(sb, "video", "thumbnail_loc", url.getThumbnailUrl());
|
||||
renderTag(sb, "video", "title", url.getTitle());
|
||||
renderTag(sb, "video", "description", url.getDescription());
|
||||
renderTag(sb, "video", "rating", url.getRating());
|
||||
renderTag(sb, "video", "view_count", url.getViewCount());
|
||||
if (url.getPublicationDate() != null) {
|
||||
renderTag(sb, "video", "publication_date", dateFormat.format(url.getPublicationDate()));
|
||||
}
|
||||
if (url.getTags() != null) {
|
||||
for (String tag : url.getTags()) {
|
||||
renderTag(sb, "video", "tag", tag);
|
||||
}
|
||||
}
|
||||
renderTag(sb, "video", "category", url.getCategory());
|
||||
renderTag(sb, "video", "family_friendly", url.getFamilyFriendly());
|
||||
renderTag(sb, "video", "duration", url.getDurationInSeconds());
|
||||
sb.append(" </video:video>\n");
|
||||
super.render(url, out, dateFormat, sb.toString());
|
||||
|
||||
}
|
||||
|
||||
public String getXmlNamespaces() {
|
||||
return "xmlns:video=\"http://www.google.com/schemas/sitemap-video/1.1\"";
|
||||
}
|
||||
|
||||
public void render(GoogleVideoSitemapUrl url, StringBuilder sb, W3CDateFormat dateFormat) {
|
||||
StringBuilder tagSb = new StringBuilder();
|
||||
tagSb.append(" <video:video>\n");
|
||||
renderTag(tagSb, "video", "content_loc", url.getContentUrl());
|
||||
if (url.getPlayerUrl() != null) {
|
||||
tagSb.append(" <video:player_loc allow_embed=\"");
|
||||
tagSb.append(url.getAllowEmbed());
|
||||
tagSb.append("\">");
|
||||
tagSb.append(url.getPlayerUrl());
|
||||
tagSb.append("</video:player_loc>\n");
|
||||
}
|
||||
renderTag(tagSb, "video", "thumbnail_loc", url.getThumbnailUrl());
|
||||
renderTag(tagSb, "video", "title", url.getTitle());
|
||||
renderTag(tagSb, "video", "description", url.getDescription());
|
||||
renderTag(tagSb, "video", "rating", url.getRating());
|
||||
renderTag(tagSb, "video", "view_count", url.getViewCount());
|
||||
if (url.getPublicationDate() != null) {
|
||||
renderTag(tagSb, "video", "publication_date", dateFormat.format(url.getPublicationDate()));
|
||||
}
|
||||
if (url.getTags() != null) {
|
||||
for (String tag : url.getTags()) {
|
||||
renderTag(tagSb, "video", "tag", tag);
|
||||
}
|
||||
}
|
||||
renderTag(tagSb, "video", "category", url.getCategory());
|
||||
renderTag(tagSb, "video", "family_friendly", url.getFamilyFriendly());
|
||||
renderTag(tagSb, "video", "duration", url.getDurationInSeconds());
|
||||
tagSb.append(" </video:video>\n");
|
||||
super.render(url, sb, dateFormat, tagSb.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,11 +1,8 @@
|
|||
package com.redfin.sitemapgenerator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
|
||||
interface ISitemapUrlRenderer<T extends ISitemapUrl> {
|
||||
|
||||
public Class<T> getUrlClass();
|
||||
public String getXmlNamespaces();
|
||||
public void render(T url, OutputStreamWriter out, W3CDateFormat dateFormat) throws IOException;
|
||||
public void render(T url, StringBuilder sb, W3CDateFormat dateFormat);
|
||||
}
|
||||
|
|
|
@ -59,10 +59,12 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
|
|||
UrlUtils.checkUrl(url.getUrl().toString(), baseUrl);
|
||||
if (urls.size() == maxUrls) {
|
||||
if (!allowMultipleSitemaps) throw new RuntimeException("More than " + maxUrls + " urls, but allowMultipleSitemaps is false. Enable allowMultipleSitemaps to split the sitemap into multiple files with a sitemap index.");
|
||||
if (mapCount == 0) mapCount++;
|
||||
writeSiteMap();
|
||||
mapCount++;
|
||||
urls.clear();
|
||||
if (baseDir != null) {
|
||||
if (mapCount == 0) mapCount++;
|
||||
writeSiteMap();
|
||||
mapCount++;
|
||||
urls.clear();
|
||||
}
|
||||
}
|
||||
urls.add(url);
|
||||
return getThis();
|
||||
|
@ -163,6 +165,41 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
|
|||
return outFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes out the sitemaps as a list of strings.
|
||||
* Each string in the list is a formatted list of URLs.
|
||||
* We return a list because the URLs may not all fit --
|
||||
* google specifies a maximum of 50,000 URLs in one sitemap.
|
||||
* @return a list of XML-formatted strings
|
||||
*/
|
||||
public List<String> writeAsStrings() {
|
||||
List<String> listOfSiteMapStrings = new ArrayList<String>();
|
||||
for (int start = 0; start < urls.size(); start += maxUrls) {
|
||||
int end = start + maxUrls;
|
||||
if (end > urls.size()) {
|
||||
end = urls.size();
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
writeSiteMapAsString(sb, urls.subList(start, end));
|
||||
listOfSiteMapStrings.add(sb.toString());
|
||||
}
|
||||
return listOfSiteMapStrings;
|
||||
}
|
||||
|
||||
private void writeSiteMapAsString(StringBuilder sb, List<U> urls) {
|
||||
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
|
||||
sb.append("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" ");
|
||||
if (renderer.getXmlNamespaces() != null) {
|
||||
sb.append(renderer.getXmlNamespaces());
|
||||
sb.append(' ');
|
||||
}
|
||||
sb.append(">\n");
|
||||
for (U url : urls) {
|
||||
renderer.render(url, sb, dateFormat);
|
||||
}
|
||||
sb.append("</urlset>");
|
||||
}
|
||||
|
||||
/** After you've called {@link #write()}, call this to generate a sitemap index of all sitemaps you generated.
|
||||
*
|
||||
*/
|
||||
|
@ -179,6 +216,9 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
|
|||
}
|
||||
|
||||
private void writeSiteMap() {
|
||||
if (baseDir == null) {
|
||||
throw new NullPointerException("To write to files, baseDir must not be null");
|
||||
}
|
||||
if (urls.size() == 0) return;
|
||||
String fileNamePrefix;
|
||||
if (mapCount > 0) {
|
||||
|
@ -208,18 +248,9 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
|
|||
}
|
||||
|
||||
private void writeSiteMap(OutputStreamWriter out) throws IOException {
|
||||
out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
|
||||
out.write("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" ");
|
||||
|
||||
if (renderer.getXmlNamespaces() != null) {
|
||||
out.write(renderer.getXmlNamespaces());
|
||||
out.write(' ');
|
||||
}
|
||||
out.write(">\n");
|
||||
for (U url : urls) {
|
||||
renderer.render(url, out, dateFormat);
|
||||
}
|
||||
out.write("</urlset>");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
writeSiteMapAsString(sb, urls);
|
||||
out.write(sb.toString());
|
||||
out.close();
|
||||
}
|
||||
|
||||
|
|
|
@ -14,5 +14,13 @@ class SitemapGeneratorOptions extends
|
|||
public SitemapGeneratorOptions(String baseUrl, File baseDir) throws MalformedURLException {
|
||||
this(new URL(baseUrl), baseDir);
|
||||
}
|
||||
|
||||
public SitemapGeneratorOptions(URL baseUrl) {
|
||||
super(baseUrl);
|
||||
}
|
||||
|
||||
public SitemapGeneratorOptions(String baseUrl) throws MalformedURLException {
|
||||
super(new URL(baseUrl));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package com.redfin.sitemapgenerator;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
|
@ -57,6 +55,25 @@ public class WebSitemapGenerator extends SitemapGenerator<WebSitemapUrl,WebSitem
|
|||
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 WebSitemapGenerator(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 WebSitemapGenerator(URL baseUrl) {
|
||||
this(new SitemapGeneratorOptions(baseUrl));
|
||||
}
|
||||
|
||||
private static class Renderer extends AbstractSitemapUrlRenderer<WebSitemapUrl> implements ISitemapUrlRenderer<WebSitemapUrl> {
|
||||
|
||||
|
@ -64,13 +81,13 @@ public class WebSitemapGenerator extends SitemapGenerator<WebSitemapUrl,WebSitem
|
|||
return WebSitemapUrl.class;
|
||||
}
|
||||
|
||||
public void render(WebSitemapUrl url, OutputStreamWriter out, W3CDateFormat dateFormat) throws IOException {
|
||||
super.render(url, out, dateFormat, null);
|
||||
}
|
||||
|
||||
public String getXmlNamespaces() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void render(WebSitemapUrl url, StringBuilder sb, W3CDateFormat dateFormat) {
|
||||
super.render(url, sb, dateFormat, null);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -291,6 +291,31 @@ public class SitemapGeneratorTest extends TestCase {
|
|||
assertEquals("sitemap didn't match", SITEMAP1, actual);
|
||||
}
|
||||
|
||||
public void testBaseDirIsNullThrowsNullPointerException() throws Exception {
|
||||
wsg = WebSitemapGenerator.builder("http://www.example.com", null).autoValidate(true).maxUrls(10).build();
|
||||
wsg.addUrl("http://www.example.com/index.html");
|
||||
Exception e = null;
|
||||
try {
|
||||
wsg.write();
|
||||
} catch (Exception ex) {
|
||||
e = ex;
|
||||
}
|
||||
assertTrue(e instanceof NullPointerException);
|
||||
assertEquals("Correct exception was not thrown", e.getMessage(), "To write to files, baseDir must not be null");
|
||||
}
|
||||
|
||||
public void testWriteAsStringsMoreThanOneString() throws Exception {
|
||||
wsg = WebSitemapGenerator.builder("http://www.example.com", null).autoValidate(true).maxUrls(10).build();
|
||||
for (int i = 0; i < 9; i++) {
|
||||
wsg.addUrl("http://www.example.com/"+i);
|
||||
}
|
||||
wsg.addUrl("http://www.example.com/9");
|
||||
wsg.addUrl("http://www.example.com/just-one-more");
|
||||
List<String> siteMapsAsStrings = wsg.writeAsStrings();
|
||||
assertEquals("First string didn't match", SITEMAP1, siteMapsAsStrings.get(0));
|
||||
assertEquals("Second string didn't match", SITEMAP_PLUS_ONE, siteMapsAsStrings.get(1));
|
||||
}
|
||||
|
||||
private String writeSingleSiteMap(WebSitemapGenerator wsg) {
|
||||
List<File> files = wsg.write();
|
||||
assertEquals("Too many files: " + files.toString(), 1, files.size());
|
||||
|
|
Loading…
Reference in New Issue