diff --git a/README.md b/README.md index 7949f0d..6ec3977 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ If you want to use "ping google / bing" functionality, also add this library to ### How to create a sitemap: // create web sitemap for web http://www.javavids.com - SitemapGenerator sg = new SitemapGenerator("http://www.javavids.com"); + SitemapGenerator sg = SitemapGenerator.of("http://www.javavids.com"); // add some URLs sg.addPage(WebPage.builder().nameRoot().priorityMax().changeFreqNever().lastModNow().build()) .addPage(WebPage.builder().name("latest.php").build()) @@ -42,14 +42,14 @@ If you want to use "ping google / bing" functionality, also add this library to File file = new File("/var/www/sitemap.xml"); List pages = Arrays.asList("firstPage", "secondPage", "otherPage"); // create web sitemap for web http://www.javavids.com - new SitemapGenerator("http://www.javavids.com") + SitemapGenerator.of("http://www.javavids.com") .addPage(WebPage.builder().nameRoot().priorityMax().changeFreqNever().lastModNow().build()) .addPages(urls, page -> WebPage.builder().name("dir/" + page).priorityMax().changeFreqNever().lastModNow().build()) .constructAndSaveSitemap(file); ### How to create a sitemap index: - SitemapIndexGenerator sitemapIndexGenerator = new SitemapIndexGenerator("http://javalibs.com"); + SitemapIndexGenerator sitemapIndexGenerator = SitemapIndexGenerator.of("http://javalibs.com"); sitemapIndexGenerator.addPage(WebPage.builder() .name("sitemap-plugins.xml") .build() @@ -62,7 +62,7 @@ If you want to use "ping google / bing" functionality, also add this library to ### How to create RSS channel: - RssGenerator rssGenerator = new RssGenerator("http://www.topjavablogs", "Top Java Blogs", "Best Java Blogs"); + RssGenerator rssGenerator = RssGenerator.of("http://www.topjavablogs", "Top Java Blogs", "Best Java Blogs"); rssGenerator.addPage(WebPage.rssBuilder() .pubDate(new Date()) .title("News Title") diff --git a/pom.xml b/pom.xml index 274a9cb..073cf06 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 cz.jiripinkas jsitemapgenerator - 3.1 + 3.2 jar Java sitemap generator This library generates a web sitemap and can ping Google that it has changed. This project has been inspired by sitemapgen4j, but is much more focused on traditional web sitemap and ease of use. diff --git a/src/main/java/cz/jiripinkas/jsitemapgenerator/generator/RssGenerator.java b/src/main/java/cz/jiripinkas/jsitemapgenerator/generator/RssGenerator.java index 2913213..8fab353 100644 --- a/src/main/java/cz/jiripinkas/jsitemapgenerator/generator/RssGenerator.java +++ b/src/main/java/cz/jiripinkas/jsitemapgenerator/generator/RssGenerator.java @@ -18,12 +18,14 @@ public class RssGenerator extends AbstractGenerator { /** * Create RssGenerator * + * @deprecated use {@link #of(String, boolean, String, String)} * @param baseUrl Base URL * @param root If Base URL is root (for example http://www.javavids.com or if * it's some path like http://www.javalibs.com/blog) * @param webTitle Web title * @param webDescription Web description */ + @Deprecated public RssGenerator(String baseUrl, boolean root, String webTitle, String webDescription) { super(baseUrl, root); this.webTitle = webTitle; @@ -33,16 +35,37 @@ public RssGenerator(String baseUrl, boolean root, String webTitle, String webDes /** * Create RssGenerator. Root = true. * + * @deprecated use {@link #of(String, String, String)} * @param baseUrl Base URL * @param webTitle Web title * @param webDescription Web description */ + @Deprecated public RssGenerator(String baseUrl, String webTitle, String webDescription) { super(baseUrl); this.webTitle = webTitle; this.webDescription = webDescription; } + /** + * Helper method to create an instance of SitemapGenerator + * @param baseUrl + * @return + */ + public static RssGenerator of(String baseUrl, boolean root, String webTitle, String webDescription) { + return new RssGenerator(baseUrl, root, webTitle, webDescription); + } + + /** + * Helper method to create an instance of SitemapGenerator + * @param baseUrl + * @return + */ + public static RssGenerator of(String baseUrl, String webTitle, String webDescription) { + return new RssGenerator(baseUrl, webTitle, webDescription); + } + + /** * This will construct RSS from web pages. Web pages are sorted using * lastMod in descending order (latest is first) diff --git a/src/main/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapGenerator.java b/src/main/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapGenerator.java index a4dbf2f..a2d28fb 100644 --- a/src/main/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapGenerator.java +++ b/src/main/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapGenerator.java @@ -1,6 +1,5 @@ package cz.jiripinkas.jsitemapgenerator.generator; -import cz.jiripinkas.jsitemapgenerator.AbstractGenerator; import cz.jiripinkas.jsitemapgenerator.AbstractSitemapGenerator; import cz.jiripinkas.jsitemapgenerator.Image; import cz.jiripinkas.jsitemapgenerator.WebPage; @@ -22,10 +21,20 @@ public enum AdditionalNamespace { private StringBuilder additionalNamespacesStringBuilder = new StringBuilder(); + /** + * @deprecated Use {@link #of(String)} + * @param baseUrl + */ + @Deprecated public SitemapGenerator(String baseUrl) { super(baseUrl); } + /** + * @deprecated Use {@link #of(String, AdditionalNamespace[])} )} + * @param baseUrl + */ + @Deprecated public SitemapGenerator(String baseUrl, AdditionalNamespace[] additionalNamespaces) { this(baseUrl); if (Arrays.asList(additionalNamespaces).contains(AdditionalNamespace.IMAGE)) { @@ -33,6 +42,24 @@ public SitemapGenerator(String baseUrl, AdditionalNamespace[] additionalNamespac } } + /** + * Helper method to create an instance of SitemapGenerator + * @param baseUrl + * @return + */ + public static SitemapGenerator of(String baseUrl) { + return new SitemapGenerator(baseUrl); + } + + /** + * Helper method to create an instance of SitemapGenerator + * @param baseUrl + * @return + */ + public static SitemapGenerator of(String baseUrl, AdditionalNamespace[] additionalNamespaces) { + return new SitemapGenerator(baseUrl, additionalNamespaces); + } + /** * Construct sitemap into array of Strings. The URLs will be ordered using * priority in descending order (URLs with higher priority will be at the diff --git a/src/main/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapIndexGenerator.java b/src/main/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapIndexGenerator.java index d277ac7..e3512c9 100644 --- a/src/main/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapIndexGenerator.java +++ b/src/main/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapIndexGenerator.java @@ -11,10 +11,19 @@ public class SitemapIndexGenerator extends AbstractSitemapGenerator { + /** + * @deprecated use {@link #of(String)} + * @param baseUrl Base url + */ + @Deprecated public SitemapIndexGenerator(String baseUrl) { super(baseUrl); } + public static SitemapIndexGenerator of(String baseUrl) { + return new SitemapIndexGenerator(baseUrl); + } + @Override public String[] constructSitemap() { ArrayList out = new ArrayList<>(); diff --git a/src/test/java/cz/jiripinkas/jsitemapgenerator/WebPageTest.java b/src/test/java/cz/jiripinkas/jsitemapgenerator/WebPageTest.java index 4e5e7a9..64187fb 100644 --- a/src/test/java/cz/jiripinkas/jsitemapgenerator/WebPageTest.java +++ b/src/test/java/cz/jiripinkas/jsitemapgenerator/WebPageTest.java @@ -10,7 +10,7 @@ public class WebPageTest { @Test(expected = InvalidUrlException.class) public void testConstruct() { - new SitemapGenerator("www.javavids.com"); + SitemapGenerator.of("www.javavids.com"); } @Test(expected = InvalidPriorityException.class) diff --git a/src/test/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapGeneratorTest.java b/src/test/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapGeneratorTest.java index 1e62423..e159e19 100644 --- a/src/test/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapGeneratorTest.java +++ b/src/test/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapGeneratorTest.java @@ -19,7 +19,7 @@ public class SitemapGeneratorTest { @Before public void setUp() { - sitemapGenerator = new SitemapGenerator("http://www.javavids.com"); + sitemapGenerator = SitemapGenerator.of("http://www.javavids.com"); sitemapGenerator.addPage(WebPage.builder().name("index.php").priority(1.0).changeFreqNever().lastMod(new Date()).build()); sitemapGenerator.addPage(WebPage.builder().name("latest.php").build()); sitemapGenerator.addPage(WebPage.builder().name("contact.php").build()); @@ -35,14 +35,14 @@ public void testConstructImage() { @Test public void testConstructSitemapWithImagesHeader() { - sitemapGenerator = new SitemapGenerator("http://www.javavids.com", new AdditionalNamespace[] { AdditionalNamespace.IMAGE }); + sitemapGenerator = SitemapGenerator.of("http://www.javavids.com", new AdditionalNamespace[] { AdditionalNamespace.IMAGE }); String sitemapString = sitemapGenerator.constructSitemapString(); Assert.assertEquals("\n\n", sitemapString); } @Test public void testConstructSitemapWithImages() { - sitemapGenerator = new SitemapGenerator("http://www.javavids.com", new AdditionalNamespace[] { AdditionalNamespace.IMAGE }); + sitemapGenerator = SitemapGenerator.of("http://www.javavids.com", new AdditionalNamespace[] { AdditionalNamespace.IMAGE }); WebPage webPage = WebPage.builder().nameRoot().build(); webPage.addImage(WebPage.imageBuilder().loc("http://www.javavids.com/favicon.ico").build()); sitemapGenerator.addPage(webPage); diff --git a/src/test/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapIndexGeneratorTest.java b/src/test/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapIndexGeneratorTest.java index d48ca88..3778fa9 100644 --- a/src/test/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapIndexGeneratorTest.java +++ b/src/test/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapIndexGeneratorTest.java @@ -17,7 +17,7 @@ public class SitemapIndexGeneratorTest { @Before public void setUp() { - sitemapIndexGenerator = new SitemapIndexGenerator("http://javalibs.com"); + sitemapIndexGenerator = SitemapIndexGenerator.of("http://javalibs.com"); sitemapIndexGenerator.addPage(WebPage.builder().name("sitemap-plugins.xml").lastModNow().build()); sitemapIndexGenerator.addPage(WebPage.builder().name("sitemap-archetypes.xml").lastModNow().build()); } diff --git a/src/test/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapPathTest.java b/src/test/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapPathTest.java index 73ebf00..04ba5f8 100644 --- a/src/test/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapPathTest.java +++ b/src/test/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapPathTest.java @@ -22,7 +22,7 @@ public void setUp() { @Test public void testSitemapPaths() { - SitemapGenerator sitemapGenerator = new SitemapGenerator("http://www.javavids.com/"); + SitemapGenerator sitemapGenerator = SitemapGenerator.of("http://www.javavids.com/"); Date lastModDate = new Date(); sitemapGenerator.addPage(WebPage.builder() .name("/index.php") @@ -53,7 +53,7 @@ public void testSitemapPaths() { @Test public void testSitemapPaths2() { - SitemapGenerator sitemapGenerator = new SitemapGenerator("http://www.javavids.com/"); + SitemapGenerator sitemapGenerator = SitemapGenerator.of("http://www.javavids.com/"); sitemapGenerator.addPage(WebPage.builder() .name("/x0") .priority(1.0) diff --git a/src/test/java/cz/jiripinkas/jsitemapgenerator/rss/RssGeneratorTest.java b/src/test/java/cz/jiripinkas/jsitemapgenerator/rss/RssGeneratorTest.java index 086c059..24ec123 100644 --- a/src/test/java/cz/jiripinkas/jsitemapgenerator/rss/RssGeneratorTest.java +++ b/src/test/java/cz/jiripinkas/jsitemapgenerator/rss/RssGeneratorTest.java @@ -19,11 +19,11 @@ public class RssGeneratorTest { @Before public void setUp() { - rssGenerator = new RssGenerator("http://www.topjavablogs.com", "Top Java Blogs", "News from Java community"); + rssGenerator = RssGenerator.of("http://www.topjavablogs.com", "Top Java Blogs", "News from Java community"); } @Test - public void testConstructRssEmptyItemsShouldThrowException() throws SAXException, IOException { + public void testConstructRssEmptyItemsShouldThrowException() { try { String rss = rssGenerator.constructRss(); ByteArrayInputStream xml = new ByteArrayInputStream(rss.getBytes("UTF-8"));