Whitespace Handling

AndHow generally removes leading and trailing whitespace from values as they are loaded, and each Property has a Trimmer instance to do the trimming.

Depending on where values are loaded from (i.e. which loader is used) trimming of String type values maybe enabled or disabled. For instance, if a String value is loaded from JNDI (the StdJndiLoader) or as a fixed value in code (StdFixedValueLoader), no trimming is done for String values because it is assumed that the value is in its final form. Non-String values are always trimmed as they are loaded.

Non-String Property Trimming

The TrimToNullTrimmer is used by most non-text properties and simply removes all whitespace on either end of a value. If the result is a zero length string, it becomes null.

String Property Trimming

StrProp, and likely most other text based properties, use a special QuotedSpacePreservingTrimmer (QSPT). The QSPT first trims to null, then, if the remaining text begins and ends with double quotes, those quotes are removed and the string inside the quotes, including whitespace, is preserved as the actual value.

Here are a few examples of the QSPT trimming behavior, using dots ()to represent whitespace and -> to separate the raw value on the left from the trimmed value on the right:

Raw ValueQSPT Trimmed ValueNotes

[null]

An all whitespace raw value is trimmed to null

●●●abc●●●

abc

whitespace on either side of text removed

"●abc●"

●abc●

Quotes are removed and all characters inside preserved

●"●abc●"●

●abc●

same result - whitespace outside the quotes is removed

●a "word" here●

a "word" here

No special quote handling here - there is text outside the quotes

●"●a "word" here●"●

●a "word" here●

After trimming outer whitespace, leading and trailing double quotes were found

●""●

[empty string]

Using quotes, it is possible to assign an empty string

The trimmer for a Property can be changed if a different behaviour is needed:

StrProp TRIM_ME_TO_NULL =
    StrProp.builder.trimmer(TrimToNullTrimmer.instance()).build();

Last updated