Class RegexBasedInterpolator

  • All Implemented Interfaces:
    BasicInterpolator, Interpolator

    public class RegexBasedInterpolator
    extends java.lang.Object
    implements Interpolator
    Expansion of the original RegexBasedInterpolator, found in plexus-utils, this interpolator provides options for setting custom prefix/suffix regex parts, and includes a RecursionInterceptor parameter in its interpolate(..) call, to allow the detection of cyclical expression references.
    • Field Detail

      • startRegex

        private java.lang.String startRegex
      • endRegex

        private java.lang.String endRegex
      • existingAnswers

        private java.util.Map existingAnswers
      • valueSources

        private java.util.List<ValueSource> valueSources
      • reusePatterns

        private boolean reusePatterns
      • cacheAnswers

        private boolean cacheAnswers
      • compiledPatterns

        private java.util.Map<java.lang.String,​java.util.regex.Pattern> compiledPatterns
        the key is the regex the value is the Pattern At the class construction time the Map will contains the default Pattern
    • Constructor Detail

      • RegexBasedInterpolator

        public RegexBasedInterpolator()
        Setup a basic interpolator.

        NOTE: You will have to call

        addValueSource(ValueSource) at least once if you use this constructor!
      • RegexBasedInterpolator

        public RegexBasedInterpolator​(boolean reusePatterns)
        Parameters:
        reusePatterns - already compiled patterns will be reused
      • RegexBasedInterpolator

        public RegexBasedInterpolator​(java.lang.String startRegex,
                                      java.lang.String endRegex)
        Setup an interpolator with no value sources, and the specified regex pattern prefix and suffix in place of the default one.

        NOTE: You will have to call addValueSource(ValueSource) at least once if you use this constructor!

        Parameters:
        startRegex - start of the regular expression to use
        endRegex - end of the regular expression to use
      • RegexBasedInterpolator

        public RegexBasedInterpolator​(java.util.List valueSources)
        Setup a basic interpolator with the specified list of value sources.
        Parameters:
        valueSources - The list of value sources to use
      • RegexBasedInterpolator

        public RegexBasedInterpolator​(java.lang.String startRegex,
                                      java.lang.String endRegex,
                                      java.util.List valueSources)
        Setup an interpolator with the specified value sources, and the specified regex pattern prefix and suffix in place of the default one.
        Parameters:
        startRegex - start of the regular expression to use
        endRegex - end of the regular expression to use
        valueSources - The list of value sources to use
    • Method Detail

      • interpolate

        public java.lang.String interpolate​(java.lang.String input,
                                            java.lang.String thisPrefixPattern,
                                            RecursionInterceptor recursionInterceptor)
                                     throws InterpolationException
        Attempt to resolve all expressions in the given input string, using the given pattern to first trim an optional prefix from each expression. The supplied recursion interceptor will provide protection from expression cycles, ensuring that the input can be resolved or an exception is thrown.
        Specified by:
        interpolate in interface Interpolator
        Parameters:
        input - The input string to interpolate
        thisPrefixPattern - An optional pattern that should be trimmed from the start of any expressions found in the input.
        recursionInterceptor - Used to protect the interpolation process from expression cycles, and throw an exception if one is detected.
        Returns:
        interpolated string.
        Throws:
        InterpolationException - in case of an error.
      • getPattern

        private java.util.regex.Pattern getPattern​(java.lang.String regExp)
      • interpolate

        private java.lang.String interpolate​(java.lang.String input,
                                             RecursionInterceptor recursionInterceptor,
                                             java.util.regex.Pattern expressionPattern,
                                             int realExprGroup)
                                      throws InterpolationException
        Entry point for recursive resolution of an expression and all of its nested expressions.
        Throws:
        InterpolationException
      • getFeedback

        public java.util.List getFeedback()
        Return any feedback messages and errors that were generated - but suppressed - during the interpolation process. Since unresolvable expressions will be left in the source string as-is, this feedback is optional, and will only be useful for debugging interpolation problems.
        Specified by:
        getFeedback in interface Interpolator
        Returns:
        a List that may be interspersed with String and Throwable instances.
      • clearFeedback

        public void clearFeedback()
        Clear the feedback messages from previous interpolate(..) calls.
        Specified by:
        clearFeedback in interface Interpolator
      • isReusePatterns

        public boolean isReusePatterns()
      • setReusePatterns

        public void setReusePatterns​(boolean reusePatterns)
      • isCacheAnswers

        public boolean isCacheAnswers()
        Specified by:
        isCacheAnswers in interface Interpolator
        Returns:
        state of the cacheAnswers
      • setCacheAnswers

        public void setCacheAnswers​(boolean cacheAnswers)
        Specified by:
        setCacheAnswers in interface Interpolator
        Parameters:
        cacheAnswers - true/false.