+ /*
+ * replaces characters that cannot appera in HTML with escaped equivalents.
+ */
+ public static String encodeHTML(String aText) {
+ final char[] CHARACTERS_TO_ESCAPE = { '&', '<', '>', '"', '\'' };
+ final String[] ESCAPE_CODES = { "&", "<", ">", """, "'" };
+
+ int position, nextPosition;
+ int i;
+ StringBuffer result = new StringBuffer();
+
+ position=0;
+
+ do {
+ nextPosition = indexOfCharacters(aText, CHARACTERS_TO_ESCAPE, position);
+
+ if (nextPosition<0)
+ nextPosition = aText.length();
+
+ result.append(aText.substring(position, nextPosition));
+
+ if (nextPosition<aText.length())
+ for (i=0; i<CHARACTERS_TO_ESCAPE.length; i++) {
+ if (CHARACTERS_TO_ESCAPE[i] == aText.charAt(nextPosition)) {
+ result.append(ESCAPE_CODES[i]);
+ break;
+ }
+ }
+ position=nextPosition+1;
+ }
+ while (nextPosition<aText.length()) ;
+
+ return result.toString();
+ }
+