Package de.odysseus.calyxo.forms.conf.impl

Source Code of de.odysseus.calyxo.forms.conf.impl.FormsRootConfigImpl

/*
* 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.RootConfigImpl;
import de.odysseus.calyxo.forms.conf.FormConfig;
import de.odysseus.calyxo.forms.conf.FormsConfig;
import de.odysseus.calyxo.forms.conf.FormsRootConfig;
import de.odysseus.calyxo.forms.conf.ValidatorsConfig;


/**
* Forms root configuration implementation.
*
* @author Christoph Beck
* @author Oliver Stuhr
*/
public class FormsRootConfigImpl extends RootConfigImpl implements FormsRootConfig {
  private ValidatorsConfigImpl validatorsConfig;
  private HashMap formsConfigsByLocale = new HashMap();
  private HashMap formsConfigs = new HashMap();
  private HashMap localesByLanguage = new HashMap();
  private Locale defaultLocale = new Locale("", "");

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.base.conf.impl.RootConfigImpl#merge(de.odysseus.calyxo.base.conf.impl.RootConfigImpl)
   */
  protected void merge(RootConfigImpl other) throws ConfigException {
    merge((FormsRootConfigImpl)other);
  }

  /**
   * Merge other validation element into the receiver.
   * This method merges the validators element and forms elements.
   */
  void merge(FormsRootConfigImpl other) throws ConfigException {
    if (validatorsConfig == null) {
      validatorsConfig = other.validatorsConfig;
    } else {
      validatorsConfig.merge(other.validatorsConfig);
    }
    Iterator configs = other.getFormsConfigs();
    while (configs.hasNext()) {
      FormsConfigImpl value = (FormsConfigImpl)configs.next();
      String key = getFormsKey(value);
      FormsConfigImpl formsConfig =
        (FormsConfigImpl)formsConfigs.get(key);
      if (formsConfig == null) {
        formsConfigs.put(key, value);
      } else {
        formsConfig.merge(value);
      }
    }
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_getElementName()
   */
  protected String _getElementName() {
    return "calyxo-forms-config";
  }

  /*
   * (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(getValidatorsConfig());
    list.add(getFormsConfigs());
  }

  /**
   * Initialize forms by locale lookup.
   * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_init2()
   */
  protected void _init2() throws ConfigException {
    super._init2();
    Iterator iter = formsConfigs.values().iterator();
    while (iter.hasNext()) {
      FormsConfigImpl value = (FormsConfigImpl)iter.next();
      formsConfigsByLocale.put(value.getLocale(), value);
    }
  }

  /**
   * Create forms key. This key is used, before the locales
   * of forms elements have been computed.
   */
  private String getFormsKey(FormsConfigImpl value) {
    StringBuffer key = new StringBuffer();
    if (value.getLanguage() != null) {
      key.append(value.getLanguage());
      if (value.getCountry() != null) {
        key.append("_");
        key.append(value.getCountry());
        if (value.getVariant() != null) {
          key.append("_");
          key.append(value.getVariant());
        }
      }
    }
    return key.toString();
  }

  /**
   * Lookup form by name and locale (exact match).
   */
  private FormConfig getFormConfig(String name, Locale locale) {
    FormsConfig formsConfig = (FormsConfig)formsConfigsByLocale.get(locale);
    if (formsConfig != null) {
      FormConfig formConfig = formsConfig.getFormConfig(name);
      if (formConfig != null) {
        return formConfig;
      }
    }
    return null;
  }

  /**
   * Get locale specified by language and country codes from locale cache.
   * This method is used by to find locale generalizations. A new locale
   * instance is created and cached if necessary.
   */
  private Locale getCachedLocale(String language, String country) {
    HashMap byCountry = (HashMap)localesByLanguage.get(language);
    if (byCountry == null) {
      localesByLanguage.put(language, byCountry = new HashMap());
    }
    Locale locale = (Locale)byCountry.get(country);
    if (locale == null) {
      byCountry.put(country, locale = new Locale(language, country));
    }
    return locale;
  }

  /**
   * Generalize locale
   */
  Locale generalize(Locale locale) {
    if (locale == defaultLocale) { // quick check
      return null;
    } else {
      if (locale.getVariant().length() > 0) {
        return getCachedLocale(locale.getLanguage(), locale.getCountry());
      } else if (locale.getCountry().length() > 0) {
        return getCachedLocale(locale.getLanguage(), "");
      } else if (locale.getLanguage().length() > 0) {
        return defaultLocale;
      }
      return null;
    }
  }

  /**
   * Add forms element
   */
  public void add(FormsConfigImpl value) throws ConfigException {
    String key = getFormsKey(value);
    if (formsConfigs.containsKey(key)) {
      throw new ConfigException("Duplicate forms for key '" + key + "' in " + toInlineString());
    }
    formsConfigs.put(key, value);
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.forms.conf.FormsRootConfig#getFormsConfigs()
   */
  public Iterator getFormsConfigs() {
    return formsConfigs.values().iterator();
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.forms.conf.FormsRootConfig#findFormConfig(java.lang.String, java.util.Locale)
   */
  public FormConfig findFormConfig(String name, Locale locale) {
    if (locale == null) {
      locale = defaultLocale;
    }
    do {
      FormConfig result = getFormConfig(name, locale);
      if (result != null)
        return result;
      locale = generalize(locale);
    } while (locale != null);
    return null;
  }

  /**
   * Set the validators element
   */
  public void setValidatorsConfig(ValidatorsConfigImpl value) {
    validatorsConfig = value;
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.forms.conf.FormsRootConfig#getValidatorsConfig()
   */
  public ValidatorsConfig getValidatorsConfig() {
    return validatorsConfig;
  }
}
TOP

Related Classes of de.odysseus.calyxo.forms.conf.impl.FormsRootConfigImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.