View Javadoc
1   package org.codehaus.plexus.interpolation;
2   
3   /*
4    * Copyright 2001-2008 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 java.util.Collections;
20  import java.util.List;
21  
22  import org.codehaus.plexus.interpolation.util.ValueSourceUtils;
23  
24  /**
25   * {@link ValueSource} implementation which simply wraps another value source,
26   * and trims any of a set of possible expression prefixes before delegating the
27   * modified expression to be resolved by the real value source.
28   *
29   * @author jdcasey
30   */
31  public class PrefixedValueSourceWrapper implements FeedbackEnabledValueSource, QueryEnabledValueSource {
32  
33      private final ValueSource valueSource;
34  
35      private final String[] possiblePrefixes;
36  
37      private boolean allowUnprefixedExpressions;
38  
39      private String lastExpression;
40  
41      /**
42       * Wrap the given value source, but first trim the given prefix from any
43       * expressions before they are passed along for resolution. If an expression
44       * doesn't start with the given prefix, do not resolve it.
45       *
46       * @param valueSource The {@link ValueSource} to wrap.
47       * @param prefix      The expression prefix to trim.
48       */
49      public PrefixedValueSourceWrapper(ValueSource valueSource, String prefix) {
50          this.valueSource = valueSource;
51          possiblePrefixes = new String[] {prefix};
52      }
53  
54      /**
55       * Wrap the given value source, but first trim the given prefix from any
56       * expressions before they are passed along for resolution. If an expression
57       * doesn't start with the given prefix and the allowUnprefixedExpressions flag
58       * is set to true, simply pass the expression through to the nested value source
59       * unchanged. If this flag is false, only allow resolution of those expressions
60       * that start with the specified prefix.
61       *
62       * @param valueSource                The {@link ValueSource} to wrap.
63       * @param prefix                     The expression prefix to trim.
64       * @param allowUnprefixedExpressions Flag telling the wrapper whether to
65       *                                   continue resolving expressions that don't start with the prefix it tracks.
66       */
67      public PrefixedValueSourceWrapper(ValueSource valueSource, String prefix, boolean allowUnprefixedExpressions) {
68          this.valueSource = valueSource;
69          possiblePrefixes = new String[] {prefix};
70          this.allowUnprefixedExpressions = allowUnprefixedExpressions;
71      }
72  
73      /**
74       * Wrap the given value source, but first trim one of the given prefixes from any
75       * expressions before they are passed along for resolution. If an expression
76       * doesn't start with one of the given prefixes, do not resolve it.
77       *
78       * @param valueSource      The {@link ValueSource} to wrap.
79       * @param possiblePrefixes The List of expression prefixes to trim.
80       */
81      public PrefixedValueSourceWrapper(ValueSource valueSource, List<String> possiblePrefixes) {
82          this.valueSource = valueSource;
83          this.possiblePrefixes = possiblePrefixes.toArray(new String[possiblePrefixes.size()]);
84      }
85  
86      /**
87       * Wrap the given value source, but first trim one of the given prefixes from any
88       * expressions before they are passed along for resolution. If an expression
89       * doesn't start with the given prefix and the allowUnprefixedExpressions flag
90       * is set to true, simply pass the expression through to the nested value source
91       * unchanged. If this flag is false, only allow resolution of those expressions
92       * that start with the specified prefix.
93       *
94       * @param valueSource                The {@link ValueSource} to wrap.
95       * @param possiblePrefixes           The List of expression prefixes to trim.
96       * @param allowUnprefixedExpressions Flag telling the wrapper whether to
97       *                                   continue resolving expressions that don't start with one of the prefixes it tracks.
98       */
99      public PrefixedValueSourceWrapper(
100             ValueSource valueSource, List<String> possiblePrefixes, boolean allowUnprefixedExpressions) {
101         this.valueSource = valueSource;
102         this.possiblePrefixes = possiblePrefixes.toArray(new String[possiblePrefixes.size()]);
103         this.allowUnprefixedExpressions = allowUnprefixedExpressions;
104     }
105 
106     /**
107      * Uses {@link ValueSourceUtils#trimPrefix(String, java.util.Collection, boolean)} to
108      * get the trimmed expression. If this expression is null (because the original
109      * expression was null, or because the expression is unprefixed and unprefixed
110      * expressions are not allowed here), then return null; otherwise, return the
111      * nested {@link ValueSource#getValue(String)} result.
112      */
113     public Object getValue(String expression) {
114         lastExpression = ValueSourceUtils.trimPrefix(expression, possiblePrefixes, allowUnprefixedExpressions);
115 
116         if (lastExpression == null) {
117             return null;
118         }
119 
120         return valueSource.getValue(lastExpression);
121     }
122 
123     /**
124      * If the nested {@link ValueSource} implements {@link FeedbackEnabledValueSource},
125      * then return that source's feedback list. Otherwise, return {@link Collections#EMPTY_LIST}.
126      */
127     public List getFeedback() {
128         return (valueSource instanceof FeedbackEnabledValueSource) ? valueSource.getFeedback() : Collections.EMPTY_LIST;
129     }
130 
131     /**
132      * If the nested {@link ValueSource} implements {@link QueryEnabledValueSource},
133      * then return that source's last expression. Otherwise, return the last expression
134      * that was processed by the wrapper itself.
135      */
136     public String getLastExpression() {
137         return (valueSource instanceof QueryEnabledValueSource)
138                 ? ((QueryEnabledValueSource) valueSource).getLastExpression()
139                 : lastExpression;
140     }
141 
142     /**
143      * If the nested {@link ValueSource} implements {@link FeedbackEnabledValueSource},
144      * then clear that source's feedback list.
145      */
146     public void clearFeedback() {
147         valueSource.clearFeedback();
148     }
149 }