/**
*
*/
package org.chris.portmapper.util;
import java.util.HashMap;
import java.util.Map;
/**
* @author chris
* @version $Id: EncodingUtilities.java 61 2009-08-15 14:58:46Z christoph $
*/
public class EncodingUtilities {
private static Map<Character, String> knownEncodings;
static {
knownEncodings = new HashMap<Character, String>();
knownEncodings.put('<', "<");
knownEncodings.put('>', ">");
knownEncodings.put('&', "&");
}
/**
* Replace all special characters with their html entities. This was found
* at {@link http
* ://www.owasp.org/index.php/How_to_perform_HTML_entity_encoding_in_Java}
*
* @param s
* the string in which to replace the special characters.
* @return the result of the replacement.
*/
public static String htmlEntityEncode(String s) {
StringBuffer buf = new StringBuffer();
int len = (s == null ? -1 : s.length());
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0'
&& c <= '9') {
buf.append(c);
} else {
if (knownEncodings.containsKey(c)) {
buf.append(knownEncodings.get(c));
} else {
buf.append("" + (int) c + ";");
}
}
}
return buf.toString();
}
}