Java String Unquote unquoteHtmlContent(String x)

Here you can find the source of unquoteHtmlContent(String x)

Description

unquote Html Content

License

Open Source License

Declaration

static public String unquoteHtmlContent(String x) 

Method Source Code

//package com.java2s;

public class Main {
    private static final char[] htmlIn = { '&', '"', '\'', '<', '>', '\n' };
    private static final String[] htmlOut = { "&amp;", "&quot;", "&#39;", "&lt;", "&gt;", "\n<p>" };

    static public String unquoteHtmlContent(String x) {
        return unreplace(x, htmlOut, htmlIn);
    }//from   w w  w .ja va 2s  .c o  m

    /**
     * Replace all occurences of orgReplace with orgChar; inverse of replace().
     *
     * @param x          operate on this string
     * @param orgReplace get rid of these
     * @param orgChar    replace with these
     * @return resulting string
     */
    static public String unreplace(String x, String[] orgReplace, char[] orgChar) {
        // common case no replacement
        boolean ok = true;
        for (String anOrgReplace : orgReplace) {
            int pos = x.indexOf(anOrgReplace);
            ok = (pos < 0);
            if (!ok)
                break;
        }
        if (ok)
            return x;

        // gotta do it
        StringBuilder result = new StringBuilder(x);
        for (int i = 0; i < orgReplace.length; i++) {
            int pos = result.indexOf(orgReplace[i]);
            if (pos >= 0) {
                unreplace(result, orgReplace[i], orgChar[i]);
            }
        }

        return result.toString();
    }

    /**
     * Replace any String "out" in sb with char "in".
     *
     * @param sb  StringBuilder to replace
     * @param out repalce this String
     * @param in  with this char
     */
    static public void unreplace(StringBuilder sb, String out, char in) {
        int pos;
        while (0 <= (pos = sb.indexOf(out))) {
            sb.setCharAt(pos, in);
            sb.delete(pos + 1, pos + out.length());
        }
    }
}

Related

  1. unquoteAtom(String atom)
  2. unquoteCharacterLiteral(String charLiteralStr)
  3. unquoteCString(String str)
  4. unquotedCharLiteral(char c)
  5. unquotedString(String str)
  6. unQuoteIdentifier(String identifier, boolean useAnsiQuotedIdentifiers)
  7. unQuoteIdentifier(String identifier, String quoteChar)
  8. unQuoteIdentifier(String identifier, String quoteChar)
  9. unquoteImpl(String s, char delim)