Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stavrossk/def378ce3f7e4b7a8a8a60e5523e4418 to your computer and use it in GitHub Desktop.
Save stavrossk/def378ce3f7e4b7a8a8a60e5523e4418 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