Java HTML Decode htmlEntityDecode(String s)

Here you can find the source of htmlEntityDecode(String s)

Description

the following method was taken from suns Decoder and stands under CDDL look here for a converter: http://gmailassistant.sourceforge.net/src/org/freeshell/zs/common/HtmlManipulator.java.html

License

Apache License

Declaration

public static String htmlEntityDecode(String s) 

Method Source Code

//package com.java2s;
/**/*from w ww  .ja  v a 2  s .  c  o  m*/
 * Copyright (C) 2010 Peter Karich <jetwick_@_pannous_._info>
 *
 * 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.
 */

public class Main {
    /**
     * the following method was taken from suns Decoder and stands under CDDL
     * 
     * look here for a converter:
     * http://gmailassistant.sourceforge.net/src/org/freeshell/zs/common/HtmlManipulator.java.html
     */
    public static String htmlEntityDecode(String s) {
        int i = 0, j = 0, pos = 0;
        StringBuffer sb = new StringBuffer();
        while ((i = s.indexOf("&", pos)) != -1
                && (j = s.indexOf(';', i)) != -1) {
            int n = -1;
            for (i += 1; i < j; ++i) {
                char c = s.charAt(i);
                if ('0' <= c && c <= '9')
                    n = (n == -1 ? 0 : n * 10) + c - '0';
                else
                    break;
            }

            // skip malformed html entities
            if (i != j)
                n = -1;

            if (n != -1) {
                sb.append((char) n);
            } else {
                // force deletion of chars                
                for (int k = pos; k < i - 1; ++k) {
                    sb.append(s.charAt(k));
                }
                sb.append(" ");
            }
            // skip ';'
            i = j + 1;
            pos = i;
        }
        if (sb.length() == 0)
            return s;
        else
            sb.append(s.substring(pos, s.length()));
        return sb.toString();

    }
}

Related

  1. HTMLDecode(String s)
  2. htmlDecode(String s)
  3. htmlDecode(String strSrc)
  4. htmlDecoder(String content)
  5. htmlEntityDecodeSingle(String s)
  6. toHtmlString(String src, boolean noSingleQuotes)