Java String Unescape unescape(String s)

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

Description

unescape

License

Open Source License

Declaration

static public String unescape(String s) 

Method Source Code

//package com.java2s;
/**/*from   www .j ava  2s .c o  m*/
This software is released under the terms of the Apache License version 2.
For details of the license, see http://www.apache.org/licenses/LICENSE-2.0.
*/

public class Main {
    static public final String[][] DEFAULTTRANSTABLE = { { "amp", "&" }, { "lt", "<" }, { "gt", ">" },
            { "quot", "\"" }, { "apos", "'" }, };

    static public String unescape(String s) {
        return unescape(s, null);
    }

    static public String unescape(String s, String[][] translations) {
        int count, len;
        boolean found;
        StringBuilder u; // returned string with entities unescaped 
        int p; // insertion point into u 
        int q; // next char from s 
        int stop;
        StringBuilder entity;

        if (translations == null)
            translations = DEFAULTTRANSTABLE;

        if (s == null)
            len = 0;
        else
            len = s.length();
        u = new StringBuilder();
        if (u == null)
            return null;
        p = 0;
        q = 0;
        stop = (len);
        entity = new StringBuilder();

        while (q < stop) {
            char c = s.charAt(q++);
            switch (c) {
            case '&': // see if this is a legitimate entity 
                entity.setLength(0);
                // move forward looking for a semicolon; 
                for (found = true, count = 0;; count++) {
                    if (q + count >= len)
                        break;
                    c = s.charAt(q + count);
                    if (c == ';')
                        break;
                    if ((count == 0 && !namechar1(c)) || (count > 0 && !namecharn(c))) {
                        found = false; // not a legitimate entity
                        break;
                    }
                    entity.append(c);
                }
                if (q + count >= len || count == 0 || !found) {
                    // was not in correct form for entity 
                    u.append('&');
                } else { // looks legitimate 
                    String test = entity.toString();
                    String replacement = null;
                    for (String[] trans : translations) {
                        if (trans[0].equals(test)) {
                            replacement = trans[1];
                            break;
                        }
                    }
                    if (replacement == null) { // no translation, ignore 
                        u.append('&');
                    } else { // found it 
                        q += (count + 1); // skip input entity, including trailing semicolon
                        u.append(replacement);
                    }
                }
                break;
            default:
                u.append(c);
                break;
            }
        }
        return u.toString();
    }

    static boolean namechar1(char c) {
        return (":_?".indexOf(c) >= 0 || "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(c) >= 0
                || ((int) c) > 127);
    }

    static boolean namecharn(char c) {
        return (namechar1(c) || "-.".indexOf(c) >= 0 || "0123456789".indexOf(c) >= 0);
    }
}

Related

  1. unescape(String s)
  2. unescape(String s)
  3. unescape(String s)
  4. unescape(String s)
  5. unescape(String s)
  6. unescape(String s)
  7. unescape(String s)
  8. unescape(String s)
  9. Unescape(String s)