Java String Unescape unescape(StringBuilder escapedText, int index)

Here you can find the source of unescape(StringBuilder escapedText, int index)

Description

Unescape html This method modify the StringBuilder in place.

License

Open Source License

Parameter

Parameter Description
escapedText the working text
index the beginning index

Declaration

public static void unescape(StringBuilder escapedText, int index) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**// www .j  av  a 2  s  .c  o  m
     * Table conversion between escaped/unescaped HTML symbols
     */
    private static final String[][] HTML_ESCAPE_TABLE = { { "&lt;", "<" }, { "&gt;", ">" }, { "&amp;", "&" },
            { "&quot;", "\"" }, { "&agrave;", "\u00e0" }, { "&Agrave;", "\u00c0" }, { "&acirc;", "\u00e2" },
            { "&auml;", "\u00e4" }, { "&Auml;", "\u00c4" }, { "&Acirc;", "\u00c2" }, { "&aring;", "\u00e5" },
            { "&Aring;", "\u00c5" }, { "&aelig;", "\u00e6" }, { "&AElig;", "\u00c6" }, { "&ccedil;", "\u00e7" },
            { "&Ccedil;", "\u00c7" }, { "&eacute;", "\u00e9" }, { "&Eacute;", "\u00c9" }, { "&egrave;", "\u00e8" },
            { "&Egrave;", "\u00c8" }, { "&ecirc;", "\u00ea" }, { "&Ecirc;", "\u00ca" }, { "&euml;", "\u00eb" },
            { "&Euml;", "\u00cb" }, { "&iuml;", "\u00ef" }, { "&Iuml;", "\u00cf" }, { "&ocirc;", "\u00f4" },
            { "&Ocirc;", "\u00d4" }, { "&ouml;", "\u00f6" }, { "&Ouml;", "\u00d6" }, { "&oslash;", "\u00f8" },
            { "&Oslash;", "\u00d8" }, { "&szlig;", "\u00df" }, { "&ugrave;", "\u00f9" }, { "&Ugrave;", "\u00d9" },
            { "&ucirc;", "\u00fb" }, { "&Ucirc;", "\u00db" }, { "&uuml;", "\u00fc" }, { "&Uuml;", "\u00dc" },
            { "&nbsp;", " " }, { "&reg;", "\u00a9" }, { "&copy;", "\u00ae" }, { "&euro;", "\u20a0" } };

    /**
     * Unescape html
     *
     * This method modify the {@link StringBuilder} in place.
     *
     * @param escapedText
     *            the working text
     * @param index
     *            the beginning index
     */
    public static void unescape(StringBuilder escapedText, int index) {
        int start, end, table_index;

        start = escapedText.indexOf("&", index);
        if (start > -1) {
            end = escapedText.indexOf(";", start);
            // we don't start from the beginning
            // the next time, to handle the case of
            // the &
            index = start + 1;

            if (end > start) {
                String temp = escapedText.substring(start, end + 1);
                // search in HTML_ESCAPE_TABLE[][] if temp is there
                table_index = 0;
                while (table_index < HTML_ESCAPE_TABLE.length) {
                    if (HTML_ESCAPE_TABLE[table_index][0].equals(temp)) {
                        break;
                    } else {
                        table_index++;
                    }
                }
                if (table_index < HTML_ESCAPE_TABLE.length) {
                    escapedText.replace(start, end + 1, HTML_ESCAPE_TABLE[table_index][1]);
                    unescape(escapedText, index); // recursive call
                }
            }
        }
        return;
    }
}

Related

  1. unescape(String value)
  2. unescape(String value)
  3. unEscape(String value)
  4. unescape(String value)
  5. unescape(String x)
  6. unEscapeString(final String src)
  7. unescapeString(String escapedString)
  8. unescapeString(String iccProfileSrc)
  9. unescapeString(String in)