/*
* 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.forms.conf.impl;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import de.odysseus.calyxo.base.conf.ConfigException;
import de.odysseus.calyxo.base.conf.impl.ConfigImpl;
import de.odysseus.calyxo.forms.conf.FormConfig;
import de.odysseus.calyxo.forms.conf.FormsConfig;
/**
* Forms configuration implementation.
*
* @author Christoph Beck
* @author Oliver Stuhr
*/
public class FormsConfigImpl extends ConfigImpl implements FormsConfig {
private HashMap formConfigs = new HashMap();
private String language;
private String country;
private String variant;
private Locale locale;
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_getElementName()
*/
protected String _getElementName() {
return "forms";
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_putAttributes(de.odysseus.calyxo.base.conf.impl.ConfigImpl.Attributes)
*/
protected void _putAttributes(Attributes map) {
super._putAttributes(map);
map.put("language", language);
map.put("country", country);
map.put("variant", variant);
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_addChildren(de.odysseus.calyxo.base.conf.impl.ConfigImpl.Elements)
*/
protected void _addChildren(Elements list) {
super._addChildren(list);
list.add(getFormConfigs());
}
/**
* Initialize locale.
* @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_init()
*/
protected void _init() throws ConfigException {
super._init();
locale = createLocale();
}
/**
* Merge other forms into receiver.
* Simply add all form elements.
*/
protected void merge(FormsConfigImpl other) throws ConfigException {
if (other != null) {
Iterator configs = other.getFormConfigs();
while (configs.hasNext()) {
add((FormConfigImpl)configs.next());
}
}
}
/**
* Create locale from language, country and variant settings.
* @throws ConfigException if settings are invalid
*/
private Locale createLocale() throws ConfigException {
if (language == null) {
if (country != null || variant != null) {
throw new ConfigException("Must not specify country or variant without language in '" + toInlineString() + "'");
}
return new Locale("", "");
} else {
if (country == null) {
if (variant != null) {
throw new ConfigException("Must not specify variant without country in '" + toInlineString() + "'");
}
return new Locale(language, "");
} else {
if (variant == null) {
return new Locale(language, country);
} else {
return new Locale(language, country, variant);
}
}
}
}
/**
* Get language
*/
public String getLanguage() {
return language;
}
/**
* Set language
*/
public void setLanguage(String string) {
language = string;
}
/**
* Get country
*/
public String getCountry() {
return country;
}
/**
* Set country
*/
public void setCountry(String string) {
country = string;
}
/**
* Get variant
*/
public String getVariant() {
return variant;
}
/**
* Set variant
*/
public void setVariant(String string) {
variant = string;
}
/**
* Add form element.
*/
public void add(FormConfigImpl value) throws ConfigException {
if (formConfigs.containsKey(value.getName())) {
throw new ConfigException("Duplicate form '" + value.getName() + "' in " + toInlineString());
}
formConfigs.put(value.getName(), value);
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.forms.conf.FormsConfig#getFormConfigs()
*/
public Iterator getFormConfigs() {
return formConfigs.values().iterator();
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.forms.conf.FormsConfig#getFormConfig(java.lang.String)
*/
public FormConfig getFormConfig(String name) {
return (FormConfigImpl)formConfigs.get(name);
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.forms.conf.FormsConfig#getLocale()
*/
public Locale getLocale() {
return locale;
}
}