I've got a string that has curly quotes in it. I'd like to replace those with HTML entities to make sure they don't confuse other downstream systems. For my first attempt, I just added matching for the characters I wanted to replace, entering them directly in my code:
public static String escapeXml(String s) {
StringBuilder sb = new StringBuilder();
char characters[] = s.toCharArray();
for ( int i = 0; i < characters.length; i++ ) {
char c = characters[i];
switch (c) {
// other escape characters deleted for clarity
case '“':
sb.append("“");
break;
case '”':
sb.append("”");
break;
case '‘':
sb.append("‘");
break;
case '’':
sb.append("’");
break;
default:
sb.append(c);
break;
}
}
return sb.toString();
}
This compiled and worked fine on my Mac, but when our CI server (which runs on Linux) tried to build it, it choked:
Out.java:[347,16] duplicate case label
Apparently some part of the build chain on the Linux box can't recognize and distinguish among these fancy characters.
My next attempt was to use Unicode escaping. Unfortunately, this won't even compile on my Mac:
...
case '\u8220':
sb.append("“");
break;
case '/u8221':
sb.append("”");
break;
...
My compiler throws this complaint:
Out.java:[346,21] unclosed character literal
I'm baffled as to how one might do this bit of substitution and have it work reliably across platforms. Does anybody have any pointers? Thanks in advance.