001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of 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,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.calyxo.base;
017:
018: import java.io.Serializable;
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.Locale;
022:
023: /**
024: * A message can be considered as a resource which may take arguments.
025: * The resource is determined by a bundle and key. Arguments may be
026: * added and are of inner class type Arg. The message resource usually
027: * resolves to a message template, which is used to merge the arguments
028: * into the final message string according to some locale.
029: * This message string is produced with the <code>format()</code> method.
030: * Since arguments do not have names, their order is important.
031: *
032: * @see de.odysseus.calyxo.base.I18nSupport
033: * @author Christoph Beck
034: */
035: public class Message implements Serializable {
036:
037: /**
038: * Argument base class.
039: * An argument is required to answer an object to be merged
040: * into a message. This object may or may not be a formatted
041: * string.
042: *
043: * @author Christoph Beck
044: */
045: public static abstract class Arg implements Serializable {
046: public abstract Object getValue(Locale locale, I18nSupport i18n);
047: }
048:
049: /**
050: * A resource message argument is specified by a bundle/key pair.
051: * The <code>getValue()</code> method looks up the corresponding
052: * resource value.
053: *
054: * @author Christoph Beck
055: */
056: public static class ResourceArg extends Arg {
057: private final String bundle;
058: private final String key;
059:
060: public ResourceArg(String bundle, String key) {
061: this .bundle = bundle;
062: this .key = key;
063: }
064:
065: public String toString() {
066: return "${" + key + "}";
067: }
068:
069: public Object getValue(Locale locale, I18nSupport i18n) {
070: return i18n.getResource(locale, bundle, key);
071: }
072: }
073:
074: /**
075: * A value message argument simply holds the object which is then
076: * answered in the <code>getValue()</code> method.
077: *
078: * @author Christoph Beck
079: */
080: public static class ValueArg extends Arg {
081: private final Object value;
082:
083: public ValueArg(Object value) {
084: this .value = value;
085: }
086:
087: public String toString() {
088: return value == null ? "" : value.toString();
089: }
090:
091: public Object getValue(Locale locale, I18nSupport i18n) {
092: return value;
093: }
094: }
095:
096: private String key;
097: private String bundle;
098: private ArrayList args = new ArrayList(4);
099:
100: /**
101: * Default contructor.
102: */
103: public Message() {
104: super ();
105: }
106:
107: /**
108: * Construct message by specifying the message resource bundle/key pair.
109: * @param bundle the bundle used to lookup the message resource.
110: * @param key the key used to lookup the message resource.
111: */
112: public Message(String bundle, String key) {
113: super ();
114:
115: this .bundle = bundle;
116: this .key = key;
117: }
118:
119: /**
120: * Construct message by specifying the message resource bundle/key pair
121: * and one argument.
122: * @param bundle the bundle used to lookup the message resource.
123: * @param key the key used to lookup the message resource.
124: */
125: public Message(String bundle, String key, Arg arg) {
126: this (bundle, key);
127: add(arg);
128: }
129:
130: /**
131: * Construct message by specifying the message resource bundle/key pair
132: * and two arguments.
133: * @param bundle the bundle used to lookup the message resource.
134: * @param key the key used to lookup the message resource.
135: */
136: public Message(String bundle, String key, Arg arg1, Arg arg2) {
137: this (bundle, key, arg1);
138: add(arg2);
139: }
140:
141: /**
142: * Construct message by specifying the message resource bundle/key pair
143: * and arguments.
144: * @param bundle the bundle used to lookup the message resource.
145: * @param key the key used to lookup the message resource.
146: */
147: public Message(String bundle, String key, Arg[] args) {
148: this (bundle, key);
149: for (int i = 0; i < args.length; i++) {
150: add(args[i]);
151: }
152: }
153:
154: /**
155: * Add a message argument.
156: */
157: public void add(Arg arg) {
158: args.add(arg);
159: }
160:
161: /**
162: * Answer arguments iterator
163: */
164: public Iterator getArgs() {
165: return args.iterator();
166: }
167:
168: /**
169: * Get resource bundle name
170: */
171: public String getBundle() {
172: return bundle;
173: }
174:
175: /**
176: * Get resource key
177: */
178: public String getKey() {
179: return key;
180: }
181:
182: /**
183: * Set the resource bundle name
184: */
185: public void setBundle(String string) {
186: bundle = string;
187: }
188:
189: /**
190: * Set the resource key
191: */
192: public void setKey(String string) {
193: key = string;
194: }
195:
196: /**
197: * Format the message to a message string.
198: * The supplied i18n support is used to get the message string.
199: * @param locale the desired locale
200: * @param i18n support
201: * @return formatted message string
202: */
203: public String format(Locale locale, I18nSupport i18n) {
204: Object[] values = new Object[args.size()];
205: for (int i = 0; i < values.length; i++) {
206: Message.Arg arg = (Message.Arg) args.get(i);
207: values[i] = arg.getValue(locale, i18n);
208: }
209: return i18n.getMessage(locale, bundle, key, values);
210: }
211:
212: /*
213: * (non-Javadoc)
214: * @see java.lang.Object#toString()
215: */
216: public String toString() {
217: StringBuffer msg = new StringBuffer();
218: if (key != null) {
219: msg.append(key);
220: } else {
221: msg.append("<no key>");
222: }
223: msg.append("(");
224: Iterator args = getArgs();
225: while (args.hasNext()) {
226: Arg arg = (Arg) args.next();
227: msg.append(arg);
228: if (args.hasNext()) {
229: msg.append(",");
230: }
231: }
232: msg.append(")");
233: return msg.toString();
234: }
235: }
|