1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springmodules.validation.bean.converter;
18
19 import org.springframework.util.ClassUtils;
20
21 /**
22 * Converts simple error codes to an error code that expresses the class and perhaps
23 * the property that are associated with the error code.
24 *
25 * @author Uri Boness
26 */
27 public class ModelAwareErrorCodeConverter implements ErrorCodeConverter {
28
29 final static String ERROR_CODE_SEPERATOR_PREFIX = "[";
30
31 final static String ERROR_CODE_SEPERATOR_SUFFIX = "]";
32
33 final static String PROPERTY_SEPERATOR = ".";
34
35 private boolean useFullyQualifiedClassName;
36
37 /**
38 * Constructs a new ModelAwareErrorCodeConverter that uses class simple names. For example, the validation error code
39 * <code>error_code</code> for class <code>org.springmodules.validation.sample.Person</code> will be converted
40 * to <code>Person[error_code]</code>.
41 */
42 public ModelAwareErrorCodeConverter() {
43 this(false);
44 }
45
46 /**
47 * Constructs a new ModelAwareErrorCodeConverter. The given argument idicateds whether a fully qualified name should
48 * be used for the converted error codes. For example, if the validation error code <code>error_code</code> for
49 * class <code>org.springmodules.validation.sample.Person</code> will be converted with fully qualified name set
50 * to <code>true</code>, the converted error code will be
51 * <code>org.springmodules.validation.sample.Person[error_code]</code>.
52 */
53 public ModelAwareErrorCodeConverter(boolean useFullyQualifiedClassName) {
54 this.useFullyQualifiedClassName = useFullyQualifiedClassName;
55 }
56
57 /**
58 * Converts the given error code to the following format:<br/> <code>short_class_name[errorCode]</code></br>
59 * where <code>short_class_name</code> is the name of the given class with its package stripped, and
60 * <code>error_code</code> is the given error code.
61 *
62 * @param errorCode The given error code (the one to convert)
63 * @param clazz The given class
64 * @return The converted error code.
65 */
66 public String convertGlobalErrorCode(String errorCode, Class clazz) {
67 String className = (useFullyQualifiedClassName) ? clazz.getName() : ClassUtils.getShortName(clazz);
68 return new StringBuffer(className)
69 .append(ERROR_CODE_SEPERATOR_PREFIX)
70 .append(errorCode)
71 .append(ERROR_CODE_SEPERATOR_SUFFIX)
72 .toString();
73 }
74
75 /**
76 * Converts the given error code to the following format:<br/>
77 * <code>short_class_name.property_name[errorCode]</code></br>
78 * where <code>short_class_name</code> is the name of the given class with its package stripped,
79 * <code>property_name</code> is the given property name, and <code>error_code</code> is the given
80 * error code.
81 *
82 * @param errorCode The given error code (the one to convert)
83 * @param clazz The given class
84 * @param propertyName The property name
85 * @return The converted error code.
86 */
87 public String convertPropertyErrorCode(String errorCode, Class clazz, String propertyName) {
88 String className = (useFullyQualifiedClassName) ? clazz.getName() : ClassUtils.getShortName(clazz);
89 return new StringBuffer(className)
90 .append(PROPERTY_SEPERATOR)
91 .append(propertyName)
92 .append(ERROR_CODE_SEPERATOR_PREFIX)
93 .append(errorCode)
94 .append(ERROR_CODE_SEPERATOR_SUFFIX)
95 .toString();
96 }
97
98
99
100 /**
101 * Determines whether the converted error codes will use the fully qualified class names of the validated class. If
102 * not, the simple class name will be used instead.
103 *
104 * @param useFullyQualifiedClassName
105 */
106 public void setUseFullyQualifiedClassName(boolean useFullyQualifiedClassName) {
107 this.useFullyQualifiedClassName = useFullyQualifiedClassName;
108 }
109
110 }