KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > bind > EscapedErrors


1 /*
2  * Copyright 2002-2006 the original author or authors.
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, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.bind;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.springframework.validation.Errors;
24 import org.springframework.validation.FieldError;
25 import org.springframework.validation.ObjectError;
26 import org.springframework.web.util.HtmlUtils;
27
28 /**
29  * Errors wrapper that adds automatic HTML escaping to the wrapped instance,
30  * for convenient usage in HTML views. Can be retrieved easily via
31  * RequestContext's <code>getErrors</code> method.
32  *
33  * <p>Note that BindTag does <i>not</i> use this class to avoid unnecessary
34  * creation of ObjectError instances. It just escapes the messages and values
35  * that get copied into the respective BindStatus instance.
36  *
37  * @author Juergen Hoeller
38  * @since 01.03.2003
39  * @see org.springframework.web.servlet.support.RequestContext#getErrors
40  * @see org.springframework.web.servlet.tags.BindTag
41  */

42 public class EscapedErrors implements Errors {
43
44     private final Errors source;
45
46
47     /**
48      * Create a new EscapedErrors instance for the given source instance.
49      */

50     public EscapedErrors(Errors source) {
51         if (source == null) {
52             throw new IllegalArgumentException JavaDoc("Cannot wrap a null instance");
53         }
54         this.source = source;
55     }
56
57     public Errors getSource() {
58         return this.source;
59     }
60
61
62     public String JavaDoc getObjectName() {
63         return this.source.getObjectName();
64     }
65
66     public void setNestedPath(String JavaDoc nestedPath) {
67         this.source.setNestedPath(nestedPath);
68     }
69
70     public String JavaDoc getNestedPath() {
71         return this.source.getNestedPath();
72     }
73
74     public void pushNestedPath(String JavaDoc subPath) {
75         this.source.pushNestedPath(subPath);
76     }
77
78     public void popNestedPath() throws IllegalStateException JavaDoc {
79         this.source.popNestedPath();
80     }
81
82
83     public void reject(String JavaDoc errorCode) {
84         this.source.reject(errorCode);
85     }
86
87     public void reject(String JavaDoc errorCode, String JavaDoc defaultMessage) {
88         this.source.reject(errorCode, defaultMessage);
89     }
90
91     public void reject(String JavaDoc errorCode, Object JavaDoc[] errorArgs, String JavaDoc defaultMessage) {
92         this.source.reject(errorCode, errorArgs, defaultMessage);
93     }
94
95     public void rejectValue(String JavaDoc field, String JavaDoc errorCode) {
96         this.source.rejectValue(field, errorCode);
97     }
98
99     public void rejectValue(String JavaDoc field, String JavaDoc errorCode, String JavaDoc defaultMessage) {
100         this.source.rejectValue(field, errorCode, defaultMessage);
101     }
102
103     public void rejectValue(String JavaDoc field, String JavaDoc errorCode, Object JavaDoc[] errorArgs, String JavaDoc defaultMessage) {
104         this.source.rejectValue(field, errorCode, errorArgs, defaultMessage);
105     }
106
107     public void addAllErrors(Errors errors) {
108         this.source.addAllErrors(errors);
109     }
110
111
112     public boolean hasErrors() {
113         return this.source.hasErrors();
114     }
115
116     public int getErrorCount() {
117         return this.source.getErrorCount();
118     }
119
120     public List JavaDoc getAllErrors() {
121         return escapeObjectErrors(this.source.getAllErrors());
122     }
123
124     public boolean hasGlobalErrors() {
125         return this.source.hasGlobalErrors();
126     }
127
128     public int getGlobalErrorCount() {
129         return this.source.getGlobalErrorCount();
130     }
131
132     public List JavaDoc getGlobalErrors() {
133         return escapeObjectErrors(this.source.getGlobalErrors());
134     }
135
136     public ObjectError getGlobalError() {
137         return escapeObjectError(this.source.getGlobalError());
138     }
139
140     public boolean hasFieldErrors() {
141         return this.source.hasFieldErrors();
142     }
143
144     public int getFieldErrorCount() {
145         return this.source.getFieldErrorCount();
146     }
147
148     public List JavaDoc getFieldErrors() {
149         return this.source.getFieldErrors();
150     }
151
152     public FieldError getFieldError() {
153         return this.source.getFieldError();
154     }
155
156     public boolean hasFieldErrors(String JavaDoc field) {
157         return this.source.hasFieldErrors(field);
158     }
159
160     public int getFieldErrorCount(String JavaDoc field) {
161         return this.source.getFieldErrorCount(field);
162     }
163
164     public List JavaDoc getFieldErrors(String JavaDoc field) {
165         return escapeObjectErrors(this.source.getFieldErrors(field));
166     }
167
168     public FieldError getFieldError(String JavaDoc field) {
169         return (FieldError) escapeObjectError(this.source.getFieldError(field));
170     }
171
172     public Object JavaDoc getFieldValue(String JavaDoc field) {
173         Object JavaDoc value = this.source.getFieldValue(field);
174         return (value instanceof String JavaDoc ? HtmlUtils.htmlEscape((String JavaDoc) value) : value);
175     }
176
177     public Class JavaDoc getFieldType(String JavaDoc field) {
178         return this.source.getFieldType(field);
179     }
180
181     private ObjectError escapeObjectError(ObjectError source) {
182         if (source == null) {
183             return null;
184         }
185         if (source instanceof FieldError) {
186             FieldError fieldError = (FieldError) source;
187             Object JavaDoc value = fieldError.getRejectedValue();
188             if (value instanceof String JavaDoc) {
189                 value = HtmlUtils.htmlEscape((String JavaDoc) value);
190             }
191             return new FieldError(
192                     fieldError.getObjectName(), fieldError.getField(), value,
193                     fieldError.isBindingFailure(), fieldError.getCodes(),
194                     fieldError.getArguments(), HtmlUtils.htmlEscape(fieldError.getDefaultMessage()));
195         }
196         return new ObjectError(
197                 source.getObjectName(), source.getCodes(), source.getArguments(),
198                 HtmlUtils.htmlEscape(source.getDefaultMessage()));
199     }
200
201     private List JavaDoc escapeObjectErrors(List JavaDoc source) {
202         List JavaDoc escaped = new ArrayList JavaDoc(source.size());
203         for (Iterator JavaDoc it = source.iterator(); it.hasNext();) {
204             ObjectError objectError = (ObjectError)it.next();
205             escaped.add(escapeObjectError(objectError));
206         }
207         return escaped;
208     }
209
210 }
211
Popular Tags