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.context;
18  
19  import org.apache.commons.lang.ArrayUtils;
20  
21  /**
22   * A simple and default implemenation of the {@link ValidationContext} interface.
23   *
24   * @author Uri Boness
25   */
26  public class DefaultValidationContext implements ValidationContext {
27  
28      private String[] tokens;
29  
30      /**
31       * Construsts a new DefaultValidationContext with a given supported validation token.
32       *
33       * @param token The validation token supported by this context.
34       */
35      public DefaultValidationContext(String token) {
36          this(new String[] { token });
37      }
38  
39      /**
40       * Construsts a new DefaultValidationContext with given supported validation tokens.
41       *
42       * @param tokens The validation tokens supported by this context.
43       */
44      public DefaultValidationContext(String[] tokens) {
45          this.tokens = tokens;
46      }
47  
48      /**
49       * @see #supportsTokens(String[])
50       */
51      public boolean supportsTokens(String token) {
52          return supportsTokens(new String[] { token });
53      }
54  
55      /**
56       * @see ValidationContext#supportsTokens(String[])
57       */
58      public boolean supportsTokens(String[] tokens) {
59          for (int i=0; i<tokens.length; i++) {
60              if (ArrayUtils.contains(this.tokens, tokens[i])) {
61                  return true;
62              }
63          }
64          return false;
65      }
66  
67  
68      //============================================== Setter/Getter =====================================================
69  
70      public String[] getTokens() {
71          return tokens;
72      }
73  
74  }