You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avalon.apache.org by do...@apache.org on 2001/02/24 05:00:14 UTC
cvs commit: jakarta-avalon/src/java/org/apache/avalon/configuration AbstractConfiguration.java Configurable.java Configuration.java ConfigurationBuilder.java ConfigurationException.java DefaultConfiguration.java DefaultConfigurationBuilder.java Reconfigurable.java SAXConfigurationHandler.java
donaldp 01/02/23 20:00:14
Added: src/java/org/apache/avalon/configuration
AbstractConfiguration.java Configurable.java
Configuration.java ConfigurationBuilder.java
ConfigurationException.java
DefaultConfiguration.java
DefaultConfigurationBuilder.java
Reconfigurable.java SAXConfigurationHandler.java
Log:
Rechecked in avalon stuff
Revision Changes Path
1.1 jakarta-avalon/src/java/org/apache/avalon/configuration/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.avalon.configuration;
import java.util.Iterator;
/**
* This is an abstract <code>Configuration</code> implementation that deals
* with methods that can be abstracted away from underlying implementations.
*
* @author <a href="mailto:fede@apache.org">Federico Barbieri</a>
* @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
* @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
* @author <a href="mailto:fumagalli@exoffice.com">Pierpaolo Fumagalli</a>
* @version CVS $Revision: 1.1 $ $Date: 2001/02/24 04:00:14 $
*/
public abstract class AbstractConfiguration
implements Configuration
{
/**
* Returns the value of the configuration element as an <code>int</code>.
*/
public int getValueAsInt()
throws ConfigurationException
{
final String value = 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( final Exception nfe )
{
throw
new ConfigurationException( "Cannot parse the value of the configuration " +
"element \"" + getName() + "\" as an integer" );
}
}
/**
* Returns the value of the configuration element as an <code>int</code>.
*/
public int getValueAsInt( final int defaultValue )
{
try
{
return getValueAsInt();
}
catch( final ConfigurationException ce )
{
return defaultValue;
}
}
/**
* Returns the value of the configuration element as a <code>long</code>.
*/
public long getValueAsLong()
throws ConfigurationException
{
final String value = 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( final Exception nfe )
{
throw new ConfigurationException( "Cannot parse the value of the " +
"configuration element \"" + getName() +
"\" as a long" );
}
}
/**
* Returns the value of the configuration element as a <code>long</code>.
*/
public long getValueAsLong( final long defaultValue )
{
try
{
return getValueAsLong();
}
catch( final ConfigurationException ce )
{
return defaultValue;
}
}
/**
* Returns the value of the configuration element as a <code>float</code>.
*/
public float getValueAsFloat()
throws ConfigurationException
{
final String value = getValue();
try
{
return Float.parseFloat( value );
}
catch( final Exception nfe )
{
throw new ConfigurationException( "Cannot parse the value of the " +
"configuration element \"" + getName() +
"\" as a float" );
}
}
/**
* Returns the value of the configuration element as a <code>float</code>.
*/
public float getValueAsFloat( final float defaultValue )
{
try
{
return getValueAsFloat();
}
catch( final ConfigurationException ce )
{
return(defaultValue);
}
}
/**
* Returns the value of the configuration element as a <code>boolean</code>.
*/
public boolean getValueAsBoolean()
throws ConfigurationException
{
final String value = getValue();
if( value.equals( "true" ) ) return true;
else if( value.equals( "false" ) ) return false;
else
{
throw new ConfigurationException( "Cannot parse the value of the " +
"configuration element \"" +
getName() + "\" as a boolean" );
}
}
/**
* Returns the value of the configuration element as a <code>boolean</code>.
*/
public boolean getValueAsBoolean( final boolean defaultValue )
{
try
{
return getValueAsBoolean();
}
catch( final ConfigurationException ce )
{
return defaultValue;
}
}
/**
* Returns the value of the configuration element as a <code>String</code>.
*/
public String getValue( final String defaultValue )
{
try
{
return getValue();
}
catch( final ConfigurationException ce )
{
return defaultValue;
}
}
/**
* Returns the value of the attribute specified by its name as an
* <code>int</code>.
*/
public int getAttributeAsInt( final String name )
throws ConfigurationException
{
final String value = 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( final Exception nfe )
{
throw new ConfigurationException( "Cannot parse the value of the attribute \"" +
name + "\" of the configuration element \"" +
getName() + "\" as an integer" );
}
}
/**
* Returns the value of the attribute specified by its name as an
* <code>int</code>.
*/
public int getAttributeAsInt( final String name, final int defaultValue )
{
try
{
return getAttributeAsInt( name );
}
catch( final ConfigurationException ce )
{
return defaultValue;
}
}
/**
* Returns the value of the attribute specified by its name as a
* <code>long</code>.
*/
public long getAttributeAsLong( final String name )
throws ConfigurationException
{
final String value = 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( final Exception nfe )
{
throw new ConfigurationException( "Cannot parse the value of the attribute \"" +
name + "\" of the configuration element \"" +
getName() + "\" as a long" );
}
}
/**
* Returns the value of the attribute specified by its name as a
* <code>long</code>.
*/
public long getAttributeAsLong( final String name, final long defaultValue )
{
try
{
return getAttributeAsLong( name );
}
catch( final ConfigurationException ce )
{
return defaultValue;
}
}
/**
* Returns the value of the attribute specified by its name as a
* <code>float</code>.
*/
public float getAttributeAsFloat( final String name )
throws ConfigurationException
{
final String value = getAttribute( name );
try
{
return Float.parseFloat( value );
}
catch( final Exception e )
{
throw new ConfigurationException( "Cannot parse the value of the attribute \"" +
name + "\" of the configuration element \"" +
getName() + "\" as a float" );
}
}
/**
* Returns the value of the attribute specified by its name as a
* <code>float</code>.
*/
public float getAttributeAsFloat( final String name, final float defaultValue )
{
try
{
return getAttributeAsFloat( name );
}
catch( final ConfigurationException ce )
{
return defaultValue;
}
}
/**
* Returns the value of the attribute specified by its name as a
* <code>boolean</code>.
*/
public boolean getAttributeAsBoolean( final String name )
throws ConfigurationException
{
final String value = getAttribute( name );
if( value.equals( "true" ) ) return true;
else if( value.equals( "false" ) ) return false;
else
{
throw new ConfigurationException( "Cannot parse the value of the attribute \"" +
name + "\" of the configuration element \"" +
getName() + "\" as a boolean" );
}
}
/**
* Returns the value of the attribute specified by its name as a
* <code>boolean</code>.
*/
public boolean getAttributeAsBoolean( final String name, final boolean defaultValue )
{
try
{
return getAttributeAsBoolean( name );
}
catch( final ConfigurationException ce )
{
return defaultValue;
}
}
/**
* Returns the value of the attribute specified by its name as a
* <code>String</code>.
*/
public String getAttribute( final String name, final String defaultValue )
{
try
{
return getAttribute( name );
}
catch( final ConfigurationException ce )
{
return defaultValue;
}
}
/**
* Return the first <code>Configuration</code> object child of this
* associated with the given name.
*/
public Configuration getChild( final String name )
{
return getChild( name, true );
}
/**
* Return the first <code>Configuration</code> object child of this
* associated with the given name.
*/
public Configuration getChild( final String name, final boolean createNew )
{
final Configuration[] children = getChildren( name );
if( children.length > 0 )
{
return children[ 0 ];
}
else
{
if( createNew )
{
return new DefaultConfiguration( name, "-" );
}
else
{
return null;
}
}
}
}
1.1 jakarta-avalon/src/java/org/apache/avalon/configuration/Configurable.java
Index: Configurable.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.avalon.configuration;
/**
* This interface should be implemented by classes that need to be
* configured with custom parameters before initialization.
* <br />
*
* The contract surrounding a <code>Configurable</code> is that the
* instantiating entity must call the <code>configure</code>
* method before it is valid. The <code>configure</code> method
* must be called after the constructor, and before any other method.
*
* @author <a href="mailto:fede@apache.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="mailto:bloritsch@apache.org">Berin Loritsch</a>
* @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
*/
public interface Configurable
{
/**
* Pass the <code>Configuration</code> to the <code>Configurable</code>
* class. This method must always be called after the constructor
* and before any other method.
*
* @param configuration the class configurations.
*/
void configure( Configuration configuration )
throws ConfigurationException;
}
1.1 jakarta-avalon/src/java/org/apache/avalon/configuration/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.avalon.configuration;
/**
* <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.
*
* @author <a href="mailto:fede@apache.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="mailto:bloritsch@apache.org">Berin Loritsch</a>
* @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
*/
public interface Configuration
{
/**
* Return the name of the node.
*
* @post getName() != null
*
* @return name of the <code>Configuration</code> node.
*/
String getName();
/**
* Return a string describing location of Configuration.
* Location can be different for different mediums (ie "file:line" for normal XML files or
* "table:primary-key" for DB based configurations);
*
* @return a string describing location of Configuration
*/
String getLocation();
/**
* 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.
* @return Configuration
*/
Configuration getChild( String child );
/**
* 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.
* @return Configuration
*/
Configuration getChild( String child, boolean createNew );
/**
* Return an <code>Iterator</code> of <code>Configuration<code>
* elements containing all node children.
*
* @return The child nodes with name
*/
Configuration[] getChildren();
/**
* Return an <code>Iterator</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.
* @return The child nodes with name
*/
Configuration[] getChildren( String name );
/**
* Return an array of all attribute names.
*/
String[] getAttributeNames();
/**
* Return the value of specified attribute.
*
* @pre paramName != null
* @post getAttribute != null
*
* @param paramName The name of the parameter you ask the value of.
* @return String value of attribute.
* @exception ConfigurationException If no attribute with that name exists.
*/
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.
* @return int value of attribute
* @exception ConfigurationException If no parameter with that name exists.
* or if conversion to <code>int</code> fails.
*/
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.
* @return long value of attribute
* @exception ConfigurationException If no parameter with that name exists.
* or if conversion to <code>long</code> fails.
*/
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.
* @return float value of attribute
* @exception ConfigurationException If no parameter with that name exists.
* or if conversion to <code>float</code> fails.
*/
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.
* @return boolean value of attribute
* @exception ConfigurationException If no parameter with that name exists.
* or if conversion to <code>boolean</code> fails.
*/
boolean getAttributeAsBoolean( String paramName ) throws ConfigurationException;
/**
* Return the <code>String</code> value of the node.
*
* @post getValue() != null
*
* @return the value of the node.
*/
String getValue() throws ConfigurationException;
/**
* 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.
*/
int getValueAsInt() throws ConfigurationException;
/**
* Return the <code>float</code> value of the node.
*
* @post getValueAsFloat() != null
*
* @return the value of the node.
* @exception ConfigurationException If conversion to <code>float</code> fails.
*/
float getValueAsFloat() throws ConfigurationException;
/**
* Return the <code>boolean</code> value of the node.
*
* @post getValueAsBoolean() != null
*
* @return the value of the node.
* @exception ConfigurationException If conversion to <code>boolean</code> fails.
*/
boolean getValueAsBoolean() throws ConfigurationException;
/**
* Return the <code>long</code> value of the node.<br>
*
* @post getValueAsLong() != null
*
* @return the value of the node.
* @exception ConfigurationException If conversion to <code>long</code> fails.
*/
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.
* @return String value of the <code>Configuration</code>, or default
* if none specified.
*/
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.
* @return int value of the <code>Configuration</code>, or default
* if none specified.
*/
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.
* @return long value of the <code>Configuration</code>, or default
* if none specified.
*/
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.
* @return float value of the <code>Configuration</code>, or default
* if none specified.
*/
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.
* @return boolean value of the <code>Configuration</code>, or default
* if none specified.
*/
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.
* @return String value of attribute. It will return the default
* value if the named attribute does not exist, or if
* the value is not set.
*/
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.
* @return int value of attribute. It will return the default
* value if the named attribute does not exist, or if
* the value is not set.
*/
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.
* @return long value of attribute. It will return the default
* value if the named attribute does not exist, or if
* the value is not set.
*/
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.
* @return float value of attribute. It will return the default
* value if the named attribute does not exist, or if
* the value is not set.
*/
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.
* @return boolean value of attribute. It will return the default
* value if the named attribute does not exist, or if
* the value is not set.
*/
boolean getAttributeAsBoolean( String name, boolean defaultValue );
}
1.1 jakarta-avalon/src/java/org/apache/avalon/configuration/ConfigurationBuilder.java
Index: ConfigurationBuilder.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.avalon.configuration;
import java.io.IOException;
import org.xml.sax.SAXException;
/**
* The interface implemented to build configurations.
*
* @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
*/
public interface ConfigurationBuilder
{
Configuration build( String resource )
throws SAXException, IOException, ConfigurationException;
}
1.1 jakarta-avalon/src/java/org/apache/avalon/configuration/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.avalon.configuration;
import org.apache.avalon.CascadingException;
/**
* Thrown when a <code>Configurable</code> component cannot be configured
* properly, or if a value cannot be retrieved properly.
*
* @author <a href="mailto:fede@apache.org">Federico Barbieri</a>
* @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
* @author <a href="mailto:fumagalli@exoffice.com">Pierpaolo Fumagalli</a>
*/
public final class ConfigurationException
extends CascadingException
{
/**
* Construct a new <code>ConfigurationException</code> instance.
*
* @param message The detail message for this exception.
*/
public ConfigurationException( final String message )
{
this( message, null );
}
/**
* Construct a new <code>ConfigurationException</code> instance.
*
* @param message The detail message for this exception.
* @param throwable the root cause of the exception
*/
public ConfigurationException( final String message, final Throwable throwable )
{
super( message, throwable );
}
}
1.1 jakarta-avalon/src/java/org/apache/avalon/configuration/DefaultConfiguration.java
Index: DefaultConfiguration.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.avalon.configuration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
/**
* This is the default <code>Configuration</code> implementation.
*
* @author <a href="mailto:fede@apache.org">Federico Barbieri</a>
* @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
* @author <a href="mailto:fumagalli@exoffice.com">Pierpaolo Fumagalli</a>
* @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
*/
public class DefaultConfiguration
extends AbstractConfiguration
{
protected final static Configuration[] EMPTY_ARRAY = new Configuration[ 0 ];
protected final String m_name;
protected final String m_location;
protected HashMap m_attributes;
protected ArrayList m_children;
protected String m_value;
/**
* Create a new <code>DefaultConfiguration</code> instance.
*/
public DefaultConfiguration( final String name, final String location )
{
m_name = name;
m_location = location;
}
/**
* Returns the name of this configuration element.
*/
public String getName()
{
return m_name;
}
/**
* Returns a description of location of element.
*/
public String getLocation()
{
return m_location;
}
/**
* 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( null != m_value ) return m_value;
else
{
throw new ConfigurationException( "No value is associated with the "+
"configuration element \"" + getName() + "\"" );
}
}
/**
* Return an array of all attribute names.
*/
public String[] getAttributeNames()
{
if( null == m_attributes ) return new String[ 0 ];
else return (String[])m_attributes.keySet().toArray( new String[ 0 ] );
}
/**
* Return an <code>Iterator</code> of <code>Configuration<code>
* elements containing all node children.
*
* @return The child nodes with name
*/
public Configuration[] getChildren()
{
if( null == m_children ) return new Configuration[ 0 ];
else return (Configuration[])m_children.toArray( new Configuration[ 0 ] );
}
/**
* 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( final String name )
throws ConfigurationException
{
final String value =
(null != m_attributes) ? (String)m_attributes.get( name ) : null;
if( null != value ) return value;
else
{
throw new ConfigurationException( "No attribute named \"" + name + "\" is " +
"associated with the configuration element \"" +
getName() + "\"" );
}
}
/**
* Return the first <code>Configuration</code> object child of this
* associated with the given name.
*/
public Configuration getChild( final String name, final boolean createNew )
{
if( null != m_children )
{
final int size = m_children.size();
for( int i = 0; i < size; i++ )
{
final Configuration configuration = (Configuration)m_children.get( i );
if( name.equals( configuration.getName() ) )
{
return configuration;
}
}
}
if( createNew )
{
return new DefaultConfiguration( name, "-" );
}
else
{
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 Configuration[] getChildren( final String name )
{
if( null == m_children ) return new Configuration[ 0 ];
else
{
final ArrayList children = new ArrayList();
final int size = m_children.size();
for( int i = 0; i < size; i++ )
{
final Configuration configuration = (Configuration)m_children.get( i );
if( name.equals( configuration.getName() ) )
{
children.add( configuration );
}
}
return (Configuration[])children.toArray( new Configuration[ 0 ] );
}
}
/**
* Append data to the value of this configuration element.
*/
public void appendValueData( final String value )
{
if( null == m_value )
{
m_value = value;
}
else
{
m_value = m_value + value;
}
}
/**
* Add an attribute to this configuration element, returning its old
* value or <b>null</b>.
*/
public String addAttribute( final String name, String value )
{
if( null == m_attributes ) m_attributes = new HashMap();
return (String) m_attributes.put( name, value );
}
/**
* Add a child <code>Configuration</code> to this configuration element.
*/
public void addChild( final Configuration configuration )
{
if( null == m_children )
{
m_children = new ArrayList();
}
m_children.add( configuration );
}
/**
* Return count of children.
*/
public int getChildCount()
{
if( null == m_children )
{
return 0;
}
return m_children.size();
}
}
1.1 jakarta-avalon/src/java/org/apache/avalon/configuration/DefaultConfigurationBuilder.java
Index: DefaultConfigurationBuilder.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.avalon.configuration;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* A SAXConfigurationBuilder builds configurations via SAX2 compliant parser.
*
* @author <a href="mailto:fede@apache.org">Federico Barbieri</a>
* @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
*/
public class DefaultConfigurationBuilder
implements ConfigurationBuilder
{
protected final static String DEFAULT_PARSER =
"org.apache.xerces.parsers.SAXParser";
protected final static String PARSER =
System.getProperty("org.xml.sax.parser", DEFAULT_PARSER );
protected SAXConfigurationHandler m_handler;
protected XMLReader m_parser;
public DefaultConfigurationBuilder()
{
this( PARSER );
}
public DefaultConfigurationBuilder( final String parserClass )
{
//yaya the bugs with some compilers and final variables ..
m_handler = getHandler();
try
{
m_parser = XMLReaderFactory.createXMLReader( parserClass );
//m_parser.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
m_parser.setContentHandler( m_handler );
m_parser.setErrorHandler( m_handler );
}
catch( final SAXException se )
{
throw new Error( "Unable to setup SAX parser" + se );
}
}
protected SAXConfigurationHandler getHandler()
{
return new SAXConfigurationHandler();
}
public Configuration build( final String resource )
throws SAXException, IOException, ConfigurationException
{
final InputStream input = new FileInputStream( resource );
try { return build( input ); }
finally
{
try { input.close(); }
catch( final IOException ioe ) {}
}
}
public Configuration build( final InputStream inputStream )
throws SAXException, IOException, ConfigurationException
{
final InputSource inputSource = new InputSource( inputStream );
return build( inputSource );
}
public Configuration build( final InputSource input )
throws SAXException, IOException, ConfigurationException
{
m_handler.clear();
m_parser.parse( input );
return m_handler.getConfiguration();
}
}
1.1 jakarta-avalon/src/java/org/apache/avalon/configuration/Reconfigurable.java
Index: Reconfigurable.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.avalon.configuration;
/**
* Extends Configurable to allow reconfiguration runtime.
*
* @author <a href="mailto:fede@apache.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="mailto:donaldp@apache.org">Peter Donald</a>
*/
public interface Reconfigurable
extends Configurable
{
void reconfigure( Configuration configuration ) throws ConfigurationException;
}
1.1 jakarta-avalon/src/java/org/apache/avalon/configuration/SAXConfigurationHandler.java
Index: SAXConfigurationHandler.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.avalon.configuration;
import java.io.IOException;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.ErrorHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
/**
* A SAXConfigurationHandler helps build Configurations out of sax events.
*
* @author <a href="mailto:fede@apache.org">Federico Barbieri</a>
* @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
*/
public class SAXConfigurationHandler
extends DefaultHandler
implements ErrorHandler
{
protected final ArrayList m_elements = new ArrayList();
protected Configuration m_configuration;
protected Locator m_locator;
public Configuration getConfiguration()
{
return m_configuration;
}
public void clear()
{
m_elements.clear();
m_locator = null;
}
public void setDocumentLocator( final Locator locator )
{
m_locator = locator;
}
public void characters( final char[] ch, int start, int end )
throws SAXException
{
final String value = (new String( ch, start, end )).trim();
if( value.equals( "" ) ) return;
final DefaultConfiguration configuration =
(DefaultConfiguration)m_elements.get( m_elements.size() - 1 );
if( 0 != configuration.getChildCount() )
{
throw new SAXException( "Not allowed to define mixed content in the " +
"element " + configuration.getName() + " at " +
configuration.getLocation() );
}
configuration.appendValueData( value );
}
public void endElement( final String namespaceURI,
final String localName,
final String rawName )
{
final int location = m_elements.size() - 1;
final Object object = m_elements.remove( location );
if( 0 == location )
{
m_configuration = (Configuration)object;
}
}
protected DefaultConfiguration createConfiguration( final String localName,
final String location )
{
return new DefaultConfiguration( localName, location );
}
public void startElement( final String namespaceURI,
final String localName,
final String rawName,
final Attributes attributes )
throws SAXException
{
final DefaultConfiguration configuration =
createConfiguration( localName, getLocationString() );
final int size = m_elements.size() - 1;
if( size > -1 )
{
final DefaultConfiguration parent =
(DefaultConfiguration)m_elements.get( size );
if( null != parent.getValue( null ) )
{
throw new SAXException( "Not allowed to define mixed content in the " +
"element " + parent.getName() + " at " +
parent.getLocation() );
}
parent.addChild( configuration );
}
m_elements.add( configuration );
final int attributesSize = attributes.getLength();
for( int i = 0; i < attributesSize; i++ )
{
final String name = attributes.getQName( i );
final String value = attributes.getValue( i );
configuration.addAttribute( name, value );
}
}
/**
* This just throws an exception on a parse error.
*/
public void error( final SAXParseException exception )
throws SAXException
{
throw exception;
}
/**
* This just throws an exception on a parse error.
*/
public void warning( final SAXParseException exception )
throws SAXException
{
throw exception;
}
/**
* This just throws an exception on a parse error.
*/
public void fatalError( final SAXParseException exception )
throws SAXException
{
throw exception;
}
protected String getLocationString()
{
if( null == m_locator ) return "Unknown";
else
{
return
m_locator.getSystemId() + ":" +
m_locator.getLineNumber() + ":" +
m_locator.getColumnNumber();
}
}
}