Skip to content

Commit

Permalink
version 3.2, deprecated *Generator constructors and instead created s…
Browse files Browse the repository at this point in the history
…tatic of methods, which simplify creation of *Generator objects
  • Loading branch information
[email protected] authored and [email protected] committed Dec 25, 2018
1 parent accb57f commit 93a68dc
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 15 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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<String> 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()
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>cz.jiripinkas</groupId>
<artifactId>jsitemapgenerator</artifactId>
<version>3.1</version>
<version>3.2</version>
<packaging>jar</packaging>
<name>Java sitemap generator</name>
<description>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.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,17 +21,45 @@ 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)) {
additionalNamespacesStringBuilder.append(" xmlns:image=\"http://www.google.com/schemas/sitemap-image/1.1\" ");
}
}

/**
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> out = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:image=\"http://www.google.com/schemas/sitemap-image/1.1\" >\n</urlset>", 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down

0 comments on commit 93a68dc

Please sign in to comment.