001 /* 002 * Copyright 2002-2006 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 package org.springframework.binding.form.support; 017 018 import java.util.ArrayList; 019 import java.util.Collection; 020 021 import org.springframework.util.StringUtils; 022 023 /** 024 * Default implementation for {@link MessageCodeStrategy}. It creates message codes as follows: 025 * <p> 026 * <code>{contextId}.{field}.{suffix}</code><br> 027 * <code>{field}.{suffix}</code> - without a contextId<br> 028 * <code>{field}</code> - without a contextId and no suffix<br> 029 * <p> 030 * If field contains a name which is separated by <code>'.'</code> like <code>'fieldcontext.field'</code>: 031 * <p> 032 * <code>{contextId}.fieldcontext.field.{suffix}</code><br> 033 * <code>{contextId}.field.{suffix}</code><br> 034 * <code>fieldcontext.field.{suffix}</code> - without a contextId<br> 035 * <code>field.{suffix}</code> - without a contextId<br> 036 * <code>fieldcontext.field</code> - without a contextId and no suffix<br> 037 * <code>field</code> - without a contextId and no suffix<br> 038 * <p> 039 * 040 * @author Mathias Broekelmann 041 * 042 */ 043 public class DefaultMessageCodeStrategy implements MessageCodeStrategy { 044 045 public String[] getMessageCodes(String contextId, String field, String[] suffixes) { 046 boolean hasContextId = StringUtils.hasText(contextId); 047 String[] fieldPathElements = StringUtils.delimitedListToStringArray(field, "."); 048 Collection keys = new ArrayList((hasContextId ? 2 * fieldPathElements.length : fieldPathElements.length) 049 * (suffixes == null ? 1 : suffixes.length)); 050 if (hasContextId) { 051 String prefix = contextId + '.'; 052 addKeys(keys, prefix, fieldPathElements, suffixes); 053 } 054 addKeys(keys, "", fieldPathElements, suffixes); 055 return (String[]) keys.toArray(new String[keys.size()]); 056 } 057 058 private void addKeys(Collection keys, String prefix, String[] fieldPathElements, String[] suffix) { 059 final int size = fieldPathElements.length; 060 final int suffixSize = suffix == null ? 0 : suffix.length; 061 for (int i = 0; i < size; i++) { 062 StringBuffer path = new StringBuffer(prefix); 063 for (int j = i; j < size; j++) { 064 path.append(fieldPathElements[j]); 065 if (j + 1 < size) { 066 path.append('.'); 067 } 068 } 069 if (suffixSize == 0) { 070 keys.add(path.toString()); 071 } else { 072 for (int j = 0; j < suffixSize; j++) { 073 String currentSuffix = suffix[j]; 074 if (StringUtils.hasText(currentSuffix)) { 075 keys.add(path.toString() + "." + currentSuffix); 076 } else { 077 keys.add(path.toString()); 078 } 079 080 } 081 } 082 } 083 } 084 085 }