Java HTML Unescape unescapeHTML(String value)

Here you can find the source of unescapeHTML(String value)

Description

unescape HTML

License

Open Source License

Declaration

public static String unescapeHTML(String value) 

Method Source Code

//package com.java2s;
// under the terms of the GNU Lesser General Public License as published

import java.util.HashMap;
import java.util.Map;

public class Main {
    private static Map<String, Character> _entities;

    public static String unescapeHTML(String value) {
        if (value == null)
            return null;
        if (value.indexOf('&') < 0)
            return value;
        Map<String, Character> ent = getHtmlEntities();
        StringBuffer sb = new StringBuffer();
        final int length = value.length();
        for (int i = 0; i < length; i++) {
            char c = value.charAt(i);
            if (c == '&') {
                char ce = 0;
                int i1 = value.indexOf(';', i + 1);
                if (i1 > i && i1 - i <= 12) {
                    if (value.charAt(i + 1) == '#') {
                        if (value.charAt(i + 2) == 'x') {
                            ce = (char) atoi(value.substring(i + 3, i1), 16);
                        } else {
                            ce = (char) atoi(value.substring(i + 2, i1));
                        }//from w ww .  ja  v  a2  s  .  c  om
                    } else {
                        synchronized (ent) {
                            Character ceObj = ent.get(value.substring(i + 1, i1));
                            ce = ceObj == null ? 0 : ceObj.charValue();
                        }
                    }
                }
                if (ce > 0) {
                    sb.append(ce);
                    i = i1;
                } else
                    sb.append(c);
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    private static synchronized Map<String, Character> getHtmlEntities() {
        if (_entities == null) {
            _entities = new HashMap<String, Character>();
            _entities.put("lt", '<');
            _entities.put("gt", '>');
            _entities.put("amp", '&');
            _entities.put("quot", '"');
            _entities.put("apos", '\'');
            _entities.put("nbsp", '\u00A0');
            _entities.put("shy", '\u00AD');
            _entities.put("copy", '\u00A9');
            _entities.put("reg", '\u00AE');
            _entities.put("trade", '\u2122');
            _entities.put("mdash", '\u2014');
            _entities.put("ndash", '\u2013');
            _entities.put("ldquo", '\u201C');
            _entities.put("rdquo", '\u201D');
            _entities.put("euro", '\u20AC');
            _entities.put("middot", '\u00B7');
            _entities.put("bull", '\u2022');
            _entities.put("laquo", '\u00AB');
            _entities.put("raquo", '\u00BB');
        }
        return _entities;
    }

    static public int atoi(String s) {
        try {
            return Integer.parseInt(s);
        } catch (Throwable ex) {
            return 0;
        }
    }

    static public int atoi(String s, int base) {
        try {
            return Integer.parseInt(s, base);
        } catch (Throwable ex) {
            return 0;
        }
    }
}

Related

  1. unescapeHTML(String source)
  2. unescapeHTML(String source)
  3. unescapeHTML(String source, int start)
  4. unescapeHTML(String str)
  5. unEscapeHtml(String text)
  6. unescapeHTML(String value)
  7. unescapeHTML2(String source, int start)
  8. unescapeHtmlByName(final String input)
  9. unescapeHtmlByNumber(final String input)