View Javadoc
1   package org.codehaus.plexus.interpolation.fixed;
2   
3   /*
4    * Copyright 2014 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.codehaus.plexus.interpolation.util.ValueSourceUtils;
20  
21  import java.util.List;
22  
23  /**
24   * {@link org.codehaus.plexus.interpolation.fixed.FixedValueSource} implementation which simply wraps another
25   * value source, and trims any of a set of possible expression prefixes before delegating the
26   * modified expression to be resolved by the real value source.
27   *
28   * @author jdcasey
29   * @author krosenvold
30   */
31  public class PrefixedValueSourceWrapper implements FixedValueSource
32  {
33  
34      private final FixedValueSource valueSource;
35  
36      private final String[] possiblePrefixes;
37  
38      private boolean allowUnprefixedExpressions;
39  
40      private String lastExpression;
41  
42      /**
43       * Wrap the given value source, but first trim the given prefix from any
44       * expressions before they are passed along for resolution. If an expression
45       * doesn't start with the given prefix, do not resolve it.
46       *
47       * @param valueSource The {@link org.codehaus.plexus.interpolation.ValueSource} to wrap.
48       * @param prefix      The expression prefix to trim.
49       */
50      public PrefixedValueSourceWrapper( FixedValueSource valueSource, String prefix )
51      {
52          this.valueSource = valueSource;
53          possiblePrefixes = new String[]{ prefix };
54      }
55  
56      /**
57       * Wrap the given value source, but first trim the given prefix from any
58       * expressions before they are passed along for resolution. If an expression
59       * doesn't start with the given prefix and the allowUnprefixedExpressions flag
60       * is set to true, simply pass the expression through to the nested value source
61       * unchanged. If this flag is false, only allow resolution of those expressions
62       * that start with the specified prefix.
63       *
64       * @param valueSource                The {@link org.codehaus.plexus.interpolation.ValueSource} to wrap.
65       * @param prefix                     The expression prefix to trim.
66       * @param allowUnprefixedExpressions Flag telling the wrapper whether to
67       *                                   continue resolving expressions that don't start with the prefix it tracks.
68       */
69      public PrefixedValueSourceWrapper( FixedValueSource valueSource, String prefix, boolean allowUnprefixedExpressions )
70      {
71          this.valueSource = valueSource;
72          possiblePrefixes = new String[]{ prefix };
73          this.allowUnprefixedExpressions = allowUnprefixedExpressions;
74      }
75  
76      /**
77       * Wrap the given value source, but first trim one of the given prefixes from any
78       * expressions before they are passed along for resolution. If an expression
79       * doesn't start with one of the given prefixes, do not resolve it.
80       *
81       * @param valueSource      The {@link org.codehaus.plexus.interpolation.ValueSource} to wrap.
82       * @param possiblePrefixes The List of expression prefixes to trim.
83       */
84      public PrefixedValueSourceWrapper( FixedValueSource valueSource, List<String> possiblePrefixes )
85      {
86          this.valueSource = valueSource;
87          this.possiblePrefixes = possiblePrefixes.toArray( new String[possiblePrefixes.size()] );
88      }
89  
90      /**
91       * Wrap the given value source, but first trim one of the given prefixes from any
92       * expressions before they are passed along for resolution. If an expression
93       * doesn't start with the given prefix and the allowUnprefixedExpressions flag
94       * is set to true, simply pass the expression through to the nested value source
95       * unchanged. If this flag is false, only allow resolution of those expressions
96       * that start with the specified prefix.
97       *
98       * @param valueSource                The {@link org.codehaus.plexus.interpolation.ValueSource} to wrap.
99       * @param possiblePrefixes           The List of expression prefixes to trim.
100      * @param allowUnprefixedExpressions Flag telling the wrapper whether to
101      *                                   continue resolving expressions that don't start with one of the prefixes it tracks.
102      */
103     public PrefixedValueSourceWrapper( FixedValueSource valueSource, List<String> possiblePrefixes,
104                                        boolean allowUnprefixedExpressions )
105     {
106         this.valueSource = valueSource;
107         this.possiblePrefixes = possiblePrefixes.toArray( new String[possiblePrefixes.size()] );
108         this.allowUnprefixedExpressions = allowUnprefixedExpressions;
109     }
110 
111 
112     public Object getValue( String expression, InterpolationState interpolationState )
113     {
114         expression = ValueSourceUtils.trimPrefix( expression, possiblePrefixes, allowUnprefixedExpressions );
115 
116         if ( expression == null )
117         {
118             return null;
119         }
120 
121         return valueSource.getValue( expression, interpolationState );
122     }
123 }