Commit 54138163 authored by Sky Cao's avatar Sky Cao
Browse files

added write to string functionality for sitemap generators

parent c24db420
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -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");
+19 −21
Original line number Diff line number Diff line
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) {
			sb.append(additionalData);
		}
		if (additionalData != null) out.write(additionalData);
		out.write("  </url>\n");
		sb.append("  </url>\n");
	}

	public void renderTag(StringBuilder sb, String namespace, String tagName, Object value) {
+33 −15
Original line number Diff line number Diff line
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
@@ -64,23 +82,23 @@ public class GoogleCodeSitemapGenerator extends SitemapGenerator<GoogleCodeSitem
			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());
		}
		
	}

	
+29 −12
Original line number Diff line number Diff line
package com.redfin.sitemapgenerator;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;

@@ -58,25 +56,44 @@ public class GoogleGeoSitemapGenerator extends SitemapGenerator<GoogleGeoSitemap
		this(new SitemapGeneratorOptions(baseUrl, baseDir));
	}
	
	private static class Renderer extends AbstractSitemapUrlRenderer<GoogleGeoSitemapUrl> implements ISitemapUrlRenderer<GoogleGeoSitemapUrl> {
	/**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)));
	}
	
		public Class<GoogleGeoSitemapUrl> getUrlClass() {
			return GoogleGeoSitemapUrl.class;
	
	/**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));
	}

		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());
	private static class Renderer extends AbstractSitemapUrlRenderer<GoogleGeoSitemapUrl> implements ISitemapUrlRenderer<GoogleGeoSitemapUrl> {

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

		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());
		}
		
	}
}
+25 −9
Original line number Diff line number Diff line
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,22 +55,40 @@ public class GoogleMobileSitemapGenerator extends SitemapGenerator<GoogleMobileS
		this(new SitemapGeneratorOptions(baseUrl, baseDir));
	}
	
	private static class Renderer extends AbstractSitemapUrlRenderer<GoogleMobileSitemapUrl> implements ISitemapUrlRenderer<GoogleMobileSitemapUrl> {
	/**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)));
	}
	
		public Class<GoogleMobileSitemapUrl> getUrlClass() {
			return GoogleMobileSitemapUrl.class;
	/**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));
	}

		public void render(GoogleMobileSitemapUrl url, OutputStreamWriter out,
				W3CDateFormat dateFormat) throws IOException {
			String additionalData = "    <mobile:mobile/>\n";
			super.render(url, out, dateFormat, additionalData);
	private static class Renderer extends AbstractSitemapUrlRenderer<GoogleMobileSitemapUrl> implements ISitemapUrlRenderer<GoogleMobileSitemapUrl> {

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

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