Skip to content

Commit

Permalink
Fix namespace URI from HTTPS to HTTP
Browse files Browse the repository at this point in the history
with centralization of constants in one class
  • Loading branch information
jeromeroucou committed Mar 7, 2024
1 parent 1bee6f2 commit d5a1097
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract class AbstractSitemapGeneratorOptions<T extends AbstractSitemapGenerato
boolean allowMultipleSitemaps = true;
String suffixStringPattern; // this will store some type of string pattern suitable per needs.
W3CDateFormat dateFormat;
int maxUrls = SitemapGenerator.MAX_URLS_PER_SITEMAP;
int maxUrls = SitemapConstants.MAX_URLS_PER_SITEMAP;
boolean autoValidate = false;
boolean gzip = false;

Expand Down Expand Up @@ -62,12 +62,14 @@ public T dateFormat(W3CDateFormat dateFormat) {
}
/**
* The maximum number of URLs to allow per sitemap; the default is the
* maximum allowed (50,000), but you can decrease it if you wish (to make
* your auto-generated sitemaps smaller)
* maximum allowed (see {@link SitemapConstants#MAX_URLS_PER_SITEMAP}), but you
* can decrease it if you wish (to make your auto-generated sitemaps smaller)
*/
public T maxUrls(int maxUrls) {
if (maxUrls > SitemapGenerator.MAX_URLS_PER_SITEMAP) {
throw new SitemapException("You can only have 50,000 URLs per sitemap; to use more, allowMultipleSitemaps and generate a sitemap index. You asked for " + maxUrls);
if (maxUrls > SitemapConstants.MAX_URLS_PER_SITEMAP) {
throw new SitemapException(String.format(
"You can only have %d URLs per sitemap; to use more, allowMultipleSitemaps and generate a sitemap index. You asked for %d",
SitemapConstants.MAX_URLS_PER_SITEMAP, maxUrls));
}
this.maxUrls = maxUrls;
return getThis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,25 @@ public static SitemapGeneratorBuilder<GoogleImageSitemapGenerator> builder(Strin

private static class Renderer extends AbstractSitemapUrlRenderer<GoogleImageSitemapUrl> implements ISitemapUrlRenderer<GoogleImageSitemapUrl> {

private static final String IMAGE_NS = "image";

public Class<GoogleImageSitemapUrl> getUrlClass() {
return GoogleImageSitemapUrl.class;
}

public String getXmlNamespaces() {
return "xmlns:image=\"https://www.google.com/schemas/sitemap-image/1.1\"";
return String.format("xmlns:%s=\"%s\"", SitemapConstants.GOOGLE_IMAGE_NS, SitemapConstants.GOOGLE_IMAGE_NS_URI);
}

public void render(GoogleImageSitemapUrl url, StringBuilder sb, W3CDateFormat dateFormat) {
StringBuilder tagSb = new StringBuilder();

for(Image image : url.getImages()) {
tagSb.append(" <").append(IMAGE_NS).append(":image>\n");
renderTag(tagSb, IMAGE_NS, "loc", image.getUrl());
renderTag(tagSb, IMAGE_NS, "caption", image.getCaption());
renderTag(tagSb, IMAGE_NS, "title", image.getTitle());
renderTag(tagSb, IMAGE_NS, "geo_location", image.getGeoLocation());
renderTag(tagSb, IMAGE_NS, "license", image.getLicense());
tagSb.append(" </").append(IMAGE_NS).append(":image>\n");
tagSb.append(" <").append(SitemapConstants.GOOGLE_IMAGE_NS).append(":image>\n");
renderTag(tagSb, SitemapConstants.GOOGLE_IMAGE_NS, "loc", image.getUrl());
renderTag(tagSb, SitemapConstants.GOOGLE_IMAGE_NS, "caption", image.getCaption());
renderTag(tagSb, SitemapConstants.GOOGLE_IMAGE_NS, "title", image.getTitle());
renderTag(tagSb, SitemapConstants.GOOGLE_IMAGE_NS, "geo_location", image.getGeoLocation());
renderTag(tagSb, SitemapConstants.GOOGLE_IMAGE_NS, "license", image.getLicense());
tagSb.append(" </").append(SitemapConstants.GOOGLE_IMAGE_NS).append(":image>\n");
}
super.render(url, sb, dateFormat, tagSb.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Class<GoogleLinkSitemapUrl> getUrlClass() {

public String getXmlNamespaces() {

return "xmlns:xhtml=\"https://www.w3.org/1999/xhtml\"";
return String.format("xmlns:xhtml=\"%s\"", SitemapConstants.GOOGLE_LINK_NS_URI);
}

public void render(final GoogleLinkSitemapUrl url, final StringBuilder sb, final W3CDateFormat dateFormat) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
* @see <a href="https://developers.google.com/search/docs/crawling-indexing/sitemaps/news-sitemap">Google Developer: News Sitemap</a>
*/
public class GoogleNewsSitemapGenerator extends SitemapGenerator<GoogleNewsSitemapUrl,GoogleNewsSitemapGenerator> {

/** 1000 URLs max in a Google News sitemap. */
public static final int MAX_URLS_PER_SITEMAP = 1000;

/** Configures a builder so you can specify sitemap generator options
*
Expand All @@ -36,14 +33,15 @@ public static SitemapGeneratorBuilder<GoogleNewsSitemapGenerator> builder(URL ba
public static SitemapGeneratorBuilder<GoogleNewsSitemapGenerator> builder(String baseUrl, File baseDir) throws MalformedURLException {
SitemapGeneratorBuilder<GoogleNewsSitemapGenerator> builder =
new SitemapGeneratorBuilder<>(baseUrl, baseDir, GoogleNewsSitemapGenerator.class);
builder.maxUrls = GoogleNewsSitemapGenerator.MAX_URLS_PER_SITEMAP;
builder.maxUrls = SitemapConstants.GOOGLE_NEWS_MAX_URLS_PER_SITEMAP;
return builder;
}

GoogleNewsSitemapGenerator(AbstractSitemapGeneratorOptions<?> options) {
super(options, new Renderer());
if (options.maxUrls > GoogleNewsSitemapGenerator.MAX_URLS_PER_SITEMAP) {
throw new SitemapException("Google News sitemaps can have only 1000 URLs per sitemap: " + options.maxUrls);
if (options.maxUrls > SitemapConstants.GOOGLE_NEWS_MAX_URLS_PER_SITEMAP) {
throw new SitemapException(String.format("Google News sitemaps can have only %d URLs per sitemap: %d",
SitemapConstants.GOOGLE_NEWS_MAX_URLS_PER_SITEMAP, options.maxUrls));
}
}

Expand Down Expand Up @@ -89,28 +87,26 @@ public GoogleNewsSitemapGenerator(URL baseUrl) {

private static class Renderer extends AbstractSitemapUrlRenderer<GoogleNewsSitemapUrl> implements ISitemapUrlRenderer<GoogleNewsSitemapUrl> {

private static final String NEWS_NS = "news";

public Class<GoogleNewsSitemapUrl> getUrlClass() {
return GoogleNewsSitemapUrl.class;
}

public String getXmlNamespaces() {
return "xmlns:news=\"https://www.google.com/schemas/sitemap-news/0.9\"";
return String.format("xmlns:%s=\"%s\"", SitemapConstants.GOOGLE_NEWS_NS, SitemapConstants.GOOGLE_NEWS_NS_URI);
}

public void render(GoogleNewsSitemapUrl url, StringBuilder sb, W3CDateFormat dateFormat) {
StringBuilder tagSb = new StringBuilder();
tagSb.append(" <").append(NEWS_NS).append(":news>\n");
tagSb.append(" <").append(NEWS_NS).append(":publication>\n");
renderSubTag(tagSb, NEWS_NS, "name", url.getPublication().getName());
renderSubTag(tagSb, NEWS_NS, "language", url.getPublication().getLanguage());
tagSb.append(" </").append(NEWS_NS).append(":publication>\n");
renderTag(tagSb, NEWS_NS, "genres", url.getGenres());
renderTag(tagSb, NEWS_NS, "publication_date", dateFormat.format(url.getPublicationDate()));
renderTag(tagSb, NEWS_NS, "title", url.getTitle());
renderTag(tagSb, NEWS_NS, "keywords", url.getKeywords());
tagSb.append(" </").append(NEWS_NS).append(":news>\n");
tagSb.append(" <").append(SitemapConstants.GOOGLE_NEWS_NS).append(":news>\n");
tagSb.append(" <").append(SitemapConstants.GOOGLE_NEWS_NS).append(":publication>\n");
renderSubTag(tagSb, SitemapConstants.GOOGLE_NEWS_NS, "name", url.getPublication().getName());
renderSubTag(tagSb, SitemapConstants.GOOGLE_NEWS_NS, "language", url.getPublication().getLanguage());
tagSb.append(" </").append(SitemapConstants.GOOGLE_NEWS_NS).append(":publication>\n");
renderTag(tagSb, SitemapConstants.GOOGLE_NEWS_NS, "genres", url.getGenres());
renderTag(tagSb, SitemapConstants.GOOGLE_NEWS_NS, "publication_date", dateFormat.format(url.getPublicationDate()));
renderTag(tagSb, SitemapConstants.GOOGLE_NEWS_NS, "title", url.getTitle());
renderTag(tagSb, SitemapConstants.GOOGLE_NEWS_NS, "keywords", url.getKeywords());
tagSb.append(" </").append(SitemapConstants.GOOGLE_NEWS_NS).append(":news>\n");
super.render(url, sb, dateFormat, tagSb.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,42 +77,41 @@ public GoogleVideoSitemapGenerator(URL baseUrl) {

private static class Renderer extends AbstractSitemapUrlRenderer<GoogleVideoSitemapUrl> implements ISitemapUrlRenderer<GoogleVideoSitemapUrl> {

private static final String VIDEO_NS = "video";

public Class<GoogleVideoSitemapUrl> getUrlClass() {
return GoogleVideoSitemapUrl.class;
}

public String getXmlNamespaces() {
return "xmlns:video=\"https://www.google.com/schemas/sitemap-video/1.1\"";
return String.format("xmlns:%s=\"%s\"", SitemapConstants.GOOGLE_VIDEO_NS, SitemapConstants.GOOGLE_VIDEO_NS_URI);
}

public void render(GoogleVideoSitemapUrl url, StringBuilder sb, W3CDateFormat dateFormat) {
StringBuilder tagSb = new StringBuilder();
tagSb.append(" <").append(VIDEO_NS).append(":video>\n");
renderTag(tagSb, VIDEO_NS, "content_loc", url.getContentUrl());
tagSb.append(" <").append(SitemapConstants.GOOGLE_VIDEO_NS).append(":video>\n");
renderTag(tagSb, SitemapConstants.GOOGLE_VIDEO_NS, "content_loc", url.getContentUrl());
if (url.getPlayerUrl() != null) {
tagSb.append(" <").append(VIDEO_NS).append(":player_loc allow_embed=\"").append(url.getAllowEmbed()).append("\">");
tagSb.append(" <").append(SitemapConstants.GOOGLE_VIDEO_NS).append(":player_loc allow_embed=\"")
.append(url.getAllowEmbed()).append("\">");
tagSb.append(url.getPlayerUrl());
tagSb.append("</").append(VIDEO_NS).append(":player_loc>\n");
tagSb.append("</").append(SitemapConstants.GOOGLE_VIDEO_NS).append(":player_loc>\n");
}
renderTag(tagSb, VIDEO_NS, "thumbnail_loc", url.getThumbnailUrl());
renderTag(tagSb, VIDEO_NS, "title", url.getTitle());
renderTag(tagSb, VIDEO_NS, "description", url.getDescription());
renderTag(tagSb, VIDEO_NS, "rating", url.getRating());
renderTag(tagSb, VIDEO_NS, "view_count", url.getViewCount());
renderTag(tagSb, SitemapConstants.GOOGLE_VIDEO_NS, "thumbnail_loc", url.getThumbnailUrl());
renderTag(tagSb, SitemapConstants.GOOGLE_VIDEO_NS, "title", url.getTitle());
renderTag(tagSb, SitemapConstants.GOOGLE_VIDEO_NS, "description", url.getDescription());
renderTag(tagSb, SitemapConstants.GOOGLE_VIDEO_NS, "rating", url.getRating());
renderTag(tagSb, SitemapConstants.GOOGLE_VIDEO_NS, "view_count", url.getViewCount());
if (url.getPublicationDate() != null) {
renderTag(tagSb, VIDEO_NS, "publication_date", dateFormat.format(url.getPublicationDate()));
renderTag(tagSb, SitemapConstants.GOOGLE_VIDEO_NS, "publication_date", dateFormat.format(url.getPublicationDate()));
}
if (url.getTags() != null) {
for (String tag : url.getTags()) {
renderTag(tagSb, VIDEO_NS, "tag", tag);
renderTag(tagSb, SitemapConstants.GOOGLE_VIDEO_NS, "tag", tag);
}
}
renderTag(tagSb, VIDEO_NS, "category", url.getCategory());
renderTag(tagSb, VIDEO_NS, "family_friendly", url.getFamilyFriendly());
renderTag(tagSb, VIDEO_NS, "duration", url.getDurationInSeconds());
tagSb.append(" </").append(VIDEO_NS).append(":video>\n");
renderTag(tagSb, SitemapConstants.GOOGLE_VIDEO_NS, "category", url.getCategory());
renderTag(tagSb, SitemapConstants.GOOGLE_VIDEO_NS, "family_friendly", url.getFamilyFriendly());
renderTag(tagSb, SitemapConstants.GOOGLE_VIDEO_NS, "duration", url.getDurationInSeconds());
tagSb.append(" </").append(SitemapConstants.GOOGLE_VIDEO_NS).append(":video>\n");
super.render(url, sb, dateFormat, tagSb.toString());
}

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

/**
* Utility class to contain basic values as constants.
**/
public final class SitemapConstants {

/**
* Private constructor to prevent instantiation.
*/
private SitemapConstants() {
}

/**
* Google Image sitemap namespace attribute.
*
* <p>See {@link GoogleImageSitemapGenerator} for more information.
*/
public static final String GOOGLE_IMAGE_NS = "image";

/**
* Google Image sitemap namespace URI.
*
* <p>See {@link GoogleImageSitemapGenerator} for more information.
*/
public static final String GOOGLE_IMAGE_NS_URI = "http://www.google.com/schemas/sitemap-image/1.1";

/**
* @see <a href="https://developers.google.com/search/docs/specialty/international/localized-versions#sitemap">
* Google Developers: Sitemap for alternate pages
*/
public static final String GOOGLE_LINK_NS_URI = "http://www.w3.org/1999/xhtml";

/**
* 1000 URLs max in a Google News sitemap file.
*
* <p>See {@link GoogleNewsSitemapGenerator} for more information.
*/
public static final int GOOGLE_NEWS_MAX_URLS_PER_SITEMAP = 1000;

/**
* Google News sitemap namespace attribute.
*
* <p>See {@link GoogleNewsSitemapGenerator} for more information.
*/
public static final String GOOGLE_NEWS_NS = "news";

/**
* Google News sitemap namespace URI.
*
* <p>See {@link GoogleNewsSitemapGenerator} for more information.
*/
public static final String GOOGLE_NEWS_NS_URI = "http://www.google.com/schemas/sitemap-news/0.9";

/**
* Google Video sitemap namespace attribute.
*
* <p>See {@link GoogleVideoSitemapGenerator} for more information.
*/
public static final String GOOGLE_VIDEO_NS = "video";

/**
* Google Video sitemap namespace URI.
*
* <p>See {@link GoogleVideoSitemapGenerator} for more information.
*/
public static final String GOOGLE_VIDEO_NS_URI = "http://www.google.com/schemas/sitemap-video/1.1";

/**
* Maximum 50000 URLs per sitemap file.
*
* @see <a href="https://www.sitemaps.org/protocol.html">
* Sitemaps XML protocol</a>
*/
public static final int MAX_URLS_PER_SITEMAP = 50000;

/** Maximum 50000 sitemaps per index allowed.
*
* @see <a href="https://www.sitemaps.org/protocol.html#index">
* Sitemaps XML protocol</a>
*/
public static final int MAX_SITEMAPS_PER_INDEX = 50000;

/**
* File name of sitemap index files, to group multiple sitemap files.
*
* @see <a href="https://www.sitemaps.org/protocol.html#index">
* Sitemaps XML protocol</a>
*/
public static final String SITEMAP_INDEX_FILE = "sitemap_index.xml";

/**
* Sitemap namespace URI to use with <code>sitemap.xml</code> file.
*
* @see <a href="https://www.sitemaps.org/protocol.html">
* Sitemaps XML protocol</a>
*/
public static final String SITEMAP_NS_URI = "http://www.sitemaps.org/schemas/sitemap/0.9";

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
import java.util.zip.GZIPOutputStream;

abstract class SitemapGenerator<U extends ISitemapUrl, T extends SitemapGenerator<U,T>> {
/** 50000 URLs per sitemap maximum */
public static final int MAX_URLS_PER_SITEMAP = 50000;

private final URL baseUrl;
private final File baseDir;
private final String fileNamePrefix;
Expand Down Expand Up @@ -193,7 +190,7 @@ public List<String> writeAsStrings() {

private void writeSiteMapAsString(StringBuilder sb, List<U> urls) {
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
sb.append("<urlset xmlns=\"https://www.sitemaps.org/schemas/sitemap/0.9\" ");
sb.append(String.format("<urlset xmlns=\"%s\" ", SitemapConstants.SITEMAP_NS_URI));
if (renderer.getXmlNamespaces() != null) {
sb.append(renderer.getXmlNamespaces());
sb.append(' ');
Expand All @@ -210,7 +207,7 @@ private void writeSiteMapAsString(StringBuilder sb, List<U> urls) {
* The sitemap index is written to {baseDir}/sitemap_index.xml
*/
public File writeSitemapsWithIndex() {
return writeSitemapsWithIndex(new File(baseDir, "sitemap_index.xml"));
return writeSitemapsWithIndex(new File(baseDir, SitemapConstants.SITEMAP_INDEX_FILE));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ public class SitemapIndexGenerator {
private final W3CDateFormat dateFormat;
private final Date defaultLastMod;
private final boolean autoValidate;
/** Maximum 50,000 sitemaps per index allowed */
public static final int MAX_SITEMAPS_PER_INDEX = 50000;

/** Options to configure sitemap index generation */
public static class Options {
private final URL baseUrl;
private final File outFile;
private W3CDateFormat dateFormat = null;
private boolean allowEmptyIndex = false;
private int maxUrls = MAX_SITEMAPS_PER_INDEX;
private int maxUrls = SitemapConstants.MAX_SITEMAPS_PER_INDEX;
private Date defaultLastMod = new Date();
private boolean autoValidate = false;
// TODO GZIP? Is that legal for a sitemap index?
Expand Down Expand Up @@ -76,11 +74,12 @@ public Options allowEmptyIndex(boolean allowEmptyIndex) {

/**
* The maximum number of sitemaps to allow per sitemap index; the default is the
* maximum allowed (1,000), but you can decrease it if you wish (for testing)
* maximum allowed (see {@link SitemapConstants#MAX_SITEMAPS_PER_INDEX}), but
* you can decrease it if you wish (for testing)
*/
Options maxUrls(int maxUrls) {
if (maxUrls > MAX_SITEMAPS_PER_INDEX) {
throw new SitemapException("You can't have more than 1000 sitemaps per index");
if (maxUrls > SitemapConstants.MAX_SITEMAPS_PER_INDEX) {
throw new SitemapException(String.format("You can't have more than %d sitemaps per index", SitemapConstants.MAX_SITEMAPS_PER_INDEX));
}
this.maxUrls = maxUrls;
return this;
Expand Down Expand Up @@ -253,7 +252,7 @@ public String writeAsString() {

private void writeAsString(StringBuilder sb) {
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
sb.append("<sitemapindex xmlns=\"https://www.sitemaps.org/schemas/sitemap/0.9\">\n");
sb.append(String.format("<sitemapindex xmlns=\"%s\">\n", SitemapConstants.SITEMAP_NS_URI));
for (SitemapIndexUrl url : urls) {
sb.append(" <sitemap>\n");
sb.append(" <loc>");
Expand Down
Loading

0 comments on commit d5a1097

Please sign in to comment.