Sanitizer
| .claude | 6 months ago | ||
| src | 5 months ago | ||
| .gitignore | 6 years ago | ||
| Jenkinsfile | 2 years ago | ||
| README.md | 6 months ago | ||
| lombok.config | 5 years ago | ||
| pom.xml | 6 months ago | ||
Annotate member variables for sanitation
public class User {
@ConcurrentSpace
private String name;
}
Instantiate
Sanitizer sanitizer = new Sanitizer(object);
Sanitize
sanitizer.sanitize();
To traverse into embedded objects, collections, and arrays of non-primitive types, enable recursion before calling sanitize():
new Sanitizer(object).recursive().sanitize();
Recursion follows non-java.* package types and Collection elements, skipping primitives, enums, and null entries.
Every annotation below is applied to String values directly. When the annotated field is one of the following, every element is cleansed in place:
StringCollection<String> (e.g. List<String>, Set<String>)String[]If the annotated Collection is unmodifiable, the cleansed values cannot be written back and the field is left untouched.
Strip accents from characters
@Accent
Replaces all concurrent spaces with single space
@ConcurrentSpace
Example:
User user = new User("John Doe");
new Sanitizer(user).sanitize();
System.out.println(user.getName());
Output
John Doe
Lowercase all characters
@Lowercase
Removes all literal occurrences of the given string. The argument is not treated as a regular expression, so characters such as ., *, and ( are matched literally.
@Remove("-")
Required
Remove leading and trailing spaces
@Trim
Remove leading and trailing spaces and return null if empty
@TrimToNull
Uppercase all characters
@Uppercase
Sanitizes against a custom class
@CustomSanitizer(MyCleanser.class)
Required
CleanserNote: The class supplied to @CustomSanitizer must implement the Cleanser interface and expose a public no-argument constructor. If the class does not implement Cleanser, the field is silently skipped. If the class cannot be instantiated reflectively, an IllegalStateException is thrown.
Reflective failures while reading or writing fields are surfaced as IllegalStateException with the originating exception attached as the cause. This replaces the previous behavior of writing stack traces to standard error.