View Javadoc

1   /*
2    * Copyright 2004-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springmodules.validation.bean.rule.resolver;
18  
19  import org.springmodules.validation.util.fel.Function;
20  import org.springmodules.validation.util.fel.FunctionExpressionParser;
21  
22  /**
23   * Resolves error arguments based on valang expressions.
24   *
25   * @author Uri Boness
26   */
27  public class FunctionErrorArgumentsResolver implements ErrorArgumentsResolver {
28  
29      private Function[] functions;
30  
31      public FunctionErrorArgumentsResolver(Function[] functions) {
32          this.functions = functions;
33      }
34  
35      public FunctionErrorArgumentsResolver(String[] expressions, FunctionExpressionParser functionExpressionParser) {
36          this(parseFunctionExpressions(expressions, functionExpressionParser));
37      }
38  
39      /**
40       * Returns the error arguments that are resolved by the configured function expressions.
41       *
42       * @see org.springmodules.validation.bean.rule.resolver.ErrorArgumentsResolver#resolveArguments(Object)
43       */
44      public Object[] resolveArguments(Object obj) {
45          Object[] args = new Object[functions.length];
46          for (int i = 0; i < args.length; i++) {
47              args[i] = functions[i].evaluate(obj);
48          }
49          return args;
50      }
51  
52      //=============================================== Helper Methods ===================================================
53  
54      protected static Function[] parseFunctionExpressions(String[] expressions, FunctionExpressionParser parser) {
55          Function[] functions = new Function[expressions.length];
56          for (int i = 0; i < expressions.length; i++) {
57              functions[i] = parser.parse(expressions[i]);
58          }
59          return functions;
60      }
61  
62  }