View Javadoc

1   /*
2    * Copyright 2004-2008 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 java.util.ArrayList;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.seasar.cubby.action.ActionErrors;
23  import org.seasar.cubby.action.FieldInfo;
24  
25  public class FieldValidationRule implements ValidationRule {
26  
27  	private static final Object[] EMPTY_VALUES = new Object[] { "" };
28  
29  	private final String fieldName;
30  
31  	private final String fieldNameKey;
32  
33  	private final List<ValidationInvoker> invokers = new ArrayList<ValidationInvoker>();
34  
35  	public FieldValidationRule(final String fieldName,
36  			final Validator... validators) {
37  		this(fieldName, fieldName, validators);
38  	}
39  
40  	public FieldValidationRule(final String fieldName,
41  			final String fieldNameKey, final Validator... validators) {
42  		this.fieldName = fieldName;
43  		this.fieldNameKey = fieldNameKey;
44  		for (final Validator validator : validators) {
45  			final ValidationInvoker invoker = createInvoker(validator);
46  			this.invokers.add(invoker);
47  		}
48  	}
49  
50  	public void apply(final Map<String, Object[]> params, final Object form,
51  			final ActionErrors errors) {
52  		final Object[] values = getValues(params, this.fieldName);
53  		for (final ValidationInvoker invoker : this.invokers) {
54  			invoker.invoke(values, errors);
55  		}
56  	}
57  
58  	private Object[] getValues(final Map<String, Object[]> params,
59  			final String fieldName) {
60  		final Object[] values = params.get(fieldName);
61  		if (values != null) {
62  			return values;
63  		}
64  		return EMPTY_VALUES;
65  	}
66  
67  	public String getFieldName() {
68  		return fieldName;
69  	}
70  
71  	public String getFieldNameKey() {
72  		return fieldNameKey;
73  	}
74  
75  	private ValidationInvoker createInvoker(final Validator validator) {
76  		final ValidationInvoker invoker;
77  		if (validator instanceof ArrayFieldValidator) {
78  			invoker = new ArrayFieldValidationInvoker(
79  					(ArrayFieldValidator) validator);
80  		} else if (validator instanceof ScalarFieldValidator) {
81  			invoker = new ScalarFieldValidationInvoker(
82  					(ScalarFieldValidator) validator);
83  		} else {
84  			throw new UnsupportedOperationException();
85  		}
86  		return invoker;
87  	}
88  
89  	interface ValidationInvoker {
90  
91  		void invoke(Object[] values, ActionErrors errors);
92  
93  	}
94  
95  	class ArrayFieldValidationInvoker implements ValidationInvoker {
96  
97  		private final ArrayFieldValidator validator;
98  
99  		public ArrayFieldValidationInvoker(final ArrayFieldValidator validator) {
100 			this.validator = validator;
101 		}
102 
103 		public void invoke(final Object[] values, final ActionErrors errors) {
104 			final ValidationContext context = new ValidationContext();
105 			final FieldInfo fieldInfo = new FieldInfo(fieldName);
106 			this.validator.validate(context, values);
107 			for (MessageInfo message : context.getMessageInfos()) {
108 				errors.add(message.builder().fieldNameKey(fieldNameKey)
109 						.toString(), fieldInfo);
110 			}
111 		}
112 
113 	}
114 
115 	class ScalarFieldValidationInvoker implements ValidationInvoker {
116 
117 		private final ScalarFieldValidator validator;
118 
119 		public ScalarFieldValidationInvoker(final ScalarFieldValidator validator) {
120 			this.validator = validator;
121 		}
122 
123 		public void invoke(final Object[] values, final ActionErrors errors) {
124 			for (int i = 0; i < values.length; i++) {
125 				final ValidationContext context = new ValidationContext();
126 				final FieldInfo fieldInfo = new FieldInfo(fieldName, i);
127 				this.validator.validate(context, values[i]);
128 				for (final MessageInfo messageInfo : context.getMessageInfos()) {
129 					final String message = messageInfo.builder().fieldNameKey(
130 							fieldNameKey).toString();
131 					errors.add(message, fieldInfo);
132 				}
133 			}
134 		}
135 
136 	}
137 
138 }