From bc5515d678a187e6370f08bd303e5671ab994fa6 Mon Sep 17 00:00:00 2001 From: Matthias Kurz Date: Sun, 27 Jan 2019 15:02:38 +0100 Subject: [PATCH] Add support to write sitemaps index as string --- .../sitemapgenerator/SitemapGenerator.java | 20 ++++++-- .../SitemapIndexGenerator.java | 51 ++++++++++++------- .../SitemapIndexGeneratorTest.java | 4 ++ 3 files changed, 52 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/redfin/sitemapgenerator/SitemapGenerator.java b/src/main/java/com/redfin/sitemapgenerator/SitemapGenerator.java index 99b5242..43eb193 100644 --- a/src/main/java/com/redfin/sitemapgenerator/SitemapGenerator.java +++ b/src/main/java/com/redfin/sitemapgenerator/SitemapGenerator.java @@ -218,9 +218,14 @@ abstract class SitemapGenerator\n"); - out.write("\n"); + public String writeAsString() { + StringBuilder sb = new StringBuilder(); + writeAsString(sb); + return sb.toString(); + } + + private void writeAsString(StringBuilder sb) { + sb.append("\n"); + sb.append("\n"); for (SitemapIndexUrl url : urls) { - out.write(" \n"); - out.write(" "); - out.write(UrlUtils.escapeXml(url.url.toString())); - out.write("\n"); + sb.append(" \n"); + sb.append(" "); + sb.append(UrlUtils.escapeXml(url.url.toString())); + sb.append("\n"); Date lastMod = url.lastMod; if (lastMod == null) lastMod = defaultLastMod; if (lastMod != null) { - out.write(" "); - out.write(dateFormat.format(lastMod)); - out.write("\n"); + sb.append(" "); + sb.append(dateFormat.format(lastMod)); + sb.append("\n"); } - out.write(" \n"); + sb.append(" \n"); } - out.write(""); + sb.append(""); + } + + private void writeSiteMap(OutputStreamWriter out) throws IOException { + StringBuilder sb = new StringBuilder(); + writeAsString(sb); + out.write(sb.toString()); } } diff --git a/src/test/java/com/redfin/sitemapgenerator/SitemapIndexGeneratorTest.java b/src/test/java/com/redfin/sitemapgenerator/SitemapIndexGeneratorTest.java index 70ab13e..ca0f1d9 100644 --- a/src/test/java/com/redfin/sitemapgenerator/SitemapIndexGeneratorTest.java +++ b/src/test/java/com/redfin/sitemapgenerator/SitemapIndexGeneratorTest.java @@ -95,6 +95,7 @@ public class SitemapIndexGeneratorTest extends TestCase { ""; String actual = TestUtil.slurpFileAndDelete(outFile); assertEquals(expected, actual); + assertEquals(expected, sig.writeAsString()); } public void testMaxUrls() throws Exception { @@ -107,6 +108,7 @@ public class SitemapIndexGeneratorTest extends TestCase { sig.write(); String actual = TestUtil.slurpFileAndDelete(outFile); assertEquals(INDEX, actual); + assertEquals(INDEX, sig.writeAsString()); } public void testOneUrl() throws Exception { @@ -123,6 +125,7 @@ public class SitemapIndexGeneratorTest extends TestCase { " \n" + ""; assertEquals(expected, actual); + assertEquals(expected, sig.writeAsString()); } public void testAddByPrefix() throws MalformedURLException { @@ -132,6 +135,7 @@ public class SitemapIndexGeneratorTest extends TestCase { sig.write(); String actual = TestUtil.slurpFileAndDelete(outFile); assertEquals(INDEX, actual); + assertEquals(INDEX, sig.writeAsString()); } }