Class StringInterpolator

java.lang.Object
org.daiitech.naftah.parser.StringInterpolator

public final class StringInterpolator extends Object
Utility class for processing and evaluating string interpolation expressions.

Interpolation patterns supported are:

  • ${variable}
  • {variable}$

The class supports interpolation from different context types, including:

This class is not instantiable.

Author:
Chakib Daii
  • Field Details

    • INTERPOLATION_PATTERN

      private static final Pattern 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

      private static Map<String,Matcher> 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 a NaftahBugError when called.
  • Method Details

    • process

      public static String process(String input, Object context)
      Processes the input string by evaluating interpolation expressions using the provided context. which can be either:
      Parameters:
      input - the input string containing interpolation patterns
      context - the context object (either DefaultContext or Map<String, Object>)
      Returns:
      the interpolated string
      Throws:
      NaftahBugError - if the context type is unsupported
    • interpolate

      public static String interpolate(String template, DefaultContext context)
      Interpolates a string template using a DefaultContext.
      Parameters:
      template - the string containing interpolation patterns
      context - the variable context to resolve interpolated values
      Returns:
      the interpolated result
    • interpolate

      public static String interpolate(String template, Map<String,Object> context)
      Interpolates a string template using a Map<String, Object> context.
      Parameters:
      template - the string containing interpolation patterns
      context - the variable map to resolve values
      Returns:
      the interpolated result
    • interpolate

      public static String interpolate(String template, Function<String,Object> replacementFunction)
      Interpolates a string template using a custom variable resolution function.
      Parameters:
      template - the string with interpolation expressions
      replacementFunction - function used to resolve variable names to values
      Returns:
      the interpolated string
    • hasInterpolation

      public static boolean hasInterpolation(String input)
      Checks whether the given input string contains any interpolation pattern.
      Parameters:
      input - the input string to test
      Returns:
      true if interpolation is present, false otherwise
    • cleanInput

      public static String cleanInput(String input)
      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: »
      This is useful for sanitizing strings that may have been enclosed in different types of quotation marks during parsing or user input.
      Parameters:
      input - the original string potentially containing delimiters
      Returns:
      a new string with all delimiter characters removed
    • getMatcher

      private static Matcher getMatcher(String input)
      Returns a Matcher for the given input, using a cached matcher if available.
      Parameters:
      input - the input string to match
      Returns:
      a reset Matcher ready to be used