CleanPatternSanitizer implements Sanitizer<String> { @Override public String execute(String content) { if (StringUtils.isBlank(content)) { return content; } Matcher matcher = _pattern.matcher(content); StringBuilder builder = new StringBuilder(); while (matcher.find()) { builder.append(matcher.group()); } return builder.toString(); } CleanPatternSanitizer(Pattern acceptedPattern);  CleanPatternSanitizer(String acceptedPattern); @Override String execute(String content); static final int FLAGS; }
@Test public void testExecuteWithCharacterClass() throws Exception { CleanPatternSanitizer sanitizer = new CleanPatternSanitizer("[\\p{L}\\p{Nd}\\.\\-\\s#&',:\\/\\\\]+"); assertEquals("P.O. Box 123", sanitizer.execute("P.O. Box 123")); assertEquals("/P.O. Box 123", sanitizer.execute("/P.O. Box 123")); assertEquals("\\P.O. Box 123", sanitizer.execute("\\P.O. Box 123")); assertEquals("b50 First St/b, iFloor 7/uscriptalerthacked/script", sanitizer.execute("<b>50 First St</b>, <i>Floor 7</u><script>alert(hacked!);</script>")); assertEquals("50 First St, Floor 7", sanitizer.execute("^50 First} St, ~Flo|or 7")); }
@Test public void testExecuteWithPattern() throws Exception { CleanPatternSanitizer sanitizer = new CleanPatternSanitizer("[\\d]{4}-[\\d]{2}-[\\d]{2}"); assertEquals("2012-06-28", sanitizer.execute("2012-06-28")); assertEquals("2012-06-282012-06-28", sanitizer.execute("2012-06-28 2012-06-28")); assertEquals("2012-06-28", sanitizer.execute("yadda yadda 2012-06-28")); assertEquals("", sanitizer.execute("^50 First} St, ~Flo|or 7")); }