haul        2002/10/18 07:05:33

  Added:       src/java/org/apache/cocoon/components/modules/input
                        AbstractMetaModule.java ChainMetaModule.java
                        DateMetaInputModule.java
  Removed:     src/java/org/apache/cocoon/components/modules/input
                        CollectionMetaModule.java
               src/java/org/apache/cocoon/components/modules/database
                        AbstractAutoIncrementModule.java
                        AutoIncrementModule.java
                        HsqlIdentityAutoIncrementModule.java
                        IfxSerialAutoIncrementModule.java
                        ManualAutoIncrementModule.java
                        MysqlAutoIncrementModule.java package.html
               src/java/org/apache/cocoon/components/language/markup/xsp
                        EsqlConnection.java EsqlConnectionCocoon2.java
                        EsqlHelper.java EsqlQuery.java
               src/java/org/apache/cocoon/components/language/markup/xsp/java
                        esql.xsl
               src/java/org/apache/cocoon/components/hsqldb Server.java
                        ServerImpl.java hsqldb.xconf hsqldb.xroles
               src/java/org/apache/cocoon/acting
                        AbstractDatabaseAction.java DatabaseAddAction.java
                        DatabaseAuthenticatorAction.java
                        DatabaseDeleteAction.java DatabaseSelectAction.java
                        DatabaseUpdateAction.java OraAddAction.java
                        OraUpdateAction.java
               src/java/org/apache/cocoon/acting/modular
                        DatabaseAction.java DatabaseAddAction.java
                        DatabaseDeleteAction.java DatabaseQueryAction.java
                        DatabaseSelectAction.java DatabaseUpdateAction.java
  Log:
  New block for database related stuff
  New block for hsqldb related stuff
  Minor changes to module related stuff (i.e. defaults changed
  to new names)
  Minor additions to modules
  New ChainMetaModule implementing chaining as discussed on list
  
  Not finished:
  - database samples not yet in block
  - change to InputModule Interface to use Iterator instead of Enumeration
  - other minor tweaks and corrections
  
  Revision  Changes    Path
  1.1                  
xml-cocoon2/src/java/org/apache/cocoon/components/modules/input/AbstractMetaModule.java
  
  Index: AbstractMetaModule.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      [EMAIL PROTECTED]
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <[EMAIL PROTECTED]>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  
  package org.apache.cocoon.components.modules.input;
  
  import org.apache.avalon.framework.activity.Disposable;
  import org.apache.avalon.framework.activity.Initializable;
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.component.ComponentSelector;
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.component.Composable;
  import org.apache.avalon.framework.thread.ThreadSafe;
  
  import org.apache.cocoon.util.HashMap;
  
  /**
   * AbstractMetaModule gives you the infrastructure for easily
   * deploying more "meta" InputModules i.e. InputModules that are
   * composed of other InputModules.  In order to get at the Logger, use
   * getLogger().
   *
   * @author <a href="mailto:haul@;apache.org">Christian Haul</a>
   * @version CVS $Id: AbstractMetaModule.java,v 1.1 2002/10/18 14:05:32 haul Exp $
   */
  public abstract class AbstractMetaModule extends AbstractInputModule
      implements InputModule, Configurable, Initializable, Composable, Disposable {
  
      /** The component manager instance */
      protected ComponentManager manager;
  
      String INPUT_MODULE_ROLE = InputModule.ROLE;
      String INPUT_MODULE_SELECTOR = INPUT_MODULE_ROLE+"Selector";
  
      protected ComponentSelector inputSelector = null;
  
  
      /**
       * Set the current <code>ComponentManager</code> instance used by this
       * <code>Composable</code>.
       */
      public void compose(ComponentManager manager) throws ComponentException {
  
          this.manager=manager;
      }
  
  
      /**
       * Obtain a reference to an InputModule.
       */
      protected InputModule obtainModule(String type) {
          
          InputModule module = null;
          try {
              if (type != null && this.inputSelector.hasComponent(type)){
                  module = (InputModule) this.inputSelector.select(type);
                  if (!(module instanceof ThreadSafe) ) {
                      this.inputSelector.release(module);
                      module = null;
                  }
              } else {
                  if (type != null)
                      if (getLogger().isWarnEnabled())
                          getLogger().warn("A problem occurred setting up '" + type
                                           +"': Selector is 
"+(this.inputSelector!=null?"not ":"")
                                           +"null, Component is "
                                           
+(this.inputSelector!=null&&this.inputSelector.hasComponent(type)?"known":"unknown"));
              }
          } catch (ComponentException ce) {
              if (getLogger().isWarnEnabled()) {
                  getLogger().warn("A problem occurred setting up '" + type
                                   +"': Selector is "+(this.inputSelector!=null?"not 
":"")
                                   +"null, Component is "
                                   
+(this.inputSelector!=null&&this.inputSelector.hasComponent(type)?"known":"unknown"));
                  getLogger().warn(ce.getMessage());
              }
          }
          return module;
      }
  
  
  }
  
  
  
  1.1                  
xml-cocoon2/src/java/org/apache/cocoon/components/modules/input/ChainMetaModule.java
  
  Index: ChainMetaModule.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      [EMAIL PROTECTED]
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <[EMAIL PROTECTED]>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.components.modules.input;
  
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.component.ComponentSelector;
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.thread.ThreadSafe;
  
  import org.apache.cocoon.components.modules.input.InputModule;
  
  import java.util.Map;
  import java.util.List;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collection;
  import java.util.Enumeration;
  import java.util.Iterator;
  /**
   * This modules allows to "chain" several other modules. If a module
   * returns "null" as attribute value, the next module in the chain is
   * queried until either a value can be obtained or the end of the
   * chain is reached.
   *
   * <p>A typical example would be to "chain" request parameters,
   * session attributes, and constants in this order. This way, an
   * application could have a default skin that could be overridden by a
   * user in her/his profile stored in the session. In addition, the
   * user could request a different skin through passing a request
   * parameter.</p>
   *
   * <p>Usage:</p>
   *
   * <p> Any number of &lt;input-module/&gt; blocks may appear in the
   * component configuration. The @name attribute is used as the name of
   * the requested input module. The complete &lt;input-module/&gt;
   * block is passed at run-time to the module and thus can contain any
   * configuration data for that particular module.</p>
   *
   * <p>Configuration:</p>
   *
   * <p>It can be controlled whether it returns a flat or a deep view,
   * i.e. whether only values from the first module are returned if
   * non-null or they are merged with values from other modules
   * <code>&lt;all-values&gt;true&lt;/all-values&gt;</code>. The same is
   * possible for the attribute names
   * (<code>&lt;all-names/&gt;</code>). In addition, empty strings could
   * be treated the same as null values
   * (<code>&lt;empty-as-null/&gt;</code>).</p>
   *
   * @author <a href="mailto:haul@;informatik.tu-darmstadt.de">Christian Haul</a>
   * @version CVS $Id: ChainMetaModule.java,v 1.1 2002/10/18 14:05:32 haul Exp $
   */
  public class ChainMetaModule extends AbstractMetaModule {
  
      class ModuleHolder {
          public String name = null;
          public InputModule input = null;
          public Configuration config = null;
      }
  
      private ModuleHolder[] inputs = null;
  
      private boolean emptyAsNull = false;
      private boolean allNames = false;
      private boolean allValues = false;
  
      private boolean initialized = false;
  
  
      public void configure(Configuration config) throws ConfigurationException {
  
          Configuration[] confs = config.getChildren("input-module");
          if (confs.length>0) {
              this.inputs = new ModuleHolder[confs.length];
              int j = 0;
              for (int i=0; i<confs.length; i++) {
                  ModuleHolder module = new ModuleHolder();
                  module.name = confs[i].getAttribute("name",null);
                  if (module.name == null) {
                      if (getLogger().isErrorEnabled())
                          getLogger().error("No name attribute for module 
configuration. Skipping.");
                      continue;
                  }
                  module.config = confs[i];
                  this.inputs[j]=module;
                  j++;
              }
          }
          this.emptyAsNull = 
config.getChild("empty-as-null").getValueAsBoolean(this.emptyAsNull);
          this.allNames = 
config.getChild("all-names").getValueAsBoolean(this.allNames);
          this.allValues = 
config.getChild("all-values").getValueAsBoolean(this.allValues);
      }
  
  
      public void initialize() {
  
          try {
              // obtain input modules
              this.inputSelector=(ComponentSelector) 
this.manager.lookup(INPUT_MODULE_SELECTOR); 
              if (this.inputSelector != null && this.inputSelector instanceof 
ThreadSafe) {
  
                  for (int i=0; i<this.inputs.length; i++) {
                      if (this.inputs[i].name != null) 
                          this.inputs[i].input = obtainModule(this.inputs[i].name);
                  }
  
              } else if (!(this.inputSelector instanceof ThreadSafe) ) {
                  this.manager.release(this.inputSelector);
                  this.inputSelector = null;
              }
  
              this.initialized = true;
  
          } catch (Exception e) {
              if (getLogger().isWarnEnabled()) 
                  getLogger().warn("A problem occurred setting up input modules :'" + 
e.getMessage());
          }
      }
  
      
      public void dispose() {
          
          if (!this.initialized) 
              if (getLogger().isErrorEnabled()) 
                  getLogger().error("Uninitialized Component! FAILING");
              else 
                  if (this.inputSelector != null) {
                      
                      for (int i=0; i<this.inputs.length; i++) {
                          if (this.inputs[i].input != null)
                              this.inputSelector.release(this.inputs[i].input);
                      }
  
                      this.manager.release(this.inputSelector);
                  }
      }
  
  
      public Object[] getAttributeValues( String attr, Configuration modeConf, Map 
objectModel ) 
          throws ConfigurationException {
  
          if (!this.initialized) {
              if (getLogger().isErrorEnabled()) 
                  getLogger().error("Uninitialized Component! FAILING");
              return null;
          }
  
          // obtain correct configuration objects
          // default vs dynamic
          Configuration[] inputConfigs = null;
          boolean allValues = this.allValues;
          boolean emptyAsNull = this.emptyAsNull;
          if (modeConf!=null) {
              inputConfigs = modeConf.getChildren("input-modules");
              emptyAsNull = 
modeConf.getChild("empty-as-null").getValueAsBoolean(emptyAsNull);
              allValues = modeConf.getChild("all-values").getValueAsBoolean(allValues);
          }
  
          Object[] value = null;
          boolean debug = getLogger().isDebugEnabled();
          List values = null;
          if (allValues) values = new ArrayList();
  
          if (inputConfigs == null) {
              // static configuration branch
              int i = 0;
              while (i < this.inputs.length && (value == null || allValues)) {
                  if (this.inputs[i].name != null) {
                      InputModule input = this.inputs[i].input;
                      if (input == null) {
                          input = obtainModule(this.inputs[i].name);
                          value = input.getAttributeValues(attr, 
this.inputs[i].config, objectModel);
                          this.inputSelector.release(input);
                      } else {
                          value = input.getAttributeValues(attr, 
this.inputs[i].config, objectModel);
                      }
                      if (emptyAsNull && value != null && value.length == 0) value = 
null;
                      if (emptyAsNull && value != null && value.length == 1 && 
                          value[0] instanceof String && ((String)value[0]).equals("")) 
value = null;
                      if (debug) getLogger().debug("read from "+this.inputs[i].name+" 
attribute "+attr+" as "+value);
                      if (allValues && value != null) 
values.addAll(Arrays.asList(value));
                  }
                  i++;
              }
          } else {
              // run-time configuration branch
              int i = 0;
              while (i < inputConfigs.length && (value == null || allValues)) {
                  String name = inputConfigs[i].getAttribute("name",null);
                  if (this.inputs[i].name != null) {
                      InputModule input = obtainModule(name);
                      value = input.getAttributeValues(attr, inputConfigs[i], 
objectModel);
                      this.inputSelector.release(input);
                      if (emptyAsNull && value != null && value.length == 0) value = 
null;
                      if (emptyAsNull && value != null && value.length == 1 && 
                          value[0] instanceof String && ((String)value[0]).equals("")) 
value = null;
                      if (debug) getLogger().debug("read from "+name+" attribute 
"+attr+" as "+value);
                      if (allValues && value != null) 
values.addAll(Arrays.asList(value));
                  }
                  i++;
              }
          }
          return (allValues? values.toArray() : value);
      }
  
  
      /** kludge - will be removed when getAttributeNames() returns java.util.Iterator 
*/
      private void addEnumeration(Collection col, Enumeration enum) {
          while (enum.hasMoreElements()) {
              col.add(enum.nextElement());
          }
      }
  
  
  
      public Enumeration getAttributeNames( Configuration modeConf, Map objectModel ) 
          throws ConfigurationException {
  
  
          if (!this.initialized) {
              if (getLogger().isErrorEnabled()) 
                  getLogger().error("Uninitialized Component! FAILING");
              return null;
          }
  
          // obtain correct configuration objects
          // default vs dynamic
          Configuration[] inputConfigs = null;
          boolean emptyAsNull = this.emptyAsNull;
          boolean allNames = this.allNames;
          if (modeConf!=null) {
              inputConfigs = modeConf.getChildren("input-modules");
              emptyAsNull = 
modeConf.getChild("empty-as-null").getValueAsBoolean(emptyAsNull);
              allNames = modeConf.getChild("all-names").getValueAsBoolean(allNames);
          }
  
          Enumeration value = null;
          Collection values = null;
          if (allNames) values = new ArrayList();
          boolean debug = getLogger().isDebugEnabled();
  
          if (inputConfigs == null) {
              // static configuration branch
              int i = 0;
              while (i < this.inputs.length && (value == null || allNames)) {
                  if (this.inputs[i].name != null) {
                      InputModule input = this.inputs[i].input;
                      if (input == null) {
                          input = obtainModule(this.inputs[i].name);
                          value = input.getAttributeNames(this.inputs[i].config, 
objectModel);
                          this.inputSelector.release(input);
                      } else {
                          value = input.getAttributeNames(this.inputs[i].config, 
objectModel);
                      }
                      if (debug) getLogger().debug("read from "+this.inputs[i].name+" 
AttributeNames as "+value);
                      if (allNames && value != null) addEnumeration(values, value);
                  }
                  i++;
              }
          } else {
              // run-time configuration branch
              int i = 0;
              while (i < inputConfigs.length && value == null) {
                  String name = inputConfigs[i].getAttribute("name",null);
                  if (this.inputs[i].name != null) {
                      InputModule input = obtainModule(name);
                      value = input.getAttributeNames(inputConfigs[i], objectModel);
                      this.inputSelector.release(input);
                      if (debug) getLogger().debug("read from "+name+" AttributeNames 
as "+value);
                      if (allNames && value != null) addEnumeration(values, value);
                  }
                  i++;
              }
          }
          return (allNames? new EnumerationHelper(values.iterator()) : value);
       }
  
  
      public Object getAttribute( String attr, Configuration modeConf, Map objectModel 
) 
          throws ConfigurationException {
  
          Object[] values = this.getAttributeValues(attr,modeConf,objectModel);
          return (values != null? values[0] : null);
      }
  
  
  }
  
  
  
  1.1                  
xml-cocoon2/src/java/org/apache/cocoon/components/modules/input/DateMetaInputModule.java
  
  Index: DateMetaInputModule.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      [EMAIL PROTECTED]
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <[EMAIL PROTECTED]>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.components.modules.input;
  
  
  import org.apache.avalon.framework.activity.Disposable;
  import org.apache.avalon.framework.activity.Initializable;
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.component.ComponentSelector;
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.component.Composable;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.framework.parameters.Parameters;
  import org.apache.avalon.framework.thread.ThreadSafe;
  
  import org.apache.cocoon.components.modules.input.InputModule;
  
  import org.apache.cocoon.matching.AbstractWildcardMatcher;
  
  import java.util.ArrayList;
  import java.util.Map;
  import java.util.HashMap;
  import java.util.Enumeration;
  import java.util.SortedSet;
  import java.util.TreeSet;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  
  /**
   * Parses a date string according to a given format and returns a date object.
   *
   * @author <a href="mailto:haul@;informatik.tu-darmstadt.de">Christian Haul</a>
   * @version CVS $Id: DateMetaInputModule.java,v 1.1 2002/10/18 14:05:32 haul Exp $
   */
  public class DateMetaInputModule extends AbstractLogEnabled
      implements InputModule, Configurable, Initializable, Composable, Disposable {
  
      /** The component manager instance */
      protected ComponentManager manager;
  
      private String defaultInput = null;
      private Configuration inputConf = null;  // will become an empty configuration 
object
                                               // during configure() so why bother 
here...
      String INPUT_MODULE_ROLE = InputModule.ROLE;
      String INPUT_MODULE_SELECTOR = INPUT_MODULE_ROLE+"Selector";
  
      private boolean initialized = false;
      private InputModule input = null;
      private ComponentSelector inputSelector = null;
      private String defaultFormat = "yyyy-MM-dd";
      private DateFormat defaultFormatter = null;
  
  
      /**
       * Set the current <code>ComponentManager</code> instance used by this
       * <code>Composable</code>.
       */
      public void compose(ComponentManager manager) throws ComponentException {
  
          this.manager=manager;
      }
  
  
      
      public void configure(Configuration config) throws ConfigurationException {
  
          this.inputConf = config.getChild("input-module");
          this.defaultInput = this.inputConf.getAttribute("name",this.defaultInput);
          this.defaultFormat = 
this.inputConf.getAttribute("format",this.defaultFormat);
          if (this.defaultFormat != null) {
              this.defaultFormatter = new SimpleDateFormat(this.defaultFormat);
          }
      }
  
  
  
      public void initialize() {
  
          try {
              // obtain input module
              this.inputSelector=(ComponentSelector) 
this.manager.lookup(INPUT_MODULE_SELECTOR); 
              if (this.defaultInput != null && 
                  this.inputSelector != null && 
                  this.inputSelector.hasComponent(this.defaultInput)
                  ){
                  this.input = (InputModule) 
this.inputSelector.select(this.defaultInput);
                  if (!(this.input instanceof ThreadSafe && this.inputSelector 
instanceof ThreadSafe) ) {
                      this.inputSelector.release(this.input);
                      this.manager.release(this.inputSelector);
                      this.input = null;
                      this.inputSelector = null;
                  }
                  this.initialized = true;
              } else {
                  if (this.defaultInput != null)
                      if (getLogger().isErrorEnabled())
                          getLogger().error("A problem occurred setting up '" + 
this.defaultInput 
                                            +"': Selector is 
"+(this.inputSelector!=null?"not ":"")
                                            +"null, Component is "
                                            
+(this.inputSelector!=null&&this.inputSelector.hasComponent(this.defaultInput)?"known":"unknown"));
                  this.initialized = true;
              }
          } catch (Exception e) {
              if (getLogger().isWarnEnabled()) 
                  getLogger().warn("A problem occurred setting up '" + 
this.defaultInput + "': " + e.getMessage());
          }
      }
  
  
  
      public void dispose() {
  
          if (!this.initialized) 
              if (getLogger().isErrorEnabled()) 
                  getLogger().error("Uninitialized Component! FAILING");
          else 
              if (this.inputSelector != null) {
                  if (this.input != null)
                      this.inputSelector.release(this.input);
                  this.manager.release(this.inputSelector);
              }
      }
  
  
      
  
      public Object[] getAttributeValues( String name, Configuration modeConf, Map 
objectModel ) 
          throws ConfigurationException {
  
          if (!this.initialized) {
              if (getLogger().isErrorEnabled()) 
                  getLogger().error("Uninitialized Component! FAILING");
              return null;
          }
          if (this.defaultInput == null) {
              if (getLogger().isWarnEnabled()) 
                  getLogger().warn("No input module given");
          }
  
          // obtain correct configuration objects
          // default vs dynamic
          Configuration inputConfig = this.inputConf;
          Configuration mConf = null;
          String inputName=null;
          String parameter=name;
          String format=null;
          DateFormat formatter=null;
          if (modeConf!=null) {
              mConf       = modeConf.getChild("values");
              inputName   = 
modeConf.getChild("input-module").getAttribute("name",null);
              parameter   = modeConf.getAttribute("parameter",parameter);
              format      = modeConf.getAttribute("format",this.defaultFormat);
              if (inputName != null) {
                  inputConfig = modeConf.getChild("input-module");
              }
          }
          if (this.defaultFormat.equals(format)) {
              formatter = this.defaultFormatter;
          } else {
              formatter = new SimpleDateFormat(format);
          }
  
          // done reading configuration
          // setup modules and read values
          Object[] values = null;
          try {
              if (this.defaultInput != null || inputName != null) {
                  if (this.input != null && inputName == null) {
                      // input module is thread safe
                      // thus we still have a reference to it
                      // and
                      // no other module is configured dynamically
                      values = 
input.getAttributeValues(parameter,this.inputConf,objectModel);
                  } else {
                      // input was not thread safe
                      // or
                      // another module is configured dynamically
                      // so acquire it again
                      ComponentSelector iputSelector = null;
                      InputModule iput = null;
                      try {
                          // obtain input module
                          if (inputName == null) {
                              inputName   = this.defaultInput;
                              inputConfig = this.inputConf;
                          }
                          
                          iputSelector=(ComponentSelector) 
this.manager.lookup(INPUT_MODULE_SELECTOR); 
                          if (inputName != null 
                              && iputSelector != null 
                              && iputSelector.hasComponent(inputName)) {
                              
                              iput = (InputModule) iputSelector.select(inputName);
                          }
                          if (iput != null) {
                              values = iput.getAttributeValues(parameter, inputConfig, 
objectModel);
                          }           
                      } catch (Exception e) {
                          if (getLogger().isWarnEnabled()) 
                              getLogger().warn("A problem occurred acquiring a value 
from '" 
                                               + inputName + "' for '"+name+"': " + 
e.getMessage());
                      } finally {
                          // release components
                          if (iputSelector != null) {
                              if (iput != null)
                                  iputSelector.release(iput);
                              this.manager.release(iputSelector);
                          }
                      }
                      
                  }
              }
              
              // done reading values
              // start converting values and assemble array
              
              Object[] dates = null;
              if (values != null) {
                  dates = new Object[values.length];
                  for (int i=0; i<values.length; i++) 
                      try {
                          dates[i] = formatter.parse(String.valueOf(values[i]));
                      } catch (Exception e) {
                          if(getLogger().isWarnEnabled()) 
                              getLogger().warn("Problem: Aquired '"+values[i]+"' from 
'" + inputName + "' for '"
                                               +name+"' using format '"+format+"' : 
"+e.getMessage());
                      }
              }
              return dates;
          } catch (Exception e) {
              if (getLogger().isWarnEnabled()) 
                  getLogger().warn("A problem occurred acquiring a value from '" + 
inputName 
                                   + "' for '"+name+"': " + e.getMessage());
          }
          return null;        
      }
  
  
  
  
  
      public Enumeration getAttributeNames( Configuration modeConf, Map objectModel ) 
          throws ConfigurationException {
  
          if (!this.initialized) {
              if (getLogger().isErrorEnabled()) 
                  getLogger().error("Uninitialized Component! FAILING");
              return null;
          }
          if (this.defaultInput == null) {
              if (getLogger().isWarnEnabled()) 
                  getLogger().warn("No input module given");
          }
  
          // obtain correct configuration objects
          // default vs dynamic
          Configuration inputConfig = this.inputConf;
          Configuration mConf = null;
          String inputName=null;
          if (modeConf!=null) {
              mConf       = modeConf.getChild("values");
              inputName   = 
modeConf.getChild("input-module").getAttribute("name",null);
              if (inputName != null) {
                  inputConfig = modeConf.getChild("input-module");
              }
          }
  
          // done reading configuration
          // setup modules and read values
          Enumeration enum = null;
          try {
              if (this.defaultInput != null || inputName != null) {
                  if (this.input != null && inputName == null) {
                      // input module is thread safe
                      // thus we still have a reference to it
                      // and
                      // no other module is configured dynamically
                      enum = input.getAttributeNames(this.inputConf,objectModel);
                  } else {
                      // input was not thread safe
                      // or
                      // another module is configured dynamically
                      // so acquire it again
                      ComponentSelector iputSelector = null;
                      InputModule iput = null;
                      try {
                          // obtain input module
                          if (inputName == null) {
                              inputName   = this.defaultInput;
                              inputConfig = this.inputConf;
                          }
                          
                          iputSelector=(ComponentSelector) 
this.manager.lookup(INPUT_MODULE_SELECTOR); 
                          if (this.defaultInput != null 
                              && iputSelector != null 
                              && iputSelector.hasComponent(inputName)) {
                              
                              iput = (InputModule) iputSelector.select(inputName);
                          }
                          if (iput != null) {
                              enum = iput.getAttributeNames(inputConfig, objectModel);
                          }           
                      } catch (Exception e) {
                          if (getLogger().isWarnEnabled()) 
                              getLogger().warn("A problem occurred acquiring names 
from '" 
                                               + inputName + "' : " + e.getMessage());
                      } finally {
                          // release components
                          if (iputSelector != null) {
                              if (iput != null)
                                  iputSelector.release(iput);
                              this.manager.release(iputSelector);
                          }
                      }
                      
                  }
              }
              return enum;
          } catch (Exception e) {
              if (getLogger().isWarnEnabled()) 
                  getLogger().warn("A problem occurred acquiring names from '" + 
inputName 
                                   + "' : " + e.getMessage());
          }
          return null;        
       }
  
  
  
  
      public Object getAttribute( String name, Configuration modeConf, Map objectModel 
) 
          throws ConfigurationException {
  
          Object[] values = this.getAttributeValues(name,modeConf,objectModel);
          return (values != null ? values[0] : null);
      }
  
  }
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to