/WebAccess/src/com/ideal/webaccess/StringUtils.java
Java | 116 lines | 77 code | 8 blank | 31 comment | 8 complexity | a0775997300c0dc42e1273a0ffaee08f MD5 | raw file
1/* 2 * Copyright (C) 2010 The IDEAL Group 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.ideal.webaccess; 18 19import java.util.*; 20 21/** 22 * Class for escaping HTML content. Code from tutorial at: 23 * http://www.rgagnon.com/javadetails/java-0307.html 24 */ 25public class StringUtils { 26 27 private StringUtils() { 28 } 29 30 private static HashMap<String, String> htmlEntities; 31 static { 32 htmlEntities = new HashMap<String, String>(); 33 htmlEntities.put("<", "<"); 34 htmlEntities.put(">", ">"); 35 htmlEntities.put("&", "&"); 36 htmlEntities.put(""", "\""); 37 htmlEntities.put("à", "ŕ"); 38 htmlEntities.put("À", "Ŕ"); 39 htmlEntities.put("â", "â"); 40 htmlEntities.put("ä", "ä"); 41 htmlEntities.put("Ä", "Ä"); 42 htmlEntities.put("Â", "Â"); 43 htmlEntities.put("å", "ĺ"); 44 htmlEntities.put("Å", "Ĺ"); 45 htmlEntities.put("æ", "ć"); 46 htmlEntities.put("Æ", "Ć"); 47 htmlEntities.put("ç", "ç"); 48 htmlEntities.put("Ç", "Ç"); 49 htmlEntities.put("é", "é"); 50 htmlEntities.put("É", "É"); 51 htmlEntities.put("è", "č"); 52 htmlEntities.put("È", "Č"); 53 htmlEntities.put("ê", "ę"); 54 htmlEntities.put("Ê", "Ę"); 55 htmlEntities.put("ë", "ë"); 56 htmlEntities.put("Ë", "Ë"); 57 htmlEntities.put("ï", "ď"); 58 htmlEntities.put("Ï", "Ď"); 59 htmlEntities.put("ô", "ô"); 60 htmlEntities.put("Ô", "Ô"); 61 htmlEntities.put("ö", "ö"); 62 htmlEntities.put("Ö", "Ö"); 63 htmlEntities.put("ø", "ř"); 64 htmlEntities.put("Ø", "Ř"); 65 htmlEntities.put("ß", "ß"); 66 htmlEntities.put("ù", "ů"); 67 htmlEntities.put("Ù", "Ů"); 68 htmlEntities.put("û", "ű"); 69 htmlEntities.put("Û", "Ű"); 70 htmlEntities.put("ü", "ü"); 71 htmlEntities.put("Ü", "Ü"); 72 htmlEntities.put(" ", " "); 73 htmlEntities.put("©", "\u00a9"); 74 htmlEntities.put("®", "\u00ae"); 75 htmlEntities.put("€", "\u20a0"); 76 } 77 78 /* 79 * Here the original recursive version. It is fine unless you pass a big 80 * string then a Stack Overflow is possible :-( public static final String 81 * unescapeHTML(String source, int start){ int i,j; i = source.indexOf("&", 82 * start); if (i > -1) { j = source.indexOf(";" ,i); if (j > i) { String 83 * entityToLookFor = source.substring(i , j + 1); String value = 84 * (String)htmlEntities.get(entityToLookFor); if (value != null) { source = 85 * new StringBuffer().append(source.substring(0 , i)) .append(value) 86 * .append(source.substring(j + 1)) .toString(); return unescapeHTML(source, 87 * i + 1); // recursive call } } } return source; } M. McNeely Jr. has sent 88 * a version with do...while()loop which is more robust. Thanks to him! 89 */ 90 91 public static final String unescapeHTML(String source) { 92 int i, j; 93 94 boolean continueLoop; 95 int skip = 0; 96 do { 97 continueLoop = false; 98 i = source.indexOf("&", skip); 99 if (i > -1) { 100 j = source.indexOf(";", i); 101 if (j > i) { 102 String entityToLookFor = source.substring(i, j + 1); 103 String value = htmlEntities.get(entityToLookFor); 104 if (value != null) { 105 source = source.substring(0, i) + value + source.substring(j + 1); 106 continueLoop = true; 107 } else if (value == null) { 108 skip = i + 1; 109 continueLoop = true; 110 } 111 } 112 } 113 } while (continueLoop); 114 return source; 115 } 116}