AbstractErrors.java 6.4 KB
Newer Older
A
Arjen Poutsma 已提交
1
/*
J
Juergen Hoeller 已提交
2
 * Copyright 2002-2013 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
 *
 * 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
J
Juergen Hoeller 已提交
34
 * @author Rossen Stoyanchev
A
Arjen Poutsma 已提交
35 36
 * @since 2.5.3
 */
37
@SuppressWarnings("serial")
A
Arjen Poutsma 已提交
38 39 40 41
public abstract class AbstractErrors implements Errors, Serializable {

	private String nestedPath = "";

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


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

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

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

62
	@Override
A
Arjen Poutsma 已提交
63 64
	public void popNestedPath() throws IllegalArgumentException {
		try {
J
Juergen Hoeller 已提交
65
			String formerNestedPath = this.nestedPathStack.pop();
A
Arjen Poutsma 已提交
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 113
			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;
	}


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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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


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

A
Arjen Poutsma 已提交
224 225 226 227 228 229 230
	/**
	 * 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) {
231 232 233
		if (field.equals(fieldError.getField())) {
			return true;
		}
J
Juergen Hoeller 已提交
234
		// Optimization: use charAt instead of endsWith (SPR-11304)
235 236
		int endIndex = field.length() - 1;
		return (field.charAt(endIndex) == '*' && fieldError.getField().startsWith(field.substring(0, endIndex)));
A
Arjen Poutsma 已提交
237 238 239
	}


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

}