donaldp 01/03/15 02:53:58 Modified: src/compat/org/apache/avalon Parameters.java Log: Fix Parameters to work with Cocoon. Revision Changes Path 1.2 +323 -3 jakarta-avalon/src/compat/org/apache/avalon/Parameters.java Index: Parameters.java =================================================================== RCS file: /home/cvs/jakarta-avalon/src/compat/org/apache/avalon/Parameters.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- Parameters.java 2001/03/15 05:52:00 1.1 +++ Parameters.java 2001/03/15 10:53:58 1.2 @@ -7,12 +7,332 @@ */ package org.apache.avalon; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import org.apache.avalon.configuration.Configuration; +import org.apache.avalon.configuration.ConfigurationException; + /** * * @author <a href="mailto:[EMAIL PROTECTED]">Pierpaolo Fumagalli</a> - * @deprecated Use org.apache.avalon.configuration.Parameters instead + * @deprecated Use org.apache.avalon.configuration.Parameters instead. */ -public final class Parameters - extends org.apache.avalon.configuration.Parameters +public class Parameters { + protected HashMap m_parameters; + + /** + * Create a new <code>Parameters</code> instance. + */ + public Parameters() + { + m_parameters = new HashMap(); + } + + /** + * Set the <code>String</code> value of a specified parameter. + * <p /> + * If the specified value is <b>null</b> the parameter is removed. + * + * @return The previous value of the parameter or <b>null</b>. + */ + public String setParameter( final String name, final String value ) + { + if( null == name ) + { + return null; + } + + if( null == value ) + { + return (String)m_parameters.remove( name ); + } + + return (String)m_parameters.put( name, value ); + } + + /** + * Return an <code>Enumeration</code> view of all parameter names. + */ + public Iterator getParameterNames() + { + return m_parameters.keySet().iterator(); + } + + /** + * Check if the specified parameter can be retrieved. + */ + public boolean isParameter( final String name ) + { + return m_parameters.containsKey( name ); + } + + /** + * Retrieve the <code>String</code> value of the specified parameter. + * <p /> + * If the specified parameter cannot be found, <b>null</b> is returned. + */ + protected String getParameter( final String name ) + { + if( null == name ) + { + return null; + } + + return (String)m_parameters.get( name ); + } + + /** + * Retrieve the <code>String</code> value of the specified parameter. + * <p /> + * If the specified parameter cannot be found, <code>defaultValue</code> + * is returned. + */ + public String getParameter( final String name, final String defaultValue ) + { + final String value = getParameter( name ); + + if( null == value ) + { + return defaultValue; + } + else + { + return value; + } + } + + /** + * Retrieve the <code>int</code> value of the specified parameter. + * <p /> + * If the specified parameter cannot be found, <code>defaultValue</code> + * is returned. + */ + public int getParameterAsInteger( final String name, final int defaultValue ) + { + final String value = getParameter( name ); + + if( null == value ) + { + return defaultValue; + } + + 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 NumberFormatException nfe ) + { + return defaultValue; + } + } + + /** + * Retrieve the <code>long</code> value of the specified parameter. + * <p /> + * If the specified parameter cannot be found, <code>defaultValue</code> + * is returned. + */ + public long getParameterAsLong( final String name, final long defaultValue ) + { + final String value = getParameter( name ); + + if( null == value ) + { + return defaultValue; + } + + 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 Long.parseLong(value); + } + } + catch( final NumberFormatException nfe ) + { + return defaultValue; + } + } + + /** + * Retrieve the <code>float</code> value of the specified parameter. + * <p /> + * If the specified parameter cannot be found, <code>defaultValue</code> + * is returned. + */ + public float getParameterAsFloat( final String name, final float defaultValue ) + { + final String value = getParameter( name ); + + if( null == value ) + { + return defaultValue; + } + + try + { + return Float.parseFloat(value); + } + catch( final NumberFormatException nfe ) + { + return defaultValue; + } + } + + /** + * Retrieve the <code>boolean</code> value of the specified parameter. + * <p /> + * If the specified parameter cannot be found, <code>defaultValue</code> + * is returned. + */ + public boolean getParameterAsBoolean( final String name, final boolean defaultValue ) + { + final String value = getParameter( name ); + + if( null == value ) + { + return defaultValue; + } + + if( value.equalsIgnoreCase("true") ) + { + return true; + } + + if( value.equalsIgnoreCase("false") ) + { + return(false); + } + + return defaultValue; + } + + /** + * Merge parameters from another <code>Parameters</code> instance + * into this. + * + * @return This <code>Parameters</code> instance. + */ + public Parameters merge( final Parameters other ) + { + final Iterator names = other.getParameterNames(); + + while( names.hasNext() ) + { + final String name = (String) names.next(); + final String value = other.getParameter( name ); + + setParameter( name, value ); + } + + return this; + } + + /** + * Create a <code>Parameters</code> object from a <code>Configuration</code> + * object. + * @deprecated Will be removed when old config removed + */ + public static Parameters + fromConfiguration( final org.apache.avalon.Configuration configuration ) + throws org.apache.avalon.ConfigurationException + { + if( null == configuration ) + { + throw new org.apache.avalon. + ConfigurationException( "You cannot convert to parameters with " + + "a null Configuration"); + } + + final Iterator parameters = configuration.getChildren("parameter"); + final Parameters param = new Parameters(); + + while( parameters.hasNext() ) + { + try + { + final org.apache.avalon.Configuration child = + (org.apache.avalon.Configuration)parameters.next(); + final String name = child.getAttribute( "name" ); + final String value = child.getAttribute( "value" ); + param.setParameter( name, value ); + } + catch( final ClassCastException cce ) + { + // ignore this. Temporary work around until the Iterator + // is guaranteed to return Configuration values. Unfortunately + // there are problems with empty strings getting in there. + } + catch( final Exception e ) + { + throw new org.apache.avalon. + ConfigurationException( "Cannot process Configurable", e ); + } + } + + return param; + } + + /** + * Create a <code>Parameters</code> object from a <code>Configuration</code> + * object. + */ + public static Parameters fromConfiguration( final Configuration configuration ) + throws ConfigurationException + { + if( null == configuration ) + { + throw new ConfigurationException( "You cannot convert to parameters with " + + "a null Configuration" ); + } + + final Configuration[] parameters = configuration.getChildren( "parameter" ); + final Parameters param = new Parameters(); + + for (int i = 0; i < parameters.length; i++ ) + { + try + { + final String name = parameters[ i ].getAttribute( "name" ); + final String value = parameters[ i ].getAttribute( "value" ); + param.setParameter( name, value ); + } + catch( final Exception e ) + { + throw new ConfigurationException( "Cannot process Configurable", e ); + } + } + + return param; + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]