package message.json; import java.util.HashMap; import java.util.Map; /** * A helper class provided out of the box to encode characters that HTML can't support * natively like <, >, &, or ". This will scan the value passed to the transform * method and replace any of these special characters with the HTML encoded equivalent. This * method will NOT work for HTML text because it will blindly encode all characters it sees which * means it will strip out any HTML tags. */ public class HTMLEncoder implements Transformer { private static final Map htmlEntities = new HashMap(); public HTMLEncoder() { if( htmlEntities.isEmpty() ) { htmlEntities.put( new Integer(34), """ ); // " - double-quote htmlEntities.put( new Integer(38), "&" ); // & - ampersand // htmlEntities.put( 39, "'"); // ' - apostrophe htmlEntities.put( new Integer(60), "<" ); // < - less-than htmlEntities.put( new Integer(62), ">" ); // > - greater-than htmlEntities.put( new Integer(160), " " ); // non-breaking space htmlEntities.put( new Integer(169), "©" ); // - copyright htmlEntities.put( new Integer(174), "®" ); // - registered trademark htmlEntities.put( new Integer(192), "À" ); // - uppercase A, grave accent htmlEntities.put( new Integer(193), "Á" ); // - uppercase A, acute accent htmlEntities.put( new Integer(194), "Â"); // - uppercase A, circumflex accent htmlEntities.put( new Integer(195), "Ã" ); // - uppercase A, tilde htmlEntities.put( new Integer(196), "Ä" ); // - uppercase A, umlaut htmlEntities.put( new Integer(197), "Å" ); // - uppercase A, ring htmlEntities.put( new Integer(198), "Æ" ); // - uppercase AE htmlEntities.put( new Integer(199), "Ç" ); // - uppercase C, cedilla htmlEntities.put( new Integer(200), "È"); // - uppercase E, grave accent htmlEntities.put( new Integer(201), "É"); // - uppercase E, acute accent htmlEntities.put( new Integer(202), "Ê" ); // - uppercase E, circumflex accent htmlEntities.put( new Integer(203), "Ë" ); // - uppercase E, umlaut htmlEntities.put( new Integer(204), "Ì" ); // - uppercase I, grave accent htmlEntities.put( new Integer(205), "Í" ); // - uppercase I, acute accent htmlEntities.put( new Integer(206), "Î" ); // - uppercase I, circumflex accent htmlEntities.put( new Integer(207), "Ï" ); // - uppercase I, umlaut htmlEntities.put( new Integer(208), "Ð" ); // - uppercase Eth, Icelandic htmlEntities.put( new Integer(209), "Ñ"); // - uppercase N, tilde htmlEntities.put( new Integer(210), "Ò"); // - uppercase O, grave accent htmlEntities.put( new Integer(211), "Ó"); // - uppercase O, acute accent htmlEntities.put( new Integer(212), "Ô" ); // - uppercase O, circumflex accent htmlEntities.put( new Integer(213), "Õ"); // - uppercase O, tilde htmlEntities.put( new Integer(214), "Ö"); // - uppercase O, umlaut htmlEntities.put( new Integer(216), "Ø"); // - uppercase O, slash htmlEntities.put( new Integer(217), "Ù"); // - uppercase U, grave accent htmlEntities.put( new Integer(218), "Ú"); // - uppercase U, acute accent htmlEntities.put( new Integer(219), "Û" ); // - uppercase U, circumflex accent htmlEntities.put( new Integer(220), "Ü" ); // - uppercase U, umlaut htmlEntities.put( new Integer(221), "Ý"); // - uppercase Y, acute accent htmlEntities.put( new Integer(222), "Þ"); // - uppercase THORN, Icelandic htmlEntities.put( new Integer(223), "ß"); // - lowercase sharps, German htmlEntities.put( new Integer(224), "à"); // - lowercase a, grave accent htmlEntities.put( new Integer(225), "á"); // - lowercase a, acute accent htmlEntities.put( new Integer(226), "â"); // - lowercase a, circumflex accent htmlEntities.put( new Integer(227), "ã"); // - lowercase a, tilde htmlEntities.put( new Integer(228), "ä"); // - lowercase a, umlaut htmlEntities.put( new Integer(229), "å"); // - lowercase a, ring htmlEntities.put( new Integer(230), "æ"); // - lowercase ae htmlEntities.put( new Integer(231), "ç" ); // - lowercase c, cedilla htmlEntities.put( new Integer(232), "è"); // - lowercase e, grave accent htmlEntities.put( new Integer(233), "é"); // - lowercase e, acute accent htmlEntities.put( new Integer(234), "ê"); // - lowercase e, circumflex accent htmlEntities.put( new Integer(235), "ë"); // - lowercase e, umlaut htmlEntities.put( new Integer(236), "ì" ); // - lowercase i, grave accent htmlEntities.put( new Integer(237), "í"); // - lowercase i, acute accent htmlEntities.put( new Integer(238), "î"); // - lowercase i, circumflex accent htmlEntities.put( new Integer(239), "ï"); // - lowercase i, umlaut htmlEntities.put( new Integer(240), "ð"); // - lowercase eth, Icelandic htmlEntities.put( new Integer(241), "ñ"); // - lowercase n, tilde htmlEntities.put( new Integer(242), "ò"); // - lowercase o, grave accent htmlEntities.put( new Integer(243), "ó"); // - lowercase o, acute accent htmlEntities.put( new Integer(244), "ô"); // - lowercase o, circumflex accent htmlEntities.put( new Integer(245), "õ"); // - lowercase o, tilde htmlEntities.put( new Integer(246), "ö"); // - lowercase o, umlaut htmlEntities.put( new Integer(248), "ø"); // - lowercase o, slash htmlEntities.put( new Integer(249), "ù"); // - lowercase u, grave accent htmlEntities.put( new Integer(250), "ú"); // - lowercase u, acute accent htmlEntities.put( new Integer(251), "û"); // - lowercase u, circumflex accent htmlEntities.put( new Integer(252), "ü"); // - lowercase u, umlaut htmlEntities.put( new Integer(253), "ý"); // - lowercase y, acute accent htmlEntities.put( new Integer(254), "þ"); // - lowercase thorn, Icelandic htmlEntities.put( new Integer(255), "ÿ"); // - lowercase y, umlaut htmlEntities.put( new Integer(8364), "€"); // Euro symbol } } public String transform(Object value) { String val = value.toString(); StringBuffer builder = new StringBuffer(); for (int i = 0; i < val.length(); ++i) { int intVal = (int)val.charAt(i); Integer intValInteger = new Integer(intVal); if( htmlEntities.containsKey( intValInteger ) ) { builder.append( htmlEntities.get( intValInteger ) ); } else if ( intVal > 128 ) { builder.append( "&#" ); builder.append( intVal ); builder.append( ";" ); } else { builder.append( val.charAt(i) ); } } return builder.toString(); } }