Package org.daiitech.naftah.parser
Class StringInterpolator
java.lang.Object
org.daiitech.naftah.parser.StringInterpolator
Utility class for processing and evaluating string interpolation expressions.
Interpolation patterns supported are:
${variable}{variable}$
The class supports interpolation from different context types, including:
DefaultContextMap<String, Object>
This class is not instantiable.
- Author:
- Chakib Daii
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final PatternRegular expression pattern used to match interpolation variables in the following formats (Left-to-Right only):{{المتغير}}{{المتغير:القيمة_الافتراضية}}{المتغير}${المتغير:القيمة_الافتراضية}$${المتغير}${المتغير:القيمة_الافتراضية}Cache of compiled matchers for given input strings to improve performance on repeated interpolation calls. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePrivate constructor to prevent instantiation. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcleanInput(String input) Cleans the input string by removing common string delimiter characters.private static MatchergetMatcher(String input) Returns aMatcherfor the given input, using a cached matcher if available.static booleanhasInterpolation(String input) Checks whether the given input string contains any interpolation pattern.static Stringinterpolate(String template, Function<String, Object> replacementFunction) Interpolates a string template using a custom variable resolution function.static Stringinterpolate(String template, Map<String, Object> context) Interpolates a string template using aMap<String, Object>context.static Stringinterpolate(String template, DefaultContext context) Interpolates a string template using aDefaultContext.static StringProcesses the input string by evaluating interpolation expressions using the provided context.
-
Field Details
-
INTERPOLATION_PATTERN
Regular expression pattern used to match interpolation variables in the following formats (Left-to-Right only):{{المتغير}}{{المتغير:القيمة_الافتراضية}}{المتغير}${المتغير:القيمة_الافتراضية}$${المتغير}${المتغير:القيمة_الافتراضية}
Each format may optionally include a default value before the variable name, separated by a colon (
:), e.g.,{{العنوان:بدون_عنوان}}. -
MATCHER_CACHE
Cache of compiled matchers for given input strings to improve performance on repeated interpolation calls.
-
-
Constructor Details
-
StringInterpolator
private StringInterpolator()Private constructor to prevent instantiation. Always throws aNaftahBugErrorwhen called.
-
-
Method Details
-
process
Processes the input string by evaluating interpolation expressions using the provided context. which can be either:DefaultContextMap<String, Object>
- Parameters:
input- the input string containing interpolation patternscontext- the context object (eitherDefaultContextorMap<String, Object>)- Returns:
- the interpolated string
- Throws:
NaftahBugError- if the context type is unsupported
-
interpolate
Interpolates a string template using aDefaultContext.- Parameters:
template- the string containing interpolation patternscontext- the variable context to resolve interpolated values- Returns:
- the interpolated result
-
interpolate
Interpolates a string template using aMap<String, Object>context.- Parameters:
template- the string containing interpolation patternscontext- the variable map to resolve values- Returns:
- the interpolated result
-
interpolate
Interpolates a string template using a custom variable resolution function.- Parameters:
template- the string with interpolation expressionsreplacementFunction- function used to resolve variable names to values- Returns:
- the interpolated string
-
hasInterpolation
Checks whether the given input string contains any interpolation pattern.- Parameters:
input- the input string to test- Returns:
trueif interpolation is present,falseotherwise
-
cleanInput
Cleans the input string by removing common string delimiter characters.This method removes the following characters from the input:
- Double quotes:
" - Left angle quote:
« - Right angle quote:
»
- Parameters:
input- the original string potentially containing delimiters- Returns:
- a new string with all delimiter characters removed
- Double quotes:
-
getMatcher
Returns aMatcherfor the given input, using a cached matcher if available.- Parameters:
input- the input string to match- Returns:
- a reset
Matcherready to be used
-