29
29
* Default implementation of the {@link MessageCodesResolver} interface.
30
30
*
31
31
* <p>Will create two message codes for an object error, in the following order (when
32
- * using the {@link Style#PREFIX_ERROR_CODE prefixed} {@link #setStyle(Style) style}):
32
+ * using the {@link Format#PREFIX_ERROR_CODE prefixed}
33
+ * {@link #setMessageCodeFormatter(MessageCodeFormatter) formatter}):
33
34
* <ul>
34
35
* <li>1.: code + "." + object name
35
36
* <li>2.: code
73
74
* </ul>
74
75
*
75
76
* <p>By default the {@code errorCode}s will be placed at the beginning of constructed
76
- * message strings. The {@link #setStyle(Style) style} property can be used to specify
77
- * alternative {@link Style styles} of concatination.
77
+ * message strings. The {@link #setMessageCodeFormatter(MessageCodeFormatter)
78
+ * messageCodeFormatter} property can be used to specify an alternative concatenation
79
+ * {@link MessageCodeFormatter format}.
78
80
*
79
81
* <p>In order to group all codes into a specific category within your resource bundles,
80
82
* e.g. "validation.typeMismatch.name" instead of the default "typeMismatch.name",
81
83
* consider specifying a {@link #setPrefix prefix} to be applied.
82
84
*
83
85
* @author Juergen Hoeller
84
86
* @author Phillip Webb
87
+ * @author Chris Beams
85
88
* @since 1.0.1
86
89
*/
87
90
@ SuppressWarnings ("serial" )
@@ -92,12 +95,12 @@ public class DefaultMessageCodesResolver implements MessageCodesResolver, Serial
92
95
*/
93
96
public static final String CODE_SEPARATOR = "." ;
94
97
95
- private static final Style DEFAULT_STYLE = Style .PREFIX_ERROR_CODE ;
98
+ private static final MessageCodeFormatter DEFAULT_FORMATTER = Format .PREFIX_ERROR_CODE ;
96
99
97
100
98
101
private String prefix = "" ;
99
102
100
- private Style style = DEFAULT_STYLE ;
103
+ private MessageCodeFormatter formatter = DEFAULT_FORMATTER ;
101
104
102
105
103
106
/**
@@ -110,11 +113,12 @@ public void setPrefix(String prefix) {
110
113
}
111
114
112
115
/**
113
- * Specify the style of message code that will be built by this resolver.
114
- * <p>Default is {@link Style#PREFIX_ERROR_CODE}.
116
+ * Specify the format for message codes built by this resolver.
117
+ * <p>The default is {@link Format#PREFIX_ERROR_CODE}.
118
+ * @since 3.2
115
119
*/
116
- public void setStyle ( Style style ) {
117
- this .style = (style == null ? DEFAULT_STYLE : style );
120
+ public void setMessageCodeFormatter ( MessageCodeFormatter formatter ) {
121
+ this .formatter = (formatter == null ? DEFAULT_FORMATTER : formatter );
118
122
}
119
123
120
124
/**
@@ -163,29 +167,7 @@ private void addCodes(Collection<String> codeList, String errorCode, String obje
163
167
}
164
168
165
169
private void addCode (Collection <String > codeList , String errorCode , String objectName , String field ) {
166
- String code = getCode (errorCode , objectName , field );
167
- codeList .add (postProcessMessageCode (code ));
168
- }
169
-
170
- private String getCode (String errorCode , String objectName , String field ) {
171
- switch (this .style ) {
172
- case PREFIX_ERROR_CODE :
173
- return toDelimitedString (errorCode , objectName , field );
174
- case POSTFIX_ERROR_CODE :
175
- return toDelimitedString (objectName , field , errorCode );
176
- }
177
- throw new IllegalStateException ("Unknown style " + this .style );
178
- }
179
-
180
- private String toDelimitedString (String ... elements ) {
181
- StringBuilder rtn = new StringBuilder ();
182
- for (String element : elements ) {
183
- if (StringUtils .hasLength (element )) {
184
- rtn .append (rtn .length () == 0 ? "" : CODE_SEPARATOR );
185
- rtn .append (element );
186
- }
187
- }
188
- return rtn .toString ();
170
+ codeList .add (postProcessMessageCode (this .formatter .format (errorCode , objectName , field )));
189
171
}
190
172
191
173
/**
@@ -222,21 +204,51 @@ protected String postProcessMessageCode(String code) {
222
204
223
205
224
206
/**
225
- * The various styles that can be used to construct message codes.
207
+ * Common message code formats.
208
+ *
209
+ * @author Phil Webb
210
+ * @author Chris Beams
211
+ * @since 3.2
212
+ * @see MessageCodeFormatter
213
+ * @see DefaultMessageCodesResolver#setMessageCodeFormatter(MessageCodeFormatter)
226
214
*/
227
- public static enum Style {
215
+ public static enum Format implements MessageCodeFormatter {
228
216
229
217
/**
230
- * Prefix the error code at the beginning of the generated message code. eg :
218
+ * Prefix the error code at the beginning of the generated message code. e.g. :
231
219
* {@code errorCode + "." + object name + "." + field}
232
220
*/
233
- PREFIX_ERROR_CODE ,
221
+ PREFIX_ERROR_CODE {
222
+ public String format (String errorCode , String objectName , String field ) {
223
+ return toDelimitedString (errorCode , objectName , field );
224
+ }
225
+ },
234
226
235
227
/**
236
- * Postfix the error code at the end of the generated message code. eg :
228
+ * Postfix the error code at the end of the generated message code. e.g. :
237
229
* {@code object name + "." + field + "." + errorCode}
238
230
*/
239
- POSTFIX_ERROR_CODE
231
+ POSTFIX_ERROR_CODE {
232
+ public String format (String errorCode , String objectName , String field ) {
233
+ return toDelimitedString (objectName , field , errorCode );
234
+ }
235
+ };
236
+
237
+ /**
238
+ * Concatenate the given elements, delimiting each with
239
+ * {@link DefaultMessageCodesResolver#CODE_SEPARATOR}, skipping zero-length or
240
+ * null elements altogether.
241
+ */
242
+ public static String toDelimitedString (String ... elements ) {
243
+ StringBuilder rtn = new StringBuilder ();
244
+ for (String element : elements ) {
245
+ if (StringUtils .hasLength (element )) {
246
+ rtn .append (rtn .length () == 0 ? "" : CODE_SEPARATOR );
247
+ rtn .append (element );
248
+ }
249
+ }
250
+ return rtn .toString ();
251
+ }
240
252
}
241
253
242
254
}
0 commit comments