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