/* * Copyright 2002-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.validation; import java.io.Serializable; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.springframework.util.StringUtils; /** * Default implementation of the {@link MessageCodesResolver} interface. * *

Will create two message codes for an object error, in the following order: *

* *

Will create four message codes for a field specification, in the following order: *

* *

For example, in case of code "typeMismatch", object name "user", field "age": *

* *

This resolution algorithm thus can be leveraged for example to show * specific messages for binding errors like "required" and "typeMismatch": *

* *

In case of array, {@link List} or {@link java.util.Map} properties, * both codes for specific elements and for the whole collection are * generated. Assuming a field "name" of an array "groups" in object "user": *

* *

In order to group all codes into a specific category within your resource bundles, * e.g. "validation.typeMismatch.name" instead of the default "typeMismatch.name", * consider specifying a {@link #setPrefix prefix} to be applied. * * @author Juergen Hoeller * @since 1.0.1 */ public class DefaultMessageCodesResolver implements MessageCodesResolver, Serializable { /** * The separator that this implementation uses when resolving message codes. */ public static final String CODE_SEPARATOR = "."; private String prefix = ""; /** * Specify a prefix to be applied to any code built by this resolver. *

Default is none. Specify, for example, "validation." to get * error codes like "validation.typeMismatch.name". */ public void setPrefix(String prefix) { this.prefix = (prefix != null ? prefix : ""); } /** * Return the prefix to be applied to any code built by this resolver. *

Returns an empty String in case of no prefix. */ protected String getPrefix() { return this.prefix; } public String[] resolveMessageCodes(String errorCode, String objectName) { return new String[] { postProcessMessageCode(errorCode + CODE_SEPARATOR + objectName), postProcessMessageCode(errorCode)}; } /** * Build the code list for the given code and field: an * object/field-specific code, a field-specific code, a plain error code. *

Arrays, Lists and Maps are resolved both for specific elements and * the whole collection. *

See the {@link DefaultMessageCodesResolver class level Javadoc} for * details on the generated codes. * @return the list of codes */ public String[] resolveMessageCodes(String errorCode, String objectName, String field, Class fieldType) { List codeList = new ArrayList(); List fieldList = new ArrayList(); buildFieldList(field, fieldList); for (Iterator it = fieldList.iterator(); it.hasNext();) { String fieldInList = (String) it.next(); codeList.add(postProcessMessageCode(errorCode + CODE_SEPARATOR + objectName + CODE_SEPARATOR + fieldInList)); } int dotIndex = field.lastIndexOf('.'); if (dotIndex != -1) { buildFieldList(field.substring(dotIndex + 1), fieldList); } for (Iterator it = fieldList.iterator(); it.hasNext();) { String fieldInList = (String) it.next(); codeList.add(postProcessMessageCode(errorCode + CODE_SEPARATOR + fieldInList)); } if (fieldType != null) { codeList.add(postProcessMessageCode(errorCode + CODE_SEPARATOR + fieldType.getName())); } codeList.add(postProcessMessageCode(errorCode)); return StringUtils.toStringArray(codeList); } /** * Add both keyed and non-keyed entries for the supplied field * to the supplied field list. */ protected void buildFieldList(String field, List fieldList) { fieldList.add(field); String plainField = field; int keyIndex = plainField.lastIndexOf('['); while (keyIndex != -1) { int endKeyIndex = plainField.indexOf(']', keyIndex); if (endKeyIndex != -1) { plainField = plainField.substring(0, keyIndex) + plainField.substring(endKeyIndex + 1); fieldList.add(plainField); keyIndex = plainField.lastIndexOf('['); } else { keyIndex = -1; } } } /** * Post-process the given message code, built by this resolver. *

The default implementation applies the specified prefix, if any. * @param code the message code as built by this resolver * @return the final message code to be returned * @see #setPrefix */ protected String postProcessMessageCode(String code) { return getPrefix() + code; } }