/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.base.misc;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import de.odysseus.calyxo.base.I18nSupport;
import de.odysseus.calyxo.base.Message;
import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.util.MapFacade;
import de.odysseus.calyxo.base.access.Accessor;
/**
* An accessor for i18n.
* This accessor serves beans:
* <ul>
* <li>locale</li>
* <li>bundle[name]
* <ul>
* <li>resource[key]</li>
* <li>message[key][arg1][...]</li>
* </ul>
* </li>
* <li>format[message]</li>
* </ul>
*
* @author Christoph Beck
*/
public class I18nAccessor implements Accessor {
private static ArrayList EMPTY_ARRAY_LIST = new ArrayList();
/**
* Bean class with properties <em>resource</em> and <em>message</em>.
*/
public class BundleBean {
String bundle;
Locale locale;
private Map resource;
private Map message;
BundleBean(Locale locale, String bundle) {
this.bundle = bundle;
this.locale = locale;
}
/**
* Answer a map to serve resource strings by key
* Use like <code>resource[key]</code>
*/
public Map getResource() {
if (resource == null) {
resource = new MapFacade() {
public Object get(Object key) {
return support.getResource(locale, bundle, key.toString());
}
};
}
return resource;
}
/**
* Answer a map to serve message strings by key and args.
* Use like <code>message[key][arg1][...]</code>.
*/
public Map getMessage() {
if (message == null) {
message = new MapFacade() {
public Object get(final Object key) {
return new MapFacade() {
private ArrayList args = EMPTY_ARRAY_LIST;
public Object get(Object value) {
if (args == EMPTY_ARRAY_LIST) {
args = new ArrayList(4);
}
if (value instanceof Object[]) {
Object[] values = (Object[])value;
for (int i = 0; i < values.length; i++) {
args.add(values[i]);
}
} else {
args.add(value);
}
return this;
}
public String toString() {
return support.getMessage(locale, bundle, key.toString(), args.toArray());
}
};
}
};
}
return message;
}
}
/**
* Bean class with properties <em>language</em> and <em>bundle</em>.
*
*/
public class Bean {
private Locale locale;
private Map bundle;
private Map format;
Bean(Locale locale) {
this.locale = locale;
}
/**
* Answer a map to serve bundle beans by name
*/
public Map getBundle() {
if (bundle == null) {
bundle = new MapFacade() {
public Object get(Object key) {
return new BundleBean(locale, key.toString());
}
};
}
return bundle;
}
/**
* Answer a map to format instances of {@link Message}.
*/
public Map getFormat() {
if (format == null) {
format = new MapFacade() {
public Object get(Object key) {
return ((Message)key).format(locale, support);
}
};
}
return format;
}
/**
* Answer the desired locale for current request.
*/
public Locale getLocale() {
return locale;
}
}
private final I18nSupport support;
/**
* Constructor
* @param moduleContext the module context for this accessor
*/
public I18nAccessor(ModuleContext moduleContext) {
support = I18nSupport.getInstance(moduleContext);
}
/**
* Answer fresh instance of inner class <code>Bean</code>
*/
public final Object get(HttpServletRequest request) {
return new Bean(support.getLocale(request));
}
/**
* Answer true.
*/
public boolean isCacheable() {
return true;
}
}