Java examples for java.lang:String HTML
Method for html escaping a text string
//package com.java2s; public class Main { public static void main(String[] argv) { String original = "java2s.com"; System.out.println(HTMLEscapeTextLine(original)); }/* ww w.j ava 2s . c om*/ private static char[] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; /** * Method for html escaping a text string * @param original The String to escape * @return The escaped String */ public static String HTMLEscapeTextLine(String original) { return HTMLEscapeSpecial(HTMLEscapeTags(original)); } public static String HTMLEscapeSpecial(String original) { if (original == null) return ""; StringBuffer out = new StringBuffer(""); char[] chars = original.toCharArray(); for (int i = 0; i < chars.length; i++) { boolean found = true; switch (chars[i]) { case 38: out.append("&"); break; //& case 198: out.append("Æ"); break; //? case 193: out.append("Á"); break; //?? case 194: out.append("Â"); break; //? case 192: out.append("À"); break; //? case 197: out.append("Å"); break; //? case 195: out.append("Ã"); break; //? case 196: out.append("Ä"); break; //? case 199: out.append("Ç"); break; //? case 208: out.append("Ð"); break; //?? case 201: out.append("É"); break; //? case 202: out.append("Ê"); break; //? case 200: out.append("È"); break; //? case 203: out.append("Ë"); break; //? case 205: out.append("Í"); break; //?? case 206: out.append("Î"); break; //? case 204: out.append("Ì"); break; //? case 207: out.append("Ï"); break; //?? case 209: out.append("Ñ"); break; //? case 211: out.append("Ó"); break; //? case 212: out.append("Ô"); break; //? case 210: out.append("Ò"); break; //? case 216: out.append("Ø"); break; //? case 213: out.append("Õ"); break; //? case 214: out.append("Ö"); break; //? case 222: out.append("Þ"); break; //? case 218: out.append("Ú"); break; //? case 219: out.append("Û"); break; //? case 217: out.append("Ù"); break; //? case 220: out.append("Ü"); break; //? case 221: out.append("Ý"); break; //?? case 225: out.append("á"); break; //? case 226: out.append("â"); break; //? case 230: out.append("æ"); break; //? case 224: out.append("à"); break; //? case 229: out.append("å"); break; //? case 227: out.append("ã"); break; //? case 228: out.append("ä"); break; //? case 231: out.append("ç"); break; //? case 233: out.append("é"); break; //? case 234: out.append("ê"); break; //? case 232: out.append("è"); break; //? case 240: out.append("ð"); break; //? case 235: out.append("ë"); break; //? case 237: out.append("í"); break; //? case 238: out.append("î"); break; //? case 236: out.append("ì"); break; //? case 239: out.append("ï"); break; //? case 241: out.append("ñ"); break; //? case 243: out.append("ó"); break; //? case 244: out.append("ô"); break; //? case 242: out.append("ò"); break; //? case 248: out.append("ø"); break; //? case 245: out.append("õ"); break; //? case 246: out.append("ö"); break; //? case 223: out.append("ß"); break; //? case 254: out.append("þ"); break; //? case 250: out.append("ú"); break; //? case 251: out.append("û"); break; //? case 249: out.append("ù"); break; //? case 252: out.append("ü"); break; //? case 253: out.append("ý"); break; //? case 255: out.append("ÿ"); break; //? case 162: out.append("¢"); break; //? default: found = false; break; } if (!found) { /// /// if (chars[i] > 127) { char c = chars[i]; int a4 = c % 16; c = (char) (c / 16); int a3 = c % 16; c = (char) (c / 16); int a2 = c % 16; c = (char) (c / 16); int a1 = c % 16; out.append("&#x" + hex[a1] + hex[a2] + hex[a3] + hex[a4] + ";"); } /// /// else if (chars[i] >= 33 && chars[i] <= 47 || chars[i] >= 58 && chars[i] <= 64 || chars[i] >= 91 && chars[i] <= 96 || chars[i] >= 123 && chars[i] <= 151) { out.append("&#" + (int) (chars[i]) + ";"); } else { out.append(chars[i]); } } } return out.toString(); } public static String HTMLEscapeTags(String original) { if (original == null) return ""; StringBuffer out = new StringBuffer(""); char[] chars = original.toCharArray(); for (int i = 0; i < chars.length; i++) { boolean found = true; switch (chars[i]) { case 60: out.append("<"); break; //< case 62: out.append(">"); break; //> case 34: out.append("""); break; //" default: found = false; break; } if (!found) out.append(chars[i]); } return out.toString(); } }