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