Class DefaultTruncateBuiltinAlgorithm
TruncateBuiltinAlgorithm
implementation; see
Configurable.setTruncateBuiltinAlgorithm(TruncateBuiltinAlgorithm)
.
To know the properties of this TruncateBuiltinAlgorithm
implementation, see the
constructor. You can find more explanation and
examples in the documentation of the truncate
built-in in the FreeMarker Manual.- Since:
- 2.3.29
-
Field Summary
Modifier and TypeFieldDescriptionInstance that uses"[...]"
as thedefaultTerminator
constructor argument, and thus is safe to use for all output charsets.static double
The value used in the constructor ofASCII_INSTANCE
andUNICODE_INSTANCE
as thewordBoundaryMinLength
argument.static String
Used byASCII_INSTANCE
as the terminator.static TemplateHTMLOutputModel
Used byASCII_INSTANCE
andUNICODE_INSTANCE
as the markup terminator; HTML<span class='truncateTerminator'>[…]</span>
, where…
is the ellipsis (…) character.static String
Used byUNICODE_INSTANCE
as the terminator.Instance that uses"[…]"
as thedefaultTerminator
constructor argument, which contains ellipsis character ("…"
, U+2026), and thus only works with UTF-8, and the cp125x charsets (like cp1250), and with some other rarely used ones. -
Constructor Summary
ConstructorDescriptionDefaultTruncateBuiltinAlgorithm(String defaultTerminator, boolean addSpaceAtWordBoundary)
Creates an instance with string (plain text) terminator.DefaultTruncateBuiltinAlgorithm(String defaultTerminator, TemplateMarkupOutputModel<?> defaultMTerminator, boolean addSpaceAtWordBoundary)
Creates an instance with a string (plain text) terminator and a markup terminator.DefaultTruncateBuiltinAlgorithm(String defaultTerminator, Integer defaultTerminatorLength, Boolean defaultTerminatorRemovesDots, TemplateMarkupOutputModel<?> defaultMTerminator, Integer defaultMTerminatorLength, Boolean defaultMTerminatorRemovesDots, boolean addSpaceAtWordBoundary, Double wordBoundaryMinLength)
Creates an instance with markup terminator. -
Method Summary
Modifier and TypeMethodDescriptionboolean
See similarly named parameter ofDefaultTruncateBuiltinAlgorithm(String, Integer, Boolean, TemplateMarkupOutputModel, Integer, Boolean, boolean, Double)
the construction}.See similarly named parameter ofDefaultTruncateBuiltinAlgorithm(String, Integer, Boolean, TemplateMarkupOutputModel, Integer, Boolean, boolean, Double)
the construction}.See similarly named parameter ofDefaultTruncateBuiltinAlgorithm(String, Integer, Boolean, TemplateMarkupOutputModel, Integer, Boolean, boolean, Double)
the construction}.boolean
int
See similarly named parameter ofDefaultTruncateBuiltinAlgorithm(String, Integer, Boolean, TemplateMarkupOutputModel, Integer, Boolean, boolean, Double)
the construction}.boolean
See similarly named parameter ofDefaultTruncateBuiltinAlgorithm(String, Integer, Boolean, TemplateMarkupOutputModel, Integer, Boolean, boolean, Double)
the construction}.protected int
getMTerminatorLength(TemplateMarkupOutputModel<?> mTerminator)
Returns the (estimated) length of the argument terminator.protected boolean
getMTerminatorRemovesDots(TemplateMarkupOutputModel terminator)
Same asgetTerminatorRemovesDots(String)
, but invoked for a markup terminator.protected boolean
getTerminatorRemovesDots(String terminator)
Tells if the dots touched by the terminator text should be removed.double
See similarly named parameter ofDefaultTruncateBuiltinAlgorithm(String, Integer, Boolean, TemplateMarkupOutputModel, Integer, Boolean, boolean, Double)
the construction}.truncate(String s, int maxLength, TemplateScalarModel terminator, Integer terminatorLength, Environment env)
Corresponds to?truncate(...)
in templates.truncateC(String s, int maxLength, TemplateScalarModel terminator, Integer terminatorLength, Environment env)
Corresponds to?truncate_c_m(...)
in templates.truncateCM(String s, int maxLength, TemplateModel terminator, Integer terminatorLength, Environment env)
Corresponds to?truncate_c_m(...)
in templates.truncateM(String s, int maxLength, TemplateModel terminator, Integer terminatorLength, Environment env)
Corresponds to?truncate_m(...)
in templates.truncateW(String s, int maxLength, TemplateScalarModel terminator, Integer terminatorLength, Environment env)
Corresponds to?truncate_w(...)
in templates.truncateWM(String s, int maxLength, TemplateModel terminator, Integer terminatorLength, Environment env)
Corresponds to?truncate_w_m(...)
in templates.
-
Field Details
-
STANDARD_ASCII_TERMINATOR
Used byASCII_INSTANCE
as the terminator.- See Also:
- Constant Field Values
-
STANDARD_UNICODE_TERMINATOR
Used byUNICODE_INSTANCE
as the terminator.- See Also:
- Constant Field Values
-
STANDARD_M_TERMINATOR
Used byASCII_INSTANCE
andUNICODE_INSTANCE
as the markup terminator; HTML<span class='truncateTerminator'>[…]</span>
, where…
is the ellipsis (…) character. Note that while the ellipsis character is not in US-ASCII, this still works safely regardless of output charset, as…
itself only contains US-ASCII characters. -
DEFAULT_WORD_BOUNDARY_MIN_LENGTH
public static final double DEFAULT_WORD_BOUNDARY_MIN_LENGTHThe value used in the constructor ofASCII_INSTANCE
andUNICODE_INSTANCE
as thewordBoundaryMinLength
argument.- See Also:
- Constant Field Values
-
ASCII_INSTANCE
Instance that uses"[...]"
as thedefaultTerminator
constructor argument, and thus is safe to use for all output charsets. Because of that, this is the default ofConfigurable.setTruncateBuiltinAlgorithm(TruncateBuiltinAlgorithm)
. ThedefaultMTerminator
(markup terminator) isSTANDARD_M_TERMINATOR
, and thewordBoundaryMinLength
isDEFAULT_WORD_BOUNDARY_MIN_LENGTH
, andaddSpaceAtWordBoundary
istrue
. -
UNICODE_INSTANCE
Instance that uses"[…]"
as thedefaultTerminator
constructor argument, which contains ellipsis character ("…"
, U+2026), and thus only works with UTF-8, and the cp125x charsets (like cp1250), and with some other rarely used ones. It does not work (becomes to a question mark) with ISO-8859-x charsets (like ISO-8859-1), which are probably the most often used charsets after UTF-8.The
defaultMTerminator
(markup terminator) isSTANDARD_M_TERMINATOR
, and thewordBoundaryMinLength
isDEFAULT_WORD_BOUNDARY_MIN_LENGTH
, andaddSpaceAtWordBoundary
istrue
.
-
-
Constructor Details
-
DefaultTruncateBuiltinAlgorithm
public DefaultTruncateBuiltinAlgorithm(String defaultTerminator, TemplateMarkupOutputModel<?> defaultMTerminator, boolean addSpaceAtWordBoundary)Creates an instance with a string (plain text) terminator and a markup terminator. See parameters inDefaultTruncateBuiltinAlgorithm(String, Integer, Boolean, TemplateMarkupOutputModel, Integer, Boolean, boolean, Double)
; the missing parameters will benull
. -
DefaultTruncateBuiltinAlgorithm
Creates an instance with string (plain text) terminator. See parameters inDefaultTruncateBuiltinAlgorithm(String, Integer, Boolean, TemplateMarkupOutputModel, Integer, Boolean, boolean, Double)
; the missing parameters will benull
. -
DefaultTruncateBuiltinAlgorithm
public DefaultTruncateBuiltinAlgorithm(String defaultTerminator, Integer defaultTerminatorLength, Boolean defaultTerminatorRemovesDots, TemplateMarkupOutputModel<?> defaultMTerminator, Integer defaultMTerminatorLength, Boolean defaultMTerminatorRemovesDots, boolean addSpaceAtWordBoundary, Double wordBoundaryMinLength)Creates an instance with markup terminator.- Parameters:
defaultTerminator
- The terminator to use if the invocation (likes?truncate(20)
) doesn't specify it. The terminator is the text appended after a truncated string, to indicate that it was truncated. Typically, it's"[...]"
or"..."
, or the same with UNICODE ellipsis character.defaultTerminatorLength
- The assumed length ofdefaultTerminator
, ornull
if the assumed length is simplydefaultTerminator.length()
.defaultTerminatorRemovesDots
- Whether dots and ellipsis characters that thedefaultTerminator
touches should be removed. Ifnull
, this will be auto-detected based on ifdefaultTerminator
starts with dot or ellipsis. The goal is to avoid outcomes where we have more dots next to each other than there are in the terminator.defaultMTerminator
- Similar todefaultTerminator
, but is markup instead of plain text. This can benull
, in which casedefaultTerminator
will be used even if?truncate_m
or similar built-in is called.defaultMTerminatorLength
- The assumed length of the terminator, ornull
if the assumed length will begetMTerminatorLength(TemplateMarkupOutputModel)
. Note that if you have HTML tags, or entity references in thedefaultMTerminator
, then the visual length differs from the string length, andgetMTerminatorLength(TemplateMarkupOutputModel)
accounts for these complications to an extent, but it for example it won't know what CSS does, or if the nested content of some HTML elements are not displayed.defaultMTerminatorRemovesDots
- Similar todefaultTerminatorRemovesDots
, but fordefaultMTerminator
. Ifnull
, anddefaultMTerminator
is HTML/XML/XHTML, then it will be examined of the first character of the terminator that's outside a HTML/XML tag or comment is dot or ellipsis (after resolving numerical character references). For other kind of markup it defaults totrue
, to be on the safe side.addSpaceAtWordBoundary
- , Whether to add a space before the terminator if the truncation happens directly after the end of a word. For example, when "too long sentence" is truncated, it will be a like "too long [...]" instead of "too long[...]". When the truncation happens inside a word, this has no effect, i.e., it will be always like "too long se[...]" (no space before the terminator). Note that only whitespace is considered to be a word separator, not punctuation, so if this istrue
, you get results like "Some sentence. [...]".wordBoundaryMinLength
- Used whentruncate(java.lang.String, int, freemarker.template.TemplateScalarModel, java.lang.Integer, freemarker.core.Environment)
ortruncateM(java.lang.String, int, freemarker.template.TemplateModel, java.lang.Integer, freemarker.core.Environment)
has to decide between word boundary truncation, and character boundary truncation; it's the minimum length, given as proportion ofmaxLength
, that word boundary truncation has to produce. If the resulting length is less, we do character boundary truncation instead. For example, ifmaxLength
is 30, and this parameter is 0.85, then: 30 * 0.85 = 25.5, rounded up that's 26, so the resulting length must be at least 26. The result of character boundary truncation will always be accepted, even if it's still too short. If this parameter isnull
, thenDEFAULT_WORD_BOUNDARY_MIN_LENGTH
will be used. If this parameter is 0, then truncation always happens at word boundary. If this parameter is 1.0, then truncation doesn't prefer word boundaries over other places.
-
-
Method Details
-
truncate
public TemplateScalarModel truncate(String s, int maxLength, TemplateScalarModel terminator, Integer terminatorLength, Environment env) throws TemplateExceptionDescription copied from class:TruncateBuiltinAlgorithm
Corresponds to?truncate(...)
in templates. Similar toTruncateBuiltinAlgorithm.truncateM(String, int, TemplateModel, Integer, Environment)
, but only allows an FTL string as terminator, and thence the return value is always an FTL string as well (notTemplateMarkupOutputModel
).- Specified by:
truncate
in classTruncateBuiltinAlgorithm
- Throws:
TemplateException
-
truncateW
public TemplateScalarModel truncateW(String s, int maxLength, TemplateScalarModel terminator, Integer terminatorLength, Environment env) throws TemplateExceptionDescription copied from class:TruncateBuiltinAlgorithm
Corresponds to?truncate_w(...)
in templates. Same asTruncateBuiltinAlgorithm.truncateWM(String, int, TemplateModel, Integer, Environment)
, but only allows an FTL string as terminator, and thence the return value is always an FTL string as well (notTemplateMarkupOutputModel
).- Specified by:
truncateW
in classTruncateBuiltinAlgorithm
- Throws:
TemplateException
-
truncateC
public TemplateScalarModel truncateC(String s, int maxLength, TemplateScalarModel terminator, Integer terminatorLength, Environment env) throws TemplateExceptionDescription copied from class:TruncateBuiltinAlgorithm
Corresponds to?truncate_c_m(...)
in templates. Same asTruncateBuiltinAlgorithm.truncateCM(String, int, TemplateModel, Integer, Environment)
, but only allows an FTL string as terminator, and thence the return value is always an FTL string as well (not markup).- Specified by:
truncateC
in classTruncateBuiltinAlgorithm
- Throws:
TemplateException
-
truncateM
public TemplateModel truncateM(String s, int maxLength, TemplateModel terminator, Integer terminatorLength, Environment env) throws TemplateExceptionDescription copied from class:TruncateBuiltinAlgorithm
Corresponds to?truncate_m(...)
in templates. This method decides automatically if it will truncate at word boundary (seeTruncateBuiltinAlgorithm.truncateWM(java.lang.String, int, freemarker.template.TemplateModel, java.lang.Integer, freemarker.core.Environment)
) or at character boundary (seeTruncateBuiltinAlgorithm.truncateCM(java.lang.String, int, freemarker.template.TemplateModel, java.lang.Integer, freemarker.core.Environment)
). While it depends on the implementation, the idea is that it should truncate at word boundary, unless that gives a too short string, in which case it falls back to truncation at character duration.The terminator and the return value can be
TemplateMarkupOutputModel
(FTL markup output type), not justString
(FTL string type), hence the "m" in the name.- Specified by:
truncateM
in classTruncateBuiltinAlgorithm
- Parameters:
s
- The input string whose length need to be limited. The caller (the FreeMarker core normally) is expected to guarantee that this won't benull
.maxLength
- The maximum length of the returned string, although the algorithm need not guarantee this strictly. For example, if this is less than the length of theterminator
string, then most algorithms should still return theterminator
string. Or, some sophisticated algorithm may counts in letters differently depending on their visual width. The goal is usually to prevent unusually long string values to ruin visual layout, while showing clearly to the user that the end of the string was cut off. If the input string is not longer than the maximum length, then it should be returned as is. The caller (the FreeMarker core normally) is expected to guarantee that this will be at least 0.terminator
- The string or markup to show at the end of the returned string if the string was actually truncated. This can benull
, in which case the default terminator of the algorithm will be used. It can be an FTL string (aTemplateScalarModel
) of any length (including 0), or aTemplateMarkupOutputModel
(typically HTML markup). If it'sTemplateMarkupOutputModel
, then the result isTemplateMarkupOutputModel
of the same output format as well, otherwise it can remainTemplateScalarModel
. Note that the length of the terminator counts into the result length that shouldn't be exceed (maxLength
) (or at least the algorithm should make an effort to avoid that).terminatorLength
- The assumed length of the terminator. If this isnull
(and typically it is), then the method decides the length of the terminator. If this is notnull
, then the method must pretend that the terminator length is this. This can be used to specify the visual length of a terminator explicitly, which can't always be decided well programmatically.env
- The runtime environment from which this algorithm was called. The caller (the FreeMarker core normally) is expected to guarantee that this won't benull
.- Returns:
- The truncated text, which is either a
TemplateScalarModel
(FTL string), or aTemplateMarkupOutputModel
. - Throws:
TemplateException
- If anything goes wrong during truncating. It's unlikely that an implementation will need this though.
-
truncateWM
public TemplateModel truncateWM(String s, int maxLength, TemplateModel terminator, Integer terminatorLength, Environment env) throws TemplateExceptionDescription copied from class:TruncateBuiltinAlgorithm
Corresponds to?truncate_w_m(...)
in templates. Similar toTruncateBuiltinAlgorithm.truncateM(String, int, TemplateModel, Integer, Environment)
, but the truncation should happen at word boundary (hence the "w"). That is, the truncation isn't allowed to truncate a word. What counts as a word, is up to the implementation, but at least inDefaultTruncateBuiltinAlgorithm
words are the sections that are separated by whitespace (so punctuation doesn't separate words).- Specified by:
truncateWM
in classTruncateBuiltinAlgorithm
- Throws:
TemplateException
-
truncateCM
public TemplateModel truncateCM(String s, int maxLength, TemplateModel terminator, Integer terminatorLength, Environment env) throws TemplateExceptionDescription copied from class:TruncateBuiltinAlgorithm
Corresponds to?truncate_c_m(...)
in templates. Similar toTruncateBuiltinAlgorithm.truncateM(String, int, TemplateModel, Integer, Environment)
, but the truncation should not prefer truncating at word boundaries over the closer approximation of the desiredmaxLength
. Hence, we say that it truncates at character boundary (hence the "c").- Specified by:
truncateCM
in classTruncateBuiltinAlgorithm
- Throws:
TemplateException
-
getDefaultTerminator
-
getDefaultTerminatorLength
public int getDefaultTerminatorLength()See similarly named parameter ofDefaultTruncateBuiltinAlgorithm(String, Integer, Boolean, TemplateMarkupOutputModel, Integer, Boolean, boolean, Double)
the construction}. -
getDefaultTerminatorRemovesDots
public boolean getDefaultTerminatorRemovesDots()See similarly named parameter ofDefaultTruncateBuiltinAlgorithm(String, Integer, Boolean, TemplateMarkupOutputModel, Integer, Boolean, boolean, Double)
the construction}. -
getDefaultMTerminator
See similarly named parameter ofDefaultTruncateBuiltinAlgorithm(String, Integer, Boolean, TemplateMarkupOutputModel, Integer, Boolean, boolean, Double)
the construction}. -
getDefaultMTerminatorLength
See similarly named parameter ofDefaultTruncateBuiltinAlgorithm(String, Integer, Boolean, TemplateMarkupOutputModel, Integer, Boolean, boolean, Double)
the construction}. -
getDefaultMTerminatorRemovesDots
public boolean getDefaultMTerminatorRemovesDots() -
getWordBoundaryMinLength
public double getWordBoundaryMinLength()See similarly named parameter ofDefaultTruncateBuiltinAlgorithm(String, Integer, Boolean, TemplateMarkupOutputModel, Integer, Boolean, boolean, Double)
the construction}. -
getAddSpaceAtWordBoundary
public boolean getAddSpaceAtWordBoundary()See similarly named parameter ofDefaultTruncateBuiltinAlgorithm(String, Integer, Boolean, TemplateMarkupOutputModel, Integer, Boolean, boolean, Double)
the construction}. -
getMTerminatorLength
protected int getMTerminatorLength(TemplateMarkupOutputModel<?> mTerminator) throws TemplateModelExceptionReturns the (estimated) length of the argument terminator. It should only count characters that are visible for the user (like in the web browser).In the implementation in
DefaultTruncateBuiltinAlgorithm
, if the markup is HTML/XML/XHTML, then this counts the characters outside tags and comments, and inside CDATA sections (ignoring the CDATA section delimiters). Furthermore, then it counts character and entity references as having length of 1. If the markup is not HTML/XML/XHTML (or subclasses of thoseMarkupOutputFormat
-s) then it doesn't know how to measure it, and simply returns 3.- Throws:
TemplateModelException
-
getTerminatorRemovesDots
Tells if the dots touched by the terminator text should be removed.The implementation in
DefaultTruncateBuiltinAlgorithm
returntrue
if the terminator starts with dot (or ellipsis).- Parameters:
terminator
- ATemplateScalarModel
orTemplateMarkupOutputModel
. Notnull
.- Throws:
TemplateModelException
-
getMTerminatorRemovesDots
protected boolean getMTerminatorRemovesDots(TemplateMarkupOutputModel terminator) throws TemplateModelExceptionSame asgetTerminatorRemovesDots(String)
, but invoked for a markup terminator.The implementation in
DefaultTruncateBuiltinAlgorithm
will skip HTML/XML tags and comments, and resolves relevant character references to find out if the first character is dot or ellipsis. But it only does this for HTML/XMl/XHTML (or subclasses of thoseMarkupOutputFormat
-s), otherwise it always returnstrue
to be on the safe side.- Throws:
TemplateModelException
-