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

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

/*
* 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 de.odysseus.calyxo.base.conf.ConfigException;
import de.odysseus.calyxo.base.conf.impl.ConfigImpl;
import de.odysseus.calyxo.forms.conf.ArgConfig;
import de.odysseus.calyxo.forms.conf.FieldConfig;
import de.odysseus.calyxo.forms.conf.MessageConfig;
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;
import de.odysseus.calyxo.forms.conf.ValidatorsConfig;


/**
* Validate configuration implementation.
*
* @author Christoph Beck
* @author Oliver Stuhr
*/
public abstract class ValidateConfigImpl extends ConfigImpl implements ValidateConfig {
  private String name;

  private MessageConfig message;
  private HashMap properties = new HashMap();

  private ValidatorConfig validatorConfig; 
  private String id;

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

  /*
   * (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("name", name);
  }

  /**
   * To be implemented by subclasses (match, convert, check).
   * @param validators validators element
   * @return the validator used by the receiver.
   */
  protected abstract ValidatorConfig getValidatorFrom(ValidatorsConfig validators);

  /**
   * Initialize validator element.
   * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_init()
   */
  protected void _init() throws ConfigException {
    super._init();
    FormsRootConfigImpl formsRootConfig =
      (FormsRootConfigImpl)_getNearestAncestor(FormsRootConfigImpl.class);
    ValidatorsConfig validators = formsRootConfig.getValidatorsConfig();
    validatorConfig = getValidatorFrom(validators);
    if (validatorConfig == null) {
      throw new ConfigException("Could not find validator named " + name + " in '" + toInlineString() + "'");
    }
  }

  /**
   * Validate message configuration
   * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_init2()
   */
  protected void _init2() throws ConfigException {
    super._init2();
    // validate message conf if we derive from base message
    if (message != null && message.getKey() == null) {
      MessageConfig baseMessage = validatorConfig.getMessageConfig();
      MessageConfig fieldMessage = getFieldConfig().getMessageConfig();
      if (fieldMessage != null && fieldMessage.getKey() != null) {
        baseMessage = fieldMessage;
      }
      if (baseMessage == null) {
        throw new ConfigException("No base message in validator '" + name + "' in '" + toInlineString() + "'");
      } else if (baseMessage.getKey() == null) {
        throw new ConfigException("No key for message in validator '" + name + "' in '" + toInlineString() + "'");
      }
      Iterator args = message.getArgConfigs();
      while (args.hasNext()) {
        ArgConfig arg = (ArgConfig)args.next();
        if (arg.getName() == null) {
          throw new ConfigException("Missing name for argument '" + arg + "' in '" + toInlineString() + "'");
        } else if (baseMessage.getArgConfig(arg.getName()) == null) {
          throw new ConfigException("No argument with name '" + arg.getName() + "' in base message of validator '" + name + "' in '" + toInlineString() + "'");
        }
      }
    }
    // make sure that all properties exist and are not final
    Iterator propertyConfigs = getPropertyConfigs();
    while (propertyConfigs.hasNext()) {
      PropertyConfig propertyConfig = (PropertyConfig)propertyConfigs.next();
      String name = propertyConfig.getName();
      ValidatorPropertyConfig validatorPropertyConfig =
        validatorConfig.getValidatorPropertyConfig(name);
      if (validatorPropertyConfig == null) {
        throw new ConfigException("Unknown property " + name + " in '" + toInlineString() + "'");
      }
      if (validatorPropertyConfig.isFinal()) {
        throw new ConfigException("Must not override property " + name + " in '" + toInlineString() + "'");
      }
    }
    // make sure abstract validator properties are defined
//    Iterator validatorPropertyConfigs = validatorConfig.getValidatorPropertyConfigs();
//    while (validatorPropertyConfigs.hasNext()) {
//      ValidatorPropertyConfig validatorPropertyConfig =
//        (ValidatorPropertyConfig)validatorPropertyConfigs.next();
//      String name = validatorPropertyConfig.getName();
//      if (!validatorPropertyConfig.isDefined() && !properties.containsKey(name)) {
//        throw new ConfigException("Missing property " + name + " in '" + toInlineString() + "'");
//      }
//    }
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.forms.conf.ValidateConfig#getFieldConfig()
   */
  public FieldConfig getFieldConfig() {
    return (FieldConfig)_getParent();
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.forms.conf.ValidateConfig#getId()
   */
  public String getId() {
    return id;
  }

  /**
   * Set unique id
   */
  public void setId(String string) {
    id = string;
  }

  /**
   * Get validator name
   */
  public String getName() {
    return name;
  }

  /**
   * Set validator name
   */
  public void setName(String string) {
    name = string;
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.forms.conf.ValidateConfig#getValidatorConfig()
   */
  public ValidatorConfig getValidatorConfig() {
    return validatorConfig;
  }

  /**
   * Add property element
   */
  public void add(ValidatePropertyConfigImpl value) {
    properties.put(value.getName(), value);
  }

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

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.forms.conf.ValidateConfig#getMessageConfig()
   */
  public MessageConfig getMessageConfig() {
    return message;
  }

  /**
   * Set message element
   */
  public void setMessageConfig(MessageConfig value) {
    message = value;
  }
}
TOP

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

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.