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

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

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

import javax.servlet.jsp.el.ELException;
import javax.servlet.jsp.el.Expression;
import javax.servlet.jsp.el.ExpressionEvaluator;
import javax.servlet.jsp.el.VariableResolver;

import de.odysseus.calyxo.base.conf.ConfigException;
import de.odysseus.calyxo.base.conf.impl.ConfigImpl;
import de.odysseus.calyxo.base.util.PropertyUtils;
import de.odysseus.calyxo.forms.conf.PropertyConfig;

/**
* Property configuration implementation.
*
* @author Christoph Beck
*/
public class PropertyConfigImpl extends ConfigImpl implements PropertyConfig {

  private static class PrintableExpression extends Expression {
    Expression expression;
    String string;
    PrintableExpression(Expression expression, String string) {
      super();
      this.expression = expression;
      this.string = string;
    }
    public Object evaluate(VariableResolver resolver) throws ELException {
      return expression.evaluate(resolver);
    }
    public String toString() {
      return string;
    }
  }

  private String name;
  private Object value;
  private boolean defined;

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

  /*
   * (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);
    map.put("value", value);
  }

  /**
   * Answer array <code>{ "value" }</code>.
   * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_getDynamicAttributes()
   */
  protected String[] _getDynamicAttributes() {
    return new String[]{ "value" };
  }

  /**
   * Answer <code>true</code> iff the <code>value</code> property has been set.
   */
  protected boolean isDefined() {
    return defined;
  }
 
  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.base.conf.PropertyConfig#set(java.lang.Object)
   */
  public void set(Object object, ExpressionEvaluator evaluator) throws ConfigException {
    if (!defined) {
      throw new ConfigException("Cannot set undefined property " + toInlineString());
    }
    if (object instanceof Map) {
      ((Map)object).put(name, value);
    } else {
      try {
        if (value instanceof String) {
          Class type = PropertyUtils.getPropertyDescriptor(object.getClass(), name).getPropertyType();
          if (type == Expression.class) {
            String s = "${" + value + "}";
            Expression e = evaluator.parseExpression(s, Object.class, this);
            PropertyUtils.setProperty(object, name, new PrintableExpression(e, (String) value));
          } else {           
            PropertyUtils.setPropertyFromString(object, name, (String)value);
          }
        } else {
          PropertyUtils.setProperty(object, name, value);
        }
      } catch (Exception e) {
        throw new ConfigException(
          "Could not set property " + name + " in " + toInlineString(), e);
      }
    }
  }

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

  /**
   * Set name property
   */
  public void setName(String name) {
    this.name = name;
  }

  /**
   * Get value property
   */
  public Object getValue() {
    return value;
  }

  /**
   * Set value property
   */
  public void setValue(Object value) {
    this.value = value;
    defined = true;
  }
}
TOP

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

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.