Skip to content

Commit

Permalink
fixes issue #25 (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
eximius313 authored and dfabulich committed Jul 20, 2016
1 parent c3dee10 commit ea0594a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ abstract class AbstractSitemapUrlRenderer<T extends WebSitemapUrl> implements IS
public void render(WebSitemapUrl url, StringBuilder sb, W3CDateFormat dateFormat, String additionalData) {
sb.append(" <url>\n");
sb.append(" <loc>");
sb.append(url.getUrl().toString());
sb.append(UrlUtils.escapeXml(url.getUrl().toString()));
sb.append("</loc>\n");
if (url.getLastMod() != null) {
sb.append(" <lastmod>");
Expand Down Expand Up @@ -35,7 +35,7 @@ public void renderTag(StringBuilder sb, String namespace, String tagName, Object
sb.append(':');
sb.append(tagName);
sb.append('>');
sb.append(value);
sb.append(UrlUtils.escapeXml(value.toString()));
sb.append("</");
sb.append(namespace);
sb.append(':');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private void writeSiteMap(OutputStreamWriter out) throws IOException {
for (SitemapIndexUrl url : urls) {
out.write(" <sitemap>\n");
out.write(" <loc>");
out.write(url.url.toString());
out.write(UrlUtils.escapeXml(url.url.toString()));
out.write("</loc>\n");
Date lastMod = url.lastMod;

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

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class UrlUtils {
private static Map<String,String> ENTITIES = new HashMap<String,String>();
static {
ENTITIES.put("&", "&amp;");
ENTITIES.put("'", "&apos;");
ENTITIES.put("\"", "&quot;");
ENTITIES.put(">", "&gt;");
ENTITIES.put("<", "&lt;");
}
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) {
// Is there a better test to use here?
Expand Down

0 comments on commit ea0594a

Please sign in to comment.