avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: avalon/src/java/org/apache/avalon/framework/configuration ConfigurationUtil.java
Date Mon, 07 Apr 2003 08:36:58 GMT
donaldp     2003/04/07 01:36:58

  Modified:    src/java/org/apache/avalon/framework/configuration
                        ConfigurationUtil.java
  Log:
  Add conversion DOM to configuration
  
  Revision  Changes    Path
  1.14      +59 -9     avalon/src/java/org/apache/avalon/framework/configuration/ConfigurationUtil.java
  
  Index: ConfigurationUtil.java
  ===================================================================
  RCS file: /home/cvs/avalon/src/java/org/apache/avalon/framework/configuration/ConfigurationUtil.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- ConfigurationUtil.java	28 Mar 2003 19:17:32 -0000	1.13
  +++ ConfigurationUtil.java	7 Apr 2003 08:36:58 -0000	1.14
  @@ -52,15 +52,21 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    */
  -
   package org.apache.avalon.framework.configuration;
   
  +import java.util.ArrayList;
  +import java.util.Arrays;
  +import java.util.Iterator;
   import javax.xml.parsers.DocumentBuilder;
   import javax.xml.parsers.DocumentBuilderFactory;
   import javax.xml.parsers.ParserConfigurationException;
   import org.w3c.dom.Document;
   import org.w3c.dom.Element;
   import org.w3c.dom.Text;
  +import org.w3c.dom.NamedNodeMap;
  +import org.w3c.dom.Node;
  +import org.w3c.dom.NodeList;
  +import org.w3c.dom.CharacterData;
   import java.util.ArrayList;
   import java.util.Arrays;
   import java.util.Iterator;
  @@ -83,6 +89,52 @@
       }
   
       /**
  +     * Convert a DOM Element tree into a configuration tree.
  +     *
  +     * @param element the DOM Element
  +     * @return the configuration object
  +     */
  +    public static Configuration toConfiguration( final Element element )
  +    {
  +        final DefaultConfiguration configuration =
  +            new DefaultConfiguration( element.getNodeName(), "dom-created" );
  +        final NamedNodeMap attributes = element.getAttributes();
  +        final int length = attributes.getLength();
  +        for( int i = 0; i < length; i++ )
  +        {
  +            final Node node = attributes.item( i );
  +            final String name = node.getNodeName();
  +            final String value = node.getNodeValue();
  +            configuration.setAttribute( name, value );
  +        }
  +
  +        String content = null;
  +        final NodeList nodes = element.getChildNodes();
  +        final int count = nodes.getLength();
  +        for( int i = 0; i < count; i++ )
  +        {
  +            final Node node = nodes.item( i );
  +            if( node instanceof Element )
  +            {
  +                final Configuration child = toConfiguration( (Element)node );
  +                configuration.addChild( child );
  +            }
  +            else if( node instanceof CharacterData )
  +            {
  +                final CharacterData data = (CharacterData)node;
  +                content += data.getData();
  +            }
  +        }
  +
  +        if( null != content )
  +        {
  +            configuration.setValue( content );
  +        }
  +
  +        return configuration;
  +    }
  +
  +    /**
        * Convert a configuration tree into a DOM Element tree.
        *
        * @param configuration the configuration object
  @@ -123,7 +175,7 @@
           {
               return ser.serialize( configuration );
           }
  -        catch (Exception e ) 
  +        catch( Exception e )
           {
               return e.getMessage();
           }
  @@ -140,10 +192,8 @@
        */
       public static boolean equals( final Configuration c1, final Configuration c2 )
       {
  -        return c1.getName().equals( c2.getName() )
  -            && areValuesEqual( c1, c2 )
  -            && areAttributesEqual( c1, c2 )
  -            && areChildrenEqual( c1, c2 );
  +        return c1.getName().equals( c2.getName() ) && areValuesEqual( c1, c2 )
&&
  +            areAttributesEqual( c1, c2 ) && areChildrenEqual( c1, c2 );
       }
   
       /**
  @@ -240,8 +290,8 @@
       {
           final String value1 = c1.getValue( null );
           final String value2 = c2.getValue( null );
  -        return ( value1 == null && value2 == null ) 
  -            || ( value1 != null && value1.equals( value2 ) );
  +        return ( value1 == null && value2 == null ) ||
  +            ( value1 != null && value1.equals( value2 ) );
       }
   
       /**
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org


Mime
View raw message