Add support to write sitemaps index as string

This commit is contained in:
Matthias Kurz 2019-01-27 15:02:38 +01:00
parent 6a605cf6ce
commit bc5515d678
No known key found for this signature in database
GPG Key ID: 0B4AAA92F1117EF5
3 changed files with 52 additions and 23 deletions

View File

@ -218,9 +218,14 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
* The sitemap index is written to {baseDir}/sitemap_index.xml * The sitemap index is written to {baseDir}/sitemap_index.xml
*/ */
public File writeSitemapsWithIndex() { public File writeSitemapsWithIndex() {
if (!finished) throw new RuntimeException("Sitemaps not generated yet; call write() first"); return writeSitemapsWithIndex(new File(baseDir, "sitemap_index.xml"));
File outFile = new File(baseDir, "sitemap_index.xml"); }
return writeSitemapsWithIndex(outFile);
/**
* After you've called {@link #write()}, call this to generate a sitemap index of all sitemaps you generated.
*/
public String writeSitemapsWithIndexAsString() {
return prepareSitemapIndexGenerator(null).writeAsString();
} }
/** /**
@ -229,11 +234,16 @@ abstract class SitemapGenerator<U extends ISitemapUrl, THIS extends SitemapGener
* @param outFile the destination file of the sitemap index. * @param outFile the destination file of the sitemap index.
*/ */
public File writeSitemapsWithIndex(File outFile) { public File writeSitemapsWithIndex(File outFile) {
prepareSitemapIndexGenerator(outFile).write();
return outFile;
}
private SitemapIndexGenerator prepareSitemapIndexGenerator(File outFile) {
if (!finished) throw new RuntimeException("Sitemaps not generated yet; call write() first"); if (!finished) throw new RuntimeException("Sitemaps not generated yet; call write() first");
SitemapIndexGenerator sig; SitemapIndexGenerator sig;
sig = new SitemapIndexGenerator.Options(baseUrl, outFile).dateFormat(dateFormat).autoValidate(autoValidate).build(); sig = new SitemapIndexGenerator.Options(baseUrl, outFile).dateFormat(dateFormat).autoValidate(autoValidate).build();
sig.addUrls(fileNamePrefix, fileNameSuffix, mapCount).write(); sig.addUrls(fileNamePrefix, fileNameSuffix, mapCount);
return outFile; return sig;
} }
private void writeSiteMap() throws IOException { private void writeSiteMap() throws IOException {

View File

@ -221,18 +221,21 @@ public class SitemapIndexGenerator {
/** Writes out the sitemap index */ /** Writes out the sitemap index */
public void write() { public void write() {
if (!allowEmptyIndex && urls.isEmpty()) throw new RuntimeException("No URLs added, sitemap index would be empty; you must add some URLs with addUrls");
try {
FileWriter out = null;
try { try {
// TODO gzip? is that legal for a sitemap index? // TODO gzip? is that legal for a sitemap index?
out = new FileWriter(outFile); write(new FileWriter(outFile));
writeSiteMap(out);
out.flush();
if (autoValidate) SitemapValidator.validateSitemapIndex(outFile);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Problem writing sitemap index file " + outFile, e); throw new RuntimeException("Problem writing sitemap index file " + outFile, e);
}
}
private void write(OutputStreamWriter out) {
if (!allowEmptyIndex && urls.isEmpty()) throw new RuntimeException("No URLs added, sitemap index would be empty; you must add some URLs with addUrls");
try {
try {
writeSiteMap(out);
out.flush();
if (autoValidate) SitemapValidator.validateSitemapIndex(outFile);
} catch (SAXException e) { } catch (SAXException e) {
throw new RuntimeException("Problem validating sitemap index file (bug?)", e); throw new RuntimeException("Problem validating sitemap index file (bug?)", e);
} finally { } finally {
@ -246,26 +249,38 @@ public class SitemapIndexGenerator {
} }
private void writeSiteMap(OutputStreamWriter out) throws IOException { public String writeAsString() {
out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); StringBuilder sb = new StringBuilder();
out.write("<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n"); writeAsString(sb);
return sb.toString();
}
private void writeAsString(StringBuilder sb) {
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
sb.append("<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n");
for (SitemapIndexUrl url : urls) { for (SitemapIndexUrl url : urls) {
out.write(" <sitemap>\n"); sb.append(" <sitemap>\n");
out.write(" <loc>"); sb.append(" <loc>");
out.write(UrlUtils.escapeXml(url.url.toString())); sb.append(UrlUtils.escapeXml(url.url.toString()));
out.write("</loc>\n"); sb.append("</loc>\n");
Date lastMod = url.lastMod; Date lastMod = url.lastMod;
if (lastMod == null) lastMod = defaultLastMod; if (lastMod == null) lastMod = defaultLastMod;
if (lastMod != null) { if (lastMod != null) {
out.write(" <lastmod>"); sb.append(" <lastmod>");
out.write(dateFormat.format(lastMod)); sb.append(dateFormat.format(lastMod));
out.write("</lastmod>\n"); sb.append("</lastmod>\n");
} }
out.write(" </sitemap>\n"); sb.append(" </sitemap>\n");
} }
out.write("</sitemapindex>"); sb.append("</sitemapindex>");
}
private void writeSiteMap(OutputStreamWriter out) throws IOException {
StringBuilder sb = new StringBuilder();
writeAsString(sb);
out.write(sb.toString());
} }
} }

View File

@ -95,6 +95,7 @@ public class SitemapIndexGeneratorTest extends TestCase {
"</sitemapindex>"; "</sitemapindex>";
String actual = TestUtil.slurpFileAndDelete(outFile); String actual = TestUtil.slurpFileAndDelete(outFile);
assertEquals(expected, actual); assertEquals(expected, actual);
assertEquals(expected, sig.writeAsString());
} }
public void testMaxUrls() throws Exception { public void testMaxUrls() throws Exception {
@ -107,6 +108,7 @@ public class SitemapIndexGeneratorTest extends TestCase {
sig.write(); sig.write();
String actual = TestUtil.slurpFileAndDelete(outFile); String actual = TestUtil.slurpFileAndDelete(outFile);
assertEquals(INDEX, actual); assertEquals(INDEX, actual);
assertEquals(INDEX, sig.writeAsString());
} }
public void testOneUrl() throws Exception { public void testOneUrl() throws Exception {
@ -123,6 +125,7 @@ public class SitemapIndexGeneratorTest extends TestCase {
" </sitemap>\n" + " </sitemap>\n" +
"</sitemapindex>"; "</sitemapindex>";
assertEquals(expected, actual); assertEquals(expected, actual);
assertEquals(expected, sig.writeAsString());
} }
public void testAddByPrefix() throws MalformedURLException { public void testAddByPrefix() throws MalformedURLException {
@ -132,6 +135,7 @@ public class SitemapIndexGeneratorTest extends TestCase {
sig.write(); sig.write();
String actual = TestUtil.slurpFileAndDelete(outFile); String actual = TestUtil.slurpFileAndDelete(outFile);
assertEquals(INDEX, actual); assertEquals(INDEX, actual);
assertEquals(INDEX, sig.writeAsString());
} }
} }