Created
December 6, 2018 03:46
-
-
Save mutsune/935f9ad586599590afbc1260f9e25583 to your computer and use it in GitHub Desktop.
(Part of) custom apache commons StringEscapeUtil like PHP htmlspecialchars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("\"", """); // " - double-quote | |
map.put("'", "'"); // ' - apostrophe | |
} | |
// default escapes | |
map.put("&", "&"); // & - ampersand | |
map.put("<", "<"); // < - less-than | |
map.put(">", ">"); // > - 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