|
XmlConfigLoader |
|
/*
** Luxor - XML User Interface Language (XUL) Toolkit
** Copyright (c) 2001, 2002 by Gerald Bauer
**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**
*/
package luxor.core.config;
import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;
import luxor.core.*;
import luxor.status.*;
public class XmlConfigLoader
{
static Logger T = Logger.getLogger( XmlConfigLoader.class );
Map _map;
XmlConfigRoot _root;
public XmlConfigLoader( Map map )
{
_map = map;
}
public void load( InputStream in ) throws JDOMException
{
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build( in );
Element config = doc.getRootElement();
createElements( null, config );
if( _root != null )
{
// loop over config data and add it to lookup map
Iterator it = _root.getEntries().iterator();
while( it.hasNext() )
{
XmlConfigNode node = ( XmlConfigNode ) it.next();
String id = node.getId();
Object value = node.getValue();
String clazz = value.getClass().getName();
T.debug( clazz + ":" + id + "=" + value.toString() );
_map.put( id, value );
}
}
}
protected void createElements( XmlConfigContainer parent, Element element )
{
String type = element.getName();
if( type.equals( XmlConfig.Element.CONFIG ) )
{
parent = _root = new XmlConfigRoot( element );
}
else if( type.equals( XmlConfig.Element.ARRAY ) )
{
XmlConfigArray data = new XmlConfigArray( element );
parent.add( data );
parent = data;
}
else if( type.equals( XmlConfig.Element.INT ) )
{
XmlConfigInt data = new XmlConfigInt( element );
parent.add( data );
}
else if( type.equals( XmlConfig.Element.STRING ) )
{
XmlConfigString data = new XmlConfigString( element );
parent.add( data );
}
else
{
Xul.syntax( "unsupported element '" + type + "' inside xml config file" );
return;
// don't process children of unsupported element
}
// process children
List list = element.getChildren();
for( Iterator it = list.iterator(); it.hasNext(); )
{
Element child = ( Element ) it.next();
createElements( parent, child );
}
}
}
|
XmlConfigLoader |
|