merge of '7e9829897ed454bcb4a8e64b029060f7e90cbbfa'

and 'b1a1e2e4c3f77f38c7c7f6ebe23d5225c4e42873'
This commit is contained in:
str4d
2012-01-19 05:21:00 +00:00

View File

@ -1468,6 +1468,9 @@ public class DataHelper {
return rv; return rv;
} }
private static final String escapeChars[] = {"&", "\"", "<", ">"};
private static final String escapeCodes[] = {"&amp;", "&quot;", "&lt;", "&gt;"};
/** /**
* Escape a string for inclusion in HTML * Escape a string for inclusion in HTML
* @param unescaped the unescaped string, may be null * @param unescaped the unescaped string, may be null
@ -1475,15 +1478,9 @@ public class DataHelper {
*/ */
public static String escapeHTML(String unescaped) { public static String escapeHTML(String unescaped) {
if (unescaped == null) return null; if (unescaped == null) return null;
Map<String, String> map = new HashMap<String, String>(); String escaped = unescaped;
map.put("\"","&quot;"); for (int i = 0; i < escapeChars.length; i++) {
map.put("<","&lt;"); escaped = escaped.replaceAll(escapeChars[i], escapeCodes[i]);
map.put(">","&gt;");
String escaped = unescaped.replaceAll("&","&amp;");
for (Map.Entry<String, String> entry : map.entrySet()) {
String k = entry.getKey();
String v = entry.getValue();
escaped = escaped.replaceAll(k, v);
} }
return escaped; return escaped;
} }
@ -1495,16 +1492,9 @@ public class DataHelper {
*/ */
public static String unescapeHTML(String escaped) { public static String unescapeHTML(String escaped) {
if (escaped == null) return null; if (escaped == null) return null;
Map<String, String> map = new HashMap<String, String>();
map.put("&amp;","&");
map.put("&quot;","\"");
map.put("&lt;","<");
map.put("&gt;",">");
String unescaped = escaped; String unescaped = escaped;
for (Map.Entry<String, String> entry : map.entrySet()) { for (int i = 0; i < escapeChars.length; i++) {
String k = entry.getKey(); unescaped = unescaped.replaceAll(escapeCodes[i], escapeChars[i]);
String v = entry.getValue();
unescaped = unescaped.replaceAll(k, v);
} }
return unescaped; return unescaped;
} }