parent
c3dee10152
commit
ea0594afa1
|
@ -5,7 +5,7 @@ abstract class AbstractSitemapUrlRenderer<T extends WebSitemapUrl> implements IS
|
||||||
public void render(WebSitemapUrl url, StringBuilder sb, W3CDateFormat dateFormat, String additionalData) {
|
public void render(WebSitemapUrl url, StringBuilder sb, W3CDateFormat dateFormat, String additionalData) {
|
||||||
sb.append(" <url>\n");
|
sb.append(" <url>\n");
|
||||||
sb.append(" <loc>");
|
sb.append(" <loc>");
|
||||||
sb.append(url.getUrl().toString());
|
sb.append(UrlUtils.escapeXml(url.getUrl().toString()));
|
||||||
sb.append("</loc>\n");
|
sb.append("</loc>\n");
|
||||||
if (url.getLastMod() != null) {
|
if (url.getLastMod() != null) {
|
||||||
sb.append(" <lastmod>");
|
sb.append(" <lastmod>");
|
||||||
|
@ -35,7 +35,7 @@ abstract class AbstractSitemapUrlRenderer<T extends WebSitemapUrl> implements IS
|
||||||
sb.append(':');
|
sb.append(':');
|
||||||
sb.append(tagName);
|
sb.append(tagName);
|
||||||
sb.append('>');
|
sb.append('>');
|
||||||
sb.append(value);
|
sb.append(UrlUtils.escapeXml(value.toString()));
|
||||||
sb.append("</");
|
sb.append("</");
|
||||||
sb.append(namespace);
|
sb.append(namespace);
|
||||||
sb.append(':');
|
sb.append(':');
|
||||||
|
|
|
@ -225,7 +225,7 @@ public class SitemapIndexGenerator {
|
||||||
for (SitemapIndexUrl url : urls) {
|
for (SitemapIndexUrl url : urls) {
|
||||||
out.write(" <sitemap>\n");
|
out.write(" <sitemap>\n");
|
||||||
out.write(" <loc>");
|
out.write(" <loc>");
|
||||||
out.write(url.url.toString());
|
out.write(UrlUtils.escapeXml(url.url.toString()));
|
||||||
out.write("</loc>\n");
|
out.write("</loc>\n");
|
||||||
Date lastMod = url.lastMod;
|
Date lastMod = url.lastMod;
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,31 @@ package com.redfin.sitemapgenerator;
|
||||||
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
class UrlUtils {
|
class UrlUtils {
|
||||||
|
private static Map<String,String> ENTITIES = new HashMap<String,String>();
|
||||||
|
static {
|
||||||
|
ENTITIES.put("&", "&");
|
||||||
|
ENTITIES.put("'", "'");
|
||||||
|
ENTITIES.put("\"", """);
|
||||||
|
ENTITIES.put(">", ">");
|
||||||
|
ENTITIES.put("<", "<");
|
||||||
|
}
|
||||||
|
private static Pattern PATTERN = Pattern.compile("(&|'|\"|>|<)");
|
||||||
|
|
||||||
|
static String escapeXml(String string){
|
||||||
|
Matcher matcher = PATTERN.matcher(string);
|
||||||
|
StringBuffer sb = new StringBuffer();
|
||||||
|
while(matcher.find()) {
|
||||||
|
matcher.appendReplacement(sb, ENTITIES.get(matcher.group(1)));
|
||||||
|
}
|
||||||
|
matcher.appendTail(sb);
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
static void checkUrl(URL url, URL baseUrl) {
|
static void checkUrl(URL url, URL baseUrl) {
|
||||||
// Is there a better test to use here?
|
// Is there a better test to use here?
|
||||||
|
|
Loading…
Reference in New Issue