Package de.odysseus.calyxo.panels.conf.impl

Source Code of de.odysseus.calyxo.panels.conf.impl.PanelsRootConfigImpl

/*
* 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.panels.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.panels.conf.PanelConfig;
import de.odysseus.calyxo.panels.conf.PanelsRootConfig;
import de.odysseus.calyxo.panels.conf.PanelsConfig;

/**
*
* @author Christoph Beck
*/
public class PanelsRootConfigImpl extends RootConfigImpl implements PanelsRootConfig {
  private HashMap panelsConfigsByLocale = new HashMap();
  private HashMap panelsConfigs = 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((PanelsRootConfigImpl)other)
  }

  void merge(PanelsRootConfigImpl other) throws ConfigException {
    Iterator configs = other.getPanelsConfigs();
    while (configs.hasNext()) {
      PanelsConfigImpl value = (PanelsConfigImpl)configs.next();
      String key = getPanelsKey(value);
      PanelsConfigImpl panelsConfig =
        (PanelsConfigImpl)panelsConfigs.get(key);
      if (panelsConfig == null) {
        panelsConfigs.put(key, value);
      } else {
        panelsConfig.merge(value);
      }
    }
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_getElementName()
   */
  protected String _getElementName() {
    return "calyxo-panels-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(getPanelsConfigs());
  }

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

  /**
   * Create panels key. This key is used until the locales
   * of panels elements have been computed.
   */
  private String getPanelsKey(PanelsConfigImpl 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 panel by name and locale (exact match).
   */
  private PanelConfig getPanelConfig(String name, Locale locale) {
    PanelsConfig panelsConfig = (PanelsConfig)panelsConfigsByLocale.get(locale);
    if (panelsConfig != null) {
      PanelConfig panelConfig = panelsConfig.getPanelConfig(name);
      if (panelConfig != null) {
        return panelConfig;
      }
    }
    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 panels element
   */
  public void add(PanelsConfigImpl value) throws ConfigException {
    String key = getPanelsKey(value);
    if (panelsConfigs.containsKey(key)) {
      throw new ConfigException("Duplicate panels for key '" + key + "' in " + toInlineString());
    }
    panelsConfigs.put(key, value);
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.panels.conf.PanelsRootConfig#getPanelsConfigs()
   */
  public Iterator getPanelsConfigs() {
    return panelsConfigs.values().iterator();
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.panels.conf.PanelsRootConfig#findPanelConfig(java.lang.String, java.util.Locale)
   */
  public PanelConfig findPanelConfig(String name, Locale locale) {
    if (locale == null) {
      locale = defaultLocale;
    }
    do {
      PanelConfig result = getPanelConfig(name, locale);
      if (result != null)
        return result;
      locale = generalize(locale);
    } while (locale != null);
    return null;
  }
}
TOP

Related Classes of de.odysseus.calyxo.panels.conf.impl.PanelsRootConfigImpl

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.