Java HTML Unescape htmlUnescape(String source)

Here you can find the source of htmlUnescape(String source)

Description

Reverses htmlEscape.

License

Apache License

Declaration

public static String htmlUnescape(String source) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    private static Map e2i = new HashMap();

    /**// w  w w  .  ja  va  2  s. c om
     * Reverses htmlEscape.
     **/
    public static String htmlUnescape(String source) {
        StringBuffer buf = new StringBuffer();
        if (source != null) {
            for (int i = 0; i < source.length(); ++i) {
                char ch = source.charAt(i);
                if (ch == '&') {
                    int semi = source.indexOf(';', i + 1);
                    if (semi == -1) {
                        buf.append(ch);
                        continue;
                    }
                    String entity = source.substring(i + 1, semi); //semi-1);
                    Integer iso = null;
                    if (entity.charAt(0) == '#')
                        iso = new Integer(entity.substring(1));
                    else
                        iso = (Integer) e2i.get(entity);
                    if (iso == null) {
                        buf.append("&" + entity + ";");
                    } else {
                        buf.append((char) (iso.intValue()));
                        i = semi;
                    }
                } else {
                    buf.append(ch);
                }
            }
        }
        return buf.toString();
    }
}

Related

  1. htmlUnescape(String s)
  2. unEscapeHTML(final String escapedHTML)
  3. unescapeHtml(final String input)
  4. unescapeHTML(String comment)
  5. unescapeHTML(String html)