PageRenderTime 36ms CodeModel.GetById 16ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  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("&lt;", "<");
 34        htmlEntities.put("&gt;", ">");
 35        htmlEntities.put("&amp;", "&");
 36        htmlEntities.put("&quot;", "\"");
 37        htmlEntities.put("&agrave;", "ŕ");
 38        htmlEntities.put("&Agrave;", "Ŕ");
 39        htmlEntities.put("&acirc;", "â");
 40        htmlEntities.put("&auml;", "ä");
 41        htmlEntities.put("&Auml;", "Ä");
 42        htmlEntities.put("&Acirc;", "Â");
 43        htmlEntities.put("&aring;", "ĺ");
 44        htmlEntities.put("&Aring;", "Ĺ");
 45        htmlEntities.put("&aelig;", "ć");
 46        htmlEntities.put("&AElig;", "Ć");
 47        htmlEntities.put("&ccedil;", "ç");
 48        htmlEntities.put("&Ccedil;", "Ç");
 49        htmlEntities.put("&eacute;", "é");
 50        htmlEntities.put("&Eacute;", "É");
 51        htmlEntities.put("&egrave;", "č");
 52        htmlEntities.put("&Egrave;", "Č");
 53        htmlEntities.put("&ecirc;", "ę");
 54        htmlEntities.put("&Ecirc;", "Ę");
 55        htmlEntities.put("&euml;", "ë");
 56        htmlEntities.put("&Euml;", "Ë");
 57        htmlEntities.put("&iuml;", "ď");
 58        htmlEntities.put("&Iuml;", "Ď");
 59        htmlEntities.put("&ocirc;", "ô");
 60        htmlEntities.put("&Ocirc;", "Ô");
 61        htmlEntities.put("&ouml;", "ö");
 62        htmlEntities.put("&Ouml;", "Ö");
 63        htmlEntities.put("&oslash;", "ř");
 64        htmlEntities.put("&Oslash;", "Ř");
 65        htmlEntities.put("&szlig;", "ß");
 66        htmlEntities.put("&ugrave;", "ů");
 67        htmlEntities.put("&Ugrave;", "Ů");
 68        htmlEntities.put("&ucirc;", "ű");
 69        htmlEntities.put("&Ucirc;", "Ű");
 70        htmlEntities.put("&uuml;", "ü");
 71        htmlEntities.put("&Uuml;", "Ü");
 72        htmlEntities.put("&nbsp;", " ");
 73        htmlEntities.put("&copy;", "\u00a9");
 74        htmlEntities.put("&reg;", "\u00ae");
 75        htmlEntities.put("&euro;", "\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}