Commit be330d6e authored by Ankita Nellimarla's avatar Ankita Nellimarla Committed by Dan Fabulich
Browse files

adding support for custom suffices for sitemap generation

parent e0c7147b
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ abstract class AbstractSitemapGeneratorOptions<THIS extends AbstractSitemapGener
	URL baseUrl;
	String fileNamePrefix = "sitemap";
	boolean allowMultipleSitemaps = true;
	String suffixStringPattern; // this will store some type of string pattern suitable per needs.
	W3CDateFormat dateFormat;
	int maxUrls = SitemapGenerator.MAX_URLS_PER_SITEMAP;
	boolean autoValidate = false;
@@ -31,6 +32,12 @@ abstract class AbstractSitemapGeneratorOptions<THIS extends AbstractSitemapGener
		this.fileNamePrefix = fileNamePrefix;
		return getThis();
	}

	public THIS suffixStringPattern(String pattern) {
		this.suffixStringPattern = pattern;
		return getThis();
	}

	/** When more than the maximum number of URLs are passed in, should we split into multiple sitemaps automatically, or just throw an exception? */
	public THIS allowMultipleSitemaps(boolean allowMultipleSitemaps) {
		this.allowMultipleSitemaps = allowMultipleSitemaps;
+8 −2
Original line number Diff line number Diff line
@@ -45,8 +45,14 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
		autoValidate = options.autoValidate;
		gzip = options.gzip;
		this.renderer = renderer;

		if(options.suffixStringPattern != null && !options.suffixStringPattern.isEmpty()) {
			fileNameSuffix = gzip ? options.suffixStringPattern + ".xml.gz" : options.suffixStringPattern + ".xml";
		}
		else {
			fileNameSuffix = gzip ? ".xml.gz" : ".xml";
		}
	}

	/** Add one URL of the appropriate type to this sitemap.
	 * If we have reached the maximum number of URLs, we'll throw an exception if {@link #allowMultipleSitemaps} is false,
+18 −1
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.List;
import java.util.zip.GZIPInputStream;
@@ -193,6 +194,22 @@ public class SitemapGeneratorTest extends TestCase {
		} catch (RuntimeException e) {}
	}

	public void testSuffixPresent() throws MalformedURLException {
		wsg = WebSitemapGenerator.builder("http://www.example.com", dir).suffixStringPattern("01").build();
        wsg.addUrl("http://www.example.com/url1");
        wsg.addUrl("http://www.example.com/url2");
		List<File> files = wsg.write();
		assertEquals("Sitemap has a suffix now", "sitemap01.xml", files.get(0).getName());
	}

    public void testNullSuffixPassed() throws MalformedURLException {
        wsg = WebSitemapGenerator.builder("http://www.example.com", dir).suffixStringPattern("").build();
        wsg.addUrl("http://www.example.com/url1");
        wsg.addUrl("http://www.example.com/url2");
        List<File> files = wsg.write();
        assertEquals("Sitemap has a suffix now", "sitemap.xml", files.get(0).getName());
    }

	public void testTooManyUrls() throws Exception {
		wsg = WebSitemapGenerator.builder("http://www.example.com", dir).allowMultipleSitemaps(false).build();
		for (int i = 0; i < SitemapGenerator.MAX_URLS_PER_SITEMAP; i++) {