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 java.io.StringReader;
20  import java.util.Map;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  import org.springmodules.validation.valang.functions.Function;
25  import org.springmodules.validation.valang.functions.TargetBeanFunction;
26  import org.springmodules.validation.valang.parser.ParseException;
27  import org.springmodules.validation.valang.parser.ValangParser;
28  
29  /**
30   * Resolves error arguments based on valang expressions.
31   *
32   * @author Uri Boness
33   */
34  public class ValangErrorArgumentsResolver implements ErrorArgumentsResolver {
35  
36      private final static Logger logger = LoggerFactory.getLogger(ValangErrorArgumentsResolver.class);
37  
38      private Function[] functions;
39  
40      /**
41       * Creates a new ValangErrorArgumentsResolver with no argument expressions, that is, there will be no arguments.
42       */
43      public ValangErrorArgumentsResolver() {
44          this(new String[0]);
45      }
46  
47      /**
48       * Creates a new ValangErrorArgumentsResolver with the given argument expressions. This constructor uses the default
49       * {@link ValangParser).
50       *
51       * @param expressions The argument valang expressions.
52       */
53      public ValangErrorArgumentsResolver(String[] expressions) {
54          this(expressions, null);
55      }
56  
57      /**
58       * Creates a new ValangErrorArgumentsResolver with given argument expressions and custom functions that will be used
59       * by the valang parser.
60       *
61       * @param expressions The arguments valang expressions.
62       * @param functionsByName A map of custom valang functiosns where the key is the function name and the value is
63       * the name of the function class.
64       */
65      public ValangErrorArgumentsResolver(String[] expressions, Map functionsByName) {
66          if (expressions == null) {
67              expressions = new String[0];
68          }
69          functions = new Function[expressions.length];
70          for (int i = 0; i < expressions.length; i++) {
71              functions[i] = parseFunction(expressions[i], functionsByName);
72          }
73      }
74  
75      /**
76       * Returns the error arguments that are resolved by the configured valang expressions.
77       *
78       * @see org.springmodules.validation.bean.rule.resolver.ErrorArgumentsResolver#resolveArguments(Object)
79       */
80      public Object[] resolveArguments(Object obj) {
81          Object[] args = new Object[functions.length];
82          for (int i = 0; i < args.length; i++) {
83              args[i] = functions[i].getResult(obj);
84          }
85          return args;
86      }
87  
88      //=============================================== Helper Methods ===================================================
89  
90      /**
91       * Parses a valang {@link Function} from the given argument expression and custom functions.
92       *
93       * @param expression The given argument expression.
94       * @param functionsByName The custom valang functions.
95       * @return The parsed function.
96       */
97      protected Function parseFunction(String expression, Map functionsByName) {
98          ValangParser parser = new ValangParser(new StringReader(expression));
99          parser.setFunctionsByName(functionsByName);
100         try {
101             return parser.function(new TargetBeanFunction());
102         } catch (ParseException pe) {
103             logger.error("Could not parse valang expression '" + expression + "' to a function", pe);
104             throw new IllegalArgumentException("Could not parse valang expression '" + expression + "' to a function");
105         }
106     }
107 
108 }