KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > i18n > RiotMessageCodesResolver


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.i18n;
25
26 import java.util.ArrayList JavaDoc;
27
28 import org.riotfamily.common.beans.PropertyUtils;
29 import org.springframework.util.StringUtils;
30
31 /**
32  * AdvancedMessageCodesResolver implementation used by Riot.
33  *
34  * @author Felix Gnass [fgnass at neteye dot de]
35  */

36 public class RiotMessageCodesResolver implements AdvancedMessageCodesResolver {
37
38     private static final char SEPARATOR = '.';
39
40     private static final String JavaDoc ERROR_PREFIX = "error.";
41     
42     private static final String JavaDoc HINT_SUFFIX = ".hint";
43     
44     public String JavaDoc[] resolveMessageCodes(String JavaDoc errorCode, String JavaDoc objectName) {
45         if (errorCode.startsWith(ERROR_PREFIX)) {
46             return new String JavaDoc[] {
47                     errorCode
48             };
49         }
50         else {
51             return new String JavaDoc[] {
52                 ERROR_PREFIX + objectName + SEPARATOR + errorCode,
53                 ERROR_PREFIX + errorCode
54             };
55         }
56     }
57
58     public String JavaDoc[] resolveMessageCodes(String JavaDoc errorCode, String JavaDoc objectName,
59             String JavaDoc field, Class JavaDoc fieldType) {
60         
61         if (errorCode.startsWith(ERROR_PREFIX)) {
62             return new String JavaDoc[] {
63                     errorCode
64             };
65         }
66         else {
67             return new String JavaDoc[] {
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 JavaDoc[] resolveLabel(String JavaDoc objectName, Class JavaDoc objectClass) {
76         ArrayList JavaDoc codes = new ArrayList JavaDoc(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 JavaDoc[] resolveLabel(String JavaDoc objectName, Class JavaDoc objectClass,
87             String JavaDoc field) {
88         
89         ArrayList JavaDoc codes = new ArrayList JavaDoc(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 JavaDoc[] resolveHint(String JavaDoc objectName, Class JavaDoc objectClass,
101             String JavaDoc field) {
102         
103         ArrayList JavaDoc codes = new ArrayList JavaDoc(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