Skip to content

Commit

Permalink
added addPages(Collection<T> webPages, Function<T, WebPage> mapper) m…
Browse files Browse the repository at this point in the history
…ethod on SitemapGenerator so that it's possible to use functional programming syntax to populate sitemap
  • Loading branch information
[email protected] authored and [email protected] committed Dec 25, 2018
1 parent d9e5d1f commit 6002436
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 23 deletions.
37 changes: 17 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ First add this library to classpath:
<dependency>
<groupId>cz.jiripinkas</groupId>
<artifactId>jsitemapgenerator</artifactId>
<version>3.0</version>
<version>3.1</version>
</dependency>

If you want to use "ping google / bing" functionality, also add this library to classpath:
Expand All @@ -25,30 +25,27 @@ 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 sitemapGenerator = new SitemapGenerator("http://www.javavids.com");
SitemapGenerator sg = new SitemapGenerator("http://www.javavids.com");
// add some URLs
sitemapGenerator.addPage(WebPage.builder()
.name("index.php")
.priorityMax()
.changeFreqNever()
.lastModNow()
.build()
);
sitemapGenerator.addPage(WebPage.builder()
.name("latest.php")
.build()
);
sitemapGenerator.addPage(WebPage.builder()
.name("contact.php")
.build()
);
sg.addPage(WebPage.builder().nameRoot().priorityMax().changeFreqNever().lastModNow().build())
.addPage(WebPage.builder().name("latest.php").build())
.addPage(WebPage.builder().name("contact.php").build());
// generate sitemap and save it to file /var/www/sitemap.xml
File file = new File("/var/www/sitemap.xml");
sitemapGenerator.constructAndSaveSitemap(file);
sg.constructAndSaveSitemap(file);
// inform Google that this sitemap has changed
sitemapGenerator.pingGoogle();
sg.pingGoogle();

### How to create a sitemap populated with list of pages:

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")
.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:

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.0</version>
<version>3.1</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
@@ -1,12 +1,13 @@
package cz.jiripinkas.jsitemapgenerator;

import cz.jiripinkas.jsitemapgenerator.exception.InvalidUrlException;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;

import cz.jiripinkas.jsitemapgenerator.exception.InvalidUrlException;
import java.util.function.Function;

public abstract class AbstractGenerator {

Expand Down Expand Up @@ -69,4 +70,19 @@ public AbstractGenerator addPages(Collection<WebPage> webPages) {
return this;
}

/**
* Add collection of pages to sitemap
*
* @param <T> This is the type parameter
* @param webPages Collection of pages
* @param mapper Mapper function which transforms some object to WebPage
* @return this
*/
public <T> AbstractGenerator addPages(Collection<T> webPages, Function<T, WebPage> mapper) {
for (T element : webPages) {
addPage(mapper.apply(element));
}
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;

public class RssGenerator extends AbstractGenerator {

Expand Down Expand Up @@ -122,5 +123,19 @@ public RssGenerator addPages(Collection<WebPage> webPages) {
return this;
}

/**
* Add collection of pages to sitemap
*
* @param webPages Collection of pages
* @param mapper Mapper function which transforms some object to WebPage
* @return this
*/
public <T> RssGenerator addPages(Collection<T> webPages, Function<T, WebPage> mapper) {
for (T element : webPages) {
addPage(mapper.apply(element));
}
return this;
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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 @@ -11,6 +12,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Function;

public class SitemapGenerator extends AbstractSitemapGenerator {

Expand Down Expand Up @@ -154,4 +156,18 @@ public SitemapGenerator addPages(Collection<WebPage> webPages) {
return this;
}

/**
* Add collection of pages to sitemap
*
* @param webPages Collection of pages
* @param mapper Mapper function which transforms some object to WebPage
* @return this
*/
public <T> SitemapGenerator addPages(Collection<T> webPages, Function<T, WebPage> mapper) {
for (T element : webPages) {
addPage(mapper.apply(element));
}
return this;
}

}

0 comments on commit 6002436

Please sign in to comment.