/**
* Build a configuration object using a naming context.
*/
public Configuration build( final Context context ) throws NamingException
{
final DefaultConfiguration configuration;
final String absoluteName = context.getNameInNamespace();
final NameParser parser = context.getNameParser( absoluteName );
final Name parsedName = parser.parse( absoluteName );
String name = absoluteName;
String prefix = "";
//if composite name, use only the relative name.
final int position = parsedName.size();
if( position > 0 )
{
name = parsedName.get( position - 1 );
}
if( context instanceof DirContext )
{
//extract element name, and namespace prefix
final Attributes attrs = ( (DirContext)context ).getAttributes( "" );
final NamingEnumeration attributes = attrs.getAll();
while( attributes.hasMore() )
{
final Attribute attribute = (Attribute)attributes.next();
final String id = attribute.getID();
if( name.startsWith( id ) )
{
name = (String)attribute.get();
if( m_enableNamespaces ) prefix = id;
attrs.remove( id );
break;
}
}
configuration = new DefaultConfiguration( name, null, "", prefix );
copyAttributes( attrs, configuration );
}
else
configuration = new DefaultConfiguration( name, null, "", prefix );
final NamingEnumeration bindings = context.listBindings( "" );
while( bindings.hasMore() )
{
final Binding binding = (Binding)bindings.next();
final Object object = binding.getObject();
if( ( object instanceof Number ) ||
( object instanceof String ) )
{
configuration.setValue( object.toString() );
}
if( object instanceof Context )
{
final Context child = (Context)object;
configuration.addChild( build( child ) );
}
}
return configuration;
}