You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by re...@locus.apache.org on 2000/07/14 18:44:53 UTC

cvs commit: jakarta-slide/src/share/org/apache/slide/util/conf AbstractConfiguration.java Configuration.java ConfigurationElement.java ConfigurationException.java ConfigurationImpl.java Element.java Populate.java SAXConfigurationBuilder.java

remm        00/07/14 09:44:53

  Added:       src/share/org/apache/slide/util/conf
                        AbstractConfiguration.java Configuration.java
                        ConfigurationElement.java
                        ConfigurationException.java ConfigurationImpl.java
                        Element.java Populate.java
                        SAXConfigurationBuilder.java
  Log:
  - Add an abstraction layer for configuration parsing. The default
    implementation is based on some Avalon 2.1 code.
  
  Revision  Changes    Path
  1.1                  jakarta-slide/src/share/org/apache/slide/util/conf/AbstractConfiguration.java
  
  Index: AbstractConfiguration.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  package org.apache.slide.util.conf;
  
  import java.util.Enumeration;
  
  /**
   * This is an abstract <code>Configuration</code> implementation that deals
   * with methods that can be abstracted away from underlying implementations.
   *
   * @author <a href="mailto:scoobie@betaversion.org">Federico Barbieri</a>
   *         (Betaversion Productions)
   * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
   *         (Apache Software Foundation)
   * @author <a href="mailto:fumagalli@exoffice.com">Pierpaolo Fumagalli</a>
   *         (Apache Software Foundation, Exoffice Technologies)
   * @version CVS $Revision: 1.1 $ $Date: 2000/07/14 16:44:52 $
   */
  public abstract class AbstractConfiguration implements Configuration {
      /**
       * The location string containing information about this
       * <code>Configuration</code> location in the source file.
       */
      protected String location=null;
  
      /**
       * Construct a new <code>AbstractConfiguration</code> instance.
       */
      protected AbstractConfiguration() {
          this(null,-1);
      }
  
      /**
       * Construct a new <code>AbstractConfiguration</code> instance.
       */
      protected AbstractConfiguration(String source, int line) {
          super();
          this.location="";
          if (source!=null) this.location=source;
          if ((line>=0)&&(this.location.length()>0)) this.location+=" ";
          if (line>0) this.location+="line "+line;
          if (this.location.length()>0) this.location="("+this.location+")";
          else this.location=null;
      }
  
      /**
       * Returns the value of the configuration element as an <code>int</code>.
       */
      public int getValueAsInt()
  	throws ConfigurationException {
          String value=this.getValue();
          try {
              if (value.startsWith("0x"))
                  return(Integer.parseInt(value.substring(2),16));
              else if (value.startsWith("0o"))
                  return(Integer.parseInt(value.substring(2),8));
              else if (value.startsWith("0b"))
                  return(Integer.parseInt(value.substring(2),2));
              else return(Integer.parseInt(value));
          } catch (NumberFormatException e) {
              throw new ConfigurationException("Cannot parse the value of the "+
                  "configuration element \""+this.getName()+"\" as an integer",
                  this);
          }
  	}
  
      /**
       * Returns the value of the configuration element as a <code>long</code>.
       */
      public long getValueAsLong()
  	throws ConfigurationException {
          String value=this.getValue();
          try {
              if (value.startsWith("0x"))
                  return(Long.parseLong(value.substring(2),16));
              else if (value.startsWith("0o"))
                  return(Long.parseLong(value.substring(2),8));
              else if (value.startsWith("0b"))
                  return(Long.parseLong(value.substring(2),2));
              else return(Integer.parseInt(value));
          } catch (NumberFormatException e) {
              throw new ConfigurationException("Cannot parse the value of the "+
                  "configuration element \""+this.getName()+"\" as a long", this);
          }
  	}
  
      /**
       * Returns the value of the configuration element as a <code>float</code>.
       */
      public float getValueAsFloat()
  	throws ConfigurationException {
          String value=this.getValue();
          try {
              return(Float.parseFloat(value));
          } catch (NumberFormatException e) {
              throw new ConfigurationException("Cannot parse the value of the "+
                  "configuration element \""+this.getName()+"\" as a float",
                  this);
          }
      }
  
      /**
       * Returns the value of the configuration element as a <code>boolean</code>.
       */
      public boolean getValueAsBoolean()
  	throws ConfigurationException {
          String value=this.getValue();
          if (value.equals("true")) return(true);
          if (value.equals("false")) return(false);
          throw new ConfigurationException("Cannot parse the value of the "+
              "configuration element \""+this.getName()+"\" as a boolean",
              this);
      }
  
      /**
       * Returns the value of the configuration element as a <code>String</code>.
       */
      public String getValue(String defaultValue) {
          try {
              return(this.getValue());
          } catch (ConfigurationException e) {
              return(defaultValue);
          }
      }
  
      /**
       * Returns the value of the configuration element as an <code>int</code>.
       */
      public int getValueAsInt(int defaultValue) {
          try {
              return(this.getValueAsInt());
          } catch (ConfigurationException e) {
              return(defaultValue);
          }
      }
  
      /**
       * Returns the value of the configuration element as a <code>long</code>.
       */
      public long getValueAsLong(long defaultValue) {
          try {
              return(this.getValueAsLong());
          } catch (ConfigurationException e) {
              return(defaultValue);
          }
      }
  
      /**
       * Returns the value of the configuration element as a <code>float</code>.
       */
      public float getValueAsFloat(float defaultValue) {
          try {
              return(this.getValueAsFloat());
          } catch (ConfigurationException e) {
              return(defaultValue);
          }
      }
  
      /**
       * Returns the value of the configuration element as a <code>boolean</code>.
       */
      public boolean getValueAsBoolean(boolean defaultValue) {
          try {
              return(this.getValueAsBoolean());
          } catch (ConfigurationException e) {
              return(defaultValue);
          }
      }
  
      /**
       * Returns the value of the attribute specified by its name as an
       * <code>int</code>.
       */
      public int getAttributeAsInt(String name)
  	throws ConfigurationException {
          String value=this.getAttribute(name);
          try {
              if (value.startsWith("0x"))
                  return(Integer.parseInt(value.substring(2),16));
              else if (value.startsWith("0o"))
                  return(Integer.parseInt(value.substring(2),8));
              else if (value.startsWith("0b"))
                  return(Integer.parseInt(value.substring(2),2));
              else return(Integer.parseInt(value));
          } catch (NumberFormatException e) {
              throw new ConfigurationException("Cannot parse the value of the "+
                  "attribute \""+name+"\" of the configuration element \""+
                  this.getName()+"\" as an integer",this);
          }
      }
  
      /**
       * Returns the value of the attribute specified by its name as a
       * <code>long</code>.
       */
      public long getAttributeAsLong(String name)
  	throws ConfigurationException {
          String value=this.getAttribute(name);
          try {
              if (value.startsWith("0x"))
                  return(Long.parseLong(value.substring(2),16));
              else if (value.startsWith("0o"))
                  return(Long.parseLong(value.substring(2),8));
              else if (value.startsWith("0b"))
                  return(Long.parseLong(value.substring(2),2));
              else return(Integer.parseInt(value));
          } catch (NumberFormatException e) {
              throw new ConfigurationException("Cannot parse the value of the "+
                  "attribute \""+name+"\" of the configuration element \""+
                  this.getName()+"\" as a long", this);
          }
      }
  
      /**
       * Returns the value of the attribute specified by its name as a
       * <code>float</code>.
       */
      public float getAttributeAsFloat(String name)
  	throws ConfigurationException {
          String value=this.getAttribute(name);
          try {
              return(Float.parseFloat(value));
          } catch (NumberFormatException e) {
              throw new ConfigurationException("Cannot parse the value of the "+
                  "attribute \""+name+"\" of the configuration element \""+
                  this.getName()+"\" as a float", this);
          }
      }
  
      /**
       * Returns the value of the attribute specified by its name as a
       * <code>boolean</code>.
       */
      public boolean getAttributeAsBoolean(String name)
  	throws ConfigurationException {
          String value=this.getAttribute(name);
          if (value.equals("true")) return(true);
          if (value.equals("false")) return(false);
          throw new ConfigurationException("Cannot parse the value of the "+
              "attribute \""+name+"\" of the configuration element \""+
              this.getName()+"\" as a boolean", this);
      }
  
      /**
       * Returns the value of the attribute specified by its name as a
       * <code>String</code>.
       */
      public String getAttribute(String name, String defaultValue) {
          try {
              return(this.getAttribute(name));
          } catch (ConfigurationException e) {
              return(defaultValue);
          }
      }
  
      /**
       * Returns the value of the attribute specified by its name as an
       * <code>int</code>.
       */
      public int getAttributeAsInt(String name, int defaultValue) {
          try {
              return(this.getAttributeAsInt(name));
          } catch (ConfigurationException e) {
              return(defaultValue);
          }
      }
  
      /**
       * Returns the value of the attribute specified by its name as a
       * <code>long</code>.
       */
      public long getAttributeAsLong(String name, long defaultValue) {
          try {
              return(this.getAttributeAsLong(name));
          } catch (ConfigurationException e) {
              return(defaultValue);
          }
      }
  
      /**
       * Returns the value of the attribute specified by its name as a
       * <code>float</code>.
       */
      public float getAttributeAsFloat(String name, float defaultValue) {
          try {
              return(this.getAttributeAsFloat(name));
          } catch (ConfigurationException e) {
              return(defaultValue);
          }
      }
  
      /**
       * Returns the value of the attribute specified by its name as a
       * <code>boolean</code>.
       */
      public boolean getAttributeAsBoolean(String name, boolean defaultValue) {
          try {
              return(this.getAttributeAsBoolean(name));
          } catch (ConfigurationException e) {
              return(defaultValue);
          }
      }
  
      /**
       * Return the first <code>Configuration</code> object child of this
       * associated with the given name.
       */
      public Configuration getConfiguration(String name) {
  	    Enumeration e=this.getConfigurations(name);
  	    if (e.hasMoreElements()) return((Configuration)e.nextElement());
          return(null);
  	}
  
  	/**
  	 * Return a <code>String</code> indicating the position of this
  	 * configuration element in a source file or URI or <b>null</b>.
  	 */
  	public String getLocation() {
  	    return(this.location);
  	}
  }
  
  
  
  1.1                  jakarta-slide/src/share/org/apache/slide/util/conf/Configuration.java
  
  Index: Configuration.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  package org.apache.slide.util.conf;
  
  import java.util.*;
  
  /**
   * <code>Configuration</code> is a interface encapsulating a configuration node
   * used to retrieve configuration values. This is a "read only" interface
   * preventing applications from modifying their own configurations.
   * <br />
   *
   * The contract surrounding the <code>Configuration</code> is that once
   * it is created, information never changes.  The <code>Configuration</code>
   * is built by the <code>SAXConfigurationBuilder</code> and the
   * <code>ConfigurationImpl</code> helper classes.
   *
   * @version 1.1.0, 22/05/1999.
   * @author <a href="mailto:scoobie@pop.systemy.it">Federico Barbieri</a>
   * @author <a href="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
   * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
   * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
   * @author <a href="http://java.apache.org/">Java Apache Project</a>
   */
  public interface Configuration {
  
      /**
       * Return the name of the node.
       *
       * @post getName() != null
       *
       * @returns name of the <code>Configuration</code> node.
       */
      public String getName();
  
      /**
       * Return a new <code>Configuration</code> instance encapsulating the
       * specified child node.
       *
       * @pre child != null
       * @post getConfiguration() != null
       * @param child The name of the child node.
       *
       * @returns Configuration
       *
       * @exception ConfigurationException If no child with that name exists.
       */
      public Configuration getConfiguration(String child)
      throws ConfigurationException;
  
      /**
       * Return an <code>Enumeration</code> of <code>Configuration<code>
       * elements containing all node children with the specified name.
       *
       * @pre name != null
       * @post getConfigurations() != null
       * @param name The name of the children to get.
       *
       * @returns Enumeration.  The <code>Enumeration</code> will be
       *          empty if there are no nodes by the specified name.
       */
      public Enumeration getConfigurations(String name);
  
      /**
       * Return the value of specified attribute.
       *
       * @pre paramName != null
       * @post getAttribute != null
       * @param paramName The name of the parameter you ask the value of.
       *
       * @returns String value of attribute.
       *
       * @exception ConfigurationException If no attribute with that name exists.
       */
      public String getAttribute(String paramName)
      throws ConfigurationException;
  
      /**
       * Return the <code>int</code> value of the specified attribute contained
       * in this node.
       *
       * @pre paramName != null
       * @post getAttributeAsInt() != null
       * @param paramName The name of the parameter you ask the value of.
       *
       * @returns int value of attribute
       *
       * @exception ConfigurationException If no parameter with that name exists.
       *                                   or if conversion to <code>int</code> fails.
       */
      public int getAttributeAsInt(String paramName)
      throws ConfigurationException;
  
      /**
       * Returns the value of the attribute specified by its name as a
       * <code>long</code>.
       *
       * @pre paramName != null
       * @post getAttributeAsLong() != null
       * @param paramName The name of the parameter you ask the value of.
       *
       * @returns long value of attribute
       *
       * @exception ConfigurationException If no parameter with that name exists.
       *                                   or if conversion to <code>long</code> fails.
       */
      public long getAttributeAsLong(String name)
  	throws ConfigurationException;
  
      /**
       * Return the <code>float</code> value of the specified parameter contained
       * in this node.
       *
       * @pre paramName != null
       * @post getAttributeAsFloat() != null
       * @param paramName The name of the parameter you ask the value of.
       *
       * @returns float value of attribute
       *
       * @exception ConfigurationException If no parameter with that name exists.
       *                                   or if conversion to <code>float</code> fails.
       */
      public float getAttributeAsFloat(String paramName)
      throws ConfigurationException;
  
      /**
       * Return the <code>boolean</code> value of the specified parameter contained
       * in this node.<br>
       *
       * @pre paramName != null
       * @post getAttributeAsBoolean() != null
       * @param paramName The name of the parameter you ask the value of.
       *
       * @returns boolean value of attribute
       *
       * @exception ConfigurationException If no parameter with that name exists.
       *                                   or if conversion to <code>boolean</code> fails.
       */
      public boolean getAttributeAsBoolean(String paramName)
      throws ConfigurationException;
  
      /**
       * Return the <code>String</code> value of the node.
       *
       * @post getValue() != null
       *
       * @returns the value of the node.
       */
      public String getValue();
  
      /**
       * Return the <code>int</code> value of the node.
       *
       * @post getValueAsInt() != null
       *
       * @returns the value of the node.
       *
       * @exception ConfigurationException If conversion to <code>int</code> fails.
       */
      public int getValueAsInt()
      throws ConfigurationException;
  
      /**
       * Return the <code>float</code> value of the node.
       *
       * @post getValueAsFloat() != null
       *
       * @returns the value of the node.
       *
       * @exception ConfigurationException If conversion to <code>float</code> fails.
       */
      public float getValueAsFloat()
      throws ConfigurationException;
  
      /**
       * Return the <code>boolean</code> value of the node.
       *
       * @post getValueAsBoolean() != null
       *
       * @returns the value of the node.
       *
       * @exception ConfigurationException If conversion to <code>boolean</code> fails.
       */
      public boolean getValueAsBoolean()
      throws ConfigurationException;
  
      /**
       * Return the <code>long</code> value of the node.<br>
       *
       * @post getValueAsLong() != null
       *
       * @returns the value of the node.
       *
       * @exception ConfigurationException If conversion to <code>long</code> fails.
       */
      public long getValueAsLong()
      throws ConfigurationException;
  
      /**
       * Returns the value of the configuration element as a <code>String</code>.
       * If the configuration value is not set, the default value will be
       * used.
       *
       * @pre defaultValue != null
       * @post getValue(defaultValue) != null
       * @param defaultValue The default value desired.
       *
       * @returns String value of the <code>Configuration</code>, or default
       *          if none specified.
       */
  	public String getValue(String defaultValue);
  
      /**
       * Returns the value of the configuration element as an <code>int</code>.
       * If the configuration value is not set, the default value will be
       * used.
       *
       * @pre defaultValue != null
       * @post getValueAsInt(defaultValue) != null
       * @param defaultValue The default value desired.
       *
       * @returns int value of the <code>Configuration</code>, or default
       *          if none specified.
       */
      public int getValueAsInt(int defaultValue);
  
      /**
       * Returns the value of the configuration element as a <code>long</code>.
       * If the configuration value is not set, the default value will be
       * used.
       *
       * @pre defaultValue != null
       * @post getValueAsLong(defaultValue) != null
       * @param defaultValue The default value desired.
       *
       * @returns long value of the <code>Configuration</code>, or default
       *          if none specified.
       */
      public long getValueAsLong(long defaultValue);
  
      /**
       * Returns the value of the configuration element as a <code>float</code>.
       * If the configuration value is not set, the default value will be
       * used.
       *
       * @pre defaultValue != null
       * @post getValueAsFloat(defaultValue) != null
       * @param defaultValue The default value desired.
       *
       * @returns float value of the <code>Configuration</code>, or default
       *          if none specified.
       */
      public float getValueAsFloat(float defaultValue);
  
      /**
       * Returns the value of the configuration element as a <code>boolean</code>.
       * If the configuration value is not set, the default value will be
       * used.
       *
       * @pre defaultValue != null
       * @post getValueAsBoolean(defaultValue) != null
       * @param defaultValue The default value desired.
       *
       * @returns boolean value of the <code>Configuration</code>, or default
       *          if none specified.
       */
      public boolean getValueAsBoolean(boolean defaultValue);
  
      /**
       * Returns the value of the attribute specified by its name as a
       * <code>String</code>, or the default value if no attribute by
       * that name exists or is empty.
       *
       * @pre name != null
       * @pre defaultValue != null
       * @post getAttribute(name, defaultValue) != null
       * @param name The name of the attribute you ask the value of.
       * @param defaultValue The default value desired.
       *
       * @returns String value of attribute. It will return the default
       *          value if the named attribute does not exist, or if
       *          the value is not set.
       */
  	public String getAttribute(String name, String defaultValue);
  
      /**
       * Returns the value of the attribute specified by its name as a
       * <code>int</code>, or the default value if no attribute by
       * that name exists or is empty.
       *
       * @pre name != null
       * @pre defaultValue != null
       * @post getAttributeAsInt(name, defaultValue) != null
       * @param name The name of the attribute you ask the value of.
       * @param defaultValue The default value desired.
       *
       * @returns int value of attribute. It will return the default
       *          value if the named attribute does not exist, or if
       *          the value is not set.
       */
      public int getAttributeAsInt(String name, int defaultValue);
  
      /**
       * Returns the value of the attribute specified by its name as a
       * <code>long</code>, or the default value if no attribute by
       * that name exists or is empty.
       *
       * @pre name != null
       * @pre defaultValue != null
       * @post getAttributeAsLong(name, defaultValue) != null
       * @param name The name of the attribute you ask the value of.
       * @param defaultValue The default value desired.
       *
       * @returns long value of attribute. It will return the default
       *          value if the named attribute does not exist, or if
       *          the value is not set.
       */
      public long getAttributeAsLong(String name, long defaultValue);
  
      /**
       * Returns the value of the attribute specified by its name as a
       * <code>float</code>, or the default value if no attribute by
       * that name exists or is empty.
       *
       * @pre name != null
       * @pre defaultValue != null
       * @post getAttributeAsFloat(name, defaultValue) != null
       * @param name The name of the attribute you ask the value of.
       * @param defaultValue The default value desired.
       *
       * @returns float value of attribute. It will return the default
       *          value if the named attribute does not exist, or if
       *          the value is not set.
       */
      public float getAttributeAsFloat(String name, float defaultValue);
  
      /**
       * Returns the value of the attribute specified by its name as a
       * <code>boolean</code>, or the default value if no attribute by
       * that name exists or is empty.
       *
       * @pre name != null
       * @pre defaultValue != null
       * @post getAttributeAsBoolean(name, defaultValue) != null
       * @param name The name of the attribute you ask the value of.
       * @param defaultValue The default value desired.
       *
       * @returns boolean value of attribute. It will return the default
       *          value if the named attribute does not exist, or if
       *          the value is not set.
       */
      public boolean getAttributeAsBoolean(String name, boolean defaultValue);
  
  	/**
  	 * Return a <code>String</code> indicating the position of this
  	 * configuration element in a source file or URI.
       *
       * @returns String if a source file or URI is specified.  Otherwise
       *          it returns <code>null</code>
  	 */
  	public String getLocation();
  }
  
  
  
  1.1                  jakarta-slide/src/share/org/apache/slide/util/conf/ConfigurationElement.java
  
  Index: ConfigurationElement.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  package org.apache.slide.util.conf;
  
  import java.util.*;
  
  /**
   * <code>ConfigurationElement</code> is a class implementing the 
   * <code>Configuration</code> interface using a tree of Elements as
   * configuration container. The <code>Elements</code> tree is generated from the
   * configuration DOM. Each <code>Element</code> is encapsulated by a 
   * <code>ConfigurationElement</code>.
   *
   *
   * @version (CVS $Revision: 1.1 $ $Date: 2000/07/14 16:44:52 $)
   * @author <a href="mailto:scoobie@betaverion.org">Federico Barbieri</a>
   * @author <a href="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
   * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
   * @author <a href="http://java.apache.org/">Java Apache Project</a>
   */
  public class ConfigurationElement extends AbstractConfiguration {
  
      private Element content;
      private Vector children;
  
      public ConfigurationElement(Element content) {
          this.content = content;
          children = new Vector();
          try {
              for (Enumeration e = content.getChildren(); e.hasMoreElements();) {
                  children.addElement(new ConfigurationElement((Element) e.nextElement()));
              }
          } catch (ConfigurationException e) {
          }
      }
  
      public String getName() {
          return content.getName();
      }
  
      public String getValue() {
          return content.getData();
      }
  
      public String getAttribute(String name)
      throws ConfigurationException {
          String attribute = content.getAttributeValue(name);
          if (attribute == null) {
              throw new ConfigurationException("No attribute named \"" + name + "\" is " +
                  "associated with the configuration element \"" + this.getName() + "\"",
                  this);
          }
          return attribute;
      }
  
      public Configuration getConfiguration(String name)
      throws ConfigurationException {
  
          int index = name.indexOf('.');
          if (index == -1) {
              for (Enumeration e = children.elements(); e.hasMoreElements();) {
                  Configuration c = (Configuration) e.nextElement();
                  if (c.getName().equals(name)) {
                      return c;
                  }
              }
          } else {
              return getConfiguration(name.substring(0, index)).getConfiguration(name.substring(index + 1));
          }
          throw new ConfigurationException("No Configuration named \"" + name + "\" is " +
              "associated with the configuration element \"" + this.getName() + "\"", this);
      }
  
      public Enumeration getConfigurations(String name)
      throws ConfigurationException {
  
          int index = name.indexOf('.');
          if (index == -1) {
              Vector v = new Vector();
              for (Enumeration e = children.elements(); e.hasMoreElements();) {
                  Configuration c = (Configuration) e.nextElement();
                  if (c.getName().equals(name)) {
                      v.addElement(c);
                  }
              }
              return v.elements();
          } else {
              return getConfiguration(name.substring(0, index)).getConfigurations(name.substring(index + 1));
          }
      }
  }
  
  
  
  1.1                  jakarta-slide/src/share/org/apache/slide/util/conf/ConfigurationException.java
  
  Index: ConfigurationException.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  package org.apache.slide.util.conf;
  
  /**
   * Thrown when a <code>Configurable</code> component cannot be configured
   * properly, or if a value cannot be retrieved properly.
   *
   * @author <a href="mailto:scoobie@betaversion.org">Federico Barbieri</a>
   * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
   * @author <a href="mailto:fumagalli@exoffice.com">Pierpaolo Fumagalli</a>
   * @version CVS $Revision: 1.1 $ $Date: 2000/07/14 16:44:52 $
   */
  public class ConfigurationException extends RuntimeException {
  
      /** The current configuration */
      private Configuration configuration=null;
  
      /**
       * Construct a new <code>ConfigurationException</code> instance.
       *
       * @param message The detail message for this exception (mandatory).
       * @param conf The configuration element.
       */
      public ConfigurationException(String message, Configuration conf) {
          super(message);
          this.configuration=conf;
      }
  
      /**
       * Get the <code>Configuration</code> element.
       *
       * @returns <code>Configuration</code> element associated, or
       *          <code>null</code> if there is none.
       */
      public Configuration getConfiguration() {
          return (this.configuration);
      }
  
      /**
       * Return this <code>ConfigurationException</code> (if possible with
       * location information).
       */
      public String getMessage() {
          String msg=super.getMessage();
  
          if (this.configuration!=null) {
              msg = msg + " @ " + this.configuration.getLocation();
          }
  
          return(msg);
      }
  }
  
  
  
  1.1                  jakarta-slide/src/share/org/apache/slide/util/conf/ConfigurationImpl.java
  
  Index: ConfigurationImpl.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  package org.apache.slide.util.conf;
  
  import java.util.Enumeration;
  import java.util.Hashtable;
  import java.util.Vector;
  import java.util.NoSuchElementException;
  
  /**
   * This is the default <code>Configuration</code> implementation.
   *
   * @author <a href="mailto:scoobie@betaversion.org">Federico Barbieri</a>
   *         (Betaversion Productions)
   * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
   *         (Apache Software Foundation)
   * @author <a href="mailto:fumagalli@exoffice.com">Pierpaolo Fumagalli</a>
   *         (Apache Software Foundation, Exoffice Technologies)
   * @version CVS $Revision: 1.1 $ $Date: 2000/07/14 16:44:52 $
   */
  public class ConfigurationImpl extends AbstractConfiguration {
  
      /** The configuration attributes table. */
      private Hashtable attributes=new Hashtable();
      /** The configuration children list grouped by name. */
      private Hashtable children=new Hashtable();
      /** The configuration element name. */
      private String name=null;
      /** The configuration element value. */
      private String value=null;
  
      /**
       * Create a new <code>ConfigurationImpl</code> instance.
       */
      protected ConfigurationImpl() {
          super();
      }
  
      /**
       * Create a new <code>ConfigurationImpl</code> instance.
       */
      protected ConfigurationImpl(String name) {
          super();
          this.name=name;
      }
  
      /**
       * Create a new <code>ConfigurationImpl</code> instance.
       */
      protected ConfigurationImpl(String name, String source, int line) {
          super(source,line);
          this.name=name;
      }
  
      /**
       * Returns the name of this configuration element.
       */
  	public String getName() {
  	    return(this.name);
  	}
  
      /**
       * Returns the value of the configuration element as a <code>String</code>.
       *
       * @exception ConfigurationException If the value is not present.
       */
  	public String getValue()
  	throws ConfigurationException {
  	    if (this.value!=null) return(this.value);
          throw new ConfigurationException("No value is associated with the "+
              "configuration element \""+this.getName()+"\"", this);
      }
  
      /**
       * Returns the value of the attribute specified by its name as a
       * <code>String</code>.
       *
       * @exception ConfigurationException If the attribute is not present.
       */
  	public String getAttribute(String name)
  	throws ConfigurationException {
  	    String value=(String)this.attributes.get(name);
  	    if (value!=null) return(value);
          throw new ConfigurationException("No attribute named \""+name+"\" is "+
              "associated with the configuration element \""+this.getName()+"\"",
              this);
      }
  
      /**
       * Return the first <code>Configuration</code> object child of this
       * associated with the given name or <b>null</b>.
       *
       * @param name The name of the required child <code>Configuration</code>.
       */
      public Configuration getConfiguration(String name) {
  	    Vector v=(Vector)this.children.get(name);
          if ((v!=null) && (v.size()>0)) return((Configuration)v.firstElement());
          return(null);
  	}
  
      /**
       * Return an <code>Enumeration</code> of <code>Configuration</code> objects
       * children of this associated with the given name.
       * <br>
       * The returned <code>Enumeration</code> may be empty.
       *
       * @param name The name of the required children <code>Configuration</code>.
       */
  	public Enumeration getConfigurations(String name) {
  	    Vector v=(Vector)this.children.get(name);
  	    if (v==null) return(new EmptyEnumerationImpl());
  	    else return(v.elements());
      }
  
      /**
       * Append data to the value of this configuration element.
       */
      protected void appendValueData(String value) {
          if (this.value==null) this.value=value;
          else this.value=this.value+value;
      }
  
      /**
       * Add an attribute to this configuration element, returning its old
       * value or <b>null</b>.
       */
      protected String addAttribute(String name, String value) {
          return((String)this.attributes.put(name,value));
      }
  
      /**
       * Add a child <code>Configuration</code> to this configuration element.
       */
      public void addConfiguration(Configuration conf) {
          String name=conf.getName();
          Vector v=(Vector)this.children.get(name);
          if (v==null) {
              v=new Vector();
              this.children.put(name,v);
          }
          v.addElement(conf);
      }
  
      /** An empty <code>Enumeration</code> implementation. */
      private class EmptyEnumerationImpl implements Enumeration {
          /** Tests if this enumeration contains more elements. */
          public boolean hasMoreElements() {
              return(false);
          }
  
          /** Returns the next element of this enumeration. */
          public Object nextElement() {
              throw new NoSuchElementException();
          }
      }
  }
  
  
  
  1.1                  jakarta-slide/src/share/org/apache/slide/util/conf/Element.java
  
  Index: Element.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
   
  package org.apache.slide.util.conf;
  
  import java.util.*;
  
  /**
   * @author <a href="mailto:scoobie@betaverion.org">Federico Barbieri</a>
   */
  public class Element {
  
      private String name;
      private Hashtable attributes;
      private Vector children;
      private String data;
      private String comment;
      private Element parent;
      
      public Element getParent() {
          return parent;
      }
      
      public String getName() {
          return name;
      }
  
      public void setName(String s) {
          name = new String(s);
      }
  
      public boolean hasAttributes() {
          return !attributes.isEmpty();
      }
  
      public Enumeration getAttributeNames() {
          return attributes.keys();
      }
  
      public String getAttributeValue(String s) {
          return (String) attributes.get(s);
      }
  
      public void setAttribute(String s, String s1) {
          attributes.put(s, s1);
      }
  
      public void removeAttribute(String s) {
          attributes.remove(s);
      }
  
      public void clearAttributes() {
          attributes.clear();
      }
  
      public Enumeration getChildren() {
          return children.elements();
      }
  
      public Enumeration getChildren(String s) {
          Vector vector = new Vector();
          for(Enumeration enumeration = getChildren(); enumeration.hasMoreElements();) {
              Element element = (Element) enumeration.nextElement();
              if(element.getName().equals(s))
                  vector.addElement(element);
          }
          return vector.elements();
      }
  
      public Element getChild(String name) {
          Enumeration enumeration = getChildren(name);
          if (!enumeration.hasMoreElements()) {
              return null;
          }
          return (Element) enumeration.nextElement();
      }
  
      public boolean hasChildren() {
          return !children.isEmpty();
      }
  
      public int countChildren() {
          return children.size();
      }
  
      public void addChild(Element element)
      throws NullPointerException {
          if(element == null) {
              throw new NullPointerException("Child cannot be null");
          }
          children.addElement(element);
      }
  
      public void clearChildren() {
          children.removeAllElements();
      }
  
      public String getData() {
          return data;
      }
  
      public void setData(Object s) {
          data = new String(s.toString()).trim();
      }
  
      public void clearData() {
          data = new String();
      }
  
      public String getComment() {
          return comment;
      }
  
      public void setComment(String s) {
          comment = new String(s);
      }
  
      public void clearComment() {
          comment = new String();
      }
  
      public Element() {
          this("", null);
      }
  
      public Element(String s, Element parent) {
          this.parent = parent;
          name = new String(s);
          attributes = new Hashtable();
          children = new Vector();
          data = new String();
          comment = new String();
      }
      
      public String toString() {
          StringBuffer buffer = new StringBuffer();
          buffer.append("Name=" + name);
          buffer.append(" Data=" + data);
          for (Enumeration e = getChildren(); e.hasMoreElements(); ) {
              Element el = (Element) e.nextElement();
              buffer.append("  " + el.toString());
          }
          return buffer.toString();
      }
  }
  
  
  
  1.1                  jakarta-slide/src/share/org/apache/slide/util/conf/Populate.java
  
  Index: Populate.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
   
  package org.apache.slide.util.conf;
  
  import java.io.*;
  import org.xml.sax.*;
  import org.xml.sax.helpers.*;
  
  /**
   * @author <a href="mailto:scoobie@betaverion.org">Federico Barbieri</a>
   * @author <a href="mailto:arkin@exoffice.com>">Assaf Arkin</a>
   */
  public class Populate implements DocumentHandler, ErrorHandler {
      
      private Element    _root;
      private Element    _current;
      private Locator     _locator;
  
      public Element load(InputSource is, Parser parser)
      throws SAXException, IOException, ConfigurationException {
          parser.setDocumentHandler(this);
          parser.parse(is);
          return _root;
      }
      
      
      /**
       * Called to reset this object so it can be used a second time.
       */
      public void reset() {
          _root = null;
      }
      
      
      public void startDocument()
      throws SAXParseException {
          // Starting a new document with the same populate object. This object is not
          // reusable with a reset.
          if  ( _root != null ) 
              throw new SAXParseException( "Cannot start processing a new document without a reset", _locator );
      }
      
      
      public void endDocument()
          throws SAXParseException
      {
          // Not all elements have been closed, but end of document has been reached.
          // This should never happen.
          if ( _current != null )
              throw new SAXParseException( "Not all elements have been closed at end of document.", _locator );
      }
      
      
      public void startElement( String name, AttributeList attr ) {
          int     i;
          Element parent;
          
          // If this is the root element, create the first element, else
          // create a child for the current element and process it.
          if ( _current == null ) {
              _current = new Element( name, null );
              _root = _current;
          } else {
              parent = _current;
              _current = new Element( name, parent );
              parent.addChild( _current );
          }
          // Set the element's name and the attributes one by one, so the
          // object tree will be a reflection of the XML document.
          _current.setName( name );
          for ( i = 0 ; i < attr.getLength() ; ++i )
              _current.setAttribute( attr.getName( i ), attr.getValue( i ) );
      }
      
      
      /**
       * Closing the element. This is the place to check special conditions about the
       * element content. If not, just make the parent element the current element.
       */
      public void endElement( String name )
      throws SAXParseException {
          // Attempt to close an element when the root element has already been
          // closed. Should never happen.
          if ( _current == null )
              throw new SAXParseException( "Attempt to close the element " + name +
                  " when root element is already closed.", _locator );
          // Attempt to close one element when a different element is open and
          // waiting to be closed. Should never happen.
          if ( ! _current.getName().equals( name ) )
              throw new SAXParseException( "Attempt to close the element " + name +
                  " when the element " + _current.getName() + " should be closed.", _locator );
          // All we have to do is go back to the parent.
          _current = _current.getParent();
      }
      
      
      /**
       * Called when there is character data (text) inside an element. Will accumulate
       * all the character data and store it inside the object data.
       */
      public void characters( char[] ch, int start, int length )
          throws SAXParseException {
          Object          data;
          StringBuffer    buf;
          
          if ( ch == null || length == 0 ) return;
          // Attempt to place character before or after the root element.
          // Should never happen.
          if ( _current == null )
              throw new SAXParseException( "Attempt to place character before or after the root element.", _locator );
  
          // I am assuming that initially data may be just an empty string, or a null.
          // I am assuming that multiple calls to character can occur inside the
          // element and that all the character data should be collected as one.
          data = _current.getData();
          if ( data == null || ! ( data instanceof String ) || ( (String) data ).length() == 0 )
              _current.setData( new String( ch, start, length ) );
          else {
              buf = new StringBuffer( (String) data );
              buf.append( ch, start, length );
              _current.setData( buf.toString() );
          }
      }
       
      
      /**
       * Ignoreable whitespace is just space inside an element that only contains
       * other elements. We can safely ignore it.
       */
      public void ignorableWhitespace( char[] ch, int start, int length ) {
      }
      
      
      public void processingInstruction( String target, String pi ) {
      }
      
      
      /**
       * We can store the locate just to know where an error occurs.
       */
      public void setDocumentLocator( Locator locator ) {
          _locator = locator;
      }
      
      
      public void error( SAXParseException except ) {
          System.out.println( except.getMessage() );
      }
  
      public void fatalError( SAXParseException except ) {
          System.out.println( except.getMessage() );
      }
  
      public void warning( SAXParseException except ) {
          System.out.println( except.getMessage() );
      }
  }
  
  
  
  1.1                  jakarta-slide/src/share/org/apache/slide/util/conf/SAXConfigurationBuilder.java
  
  Index: SAXConfigurationBuilder.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  package org.apache.slide.util.conf;
  
  import java.util.Enumeration;
  import java.util.Hashtable;
  import java.util.Vector;
  import org.xml.sax.Attributes;
  import org.xml.sax.ContentHandler;
  import org.xml.sax.Locator;
  import org.xml.sax.SAXException;
  import org.xml.sax.ext.LexicalHandler;
  
  /**
   * This utility class will create a <code>Configuration</code> tree from an
   * XML file parsed with a SAX version 2 parser.
   *
   * @author <a href="mailto:fumagalli@exoffice.com">Pierpaolo Fumagalli</a>
   *         (Apache Software Foundation, Exoffice Technologies)
   * @version CVS $Revision: 1.1 $ $Date: 2000/07/14 16:44:52 $
   */
  public class SAXConfigurationBuilder implements ContentHandler {
  
      /** The current Locator. */
      private Locator locator=null;
      /** The stack of ConfigurationImpl object. */
      private Vector stack=new Vector();
      /** The table of namespaces prefix-Namespace mapping. */
      private Hashtable namespaces=new Hashtable();
      /** The name of the root configuration. */
      private Configuration root=null;
  
      /**
       * Construct a new <code>SAXConfigurationBuilder</code> instance.
       */
      public SAXConfigurationBuilder() {
          super();
          // Add the default namespace declaration
          Namespace ns=new Namespace();
          ns.uri="";
          ns.previousUri=ns;
          this.namespaces.put("",ns);
      }
  
      /**
       * Return the parsed configuration tree.
       */
      public Configuration getConfiguration() {
          return(this.root);
      }
  
      /**
       * Receive an object for locating the origin of SAX document events.
       */
      public void setDocumentLocator(Locator locator) {
          this.locator=locator;
      }
  
      /**
       * Receive notification of the beginning of a document.
       */
      public void startDocument()
      throws SAXException {
          this.stack.clear();
          this.stack.addElement(new ConfigurationImpl(""));
      }
  
      /**
       * Receive notification of the end of a document.
       */
      public void endDocument()
      throws SAXException {
      }
  
      /**
       * Begin the scope of a prefix-URI Namespace mapping.
       */
      public void startPrefixMapping(String p, String uri)
      throws SAXException {
          Namespace ns=new Namespace();
          ns.uri=uri;
          ns.previousUri=(Namespace)this.namespaces.get(p);
          this.namespaces.put(p,ns);
      }
  
      /**
       * End the scope of a prefix-URI mapping.
       */
      public void endPrefixMapping(String p)
      throws SAXException {
          Namespace ns=(Namespace)this.namespaces.remove(p);
          if (ns==null) throw new SAXException("Prefix '"+p+"' never declared");
          if (ns.previousUri!=null) this.namespaces.put(p,ns.previousUri);
      }
  
      /** Return an element/attribute raw name. */
      private String resolve(String uri, String loc, String raw)
      throws SAXException {
          if (raw.length()>0) return(raw);
          if (loc.length()==0) throw new SAXException("No local name found");
          Enumeration e=this.namespaces.keys();
          while (e.hasMoreElements()) {
              String prefix=(String)e.nextElement();
              Namespace ns=(Namespace)this.namespaces.get(prefix);
              if (uri.equals(ns.uri)) {
                  if (prefix.length()==0) raw=loc;
                  else raw=prefix+":"+loc;
                  break;
              }
          }
          if (raw.length()==0) throw new SAXException("Cannot get raw name");
          return(raw);
      }
  
      /**
       * Receive notification of the beginning of an element.
       */
      public void startElement(String uri, String loc, String raw, Attributes a)
      throws SAXException {
          String name=this.resolve(uri,loc,raw);
          Hashtable t=new Hashtable();
          // Process the attributes
          for (int x=0; x<a.getLength(); x++) {
              String aname=resolve(a.getURI(x),a.getLocalName(x),a.getQName(x));
              t.put(aname,a.getValue(x));
          }
          this.startElement(name,t);
      }
  
      /** Receive notification of the beginning of an element. */
      private void startElement(String name, Hashtable t)
      throws SAXException {
          ConfigurationImpl conf=null;
          if (this.locator!=null) {
              String file=this.locator.getSystemId();
              int line=this.locator.getLineNumber();
              conf=new ConfigurationImpl(name,file,line);
          } else conf=new ConfigurationImpl(name);
          // Process the attributes
          Enumeration e=t.keys();
          while (e.hasMoreElements()) {
              String anam=(String)e.nextElement();
              String aval=(String)t.get(anam);
              conf.addAttribute(anam,aval);
          }
          // Check if this is the first root configuration element
          if (this.root==null) this.root=conf;
          // Add this element to the stack
          ConfigurationImpl p=(ConfigurationImpl)this.stack.lastElement();
          p.addConfiguration(conf);
          this.stack.addElement(conf);
      }
  
      /**
       * Receive notification of the end of an element.
       */
      public void endElement(String uri, String loc, String raw)
      throws SAXException {
          // Keep resolution so that we can implement proper element
          // closing later.
          // String res = this.resolve(uri,loc,raw);
          // NOTE: (PF) Should we check for proper element closing????
          this.stack.setSize(this.stack.size()-1);
      }
  
      /**
       * Receive notification of character data.
       */
      public void characters(char ch[], int start, int len) {
          ConfigurationImpl c=(ConfigurationImpl)this.stack.lastElement();
          c.appendValueData(new String(ch,start,len));
      }
  
      /**
       * Receive notification of ignorable whitespace in element content.
       */
      public void ignorableWhitespace(char ch[], int start, int len) {
      }
  
      /**
       * Receive notification of a processing instruction.
       */
      public void processingInstruction(String target, String data) {
      }
  
      /**
       * Receive notification of a skipped entity.
       */
      public void skippedEntity(String name) {
      }
  
      /** A <code>String</code> chain for namespaces declaration */
      private static class Namespace {
          /** The current namespace URI */
          public String uri=null;
          /** The previous namespace URIs */
          public Namespace previousUri=null;
      }
  }