Given a string containing entity escapes, returns a string containing the actual Unicode characters corresponding to the escapes. - Java java.lang

Java examples for java.lang:String Unicode

Description

Given a string containing entity escapes, returns a string containing the actual Unicode characters corresponding to the escapes.

Demo Code


import org.apache.log4j.Logger;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.net.InetAddress;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class Main{
    private static Map e2i = new HashMap();
    /**//from  ww  w .ja v a 2  s  .  c o m
     * Given a string containing entity escapes, returns a string containing the actual Unicode characters
     * corresponding to the escapes.
     *
     * @param source String to unescape
     *
     * @return Unescaped string
     */
    public static String htmlUnescape(String source) {

        StringBuffer buf = new StringBuffer();

        int i;

        for (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);
                Integer iso;

                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 Tutorials