Not able to save HTML Entity in Java String - illegal character

Viewed 231

I cannot compile this:

String[][] UMLAUT_REPLACEMENTS = {{"\u0022", """},{"\u0021", "!"}};

I tried to escape the special character by using \\ but no effect.

This is the error code:

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project opk-application-util: Compilation failure: Compilation failure: 
[ERROR] /C:/eplatform/git-repos/opk-backend/opk-application-util/src/main/java/util/SonderZeichenFilter.java:[50,41] '}' expected
[ERROR] /C:/eplatform/git-repos/opk-backend/opk-application-util/src/main/java/ch/opk/util/SonderZeichenFilter.java:[50,45] ';' expected
[ERROR] /C:/eplatform/git-repos/opk-backend/opk-application-util/src/main/java/ch/opk/util/SonderZeichenFilter.java:[50,46] illegal character: '#'
[ERROR] /C:/eplatform/git-repos/opk-backend/opk-application-util/src/main/java/ch/opk/util/SonderZeichenFilter.java:[50,47] ';' expected
[ERROR] /C:/eplatform/git-repos/opk-backend/opk-application-util/src/main/java/opk/util/SonderZeichenFilter.java:[50,50] unclosed string literal
3 Answers

In Java Unicode escape sequences (\uXXXX) are handled as part of pre-processing and before String literal escape sequences are processed. Therefore when the compiler processes "\u0022" it is actually processing the String literal """ which is one empty String literal (two double quotes) followed by the opening quote of another String literal therefore resulting in the error "unclosed string literal" because there is an uneven amount of double quotes in the code.

This is a somewhat common cause for malformed Javadoc (when the author wants to write literally \uXXXX but the resulting HTML instead contains the respective Unicode character) and most IDEs are confused by this as well (e.g. \u0063lass MyClass {} is valid Java source code; \u0063 = c).

In your case you can use the special escape sequence \" to write a literal ". This will also improve readability because not everyone is familiar with the Unicode code point of ". Similarly \u0021 could be written as ! since that character has no special meaning inside a Java String. Your code could therefore be written like this:

String[][] UMLAUT_REPLACEMENTS = {{"\"", """},{"!", "!"}};

If you want the literal \uXXXX inside a Java String you will have to escape the backslash by preceeding it with another \: "\\uXXXX"

I found the solution!

So, the reason, why String[][] UMLAUT_REPLACEMENTS = {{"\u0022", """},{"\u0021", "!"}}; did not work, is, because \u0022 is already interpreted as " while compiling, which throws an error, because """ needs to be escaped.

But if you escape \u0022, it will not be recognized as character anymore.

Yet there is also a solution, which I applied.


By the way, this solution is to mask all special characters of the latin ascii letters except the very simple ones.

First, you declare a String array:

    public String escapeHtml(String input) {

    String escapedHtml = input;

String[][] UMLAUT_REPLACEMENTS =
            {
                    {"\\u0021", "&33"},
                    {"\\u0022", "&#34"},
                    {"\\u0024", "&#36"},
                    {"\\u0025", "&#37"},
                    {"\\u0026", "&#38"},
                    {"\\u0027", "&#39"},
                    {"\\u0028", "&#40"},
};

Then, you Look for the characters to replace them with the HTML Entities but use StringEscapeUtils.unescapeJava(INPUT) to unescape \uXXXX

    for (int i = 0; i < UMLAUT_REPLACEMENTS.length; i++) {
        String unescapedSign = StringEscapeUtils.unescapeJava(UMLAUT_REPLACEMENTS[i][0]);
        escapedHtml = escapedHtml.replace(unescapedSign, UMLAUT_REPLACEMENTS[i][1]);
    }


    return escapedHtml;


Thank you for your help!!
Related