1. /*
  2. * Copyright 2002-2004 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. package org.springframework.web.bind;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import org.springframework.validation.Errors;
  21. import org.springframework.validation.FieldError;
  22. import org.springframework.validation.ObjectError;
  23. import org.springframework.web.util.HtmlUtils;
  24. /**
  25. * Errors wrapper that adds automatic HTML escaping to the wrapped instance,
  26. * for convenient usage in HTML views. Can be retrieved easily via
  27. * RequestContext's getErrors method.
  28. *
  29. * <p>Note that BindTag does not use this class to avoid unnecessary creation
  30. * of ObjectError instances. It just escapes the messages and values that get
  31. * copied into the respective BindStatus instance.
  32. *
  33. * @author Juergen Hoeller
  34. * @since 01.03.2003
  35. * @see org.springframework.web.servlet.support.RequestContext#getErrors
  36. * @see org.springframework.web.servlet.tags.BindTag
  37. */
  38. public class EscapedErrors implements Errors {
  39. private final Errors source;
  40. /**
  41. * Create a new EscapedErrors instance for the given source instance.
  42. */
  43. public EscapedErrors(Errors source) {
  44. if (source == null) {
  45. throw new IllegalArgumentException("Cannot wrap a null instance");
  46. }
  47. this.source = source;
  48. }
  49. public Errors getSource() {
  50. return this.source;
  51. }
  52. public String getObjectName() {
  53. return this.source.getObjectName();
  54. }
  55. public void reject(String errorCode, String defaultMessage) {
  56. this.source.reject(errorCode, defaultMessage);
  57. }
  58. public void reject(String errorCode, Object[] errorArgs, String defaultMessage) {
  59. this.source.reject(errorCode, errorArgs, defaultMessage);
  60. }
  61. public void rejectValue(String field, String errorCode, String defaultMessage) {
  62. this.source.rejectValue(field, errorCode, defaultMessage);
  63. }
  64. public void rejectValue(String field, String errorCode, Object[] errorArgs, String defaultMessage) {
  65. this.source.rejectValue(field, errorCode, errorArgs, defaultMessage);
  66. }
  67. public boolean hasErrors() {
  68. return this.source.hasErrors();
  69. }
  70. public int getErrorCount() {
  71. return this.source.getErrorCount();
  72. }
  73. public List getAllErrors() {
  74. return escapeObjectErrors(this.source.getAllErrors());
  75. }
  76. public boolean hasGlobalErrors() {
  77. return this.source.hasGlobalErrors();
  78. }
  79. public int getGlobalErrorCount() {
  80. return this.source.getGlobalErrorCount();
  81. }
  82. public List getGlobalErrors() {
  83. return escapeObjectErrors(this.source.getGlobalErrors());
  84. }
  85. public ObjectError getGlobalError() {
  86. return escapeObjectError(this.source.getGlobalError());
  87. }
  88. public boolean hasFieldErrors(String field) {
  89. return this.source.hasFieldErrors(field);
  90. }
  91. public int getFieldErrorCount(String field) {
  92. return this.source.getFieldErrorCount(field);
  93. }
  94. public List getFieldErrors(String field) {
  95. return escapeObjectErrors(this.source.getFieldErrors(field));
  96. }
  97. public FieldError getFieldError(String field) {
  98. return (FieldError) escapeObjectError(this.source.getFieldError(field));
  99. }
  100. public Object getFieldValue(String field) {
  101. Object value = this.source.getFieldValue(field);
  102. return (value instanceof String ? HtmlUtils.htmlEscape((String) value) : value);
  103. }
  104. public void setNestedPath(String nestedPath) {
  105. this.source.setNestedPath(nestedPath);
  106. }
  107. public String getNestedPath() {
  108. return this.source.getNestedPath();
  109. }
  110. private ObjectError escapeObjectError(ObjectError source) {
  111. if (source == null) {
  112. return null;
  113. }
  114. if (source instanceof FieldError) {
  115. FieldError fieldError = (FieldError) source;
  116. Object value = fieldError.getRejectedValue();
  117. if (value instanceof String) {
  118. value = HtmlUtils.htmlEscape((String) fieldError.getRejectedValue());
  119. }
  120. return new FieldError(fieldError.getObjectName(), fieldError.getField(), value, fieldError.isBindingFailure(),
  121. fieldError.getCodes(), fieldError.getArguments(), HtmlUtils.htmlEscape(fieldError.getDefaultMessage()));
  122. }
  123. return new ObjectError(source.getObjectName(), source.getCodes(), source.getArguments(),
  124. HtmlUtils.htmlEscape(source.getDefaultMessage()));
  125. }
  126. private List escapeObjectErrors(List source) {
  127. List escaped = new ArrayList();
  128. for (Iterator it = source.iterator(); it.hasNext();) {
  129. ObjectError objectError = (ObjectError)it.next();
  130. escaped.add(escapeObjectError(objectError));
  131. }
  132. return escaped;
  133. }
  134. }