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.CubbyConstants.ATTR_CONVERSION_FAILURES;
19  
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.servlet.http.HttpServletRequest;
24  
25  import org.seasar.cubby.action.ActionErrors;
26  import org.seasar.cubby.action.FieldInfo;
27  import org.seasar.cubby.action.MessageInfo;
28  import org.seasar.cubby.internal.controller.ConversionFailure;
29  import org.seasar.cubby.internal.controller.ThreadContext;
30  import org.seasar.cubby.internal.util.RequestUtils;
31  
32  /**
33   * 要求パラメータをフォームオブジェクトへのバインドする時の型変換エラーを検証する入力検証ルールです。
34   * 
35   * @author baba
36   */
37  public class ConversionValidationRule implements ValidationRule {
38  
39  	/** リソースのキープレフィックス。 */
40  	private final String resourceKeyPrefix;
41  
42  	/**
43  	 * キーのプレフィックスなしでインスタンス化します。
44  	 */
45  	public ConversionValidationRule() {
46  		this(null);
47  	}
48  
49  	/**
50  	 * 指定されたプレフィックスをフィールド名のキーのプレフィックスとしてインスタンス化します。
51  	 * 
52  	 * @param resourceKeyPrefix
53  	 *            リソースのキープレフィックス
54  	 */
55  	public ConversionValidationRule(final String resourceKeyPrefix) {
56  		this.resourceKeyPrefix = resourceKeyPrefix;
57  	}
58  
59  	/**
60  	 * {@inheritDoc}
61  	 */
62  	public void apply(final Map<String, Object[]> params, final Object form,
63  			final ActionErrors errors) throws ValidationException {
64  		final HttpServletRequest request = ThreadContext.getRequest();
65  		final List<ConversionFailure> conversionFailures = RequestUtils
66  				.getAttribute(request, ATTR_CONVERSION_FAILURES);
67  		if (conversionFailures != null && !conversionFailures.isEmpty()) {
68  			for (final ConversionFailure conversionFailure : conversionFailures) {
69  				final MessageInfo messageInfo = conversionFailure
70  						.getMessageInfo();
71  				final String fieldNameKey;
72  				if (resourceKeyPrefix == null) {
73  					fieldNameKey = conversionFailure.getFieldName();
74  				} else {
75  					fieldNameKey = resourceKeyPrefix
76  							+ conversionFailure.getFieldName();
77  				}
78  				final String message = messageInfo.toMessage(fieldNameKey);
79  				final FieldInfo[] fieldInfos = conversionFailure
80  						.getFieldInfos();
81  				errors.add(message, fieldInfos);
82  			}
83  		}
84  	}
85  
86  }