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