Class RegexBasedInterpolator
- java.lang.Object
-
- org.codehaus.plexus.interpolation.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 aRecursionInterceptorparameter in its interpolate(..) call, to allow the detection of cyclical expression references.
-
-
Field Summary
Fields Modifier and Type Field Description private booleancacheAnswersprivate java.util.Map<java.lang.String,java.util.regex.Pattern>compiledPatternsthe key is the regex the value is the Pattern At the class construction time the Map will contains the default Patternstatic java.lang.StringDEFAULT_REGEXPprivate java.lang.StringendRegexprivate java.util.MapexistingAnswersprivate java.util.List<InterpolationPostProcessor>postProcessorsprivate booleanreusePatternsprivate java.lang.StringstartRegexprivate java.util.List<ValueSource>valueSources
-
Constructor Summary
Constructors Constructor Description RegexBasedInterpolator()Setup a basic interpolator.RegexBasedInterpolator(boolean reusePatterns)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.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.RegexBasedInterpolator(java.util.List valueSources)Setup a basic interpolator with the specified list of value sources.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddPostProcessor(InterpolationPostProcessor postProcessor)Add a new post-processor to handle final processing after recursively-interpolated value is determined.voidaddValueSource(ValueSource valueSource)Add a newValueSourceto the stack used to resolve expressions in this interpolator instance.voidclearAnswers()voidclearFeedback()Clear the feedback messages from previous interpolate(..) calls.java.util.ListgetFeedback()Return any feedback messages and errors that were generated - but suppressed - during the interpolation process.private java.util.regex.PatterngetPattern(java.lang.String regExp)java.lang.Stringinterpolate(java.lang.String input)java.lang.Stringinterpolate(java.lang.String input, java.lang.String thisPrefixPattern)java.lang.Stringinterpolate(java.lang.String input, java.lang.String thisPrefixPattern, RecursionInterceptor recursionInterceptor)Attempt to resolve all expressions in the given input string, using the given pattern to first trim an optional prefix from each expression.java.lang.Stringinterpolate(java.lang.String input, RecursionInterceptor recursionInterceptor)private java.lang.Stringinterpolate(java.lang.String input, RecursionInterceptor recursionInterceptor, java.util.regex.Pattern expressionPattern, int realExprGroup)Entry point for recursive resolution of an expression and all of its nested expressions.booleanisCacheAnswers()booleanisReusePatterns()voidremovePostProcessor(InterpolationPostProcessor postProcessor)Remove the given post-processor.voidremoveValuesSource(ValueSource valueSource)Remove the specifiedValueSourcefrom the stack used to resolve expressions in this interpolator instance.voidsetCacheAnswers(boolean cacheAnswers)voidsetReusePatterns(boolean reusePatterns)
-
-
-
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
-
postProcessors
private java.util.List<InterpolationPostProcessor> postProcessors
-
reusePatterns
private boolean reusePatterns
-
cacheAnswers
private boolean cacheAnswers
-
DEFAULT_REGEXP
public static final java.lang.String DEFAULT_REGEXP
- See Also:
- Constant Field Values
-
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 useendRegex- 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 useendRegex- end of the regular expression to usevalueSources- The list of value sources to use
-
-
Method Detail
-
addValueSource
public void addValueSource(ValueSource valueSource)
Add a newValueSourceto the stack used to resolve expressions in this interpolator instance.- Specified by:
addValueSourcein interfaceInterpolator- Parameters:
valueSource-ValueSource.
-
removeValuesSource
public void removeValuesSource(ValueSource valueSource)
Remove the specifiedValueSourcefrom the stack used to resolve expressions in this interpolator instance.- Specified by:
removeValuesSourcein interfaceInterpolator- Parameters:
valueSource-ValueSource.
-
addPostProcessor
public void addPostProcessor(InterpolationPostProcessor postProcessor)
Add a new post-processor to handle final processing after recursively-interpolated value is determined.- Specified by:
addPostProcessorin interfaceInterpolator- Parameters:
postProcessor-InterpolationPostProcessor.
-
removePostProcessor
public void removePostProcessor(InterpolationPostProcessor postProcessor)
Remove the given post-processor.- Specified by:
removePostProcessorin interfaceInterpolator- Parameters:
postProcessor-InterpolationPostProcessor.
-
interpolate
public java.lang.String interpolate(java.lang.String input, java.lang.String thisPrefixPattern, RecursionInterceptor recursionInterceptor) throws InterpolationExceptionAttempt 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:
interpolatein interfaceInterpolator- Parameters:
input- The input string to interpolatethisPrefixPattern- 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 InterpolationExceptionEntry 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:
getFeedbackin interfaceInterpolator- Returns:
- a
Listthat may be interspersed withStringandThrowableinstances.
-
clearFeedback
public void clearFeedback()
Clear the feedback messages from previous interpolate(..) calls.- Specified by:
clearFeedbackin interfaceInterpolator
-
interpolate
public java.lang.String interpolate(java.lang.String input, java.lang.String thisPrefixPattern) throws InterpolationExceptionSeeinterpolate(String, String, RecursionInterceptor).This method triggers the use of a
SimpleRecursionInterceptorinstance for protection against expression cycles.- Specified by:
interpolatein interfaceInterpolator- Parameters:
input- The input string to interpolatethisPrefixPattern- An optional pattern that should be trimmed from the start of any expressions found in the input.- Returns:
- interpolated string.
- Throws:
InterpolationException- in case of an error.
-
interpolate
public java.lang.String interpolate(java.lang.String input) throws InterpolationExceptionSeeinterpolate(String, String, RecursionInterceptor).This method triggers the use of a
SimpleRecursionInterceptorinstance for protection against expression cycles. It also leaves empty the expression prefix which would otherwise be trimmed from expressions. The result is that any detected expression will be resolved as-is.- Specified by:
interpolatein interfaceBasicInterpolator- Parameters:
input- The input string to interpolate- Returns:
- the interpolated string.
- Throws:
InterpolationException- in case of an error.
-
interpolate
public java.lang.String interpolate(java.lang.String input, RecursionInterceptor recursionInterceptor) throws InterpolationExceptionSeeinterpolate(String, String, RecursionInterceptor).This method leaves empty the expression prefix which would otherwise be trimmed from expressions. The result is that any detected expression will be resolved as-is.
- Specified by:
interpolatein interfaceBasicInterpolator- Parameters:
input- The input string to interpolaterecursionInterceptor- Used to protect the interpolation process from expression cycles, and throw an exception if one is detected.- Returns:
- the interpolated string.
- Throws:
InterpolationException- in case of an error.
-
isReusePatterns
public boolean isReusePatterns()
-
setReusePatterns
public void setReusePatterns(boolean reusePatterns)
-
isCacheAnswers
public boolean isCacheAnswers()
- Specified by:
isCacheAnswersin interfaceInterpolator- Returns:
- state of the cacheAnswers
-
setCacheAnswers
public void setCacheAnswers(boolean cacheAnswers)
- Specified by:
setCacheAnswersin interfaceInterpolator- Parameters:
cacheAnswers- true/false.
-
clearAnswers
public void clearAnswers()
- Specified by:
clearAnswersin interfaceInterpolator
-
-