1 24 package org.riotfamily.common.i18n; 25 26 import java.util.ArrayList ; 27 28 import org.riotfamily.common.beans.PropertyUtils; 29 import org.springframework.util.StringUtils; 30 31 36 public class RiotMessageCodesResolver implements AdvancedMessageCodesResolver { 37 38 private static final char SEPARATOR = '.'; 39 40 private static final String ERROR_PREFIX = "error."; 41 42 private static final String HINT_SUFFIX = ".hint"; 43 44 public String [] resolveMessageCodes(String errorCode, String objectName) { 45 if (errorCode.startsWith(ERROR_PREFIX)) { 46 return new String [] { 47 errorCode 48 }; 49 } 50 else { 51 return new String [] { 52 ERROR_PREFIX + objectName + SEPARATOR + errorCode, 53 ERROR_PREFIX + errorCode 54 }; 55 } 56 } 57 58 public String [] resolveMessageCodes(String errorCode, String objectName, 59 String field, Class fieldType) { 60 61 if (errorCode.startsWith(ERROR_PREFIX)) { 62 return new String [] { 63 errorCode 64 }; 65 } 66 else { 67 return new String [] { 68 ERROR_PREFIX + objectName + SEPARATOR + field + SEPARATOR + errorCode, 69 ERROR_PREFIX + field + SEPARATOR + errorCode, 70 ERROR_PREFIX + errorCode 71 }; 72 } 73 } 74 75 public String [] resolveLabel(String objectName, Class objectClass) { 76 ArrayList codes = new ArrayList (2); 77 if (objectName != null) { 78 codes.add(objectName); 79 } 80 if (objectClass != null) { 81 codes.add(objectClass.getName()); 82 } 83 return StringUtils.toStringArray(codes); 84 } 85 86 public String [] resolveLabel(String objectName, Class objectClass, 87 String field) { 88 89 ArrayList codes = new ArrayList (2); 90 if (objectName != null) { 91 codes.add(objectName + '.' + field); 92 } 93 if (objectClass != null) { 94 codes.add(PropertyUtils.getDeclaringClass( 95 objectClass, field).getName() + '.' + field); 96 } 97 return StringUtils.toStringArray(codes); 98 } 99 100 public String [] resolveHint(String objectName, Class objectClass, 101 String field) { 102 103 ArrayList codes = new ArrayList (2); 104 if (field == null) { 105 if (objectName != null) { 106 codes.add(objectName + HINT_SUFFIX); 107 } 108 if (objectClass != null) { 109 codes.add(PropertyUtils.getDeclaringClass( 110 objectClass, field).getName() + HINT_SUFFIX); 111 } 112 } 113 else { 114 if (objectName != null) { 115 codes.add(objectName + '.' + field + HINT_SUFFIX); 116 } 117 if (objectClass != null) { 118 codes.add(PropertyUtils.getDeclaringClass( 119 objectClass, field).getName() + '.' + field + HINT_SUFFIX); 120 } 121 } 122 return StringUtils.toStringArray(codes); 123 } 124 125 } 126 | Popular Tags |