Package de.odysseus.calyxo.forms.impl

Source Code of de.odysseus.calyxo.forms.impl.Factory

/*
* 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.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;

import javax.servlet.jsp.el.ExpressionEvaluator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.forms.Checker;
import de.odysseus.calyxo.forms.Converter;
import de.odysseus.calyxo.forms.Form;
import de.odysseus.calyxo.forms.FormFactory;
import de.odysseus.calyxo.forms.Matcher;
import de.odysseus.calyxo.forms.ValidatorBase;
import de.odysseus.calyxo.forms.conf.CheckConfig;
import de.odysseus.calyxo.forms.conf.ConvertConfig;
import de.odysseus.calyxo.forms.conf.FormConfig;
import de.odysseus.calyxo.forms.conf.MatchConfig;
import de.odysseus.calyxo.forms.conf.PropertyConfig;
import de.odysseus.calyxo.forms.conf.ValidateConfig;
import de.odysseus.calyxo.forms.conf.ValidatorConfig;
import de.odysseus.calyxo.forms.conf.ValidatorPropertyConfig;



/**
* Form factory class.
* This class creates {@link de.odysseus.calyxo.forms.impl.FormImpl}
* instances. During form initialization, it is used to create and
* initialize validators. Sharable matchers, converters and checkers are
* cached and reused on a per locale basis.
*
* @author Christoph Beck
* @author Oliver Stuhr
*/
public class Factory implements FormFactory {
  private static final Log log = LogFactory.getLog(Factory.class);

  private HashMap validators = new HashMap();

  private ClassLoader loader;
  private ExpressionEvaluator evaluator;
 
  public void init(ModuleContext context) {
    this.loader = context.getClassLoader();
    this.evaluator = context.getExpressionEvaluator();
  }
 
  /**
   * Set bean property
   */
  private void init(PropertyConfig property, Object bean) {
    if (log.isTraceEnabled())
      log.trace(property.getName() + " = " + property.getValue());
    try {
      property.set(bean, evaluator);
    } catch (Exception e) {
      log.error(
        "Could not set property " +
          bean.getClass().getName() +
          "." +
          property.getName(),
        e
      );
    }
  }

  private ValidatorBase create(ValidateConfig validateConfig) {
    ValidatorBase base = null;
    ValidatorConfig validatorConfig = validateConfig.getValidatorConfig();
    try {
      base = (ValidatorBase)loader.loadClass(validatorConfig.getType()).newInstance();
    } catch (Exception e) {
      log.error("Could not create " + validatorConfig.getType() + " instance", e);
      return null;
    }

    Iterator propertyConfigs = null;
    propertyConfigs = validatorConfig.getValidatorPropertyConfigs();
    while (propertyConfigs.hasNext()) {
      ValidatorPropertyConfig property = (ValidatorPropertyConfig)propertyConfigs.next();
      if (property.isDefined()) {
        init(property, base);
      }
    }
    propertyConfigs = validateConfig.getPropertyConfigs();
    while (propertyConfigs.hasNext()) {
      PropertyConfig property = (PropertyConfig)propertyConfigs.next();
      init(property, base);
    }

    return base;
  }

  /**
   * Get key used for cache lookups
   */
  private String getKey(ValidateConfig config, Locale locale) {
    String key = config.getId();
    if (locale != null) {
      String s = locale.toString();
      if (s.length() > 0) {
        key += "_" + locale.toString();
      }
    }
    return key;
  }

  /**
   * Get/create validator instance for specified match configuration
   */
  Validator getValidator(MatchConfig config, Locale locale) {
    String key = getKey(config, locale);
    Validator validator = (Validator)validators.get(key);
    if (validator == null) {
      Matcher matcher = (Matcher)create(config);
      matcher.localize(locale);
      validator = new Validator.Match(config, matcher);
      if (matcher.isSharable()) {
        validators.put(key, validator);
      }
    }
    return validator;
  }

  /**
   * Get/create validator instance for specified convert configuration
   */
  Validator getValidator(ConvertConfig config, Locale locale) {
    String key = getKey(config, locale);
    Validator validator = (Validator)validators.get(key);
    if (validator == null) {
      Converter converter = (Converter)create(config);
      converter.localize(locale);
      validator = new Validator.Convert(config, converter);
      if (converter.isSharable()) {
        validators.put(key, validator);
      }
    }
    return validator;
  }

  /**
   * Get/create validator instance for specified check configuration
   */
  Validator getValidator(CheckConfig config, Locale locale) {
    String key = getKey(config, locale);
    Validator validator = (Validator)validators.get(key);
    if (validator == null) {
      Checker checker = (Checker)create(config);
      checker.localize(locale);
      validator = new Validator.Check(config, checker);
      if (checker.isSharable()) {
        validators.put(key, validator);
      }
    }
    return validator;
  }

  /**
   * Create form instance
   */
  public Form createForm(FormConfig config, Locale locale) {
    return new FormImpl(this, config, locale);
  }
}
TOP

Related Classes of de.odysseus.calyxo.forms.impl.Factory

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.