001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.bind;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.springframework.validation.Errors;
024: import org.springframework.validation.FieldError;
025: import org.springframework.validation.ObjectError;
026: import org.springframework.web.util.HtmlUtils;
027:
028: /**
029: * Errors wrapper that adds automatic HTML escaping to the wrapped instance,
030: * for convenient usage in HTML views. Can be retrieved easily via
031: * RequestContext's <code>getErrors</code> method.
032: *
033: * <p>Note that BindTag does <i>not</i> use this class to avoid unnecessary
034: * creation of ObjectError instances. It just escapes the messages and values
035: * that get copied into the respective BindStatus instance.
036: *
037: * @author Juergen Hoeller
038: * @since 01.03.2003
039: * @see org.springframework.web.servlet.support.RequestContext#getErrors
040: * @see org.springframework.web.servlet.tags.BindTag
041: */
042: public class EscapedErrors implements Errors {
043:
044: private final Errors source;
045:
046: /**
047: * Create a new EscapedErrors instance for the given source instance.
048: */
049: public EscapedErrors(Errors source) {
050: if (source == null) {
051: throw new IllegalArgumentException(
052: "Cannot wrap a null instance");
053: }
054: this .source = source;
055: }
056:
057: public Errors getSource() {
058: return this .source;
059: }
060:
061: public String getObjectName() {
062: return this .source.getObjectName();
063: }
064:
065: public void setNestedPath(String nestedPath) {
066: this .source.setNestedPath(nestedPath);
067: }
068:
069: public String getNestedPath() {
070: return this .source.getNestedPath();
071: }
072:
073: public void pushNestedPath(String subPath) {
074: this .source.pushNestedPath(subPath);
075: }
076:
077: public void popNestedPath() throws IllegalStateException {
078: this .source.popNestedPath();
079: }
080:
081: public void reject(String errorCode) {
082: this .source.reject(errorCode);
083: }
084:
085: public void reject(String errorCode, String defaultMessage) {
086: this .source.reject(errorCode, defaultMessage);
087: }
088:
089: public void reject(String errorCode, Object[] errorArgs,
090: String defaultMessage) {
091: this .source.reject(errorCode, errorArgs, defaultMessage);
092: }
093:
094: public void rejectValue(String field, String errorCode) {
095: this .source.rejectValue(field, errorCode);
096: }
097:
098: public void rejectValue(String field, String errorCode,
099: String defaultMessage) {
100: this .source.rejectValue(field, errorCode, defaultMessage);
101: }
102:
103: public void rejectValue(String field, String errorCode,
104: Object[] errorArgs, String defaultMessage) {
105: this .source.rejectValue(field, errorCode, errorArgs,
106: defaultMessage);
107: }
108:
109: public void addAllErrors(Errors errors) {
110: this .source.addAllErrors(errors);
111: }
112:
113: public boolean hasErrors() {
114: return this .source.hasErrors();
115: }
116:
117: public int getErrorCount() {
118: return this .source.getErrorCount();
119: }
120:
121: public List getAllErrors() {
122: return escapeObjectErrors(this .source.getAllErrors());
123: }
124:
125: public boolean hasGlobalErrors() {
126: return this .source.hasGlobalErrors();
127: }
128:
129: public int getGlobalErrorCount() {
130: return this .source.getGlobalErrorCount();
131: }
132:
133: public List getGlobalErrors() {
134: return escapeObjectErrors(this .source.getGlobalErrors());
135: }
136:
137: public ObjectError getGlobalError() {
138: return escapeObjectError(this .source.getGlobalError());
139: }
140:
141: public boolean hasFieldErrors() {
142: return this .source.hasFieldErrors();
143: }
144:
145: public int getFieldErrorCount() {
146: return this .source.getFieldErrorCount();
147: }
148:
149: public List getFieldErrors() {
150: return this .source.getFieldErrors();
151: }
152:
153: public FieldError getFieldError() {
154: return this .source.getFieldError();
155: }
156:
157: public boolean hasFieldErrors(String field) {
158: return this .source.hasFieldErrors(field);
159: }
160:
161: public int getFieldErrorCount(String field) {
162: return this .source.getFieldErrorCount(field);
163: }
164:
165: public List getFieldErrors(String field) {
166: return escapeObjectErrors(this .source.getFieldErrors(field));
167: }
168:
169: public FieldError getFieldError(String field) {
170: return (FieldError) escapeObjectError(this .source
171: .getFieldError(field));
172: }
173:
174: public Object getFieldValue(String field) {
175: Object value = this .source.getFieldValue(field);
176: return (value instanceof String ? HtmlUtils
177: .htmlEscape((String) value) : value);
178: }
179:
180: public Class getFieldType(String field) {
181: return this .source.getFieldType(field);
182: }
183:
184: private ObjectError escapeObjectError(ObjectError source) {
185: if (source == null) {
186: return null;
187: }
188: if (source instanceof FieldError) {
189: FieldError fieldError = (FieldError) source;
190: Object value = fieldError.getRejectedValue();
191: if (value instanceof String) {
192: value = HtmlUtils.htmlEscape((String) value);
193: }
194: return new FieldError(fieldError.getObjectName(),
195: fieldError.getField(), value, fieldError
196: .isBindingFailure(), fieldError.getCodes(),
197: fieldError.getArguments(), HtmlUtils
198: .htmlEscape(fieldError.getDefaultMessage()));
199: }
200: return new ObjectError(source.getObjectName(), source
201: .getCodes(), source.getArguments(), HtmlUtils
202: .htmlEscape(source.getDefaultMessage()));
203: }
204:
205: private List escapeObjectErrors(List source) {
206: List escaped = new ArrayList(source.size());
207: for (Iterator it = source.iterator(); it.hasNext();) {
208: ObjectError objectError = (ObjectError) it.next();
209: escaped.add(escapeObjectError(objectError));
210: }
211: return escaped;
212: }
213:
214: }
|