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

add alternate links support #11

Merged
merged 4 commits into from
Dec 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/main/java/cz/jiripinkas/jsitemapgenerator/WebPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

public class WebPage implements Comparable<WebPage> {
private String name;
private Map<String, String> alternateNames;
private Date lastMod;
private ChangeFreq changeFreq;
private Double priority;
Expand All @@ -22,6 +23,14 @@ public WebPage addImage(Image image) {
return this;
}

public WebPage addAlternateName(String language, String name) {
if (alternateNames == null) {
alternateNames = new HashMap<>();
}
alternateNames.put(language, name);
return this;
}

public List<Image> getImages() {
return images;
}
Expand Down Expand Up @@ -50,6 +59,10 @@ public void setName(String name) {
this.name = name;
}

public void setAlternateNames(Map<String, String> alternateNames) {
this.alternateNames = alternateNames;
}

public void setLastMod(Date lastMod) {
this.lastMod = lastMod;
}
Expand All @@ -69,6 +82,10 @@ public String getName() {
return name;
}

public Map<String, String> getAlternateNames() {
return alternateNames;
}

public Date getLastMod() {
return lastMod;
}
Expand Down Expand Up @@ -152,6 +169,18 @@ public WebPageBuilder name(String ... nameAndDirs) {
return this;
}

/**
* Sets WebPage alternate name
*
* @param language Alternate language
* @param name Name
* @return this
*/
public WebPageBuilder alternateName(String language, String name) {
webPage.addAlternateName(language, name);
return this;
}

/**
* Sets prefix dir to name. Final name will be "dirName/name"
* @param dirName Dir name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.*;
import java.util.function.Function;

public class SitemapGenerator extends AbstractSitemapGenerator {

public enum AdditionalNamespace {
IMAGE
IMAGE, XHTML
}

private StringBuilder additionalNamespacesStringBuilder = new StringBuilder();
Expand Down Expand Up @@ -48,9 +45,14 @@ public SitemapGenerator(String baseUrl) {
@Deprecated
public SitemapGenerator(String baseUrl, AdditionalNamespace[] additionalNamespaces) {
this(baseUrl);
if (Arrays.asList(additionalNamespaces).contains(AdditionalNamespace.IMAGE)) {

List<AdditionalNamespace> additionalNamespaceList = Arrays.asList(additionalNamespaces);
if (additionalNamespaceList.contains(AdditionalNamespace.IMAGE)) {
additionalNamespacesStringBuilder.append(" xmlns:image=\"http://www.google.com/schemas/sitemap-image/1.1\" ");
}
if (additionalNamespaceList.contains(AdditionalNamespace.XHTML)) {
additionalNamespacesStringBuilder.append(" xmlns:xhtml=\"http://www.w3.org/1999/xhtml\" ");
}
}

/**
Expand Down Expand Up @@ -148,21 +150,24 @@ String constructUrl(WebPage webPage) {
StringBuilder out = new StringBuilder();
out.append("<loc>");
try {
if (webPage.getName() != null) {
String wpName = webPage.getName();
if (baseUrl.endsWith("/")) {
while (wpName.startsWith("/")) {
wpName = wpName.substring(1);
}
}
out.append(escapeXmlSpecialCharacters(new URL(baseUrl + wpName).toString()));
} else {
out.append(escapeXmlSpecialCharacters(new URL(baseUrl).toString()));
}
out.append(toUrl(baseUrl, webPage.getName()));
} catch (MalformedURLException e) {
throw new InvalidUrlException(e);
}
out.append("</loc>\n");
if (webPage.getAlternateNames() != null) {
try {
for (Map.Entry<String, String> entry : webPage.getAlternateNames().entrySet()) {
out.append("<xhtml:link rel=\"alternate\" hreflang=\"");
out.append(escapeXmlSpecialCharacters(entry.getKey()));
out.append("\" href=\"");
out.append(toUrl(baseUrl, entry.getValue()));
out.append("\"/>\n");
}
} catch (MalformedURLException e) {
throw new InvalidUrlException(e);
}
}
if (webPage.getLastMod() != null) {
out.append("<lastmod>");
out.append(dateFormat.format(webPage.getLastMod()));
Expand All @@ -181,6 +186,18 @@ String constructUrl(WebPage webPage) {
return out.toString();
}

private String toUrl(String baseUrl, String name) throws MalformedURLException {
if (name == null) {
return escapeXmlSpecialCharacters(new URL(baseUrl).toString());
}
if (baseUrl.endsWith("/")) {
while (name.startsWith("/")) {
name = name.substring(1);
}
}
return escapeXmlSpecialCharacters(new URL(baseUrl + name).toString());
}

/**
* Add single page to sitemap
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ public void testConstructUrlNotEmptyPage() {
Assert.assertEquals("<loc>http://www.javavids.com/latest.php</loc>\n", url);
}

@Test
public void testConstructAlternateUrls() {
String url = sitemapGenerator.constructUrl(WebPage.builder().name("latest.php").alternateName("de", "latest-de.php")
.alternateName("es", "latest-es.php").build());
Assert.assertEquals("<loc>http://www.javavids.com/latest.php</loc>\n<xhtml:link rel=\"alternate\" hreflang=\"de\" href=\"http://www.javavids.com/latest-de.php\"/>\n" +
"<xhtml:link rel=\"alternate\" hreflang=\"es\" href=\"http://www.javavids.com/latest-es.php\"/>\n", url);
}

@Test
public void testConstructSitemap() throws Exception {
String sitemap = sitemapGenerator.constructSitemapString();
Expand Down