adding support for custom suffices for sitemap generation
This commit is contained in:
parent
e0c7147bfe
commit
be330d6ec3
|
@ -10,6 +10,7 @@ abstract class AbstractSitemapGeneratorOptions<THIS extends AbstractSitemapGener
|
||||||
URL baseUrl;
|
URL baseUrl;
|
||||||
String fileNamePrefix = "sitemap";
|
String fileNamePrefix = "sitemap";
|
||||||
boolean allowMultipleSitemaps = true;
|
boolean allowMultipleSitemaps = true;
|
||||||
|
String suffixStringPattern; // this will store some type of string pattern suitable per needs.
|
||||||
W3CDateFormat dateFormat;
|
W3CDateFormat dateFormat;
|
||||||
int maxUrls = SitemapGenerator.MAX_URLS_PER_SITEMAP;
|
int maxUrls = SitemapGenerator.MAX_URLS_PER_SITEMAP;
|
||||||
boolean autoValidate = false;
|
boolean autoValidate = false;
|
||||||
|
@ -31,6 +32,12 @@ abstract class AbstractSitemapGeneratorOptions<THIS extends AbstractSitemapGener
|
||||||
this.fileNamePrefix = fileNamePrefix;
|
this.fileNamePrefix = fileNamePrefix;
|
||||||
return getThis();
|
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? */
|
/** 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) {
|
public THIS allowMultipleSitemaps(boolean allowMultipleSitemaps) {
|
||||||
this.allowMultipleSitemaps = allowMultipleSitemaps;
|
this.allowMultipleSitemaps = allowMultipleSitemaps;
|
||||||
|
@ -70,4 +77,4 @@ abstract class AbstractSitemapGeneratorOptions<THIS extends AbstractSitemapGener
|
||||||
THIS getThis() {
|
THIS getThis() {
|
||||||
return (THIS)this;
|
return (THIS)this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,9 +45,15 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
|
||||||
autoValidate = options.autoValidate;
|
autoValidate = options.autoValidate;
|
||||||
gzip = options.gzip;
|
gzip = options.gzip;
|
||||||
this.renderer = renderer;
|
this.renderer = renderer;
|
||||||
fileNameSuffix = gzip ? ".xml.gz" : ".xml";
|
|
||||||
|
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.
|
/** 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,
|
* If we have reached the maximum number of URLs, we'll throw an exception if {@link #allowMultipleSitemaps} is false,
|
||||||
* or else write out one sitemap immediately.
|
* or else write out one sitemap immediately.
|
||||||
|
|
|
@ -4,6 +4,7 @@ import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.zip.GZIPInputStream;
|
import java.util.zip.GZIPInputStream;
|
||||||
|
@ -192,7 +193,23 @@ public class SitemapGeneratorTest extends TestCase {
|
||||||
fail("Empty write is not allowed");
|
fail("Empty write is not allowed");
|
||||||
} catch (RuntimeException e) {}
|
} 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 {
|
public void testTooManyUrls() throws Exception {
|
||||||
wsg = WebSitemapGenerator.builder("http://www.example.com", dir).allowMultipleSitemaps(false).build();
|
wsg = WebSitemapGenerator.builder("http://www.example.com", dir).allowMultipleSitemaps(false).build();
|
||||||
for (int i = 0; i < SitemapGenerator.MAX_URLS_PER_SITEMAP; i++) {
|
for (int i = 0; i < SitemapGenerator.MAX_URLS_PER_SITEMAP; i++) {
|
||||||
|
|
Loading…
Reference in New Issue