AbstractErrors.java 6.3 KB
Newer Older
A
Arjen Poutsma 已提交
1
/*
2
 * Copyright 2002-2012 the original author or authors.
A
Arjen Poutsma 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.validation;

import java.io.Serializable;
import java.util.Collections;
import java.util.EmptyStackException;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

import org.springframework.util.StringUtils;

/**
 * Abstract implementation of the {@link Errors} interface. Provides common
 * access to evaluated errors; however, does not define concrete management
 * of {@link ObjectError ObjectErrors} and {@link FieldError FieldErrors}.
 *
 * @author Juergen Hoeller
 * @since 2.5.3
 */
36
@SuppressWarnings("serial")
A
Arjen Poutsma 已提交
37 38 39 40
public abstract class AbstractErrors implements Errors, Serializable {

	private String nestedPath = "";

J
Juergen Hoeller 已提交
41
	private final Stack<String> nestedPathStack = new Stack<String>();
A
Arjen Poutsma 已提交
42 43


44
	@Override
A
Arjen Poutsma 已提交
45 46 47 48 49
	public void setNestedPath(String nestedPath) {
		doSetNestedPath(nestedPath);
		this.nestedPathStack.clear();
	}

50
	@Override
A
Arjen Poutsma 已提交
51 52 53 54
	public String getNestedPath() {
		return this.nestedPath;
	}

55
	@Override
A
Arjen Poutsma 已提交
56 57 58 59 60
	public void pushNestedPath(String subPath) {
		this.nestedPathStack.push(getNestedPath());
		doSetNestedPath(getNestedPath() + subPath);
	}

61
	@Override
A
Arjen Poutsma 已提交
62 63
	public void popNestedPath() throws IllegalArgumentException {
		try {
J
Juergen Hoeller 已提交
64
			String formerNestedPath = this.nestedPathStack.pop();
A
Arjen Poutsma 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
			doSetNestedPath(formerNestedPath);
		}
		catch (EmptyStackException ex) {
			throw new IllegalStateException("Cannot pop nested path: no nested path on stack");
		}
	}

	/**
	 * Actually set the nested path.
	 * Delegated to by setNestedPath and pushNestedPath.
	 */
	protected void doSetNestedPath(String nestedPath) {
		if (nestedPath == null) {
			nestedPath = "";
		}
		nestedPath = canonicalFieldName(nestedPath);
		if (nestedPath.length() > 0 && !nestedPath.endsWith(Errors.NESTED_PATH_SEPARATOR)) {
			nestedPath += Errors.NESTED_PATH_SEPARATOR;
		}
		this.nestedPath = nestedPath;
	}

	/**
	 * Transform the given field into its full path,
	 * regarding the nested path of this instance.
	 */
	protected String fixedField(String field) {
		if (StringUtils.hasLength(field)) {
			return getNestedPath() + canonicalFieldName(field);
		}
		else {
			String path = getNestedPath();
			return (path.endsWith(Errors.NESTED_PATH_SEPARATOR) ?
					path.substring(0, path.length() - NESTED_PATH_SEPARATOR.length()) : path);
		}
	}

	/**
	 * Determine the canonical field name for the given field.
	 * <p>The default implementation simply returns the field name as-is.
	 * @param field the original field name
	 * @return the canonical field name
	 */
	protected String canonicalFieldName(String field) {
		return field;
	}


113
	@Override
A
Arjen Poutsma 已提交
114 115 116 117
	public void reject(String errorCode) {
		reject(errorCode, null, null);
	}

118
	@Override
A
Arjen Poutsma 已提交
119 120 121 122
	public void reject(String errorCode, String defaultMessage) {
		reject(errorCode, null, defaultMessage);
	}

123
	@Override
A
Arjen Poutsma 已提交
124 125 126 127
	public void rejectValue(String field, String errorCode) {
		rejectValue(field, errorCode, null, null);
	}

128
	@Override
A
Arjen Poutsma 已提交
129 130 131 132 133
	public void rejectValue(String field, String errorCode, String defaultMessage) {
		rejectValue(field, errorCode, null, defaultMessage);
	}


134
	@Override
A
Arjen Poutsma 已提交
135 136 137 138
	public boolean hasErrors() {
		return !getAllErrors().isEmpty();
	}

139
	@Override
A
Arjen Poutsma 已提交
140 141 142 143
	public int getErrorCount() {
		return getAllErrors().size();
	}

144
	@Override
J
Juergen Hoeller 已提交
145 146
	public List<ObjectError> getAllErrors() {
		List<ObjectError> result = new LinkedList<ObjectError>();
A
Arjen Poutsma 已提交
147 148 149 150 151
		result.addAll(getGlobalErrors());
		result.addAll(getFieldErrors());
		return Collections.unmodifiableList(result);
	}

152
	@Override
A
Arjen Poutsma 已提交
153 154 155 156
	public boolean hasGlobalErrors() {
		return (getGlobalErrorCount() > 0);
	}

157
	@Override
A
Arjen Poutsma 已提交
158 159 160 161
	public int getGlobalErrorCount() {
		return getGlobalErrors().size();
	}

162
	@Override
A
Arjen Poutsma 已提交
163
	public ObjectError getGlobalError() {
164 165
		List<ObjectError> globalErrors = getGlobalErrors();
		return (!globalErrors.isEmpty() ? globalErrors.get(0) : null);
A
Arjen Poutsma 已提交
166 167
	}

168
	@Override
A
Arjen Poutsma 已提交
169 170 171 172
	public boolean hasFieldErrors() {
		return (getFieldErrorCount() > 0);
	}

173
	@Override
A
Arjen Poutsma 已提交
174 175 176 177
	public int getFieldErrorCount() {
		return getFieldErrors().size();
	}

178
	@Override
A
Arjen Poutsma 已提交
179
	public FieldError getFieldError() {
180 181
		List<FieldError> fieldErrors = getFieldErrors();
		return (!fieldErrors.isEmpty() ? fieldErrors.get(0) : null);
A
Arjen Poutsma 已提交
182 183
	}

184
	@Override
A
Arjen Poutsma 已提交
185 186 187 188
	public boolean hasFieldErrors(String field) {
		return (getFieldErrorCount(field) > 0);
	}

189
	@Override
A
Arjen Poutsma 已提交
190 191 192 193
	public int getFieldErrorCount(String field) {
		return getFieldErrors(field).size();
	}

194
	@Override
J
Juergen Hoeller 已提交
195 196 197
	public List<FieldError> getFieldErrors(String field) {
		List<FieldError> fieldErrors = getFieldErrors();
		List<FieldError> result = new LinkedList<FieldError>();
A
Arjen Poutsma 已提交
198
		String fixedField = fixedField(field);
J
Juergen Hoeller 已提交
199 200
		for (FieldError error : fieldErrors) {
			if (isMatchingFieldError(fixedField, error)) {
A
Arjen Poutsma 已提交
201 202 203 204 205 206
				result.add(error);
			}
		}
		return Collections.unmodifiableList(result);
	}

207
	@Override
A
Arjen Poutsma 已提交
208
	public FieldError getFieldError(String field) {
209
		List<FieldError> fieldErrors = getFieldErrors(field);
P
Phillip Webb 已提交
210
		return (!fieldErrors.isEmpty() ? fieldErrors.get(0) : null);
A
Arjen Poutsma 已提交
211 212 213
	}


214
	@Override
215
	public Class<?> getFieldType(String field) {
A
Arjen Poutsma 已提交
216 217 218 219 220 221
		Object value = getFieldValue(field);
		if (value != null) {
			return value.getClass();
		}
		return null;
	}
J
Juergen Hoeller 已提交
222

A
Arjen Poutsma 已提交
223 224 225 226 227 228 229 230 231 232 233 234
	/**
	 * Check whether the given FieldError matches the given field.
	 * @param field the field that we are looking up FieldErrors for
	 * @param fieldError the candidate FieldError
	 * @return whether the FieldError matches the given field
	 */
	protected boolean isMatchingFieldError(String field, FieldError fieldError) {
		return (field.equals(fieldError.getField()) ||
				(field.endsWith("*") && fieldError.getField().startsWith(field.substring(0, field.length() - 1))));
	}


235
	@Override
A
Arjen Poutsma 已提交
236
	public String toString() {
237
		StringBuilder sb = new StringBuilder(getClass().getName());
A
Arjen Poutsma 已提交
238
		sb.append(": ").append(getErrorCount()).append(" errors");
J
Juergen Hoeller 已提交
239 240
		for (ObjectError error : getAllErrors()) {
			sb.append('\n').append(error);
A
Arjen Poutsma 已提交
241 242 243 244 245
		}
		return sb.toString();
	}

}