Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate java.util.Date to java.time.OffsetDateTime #31

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ First, add this as a dependency to your POM:
<dependency>
<groupId>io.gdcc</groupId>
<artifactId>sitemapgen4j</artifactId>
<version>2.1.0</version>
<version>2.2.0</version>
</dependency>
```

Expand All @@ -59,12 +59,18 @@ wsg.write();
To configure the URLs, construct a WebSitemapUrl with WebSitemapUrl.Options.

```java
import java.time.OffsetDateTime;

WebSitemapGenerator wsg = new WebSitemapGenerator("https://www.example.com", myDir);
WebSitemapUrl url = new WebSitemapUrl.Options("https://www.example.com/index.html")
.lastMod(new Date()).priority(1.0).changeFreq(ChangeFreq.HOURLY).build();
.lastMod(OffsetDateTime.now()).priority(1.0).changeFreq(ChangeFreq.HOURLY).build();
// this will configure the URL with lastmod=now, priority=1.0, changefreq=hourly
wsg.addUrl(url);
wsg.write();
wsg.

addUrl(url);
wsg.

write();
```

## Configuring the date format
Expand All @@ -74,8 +80,8 @@ One important configuration option for the sitemap generator is the date format.
```java

// Use DAY pattern (2009-02-07), Greenwich Mean Time timezone
W3CDateFormat dateFormat = new W3CDateFormat(Pattern.DAY);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
ZoneId zoneId = TimeZone.getTimeZone("GMT").toZoneId();
W3CDateFormat dateFormat = W3CDateFormat.DAY.withZone(zoneId);
WebSitemapGenerator wsg = WebSitemapGenerator.builder("https://www.example.com", myDir)
.dateFormat(dateFormat).build(); // actually use the configured dateFormat
wsg.addUrl("https://www.example.com/index.html");
Expand Down
1 change: 0 additions & 1 deletion TODO.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
Migrate to java.time
Migrate to java.nio

Ping search engines
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>io.gdcc</groupId>
<artifactId>sitemapgen4j</artifactId>
<packaging>jar</packaging>
<version>2.1.3-SNAPSHOT</version>
<version>2.2.0-SNAPSHOT</version>
<name>SitemapGen4J</name>

<url>https://github.com/gdcc/sitemapgen4j/</url>
Expand All @@ -25,7 +25,7 @@
<connection>scm:git:git://github.com:gdcc/sitemapgen4j.git</connection>
<developerConnection>scm:git:[email protected]:gdcc/sitemapgen4j.git</developerConnection>
<url>https://github.com/gdcc/sitemapgen4j/</url>
<tag>sitemapgen4j-2.1.2</tag>
<tag>sitemapgen4j-2.2.0</tag>
</scm>
<issueManagement>
<url>https://github.com/gdcc/sitemapgen4j/issues</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.net.URL;
import java.time.format.DateTimeFormatter;

// that weird thing with generics is so sub-classed objects will return themselves
// It makes sense, I swear! https://madbean.com/2004/mb2004-3/
Expand Down Expand Up @@ -55,7 +56,7 @@ public T allowMultipleSitemaps(boolean allowMultipleSitemaps) {
this.allowMultipleSitemaps = allowMultipleSitemaps;
return getThis();
}
/** The date formatter, typically configured with a {@link W3CDateFormat.Pattern} and/or a time zone */
/** The date formatter, typically configured with a {@link W3CDateFormat} and/or a time zone */
public T dateFormat(W3CDateFormat dateFormat) {
this.dateFormat = dateFormat;
return getThis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.util.Date;
import java.time.OffsetDateTime;

/** Container for optional URL parameters */
//that weird thing with generics is so sub-classed objects will return themselves
//It makes sense, I swear! https://madbean.com/2004/mb2004-3/
abstract class AbstractSitemapUrlOptions<U extends WebSitemapUrl, T extends AbstractSitemapUrlOptions<U,T>> {
Date lastMod;
OffsetDateTime lastMod;
ChangeFreq changeFreq;
Double priority;
URL url;
Expand All @@ -31,7 +31,7 @@ abstract class AbstractSitemapUrlOptions<U extends WebSitemapUrl, T extends Abst
* return, and search engines may use the information from both sources
* differently.
*/
public T lastMod(Date lastMod) {
public T lastMod(OffsetDateTime lastMod) {
this.lastMod = lastMod;
return getThis();
}
Expand All @@ -45,7 +45,7 @@ public T lastMod(Date lastMod) {
* @see W3CDateFormat
*/
public T lastMod(String lastMod) throws ParseException {
this.lastMod = new W3CDateFormat().parse(lastMod);
this.lastMod = W3CDateFormat.AUTO.parse(lastMod);
return getThis();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.redfin.sitemapgenerator;

import java.io.File;
import java.net.*;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;

Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/redfin/sitemapgenerator/GoogleNewsSitemapUrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.net.MalformedURLException;
import java.net.URL;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.Date;

/**
* One configurable Google News Search URL. To configure, use {@link Options}
Expand All @@ -13,35 +13,35 @@
*/
public class GoogleNewsSitemapUrl extends WebSitemapUrl {

private final Date publicationDate;
private final OffsetDateTime publicationDate;
private final String keywords;
private final String genres;
private final String title;
private final GoogleNewsPublication publication;

/** Options to configure Google News URLs */
public static class Options extends AbstractSitemapUrlOptions<GoogleNewsSitemapUrl, Options> {
private final Date publicationDate;
private final OffsetDateTime publicationDate;
private String keywords;
private String genres;
private final String title;
private final GoogleNewsPublication publication;

/** Specifies a URL and publication date (which is mandatory for Google News) */
public Options(String url, Date publicationDate, String title, GoogleNewsPublication publication) throws MalformedURLException {
public Options(String url, OffsetDateTime publicationDate, String title, GoogleNewsPublication publication) throws MalformedURLException {
this(new URL(url), publicationDate, title, publication);
}

public Options(String url, Date publicationDate, String title, String name, String language) throws MalformedURLException {
public Options(String url, OffsetDateTime publicationDate, String title, String name, String language) throws MalformedURLException {
this(new URL(url), publicationDate, title, new GoogleNewsPublication(name, language));
}

public Options(URL url, Date publicationDate, String title, String name, String language) {
public Options(URL url, OffsetDateTime publicationDate, String title, String name, String language) {
this(url, publicationDate, title, new GoogleNewsPublication(name, language));
}

/** Specifies a URL and publication date (which is mandatory for Google News) */
public Options(URL url, Date publicationDate, String title, GoogleNewsPublication publication) {
public Options(URL url, OffsetDateTime publicationDate, String title, GoogleNewsPublication publication) {
super(url, GoogleNewsSitemapUrl.class);
if (publicationDate == null) throw new NullPointerException("publicationDate must not be null");
this.publicationDate = publicationDate;
Expand Down Expand Up @@ -101,22 +101,22 @@ public Options genres(String... genres) {
}

/** Specifies a URL and publication date, title and publication (which are mandatory for Google News) */
public GoogleNewsSitemapUrl(URL url, Date publicationDate, String title, String name, String language) {
public GoogleNewsSitemapUrl(URL url, OffsetDateTime publicationDate, String title, String name, String language) {
this(new Options(url, publicationDate, title, name, language));
}

/** Specifies a URL and publication date, title and publication (which are mandatory for Google News) */
public GoogleNewsSitemapUrl(URL url, Date publicationDate, String title, GoogleNewsPublication publication) {
public GoogleNewsSitemapUrl(URL url, OffsetDateTime publicationDate, String title, GoogleNewsPublication publication) {
this(new Options(url, publicationDate, title, publication));
}

/** Specifies a URL and publication date, title and publication (which are mandatory for Google News) */
public GoogleNewsSitemapUrl(String url, Date publicationDate, String title, String name, String language) throws MalformedURLException {
public GoogleNewsSitemapUrl(String url, OffsetDateTime publicationDate, String title, String name, String language) throws MalformedURLException {
this(new Options(url, publicationDate, title, name, language));
}

/** Specifies a URL and publication date, title and publication (which are mandatory for Google News) */
public GoogleNewsSitemapUrl(String url, Date publicationDate, String title, GoogleNewsPublication publication) throws MalformedURLException {
public GoogleNewsSitemapUrl(String url, OffsetDateTime publicationDate, String title, GoogleNewsPublication publication) throws MalformedURLException {
this(new Options(url, publicationDate, title, publication));
}

Expand All @@ -131,7 +131,7 @@ public GoogleNewsSitemapUrl(Options options) {
}

/** Retrieves the publication date */
public Date getPublicationDate() {
public OffsetDateTime getPublicationDate() {
return publicationDate;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.redfin.sitemapgenerator;

import java.net.URL;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/** One configurable Google Video Search URL. To configure, use {@link Options}
Expand All @@ -21,7 +21,7 @@ public class GoogleVideoSitemapUrl extends WebSitemapUrl {
private final String description;
private final Double rating;
private final Integer viewCount;
private final Date publicationDate;
private final OffsetDateTime publicationDate;
private final List<String> tags;
private final String category;
// TODO can there be multiple categories?
Expand All @@ -40,7 +40,7 @@ public static class Options extends AbstractSitemapUrlOptions<GoogleVideoSitemap
private String description;
private Double rating;
private Integer viewCount;
private Date publicationDate;
private OffsetDateTime publicationDate;
private List<String> tags;
private String category;
// TODO can there be multiple categories?
Expand Down Expand Up @@ -137,7 +137,7 @@ public Options viewCount(int viewCount) {
}

/** The date the video was first published, in {@link W3CDateFormat}. */
public Options publicationDate(Date publicationDate) {
public Options publicationDate(OffsetDateTime publicationDate) {
this.publicationDate = publicationDate;
return this;
}
Expand Down Expand Up @@ -308,7 +308,7 @@ public Integer getViewCount() {
}

/** Retrieves the {@link Options#publicationDate}*/
public Date getPublicationDate() {
public OffsetDateTime getPublicationDate() {
return publicationDate;
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/redfin/sitemapgenerator/ISitemapUrl.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.redfin.sitemapgenerator;

import java.net.URL;
import java.util.Date;
import java.time.OffsetDateTime;

public interface ISitemapUrl {

Date getLastMod();
OffsetDateTime getLastMod();

URL getUrl();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public SitemapGenerator(AbstractSitemapGeneratorOptions<?> options, ISitemapUrlR
baseDir = options.baseDir;
baseUrl = options.baseUrl;
fileNamePrefix = options.fileNamePrefix;
W3CDateFormat dateFormat = options.dateFormat;
if (dateFormat == null) dateFormat = new W3CDateFormat();
this.dateFormat = dateFormat;
W3CDateFormat dateFormatter = options.dateFormat;
if (dateFormatter == null) dateFormatter = W3CDateFormat.AUTO;
this.dateFormat = dateFormatter;
allowEmptySitemap = options.allowEmptySitemap;
allowMultipleSitemaps = options.allowMultipleSitemaps;
maxUrls = options.maxUrls;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
Expand All @@ -24,7 +26,7 @@ public class SitemapIndexGenerator {
private final List<SitemapIndexUrl> urls = new ArrayList<>();
private final int maxUrls;
private final W3CDateFormat dateFormat;
private final Date defaultLastMod;
private final OffsetDateTime defaultLastMod;
private final boolean autoValidate;

/** Options to configure sitemap index generation */
Expand All @@ -34,7 +36,7 @@ public static class Options {
private W3CDateFormat dateFormat = null;
private boolean allowEmptyIndex = false;
private int maxUrls = SitemapConstants.MAX_SITEMAPS_PER_INDEX;
private Date defaultLastMod = new Date();
private OffsetDateTime defaultLastMod = OffsetDateTime.now();
private boolean autoValidate = false;
// TODO GZIP? Is that legal for a sitemap index?

Expand All @@ -55,7 +57,7 @@ public Options(URL baseUrl, File outFile) {
public Options(String baseUrl, File outFile) throws MalformedURLException {
this(new URL(baseUrl), outFile);
}
/** The date formatter, typically configured with a {@link W3CDateFormat.Pattern} and/or a time zone */
/** The date formatter, typically configured with a {@link W3CDateFormat} and/or a time zone */
public Options dateFormat(W3CDateFormat dateFormat) {
this.dateFormat = dateFormat;
return this;
Expand Down Expand Up @@ -89,7 +91,7 @@ Options maxUrls(int maxUrls) {
* now, but you can pass in null to omit a lastMod entirely. We don't
* recommend this; Google may not like you as much.
*/
public Options defaultLastMod(Date defaultLastMod) {
public Options defaultLastMod(OffsetDateTime defaultLastMod) {
this.defaultLastMod = defaultLastMod;
return this;
}
Expand Down Expand Up @@ -132,9 +134,9 @@ private SitemapIndexGenerator(Options options) {
this.outFile = options.outFile;
this.allowEmptyIndex = options.allowEmptyIndex;
this.maxUrls = options.maxUrls;
W3CDateFormat dateFormat = options.dateFormat;
if (dateFormat == null) dateFormat = new W3CDateFormat();
this.dateFormat = dateFormat;
W3CDateFormat dateFormatter = options.dateFormat;
if (dateFormatter == null) dateFormatter = W3CDateFormat.AUTO;
this.dateFormat = dateFormatter;
this.defaultLastMod = options.defaultLastMod;
this.autoValidate = options.autoValidate;
}
Expand Down Expand Up @@ -184,12 +186,12 @@ public SitemapIndexGenerator addUrl(URL url) {
}

/** Adds a single sitemap to the index */
public SitemapIndexGenerator addUrl(URL url, Date lastMod) {
public SitemapIndexGenerator addUrl(URL url, OffsetDateTime lastMod) {
return addUrl(new SitemapIndexUrl(url, lastMod));
}

/** Adds a single sitemap to the index */
public SitemapIndexGenerator addUrl(String url, Date lastMod) throws MalformedURLException {
public SitemapIndexGenerator addUrl(String url, OffsetDateTime lastMod) throws MalformedURLException {
return addUrl(new SitemapIndexUrl(url, lastMod));
}

Expand Down Expand Up @@ -258,7 +260,7 @@ private void writeAsString(StringBuilder sb) {
sb.append(" <loc>");
sb.append(UrlUtils.escapeXml(url.url.toString()));
sb.append("</loc>\n");
Date lastMod = url.lastMod;
OffsetDateTime lastMod = url.lastMod;

if (lastMod == null) lastMod = defaultLastMod;

Expand Down
Loading