/WebAccess/src/com/ideal/webaccess/StringUtils.java
http://eyes-free.googlecode.com/ · Java · 116 lines · 77 code · 8 blank · 31 comment · 8 complexity · a0775997300c0dc42e1273a0ffaee08f MD5 · raw file
- /*
- * Copyright (C) 2010 The IDEAL Group
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.ideal.webaccess;
- import java.util.*;
- /**
- * Class for escaping HTML content. Code from tutorial at:
- * http://www.rgagnon.com/javadetails/java-0307.html
- */
- public class StringUtils {
- private StringUtils() {
- }
- private static HashMap<String, String> htmlEntities;
- static {
- htmlEntities = new HashMap<String, String>();
- htmlEntities.put("<", "<");
- htmlEntities.put(">", ">");
- htmlEntities.put("&", "&");
- htmlEntities.put(""", "\"");
- htmlEntities.put("à", "ŕ");
- htmlEntities.put("À", "Ŕ");
- htmlEntities.put("â", "â");
- htmlEntities.put("ä", "ä");
- htmlEntities.put("Ä", "Ä");
- htmlEntities.put("Â", "Â");
- htmlEntities.put("å", "ĺ");
- htmlEntities.put("Å", "Ĺ");
- htmlEntities.put("æ", "ć");
- htmlEntities.put("Æ", "Ć");
- htmlEntities.put("ç", "ç");
- htmlEntities.put("Ç", "Ç");
- htmlEntities.put("é", "é");
- htmlEntities.put("É", "É");
- htmlEntities.put("è", "č");
- htmlEntities.put("È", "Č");
- htmlEntities.put("ê", "ę");
- htmlEntities.put("Ê", "Ę");
- htmlEntities.put("ë", "ë");
- htmlEntities.put("Ë", "Ë");
- htmlEntities.put("ï", "ď");
- htmlEntities.put("Ï", "Ď");
- htmlEntities.put("ô", "ô");
- htmlEntities.put("Ô", "Ô");
- htmlEntities.put("ö", "ö");
- htmlEntities.put("Ö", "Ö");
- htmlEntities.put("ø", "ř");
- htmlEntities.put("Ø", "Ř");
- htmlEntities.put("ß", "ß");
- htmlEntities.put("ù", "ů");
- htmlEntities.put("Ù", "Ů");
- htmlEntities.put("û", "ű");
- htmlEntities.put("Û", "Ű");
- htmlEntities.put("ü", "ü");
- htmlEntities.put("Ü", "Ü");
- htmlEntities.put(" ", " ");
- htmlEntities.put("©", "\u00a9");
- htmlEntities.put("®", "\u00ae");
- htmlEntities.put("€", "\u20a0");
- }
- /*
- * Here the original recursive version. It is fine unless you pass a big
- * string then a Stack Overflow is possible :-( public static final String
- * unescapeHTML(String source, int start){ int i,j; i = source.indexOf("&",
- * start); if (i > -1) { j = source.indexOf(";" ,i); if (j > i) { String
- * entityToLookFor = source.substring(i , j + 1); String value =
- * (String)htmlEntities.get(entityToLookFor); if (value != null) { source =
- * new StringBuffer().append(source.substring(0 , i)) .append(value)
- * .append(source.substring(j + 1)) .toString(); return unescapeHTML(source,
- * i + 1); // recursive call } } } return source; } M. McNeely Jr. has sent
- * a version with do...while()loop which is more robust. Thanks to him!
- */
- public static final String unescapeHTML(String source) {
- int i, j;
- boolean continueLoop;
- int skip = 0;
- do {
- continueLoop = false;
- i = source.indexOf("&", skip);
- if (i > -1) {
- j = source.indexOf(";", i);
- if (j > i) {
- String entityToLookFor = source.substring(i, j + 1);
- String value = htmlEntities.get(entityToLookFor);
- if (value != null) {
- source = source.substring(0, i) + value + source.substring(j + 1);
- continueLoop = true;
- } else if (value == null) {
- skip = i + 1;
- continueLoop = true;
- }
- }
- }
- } while (continueLoop);
- return source;
- }
- }