diff --git a/pom.xml b/pom.xml index aeac7e1..a17b7cd 100644 --- a/pom.xml +++ b/pom.xml @@ -1,12 +1,12 @@ 4.0.0 - com.github.dfabulich + com.github.sergiovm sitemapgen4j jar 1.0.7-SNAPSHOT SitemapGen4J - https://github.com/dfabulich/sitemapgen4j/ + https://github.com/sergiovm/sitemapgen4j SitemapGen4j is an XML sitemap generator written in Java. @@ -16,33 +16,13 @@ - scm:git:git://github.com:dfabulich/sitemapgen4j.git - scm:git:git@github.com:dfabulich/sitemapgen4j.git - https://github.com/dfabulich/sitemapgen4j/ + scm:git:git://github.com:sergiovm/sitemapgen4j.git + scm:git:git@github.com:sergiovm/sitemapgen4j.git + https://github.com/sergiovm/sitemapgen4j/ UTF-8 - - - dfabulich - Dan Fabulich - dan@fabulich.com - Redfin - http://www.redfin.com/ - -8 - - - - - ossrh - https://oss.sonatype.org/content/repositories/snapshots - - - ossrh - https://oss.sonatype.org/service/local/staging/deploy/maven2/ - - install @@ -88,31 +68,6 @@ - - org.apache.maven.plugins - maven-gpg-plugin - 1.5 - - - sign-artifacts - verify - - sign - - - - - - org.sonatype.plugins - nexus-staging-maven-plugin - 1.6.3 - true - - ossrh - https://oss.sonatype.org/ - false - - diff --git a/src/main/java/com/redfin/sitemapgenerator/GoogleLinkSitemapGenerator.java b/src/main/java/com/redfin/sitemapgenerator/GoogleLinkSitemapGenerator.java new file mode 100644 index 0000000..df129f8 --- /dev/null +++ b/src/main/java/com/redfin/sitemapgenerator/GoogleLinkSitemapGenerator.java @@ -0,0 +1,131 @@ +package com.redfin.sitemapgenerator; + +import java.io.File; +import java.net.*; +import java.util.Locale; +import java.util.Map.Entry; + +/** + * Builds a Google Link Sitemap (to indicate alternate language pages). + * + * @author Sergio Vico + * @see Creating alternate language pages Sitemaps + */ +public class GoogleLinkSitemapGenerator extends SitemapGenerator { + + private static class Renderer extends AbstractSitemapUrlRenderer + implements ISitemapUrlRenderer { + + public Class getUrlClass() { + + return GoogleLinkSitemapUrl.class; + } + + public String getXmlNamespaces() { + + return "xmlns:xhtml=\"http://www.w3.org/1999/xhtml\""; + } + + public void render(final GoogleLinkSitemapUrl url, final StringBuilder sb, final W3CDateFormat dateFormat) { + + final StringBuilder tagSb = new StringBuilder(); + for (final Entry entry : url.getAlternates().entrySet()) { + tagSb.append(" \n"); + } + super.render(url, sb, dateFormat, tagSb.toString()); + } + + } + + /** + * 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 + * @param baseDir + * Sitemap files will be generated in this directory as either "sitemap.xml" or + * "sitemap1.xml" "sitemap2.xml" and so on. + * @return a builder; call .build() on it to make a sitemap generator + */ + public static SitemapGeneratorBuilder builder(final String baseUrl, final File baseDir) + throws MalformedURLException { + + return new SitemapGeneratorBuilder(baseUrl, baseDir, + GoogleLinkSitemapGenerator.class); + } + + /** + * 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 + * @param baseDir + * Sitemap files will be generated in this directory as either "sitemap.xml" or + * "sitemap1.xml" "sitemap2.xml" and so on. + * @return a builder; call .build() on it to make a sitemap generator + */ + public static SitemapGeneratorBuilder builder(final URL baseUrl, final File baseDir) { + + return new SitemapGeneratorBuilder(baseUrl, baseDir, + GoogleLinkSitemapGenerator.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 GoogleLinkSitemapGenerator(final String baseUrl) throws MalformedURLException { + this(new SitemapGeneratorOptions(new URL(baseUrl))); + } + + /** + * Configures the generator with a base URL and directory to write the sitemap files. + * + * @param baseUrl + * All URLs in the generated sitemap(s) should appear under this base URL + * @param baseDir + * Sitemap files will be generated in this directory as either "sitemap.xml" or + * "sitemap1.xml" "sitemap2.xml" and so on. + * @throws MalformedURLException + */ + public GoogleLinkSitemapGenerator(final String baseUrl, final File baseDir) throws MalformedURLException { + 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 GoogleLinkSitemapGenerator(final URL baseUrl) { + this(new SitemapGeneratorOptions(baseUrl)); + } + + /** + * Configures the generator with a base URL and directory to write the sitemap files. + * + * @param baseUrl + * All URLs in the generated sitemap(s) should appear under this base URL + * @param baseDir + * Sitemap files will be generated in this directory as either "sitemap.xml" or + * "sitemap1.xml" "sitemap2.xml" and so on. + */ + public GoogleLinkSitemapGenerator(final URL baseUrl, final File baseDir) { + this(new SitemapGeneratorOptions(baseUrl, baseDir)); + } + + GoogleLinkSitemapGenerator(final AbstractSitemapGeneratorOptions options) { + super(options, new Renderer()); + } +} diff --git a/src/main/java/com/redfin/sitemapgenerator/GoogleLinkSitemapUrl.java b/src/main/java/com/redfin/sitemapgenerator/GoogleLinkSitemapUrl.java new file mode 100644 index 0000000..3b098f8 --- /dev/null +++ b/src/main/java/com/redfin/sitemapgenerator/GoogleLinkSitemapUrl.java @@ -0,0 +1,65 @@ +package com.redfin.sitemapgenerator; + +import java.net.*; +import java.util.*; +import java.util.Map.Entry; + +/** + * One configurable Google Link URL. To configure, use {@link Options} + * + * @author Sergio Vico + * @see Options + * @see Creating alternate language pages Sitemaps + */ +public class GoogleLinkSitemapUrl extends WebSitemapUrl { + + /** Options to configure mobile URLs */ + public static class Options extends AbstractSitemapUrlOptions { + private final Map alternates; + + private static Map convertAlternates(final Map alternates) + throws MalformedURLException { + + final Map converted = new LinkedHashMap(alternates.size()); + for (final Entry entry : alternates.entrySet()) { + converted.put(Locale.forLanguageTag(entry.getKey()), new URL(entry.getValue())); + } + return converted; + } + + /** Specifies the url */ + public Options(final String url, final Map alternates) throws MalformedURLException { + + this(new URL(url), convertAlternates(alternates)); + } + + /** Specifies the url */ + public Options(final URL url, final Map alternates) { + super(url, GoogleLinkSitemapUrl.class); + this.alternates = new LinkedHashMap(alternates); + } + } + + private final Map alternates; + + /** Specifies configures url with options */ + public GoogleLinkSitemapUrl(final Options options) { + super(options); + alternates = options.alternates; + } + + /** Specifies the url */ + public GoogleLinkSitemapUrl(final String url, final Map alternates) throws MalformedURLException { + this(new Options(url, alternates)); + } + + /** Specifies the url */ + public GoogleLinkSitemapUrl(final URL url, final Map alternates) { + this(new Options(url, alternates)); + } + + public Map getAlternates() { + + return this.alternates; + } +} diff --git a/src/test/java/com/redfin/sitemapgenerator/GoogleLinkSitemapUrlTest.java b/src/test/java/com/redfin/sitemapgenerator/GoogleLinkSitemapUrlTest.java new file mode 100644 index 0000000..dfcb49f --- /dev/null +++ b/src/test/java/com/redfin/sitemapgenerator/GoogleLinkSitemapUrlTest.java @@ -0,0 +1,79 @@ +package com.redfin.sitemapgenerator; + +import java.io.File; +import java.util.*; + +import junit.framework.TestCase; + +public class GoogleLinkSitemapUrlTest extends TestCase { + + File dir; + GoogleLinkSitemapGenerator wsg; + + @Override + public void setUp() throws Exception { + + dir = File.createTempFile(GoogleLinkSitemapUrlTest.class.getSimpleName(), ""); + dir.delete(); + dir.mkdir(); + dir.deleteOnExit(); + } + + @Override + public void tearDown() { + + wsg = null; + for (final File file : dir.listFiles()) { + file.deleteOnExit(); + file.delete(); + } + dir.delete(); + dir = null; + } + + public void testSimpleUrl() throws Exception { + + wsg = new GoogleLinkSitemapGenerator("http://www.example.com", dir); + final Map alternates = new LinkedHashMap(); + alternates.put("en-GB", "http://www.example/en/index.html"); + alternates.put("fr-FR", "http://www.example/fr/index.html"); + alternates.put("es-ES", "http://www.example/es/index.html"); + + final GoogleLinkSitemapUrl url = new GoogleLinkSitemapUrl("http://www.example.com/index.html", alternates); + wsg.addUrl(url); + //@formatter:off + final String expected = "\n" + + "\n" + + " \n" + + " http://www.example.com/index.html\n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + //@formatter:on + final String sitemap = writeSingleSiteMap(wsg); + assertEquals(expected, sitemap); + } + + private String writeSingleSiteMap(final GoogleLinkSitemapGenerator wsg) { + + final List files = wsg.write(); + assertEquals("Too many files: " + files.toString(), 1, files.size()); + assertEquals("Sitemap misnamed", "sitemap.xml", files.get(0).getName()); + return TestUtil.slurpFileAndDelete(files.get(0)); + } +} diff --git a/src/test/java/com/redfin/sitemapgenerator/SitemapGeneratorTest.java b/src/test/java/com/redfin/sitemapgenerator/SitemapGeneratorTest.java index b64d740..29c2d3c 100644 --- a/src/test/java/com/redfin/sitemapgenerator/SitemapGeneratorTest.java +++ b/src/test/java/com/redfin/sitemapgenerator/SitemapGeneratorTest.java @@ -315,6 +315,7 @@ public class SitemapGeneratorTest extends TestCase { while ((c = reader.read()) != -1) { sb.append((char)c); } + reader.close(); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/src/test/java/com/redfin/sitemapgenerator/TestUtil.java b/src/test/java/com/redfin/sitemapgenerator/TestUtil.java index 6f0738c..5146bbf 100644 --- a/src/test/java/com/redfin/sitemapgenerator/TestUtil.java +++ b/src/test/java/com/redfin/sitemapgenerator/TestUtil.java @@ -32,6 +32,7 @@ public class TestUtil { while ((c = reader.read()) != -1) { sb.append((char)c); } + reader.close(); } catch (IOException e) { throw new RuntimeException(e); }