1 16 17 package org.springframework.web.bind; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 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 42 public class EscapedErrors implements Errors { 43 44 private final Errors source; 45 46 47 50 public EscapedErrors(Errors source) { 51 if (source == null) { 52 throw new IllegalArgumentException ("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 getObjectName() { 63 return this.source.getObjectName(); 64 } 65 66 public void setNestedPath(String nestedPath) { 67 this.source.setNestedPath(nestedPath); 68 } 69 70 public String getNestedPath() { 71 return this.source.getNestedPath(); 72 } 73 74 public void pushNestedPath(String subPath) { 75 this.source.pushNestedPath(subPath); 76 } 77 78 public void popNestedPath() throws IllegalStateException { 79 this.source.popNestedPath(); 80 } 81 82 83 public void reject(String errorCode) { 84 this.source.reject(errorCode); 85 } 86 87 public void reject(String errorCode, String defaultMessage) { 88 this.source.reject(errorCode, defaultMessage); 89 } 90 91 public void reject(String errorCode, Object [] errorArgs, String defaultMessage) { 92 this.source.reject(errorCode, errorArgs, defaultMessage); 93 } 94 95 public void rejectValue(String field, String errorCode) { 96 this.source.rejectValue(field, errorCode); 97 } 98 99 public void rejectValue(String field, String errorCode, String defaultMessage) { 100 this.source.rejectValue(field, errorCode, defaultMessage); 101 } 102 103 public void rejectValue(String field, String errorCode, Object [] errorArgs, String 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 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 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 getFieldErrors() { 149 return this.source.getFieldErrors(); 150 } 151 152 public FieldError getFieldError() { 153 return this.source.getFieldError(); 154 } 155 156 public boolean hasFieldErrors(String field) { 157 return this.source.hasFieldErrors(field); 158 } 159 160 public int getFieldErrorCount(String field) { 161 return this.source.getFieldErrorCount(field); 162 } 163 164 public List getFieldErrors(String field) { 165 return escapeObjectErrors(this.source.getFieldErrors(field)); 166 } 167 168 public FieldError getFieldError(String field) { 169 return (FieldError) escapeObjectError(this.source.getFieldError(field)); 170 } 171 172 public Object getFieldValue(String field) { 173 Object value = this.source.getFieldValue(field); 174 return (value instanceof String ? HtmlUtils.htmlEscape((String ) value) : value); 175 } 176 177 public Class getFieldType(String 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 value = fieldError.getRejectedValue(); 188 if (value instanceof String ) { 189 value = HtmlUtils.htmlEscape((String ) 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 escapeObjectErrors(List source) { 202 List escaped = new ArrayList (source.size()); 203 for (Iterator 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 |