Appender
used to escape content such as HTML. A
Formatter
is usually what will call the Escaper and like a formatter it should
be singleton like and expect reuse.
When a template outputs an escaped variable the callstack is as follows:
formatter --> escaper --> appendableEscapers are also a
Function<String,String>
to allow compatibility with
zero dependency generated code that expects Escapers to
be of type Function<String,String>
.
If escaping is not needed one can use PlainText.of()
which will just pass the
strings and primitives downstream without altering them. The default escaper unless in
zero dependency mode is provided by Html.of()
.
For context specific escaping like for example XML attributes consider using a
JStacheLambda
as the escaper is not passed information where in the template
escaping is requested.
Implementing
If performance is not a concern an easier way to create an implementation is to adapt a function by usingof(Function)
.
To implement a custom escaper:
- Implement this interface or use
of(Function)
. - Register the custom escaper. See
JStacheContentType
. - Set
JStacheConfig.contentType()
to the class that has theJStacheContentType
.
- Author:
- agentgt
- See Also:
- API Note
- Implementations should be threadsafe and expect reuse!
-
Method Summary
Modifier and TypeMethodDescriptionappend
(A a, boolean b) Escapes the character if it needs escaping.append
(A a, char c) Escapes the character if it needs escaping.append
(A a, double d) Escapes the character if it needs escaping.append
(A a, int i) Escapes the character if it needs escaping.append
(A a, long l) Escapes the character if it needs escaping.append
(A a, short s) Escapes the character if it needs escaping.append
(A a, CharSequence s) Escapes the characters if it needs it.append
(A a, CharSequence csq, int start, int end) Escapes the characters if it needs it.default String
Escapes a String by using StringBuilder and callingappend(Output, CharSequence)
.static Escaper
Adapts a function to an Escaper.
-
Method Details
-
apply
Escapes a String by using StringBuilder and callingappend(Output, CharSequence)
.This method is to make Escaper implementations compatible with
zero dependency generated code
that expects Escapers to beFunction<String,String>
.- Specified by:
apply
in interfaceFunction<String,
String> - Parameters:
t
- String to ge escaped.- Returns:
- escaped content
- Throws:
UncheckedIOException
- if the appender or appendable throw anIOException
-
append
Escapes the characters if it needs it. Analogous toAppendable.append(CharSequence)
. -
append
<A extends Output<E>,E extends Exception> void append(A a, CharSequence csq, int start, int end) throws E Escapes the characters if it needs it. Analogous toAppendable.append(CharSequence, int, int)
. -
append
Escapes the character if it needs escaping. Appends a character to the output. -
append
Escapes the character if it needs escaping. The default implementation willString.valueOf(short)
and callappend(Output, CharSequence)
. Appends a short to the output. -
append
Escapes the character if it needs escaping. The default implementation willString.valueOf(int)
and callappend(Output, CharSequence)
. Appends an int to the output.Implementations should override if they want different behavior or able to support appendables that can write the native type.
-
append
Escapes the character if it needs escaping. The default implementation willString.valueOf(long)
and callappend(Output, CharSequence)
. Appends a long to the output.Implementations should override if they want different behavior or able to support appendables that can write the native type.
-
append
Escapes the character if it needs escaping. The default implementation willString.valueOf(double)
and callappend(Output, CharSequence)
. Appends a double to the output.Implementations should override if they want different behavior or able to support appendables that can write the native type.
-
append
Escapes the character if it needs escaping. The default implementation willString.valueOf(boolean)
and callappend(Output, CharSequence)
. Appends a boolean to the output.Implementations should override if they want different behavior or able to support appendables that can write the native type.
-
of
Adapts a function to an Escaper. If the function is already an Escaper then it is simply returned (noop). Thus it is safe to repeatedly call this on an Escaper. If the function is adapted the returned adapted Escaper will convert native types withString.valueOf
first and then apply the escape function.- Parameters:
escapeFunction
- returned if it is already an escaper- Returns:
- adapted Escaper
-