View Javadoc

1   /*
2    * Copyright 2004-2010 the Seasar Foundation and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  
17  package org.seasar.cubby.validator;
18  
19  import static org.seasar.cubby.internal.util.LogMessages.format;
20  
21  import java.lang.reflect.Method;
22  
23  import org.seasar.cubby.action.Validation;
24  import org.seasar.cubby.spi.beans.Attribute;
25  import org.seasar.cubby.spi.beans.BeanDesc;
26  import org.seasar.cubby.spi.beans.BeanDescFactory;
27  
28  /**
29   * バリデーションのユーティリティクラスです。
30   * 
31   * @author baba
32   */
33  public class ValidationUtils {
34  
35  	/**
36  	 * 指定されたメソッドを修飾する {@link Validation} を取得します。
37  	 * 
38  	 * @param method
39  	 *            メソッド
40  	 * @return {@link Validation}、修飾されていない場合は <code>null</code>
41  	 */
42  	public static Validation getValidation(final Method method) {
43  		return method.getAnnotation(Validation.class);
44  	}
45  
46  	/**
47  	 * 実行しているアクションメソッドの入力検証ルールの集合を取得します。
48  	 * 
49  	 * @param action
50  	 *            アクション
51  	 * @param attributeName
52  	 *            入力検証ルールの集合が定義された属性名
53  	 * @return アクションメソッドの入力検証ルールの集合
54  	 */
55  	public static ValidationRules getValidationRules(final Object action,
56  			final String attributeName) {
57  		final BeanDesc beanDesc = BeanDescFactory
58  				.getBeanDesc(action.getClass());
59  		final Attribute attribute;
60  		if (beanDesc.hasPropertyAttribute(attributeName)) {
61  			attribute = beanDesc.getPropertyAttribute(attributeName);
62  		} else if (beanDesc.hasFieldAttribute(attributeName)) {
63  			attribute = beanDesc.getFieldAttribute(attributeName);
64  		} else {
65  			throw new IllegalStateException(format("ECUB0113", action,
66  					attributeName));
67  		}
68  		if (!ValidationRules.class.isAssignableFrom(attribute.getType())) {
69  			throw new IllegalStateException(format("ECUB0114", action,
70  					attributeName, ValidationRules.class.getName()));
71  		}
72  		final ValidationRules rules = (ValidationRules) attribute
73  				.getValue(action);
74  		return rules;
75  	}
76  
77  }