mcconnell    2002/06/13 00:11:40

  Modified:    merlin/src/java/org/apache/excalibur/configuration
                        ConfigurationUtil.java
  Log:
  more selection options
  
  Revision  Changes    Path
  1.3       +56 -2     
jakarta-avalon-excalibur/merlin/src/java/org/apache/excalibur/configuration/ConfigurationUtil.java
  
  Index: ConfigurationUtil.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-excalibur/merlin/src/java/org/apache/excalibur/configuration/ConfigurationUtil.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ConfigurationUtil.java    13 Jun 2002 06:54:57 -0000      1.2
  +++ ConfigurationUtil.java    13 Jun 2002 07:11:39 -0000      1.3
  @@ -13,6 +13,7 @@
   import java.util.Vector;
   
   import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
   
   /**
    * General utility supporting static operations for generating string
  @@ -75,16 +76,31 @@
       }
   
      /**
  -    * Return all occurance of a a configuration child containing the 
supplied attribute name.
  +    * Return all occurance of a configuration child containing the supplied 
attribute name.
       * @param config the configuration
       * @param element the name of child elements to select from the 
configuration
       * @param attribute the attribute name to filter
       * @param value the attribute value to match (null will match any 
attribute value)
       * @return an array of configuration instances matching the query
       */
  -    public Configuration[] match( Configuration config, String element, 
String attribute, String value )
  +    public Configuration[] match( Configuration config, String element, 
String attribute )
  +    {
  +        return match( config, element, attribute, null );
  +    }
  +
  +   /**
  +    * Return occurance of a configuration child containing the supplied 
attribute name and value.
  +    * @param config the configuration
  +    * @param element the name of child elements to select from the 
configuration
  +    * @param attribute the attribute name to filter
  +    * @param value the attribute value to match (null will match any 
attribute value)
  +    * @return an array of configuration instances matching the query
  +    */
  +    public Configuration[] match( 
  +      final Configuration config, final String element, final String 
attribute, final String value )
       {
           Vector vector = new Vector();
  +
           Configuration[] children = config.getChildren( element );
           for( int i=0; i<children.length; i++ )
           {
  @@ -99,6 +115,44 @@
               }
           }
           return (Configuration[]) vector.toArray( EMPTY_CONFS );
  +    }
  +
  +   /**
  +    * Return the first occurance of a configuration child containing the 
supplied attribute name and value.
  +    * @param config the configuration
  +    * @param element the name of child elements to select from the 
configuration
  +    * @param attribute the attribute name to filter
  +    * @param value the attribute value to match (null will match any 
attribute value)
  +    * @return a configuration instances matching the query
  +    * @exception ConfigurationException if no matching children 
  +    */
  +    public Configuration matchFirstOccurance( 
  +       Configuration config, String element, String attribute, String value )
  +       throws ConfigurationException
  +    {
  +        Configuration[] children = config.getChildren( element );
  +        for( int i=0; i<children.length; i++ )
  +        {
  +            String v = children[i].getAttribute( attribute, null );
  +            if( v != null )
  +            {
  +                if(( value == null ) || v.equals( value ))
  +                {
  +                    // it's a match
  +                    return children[i];
  +                }
  +            }
  +        }
  +        if( value == null )
  +        {
  +            throw new ConfigurationException("No match for element named '" 
+ element 
  +              + "' with an attribute name of '" + attribute + "'.");
  +        }
  +        else
  +        {
  +            throw new ConfigurationException("No match for element named '" 
+ element 
  +              + " with an attribute name of '" + attribute + "' and value '" 
+ value + "'.");
  +        }
       }
   }
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to