View Javadoc

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