Skip to content

Instantly share code, notes, and snippets.

@mutsune
Created December 6, 2018 03:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mutsune/935f9ad586599590afbc1260f9e25583 to your computer and use it in GitHub Desktop.
Save mutsune/935f9ad586599590afbc1260f9e25583 to your computer and use it in GitHub Desktop.
(Part of) custom apache commons StringEscapeUtil like PHP htmlspecialchars
final private static CharSequenceTranslator quotes = buildAggregateTranslator(Mode.ENT_QUOTES);
final private static CharSequenceTranslator noquotes = buildAggregateTranslator(Mode.ENT_NOQUOTES);
private static AggregateTranslator buildAggregateTranslator(Mode mode) {
Map<CharSequence, CharSequence> map = new HashMap<>();
if (mode == Mode.ENT_QUOTES) {
map.put("\"", "&quot;"); // " - double-quote
map.put("'", "&apos;"); // ' - apostrophe
}
// default escapes
map.put("&", "&amp;"); // & - ampersand
map.put("<", "&lt;"); // < - less-than
map.put(">", "&gt;"); // > - greater-than
return new AggregateTranslator(new LookupTranslator(Collections.unmodifiableMap(map)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment