Skip to content

Commit

Permalink
Issue checkstyle#5162: fixed sanitizing ampersand in xml file
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach committed Sep 29, 2017
1 parent f58e003 commit 5718d8e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
21 changes: 1 addition & 20 deletions src/main/java/com/puppycrawl/tools/checkstyle/XMLLogger.java
Expand Up @@ -271,7 +271,7 @@ public static String encode(String value) {
sb.append(""");
break;
case '&':
sb.append(encodeAmpersand(value, i));
sb.append("&");
break;
case '\r':
break;
Expand Down Expand Up @@ -327,25 +327,6 @@ else if (ent.charAt(1) == '#') {
return reference;
}

/**
* Encodes ampersand in value at required position.
* @param value string value, which contains ampersand
* @param ampPosition position of ampersand in value
* @return encoded ampersand which should be used in xml
*/
private static String encodeAmpersand(String value, int ampPosition) {
final int nextSemi = value.indexOf(';', ampPosition);
final String result;
if (nextSemi == -1
|| !isReference(value.substring(ampPosition, nextSemi + 1))) {
result = "&";
}
else {
result = "&";
}
return result;
}

/**
* The registered file messages.
*/
Expand Down
13 changes: 9 additions & 4 deletions src/test/java/com/puppycrawl/tools/checkstyle/XMLLoggerTest.java
Expand Up @@ -63,11 +63,11 @@ public void testEncode()
{"'", "'"},
{"\"", """},
{"&", "&"},
{"<", "<"},
{"<", "<"},
{"abc;", "abc;"},
{"�", "�"}, //reference
{"&#0", "&#0"}, //not reference
{"�", "�"}, //not reference
{"�", "�"},
{"&#0", "&#0"},
{"�", "�"},
};
for (String[] encoding : encodings) {
final String encoded = XMLLogger.encode(encoding[0]);
Expand All @@ -84,6 +84,11 @@ public void testIsReference()
final String[] references = {
"�",
"�",
"<",
">",
"'",
""",
"&",
};
for (String reference : references) {
assertTrue("reference: " + reference,
Expand Down

0 comments on commit 5718d8e

Please sign in to comment.