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  /**
20   * {@link ValueSource} abstract implementation that wraps another value source.
21   * When an expression is resolved, this wrapped source is first used to retrieve
22   * the expression's actual value; then, the last expression processed by this
23   * source is retrieved, and the two are passed into the abstract method
24   * {@link AbstractFunctionValueSourceWrapper#executeFunction(String, Object)}
25   * together. The result of this is returned as the resolved value for the second
26   * expression.
27   * <p>This allows the first expression to be a function name that modifies the
28   * value of the second expression, which is resolved from the wrapped value
29   * source.</p>
30   */
31  public abstract class AbstractFunctionValueSourceWrapper implements ValueSource {
32  
33      private final ValueSource valueSource;
34  
35      /**
36       * Construct a new function value source instance, using the supplied {@link ValueSource}
37       * to retrieve the input values for the function(s) this class implements.
38       *
39       * @param valueSource The value source to wrap
40       */
41      protected AbstractFunctionValueSourceWrapper(ValueSource valueSource) {
42          this.valueSource = valueSource;
43      }
44  
45      /**
46       * <ol>
47       *   <li>Resolve the current expression using the embedded {@link ValueSource}</li>
48       *   <li>Retrieve the last expression processed by this value source</li>
49       *   <li>Pass the last expression (which should be the function name), along
50       *       with the value for the current expression, into the
51       *       executeFunction(..) method</li>
52       *   <li>Return the result of the executeFunction(..) as the resolved value
53       *       for the current expression.</li>
54       * </ol>
55       */
56      public Object getValue(String expression) {
57          Object value = valueSource.getValue(expression);
58  
59          String expr = expression;
60  
61          if (valueSource instanceof QueryEnabledValueSource) {
62              expr = ((QueryEnabledValueSource) valueSource).getLastExpression();
63          }
64  
65          return executeFunction(expr, value);
66      }
67  
68      /**
69       * Retrieve the embedded value source.
70       * @return {@link ValueSource}
71       */
72      protected ValueSource getValueSource() {
73          return valueSource;
74      }
75  
76      /**
77       * Execute the function referenced in the last-processed expression using the
78       * value resolved from the current expression (using the embedded {@link ValueSource}).
79       *
80       * @param expression The last expression to be processed by this value source.
81       * @param value The value for the current expression, resolved by the embedded {@link ValueSource}
82       * @return The result of modifying the current expression's value using the function named by the last expression.
83       */
84      protected abstract Object executeFunction(String expression, Object value);
85  }