Java HTML Decode htmlEntityDecodeSingle(String s)

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

Description

html Entity Decode Single

License

Apache License

Declaration

public static String htmlEntityDecodeSingle(String s) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2012 Jack Wang// w  w  w . j  a  va2  s  .  co m
 * 
 * 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 {
    public static String htmlEntityDecodeSingle(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 += 2; i < j; ++i) {
                char c = s.charAt(i);
                if ('0' <= c && c <= '9')
                    n = (n == -1 ? 0 : n * 10) + c - '0';
                else
                    break;
            }
            if (i != j)
                n = -1; // malformed entity - abort
            if (n != -1) {
                sb.append((char) n);
                i = j + 1; // skip ';'
            } else {
                for (int k = pos; k < i; ++k)
                    sb.append(s.charAt(k));
            }
            pos = i;
        }
        if (sb.length() == 0)
            return s;
        else
            sb.append(s.substring(pos, s.length()));
        return sb.toString();

    }

    public String toString(long v) {
        /*
        if (v == 0) {
           return "-
        }
        else {
           if (t < 0) {
           rv[i++] = '-';
           t = std::abs(t);
           }
           for (T b ; t ; t = b) {
           b = t/10;
           T c = t%10;
           rv[i++] = static_cast<char>('0' + c);
           }
        }
        */
        return null;

    }
}

Related

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