diff --git a/src/main/java/com/redfin/sitemapgenerator/AbstractSitemapGeneratorOptions.java b/src/main/java/com/redfin/sitemapgenerator/AbstractSitemapGeneratorOptions.java index 8521113..871493f 100644 --- a/src/main/java/com/redfin/sitemapgenerator/AbstractSitemapGeneratorOptions.java +++ b/src/main/java/com/redfin/sitemapgenerator/AbstractSitemapGeneratorOptions.java @@ -16,12 +16,15 @@ abstract class AbstractSitemapGeneratorOptions implements ISitemapUrlRenderer { - - public void render(WebSitemapUrl url, OutputStreamWriter out, W3CDateFormat dateFormat, String additionalData) - throws IOException { - out.write(" \n"); - out.write(" "); - out.write(url.getUrl().toString()); - out.write("\n"); + + public void render(WebSitemapUrl url, StringBuilder sb, W3CDateFormat dateFormat, String additionalData) { + sb.append(" \n"); + sb.append(" "); + sb.append(url.getUrl().toString()); + sb.append("\n"); if (url.getLastMod() != null) { - out.write(" "); - out.write(dateFormat.format(url.getLastMod())); - out.write("\n"); + sb.append(" "); + sb.append(dateFormat.format(url.getLastMod())); + sb.append("\n"); } if (url.getChangeFreq() != null) { - out.write(" "); - out.write(url.getChangeFreq().toString()); - out.write("\n"); + sb.append(" "); + sb.append(url.getChangeFreq().toString()); + sb.append("\n"); } if (url.getPriority() != null) { - out.write(" "); - out.write(url.getPriority().toString()); - out.write("\n"); + sb.append(" "); + sb.append(url.getPriority().toString()); + sb.append("\n"); } - if (additionalData != null) out.write(additionalData); - out.write(" \n"); + if (additionalData != null) { + sb.append(additionalData); + } + sb.append(" \n"); } public void renderTag(StringBuilder sb, String namespace, String tagName, Object value) { diff --git a/src/main/java/com/redfin/sitemapgenerator/GoogleCodeSitemapGenerator.java b/src/main/java/com/redfin/sitemapgenerator/GoogleCodeSitemapGenerator.java index db0df6f..06fe971 100644 --- a/src/main/java/com/redfin/sitemapgenerator/GoogleCodeSitemapGenerator.java +++ b/src/main/java/com/redfin/sitemapgenerator/GoogleCodeSitemapGenerator.java @@ -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 getUrlClass() { return GoogleCodeSitemapUrl.class; } - - public void render(GoogleCodeSitemapUrl url, OutputStreamWriter out, - W3CDateFormat dateFormat) throws IOException { - StringBuilder sb = new StringBuilder(); - sb.append(" \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(" \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(" \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(" \n"); + super.render(url, sb, dateFormat, tagSb.toString()); + } } diff --git a/src/main/java/com/redfin/sitemapgenerator/GoogleGeoSitemapGenerator.java b/src/main/java/com/redfin/sitemapgenerator/GoogleGeoSitemapGenerator.java index a0546cc..6c9a8a6 100644 --- a/src/main/java/com/redfin/sitemapgenerator/GoogleGeoSitemapGenerator.java +++ b/src/main/java/com/redfin/sitemapgenerator/GoogleGeoSitemapGenerator.java @@ -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 implements ISitemapUrlRenderer { @@ -64,19 +83,17 @@ public class GoogleGeoSitemapGenerator extends SitemapGenerator\n"); - sb.append(" "+url.getFormat()+"\n"); - sb.append(" \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(" \n"); + tagSb.append(" "+url.getFormat()+"\n"); + tagSb.append(" \n"); + super.render(url, sb, dateFormat, tagSb.toString()); + } } } diff --git a/src/main/java/com/redfin/sitemapgenerator/GoogleMobileSitemapGenerator.java b/src/main/java/com/redfin/sitemapgenerator/GoogleMobileSitemapGenerator.java index 12a349b..6bdd1d6 100644 --- a/src/main/java/com/redfin/sitemapgenerator/GoogleMobileSitemapGenerator.java +++ b/src/main/java/com/redfin/sitemapgenerator/GoogleMobileSitemapGenerator.java @@ -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 implements ISitemapUrlRenderer { @@ -63,16 +81,14 @@ public class GoogleMobileSitemapGenerator extends SitemapGenerator implements ISitemapUrlRenderer { public Class getUrlClass() { return GoogleNewsSitemapUrl.class; } - public void render(GoogleNewsSitemapUrl url, OutputStreamWriter out, - W3CDateFormat dateFormat) throws IOException { - StringBuilder sb = new StringBuilder(); - sb.append(" \n"); - renderTag(sb, "news", "publication_date", dateFormat.format(url.getPublicationDate())); - renderTag(sb, "news", "keywords", url.getKeywords()); - sb.append(" \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(" \n"); + renderTag(tagSb, "news", "publication_date", dateFormat.format(url.getPublicationDate())); + 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/GoogleVideoSitemapGenerator.java b/src/main/java/com/redfin/sitemapgenerator/GoogleVideoSitemapGenerator.java index 1cbf8e4..64f9301 100644 --- a/src/main/java/com/redfin/sitemapgenerator/GoogleVideoSitemapGenerator.java +++ b/src/main/java/com/redfin/sitemapgenerator/GoogleVideoSitemapGenerator.java @@ -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 implements ISitemapUrlRenderer { public Class getUrlClass() { return GoogleVideoSitemapUrl.class; } - - public void render(GoogleVideoSitemapUrl url, OutputStreamWriter out, - W3CDateFormat dateFormat) throws IOException { - StringBuilder sb = new StringBuilder(); - sb.append(" \n"); - renderTag(sb, "video", "content_loc", url.getContentUrl()); - if (url.getPlayerUrl() != null) { - sb.append(" "); - sb.append(url.getPlayerUrl()); - sb.append("\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(" \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(" \n"); + renderTag(tagSb, "video", "content_loc", url.getContentUrl()); + if (url.getPlayerUrl() != null) { + tagSb.append(" "); + tagSb.append(url.getPlayerUrl()); + tagSb.append("\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(" \n"); + super.render(url, sb, dateFormat, tagSb.toString()); + } } diff --git a/src/main/java/com/redfin/sitemapgenerator/ISitemapUrlRenderer.java b/src/main/java/com/redfin/sitemapgenerator/ISitemapUrlRenderer.java index 3c421fd..2839e52 100644 --- a/src/main/java/com/redfin/sitemapgenerator/ISitemapUrlRenderer.java +++ b/src/main/java/com/redfin/sitemapgenerator/ISitemapUrlRenderer.java @@ -1,11 +1,8 @@ package com.redfin.sitemapgenerator; -import java.io.IOException; -import java.io.OutputStreamWriter; - interface ISitemapUrlRenderer { public Class getUrlClass(); public String getXmlNamespaces(); - public void render(T url, OutputStreamWriter out, W3CDateFormat dateFormat) throws IOException; + public void render(T url, StringBuilder sb, W3CDateFormat dateFormat); } diff --git a/src/main/java/com/redfin/sitemapgenerator/SitemapGenerator.java b/src/main/java/com/redfin/sitemapgenerator/SitemapGenerator.java index b88e066..265b1e9 100644 --- a/src/main/java/com/redfin/sitemapgenerator/SitemapGenerator.java +++ b/src/main/java/com/redfin/sitemapgenerator/SitemapGenerator.java @@ -59,10 +59,12 @@ abstract class SitemapGenerator writeAsStrings() { + List listOfSiteMapStrings = new ArrayList(); + 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 urls) { + sb.append("\n"); + sb.append("\n"); + for (U url : urls) { + renderer.render(url, sb, dateFormat); + } + sb.append(""); + } + /** 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 0) { @@ -208,18 +248,9 @@ abstract class SitemapGenerator\n"); - out.write("\n"); - for (U url : urls) { - renderer.render(url, out, dateFormat); - } - out.write(""); + StringBuilder sb = new StringBuilder(); + writeSiteMapAsString(sb, urls); + out.write(sb.toString()); out.close(); } diff --git a/src/main/java/com/redfin/sitemapgenerator/SitemapGeneratorOptions.java b/src/main/java/com/redfin/sitemapgenerator/SitemapGeneratorOptions.java index a8b3aba..34c7ded 100644 --- a/src/main/java/com/redfin/sitemapgenerator/SitemapGeneratorOptions.java +++ b/src/main/java/com/redfin/sitemapgenerator/SitemapGeneratorOptions.java @@ -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)); + } } diff --git a/src/main/java/com/redfin/sitemapgenerator/WebSitemapGenerator.java b/src/main/java/com/redfin/sitemapgenerator/WebSitemapGenerator.java index fc7400b..a0814b0 100644 --- a/src/main/java/com/redfin/sitemapgenerator/WebSitemapGenerator.java +++ b/src/main/java/com/redfin/sitemapgenerator/WebSitemapGenerator.java @@ -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 implements ISitemapUrlRenderer { @@ -64,13 +81,13 @@ public class WebSitemapGenerator extends SitemapGenerator 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 files = wsg.write(); assertEquals("Too many files: " + files.toString(), 1, files.size());