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.misc;
017:
018: import java.util.ArrayList;
019: import java.util.Locale;
020: import java.util.Map;
021:
022: import javax.servlet.http.HttpServletRequest;
023:
024: import de.odysseus.calyxo.base.I18nSupport;
025: import de.odysseus.calyxo.base.Message;
026: import de.odysseus.calyxo.base.ModuleContext;
027: import de.odysseus.calyxo.base.util.MapFacade;
028: import de.odysseus.calyxo.base.access.Accessor;
029:
030: /**
031: * An accessor for i18n.
032: * This accessor serves beans:
033: * <ul>
034: * <li>locale</li>
035: * <li>bundle[name]
036: * <ul>
037: * <li>resource[key]</li>
038: * <li>message[key][arg1][...]</li>
039: * </ul>
040: * </li>
041: * <li>format[message]</li>
042: * </ul>
043: *
044: * @author Christoph Beck
045: */
046: public class I18nAccessor implements Accessor {
047: private static ArrayList EMPTY_ARRAY_LIST = new ArrayList();
048:
049: /**
050: * Bean class with properties <em>resource</em> and <em>message</em>.
051: */
052: public class BundleBean {
053: String bundle;
054: Locale locale;
055: private Map resource;
056: private Map message;
057:
058: BundleBean(Locale locale, String bundle) {
059: this .bundle = bundle;
060: this .locale = locale;
061: }
062:
063: /**
064: * Answer a map to serve resource strings by key
065: * Use like <code>resource[key]</code>
066: */
067: public Map getResource() {
068: if (resource == null) {
069: resource = new MapFacade() {
070: public Object get(Object key) {
071: return support.getResource(locale, bundle, key
072: .toString());
073: }
074: };
075: }
076: return resource;
077: }
078:
079: /**
080: * Answer a map to serve message strings by key and args.
081: * Use like <code>message[key][arg1][...]</code>.
082: */
083: public Map getMessage() {
084: if (message == null) {
085: message = new MapFacade() {
086: public Object get(final Object key) {
087: return new MapFacade() {
088: private ArrayList args = EMPTY_ARRAY_LIST;
089:
090: public Object get(Object value) {
091: if (args == EMPTY_ARRAY_LIST) {
092: args = new ArrayList(4);
093: }
094: if (value instanceof Object[]) {
095: Object[] values = (Object[]) value;
096: for (int i = 0; i < values.length; i++) {
097: args.add(values[i]);
098: }
099: } else {
100: args.add(value);
101: }
102: return this ;
103: }
104:
105: public String toString() {
106: return support.getMessage(locale,
107: bundle, key.toString(), args
108: .toArray());
109: }
110: };
111: }
112: };
113: }
114: return message;
115: }
116:
117: }
118:
119: /**
120: * Bean class with properties <em>language</em> and <em>bundle</em>.
121: *
122: */
123: public class Bean {
124: private Locale locale;
125: private Map bundle;
126: private Map format;
127:
128: Bean(Locale locale) {
129: this .locale = locale;
130: }
131:
132: /**
133: * Answer a map to serve bundle beans by name
134: */
135: public Map getBundle() {
136: if (bundle == null) {
137: bundle = new MapFacade() {
138: public Object get(Object key) {
139: return new BundleBean(locale, key.toString());
140: }
141: };
142: }
143: return bundle;
144: }
145:
146: /**
147: * Answer a map to format instances of {@link Message}.
148: */
149: public Map getFormat() {
150: if (format == null) {
151: format = new MapFacade() {
152: public Object get(Object key) {
153: return ((Message) key).format(locale, support);
154: }
155: };
156: }
157: return format;
158: }
159:
160: /**
161: * Answer the desired locale for current request.
162: */
163: public Locale getLocale() {
164: return locale;
165: }
166: }
167:
168: private final I18nSupport support;
169:
170: /**
171: * Constructor
172: * @param moduleContext the module context for this accessor
173: */
174: public I18nAccessor(ModuleContext moduleContext) {
175: support = I18nSupport.getInstance(moduleContext);
176: }
177:
178: /**
179: * Answer fresh instance of inner class <code>Bean</code>
180: */
181: public final Object get(HttpServletRequest request) {
182: return new Bean(support.getLocale(request));
183: }
184:
185: /**
186: * Answer true.
187: */
188: public boolean isCacheable() {
189: return true;
190: }
191: }
|