donaldp 2002/09/22 01:51:09
Added: src/java/org/apache/avalon/phoenix/components/util
ConfigurationUtil.java
Log:
Add in utility class to convert from Configuration to Element.PR:
Revision Changes Path
1.1
jakarta-avalon-phoenix/src/java/org/apache/avalon/phoenix/components/util/ConfigurationUtil.java
Index: ConfigurationUtil.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.txt file.
*/
package org.apache.avalon.phoenix.components.util;
import org.w3c.dom.Element;
import org.w3c.dom.Document;
import org.w3c.dom.Text;
import org.apache.avalon.framework.configuration.Configuration;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
/**
* This class has a bunch of utility methods to work
* with configuration objects.
*
* @author <a href="mailto:peter at apache.org">Peter Donald</a>
* @version $Revision: 1.1 $ $Date: 2002/09/22 08:51:09 $
*/
public class ConfigurationUtil
{
/**
* Private constructor to block instantiation.
*/
private ConfigurationUtil()
{
}
/**
* Convert a configuration tree into a DOM Element tree.
*
* @param configuration the configuration object
* @return the DOM Element
*/
public static Element toElement( final Configuration configuration )
{
try
{
final DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document document = builder.newDocument();
return createElement( document, configuration );
}
catch( ParserConfigurationException e )
{
throw new IllegalStateException( e.toString() );
}
}
/**
* Create an DOM {@link Element} from a {@link Configuration}
* object.
*
* @param document the DOM document
* @param configuration the configuration to convert
* @return the DOM Element
*/
private static Element createElement( final Document document,
final Configuration configuration )
{
final Element element = document.createElement( configuration.getName() );
final String content = configuration.getValue( null );
if( null != content )
{
final Text child = document.createTextNode( content );
element.appendChild( child );
}
final String[] names = configuration.getAttributeNames();
for( int i = 0; i < names.length; i++ )
{
final String name = names[ i ];
final String value = configuration.getAttribute( name, null );
element.setAttribute( name, value );
}
final Configuration[] children = configuration.getChildren();
for( int i = 0; i < children.length; i++ )
{
final Element child = createElement( document, children[ i ] );
element.appendChild( child );
}
return element;
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>