/*
** 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.loader;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import org.jdom.*;
import luxor.core.*;
import luxor.core.config.*;
import luxor.spi.*;
import luxor.status.*;
import luxor.util.*;

public class XulFileResourceLoader implements XulResourceLoader
{
   static Logger T = Logger.getLogger( XulFileResourceLoader.class );
   HashMap _config;

   File _root;

   public XulFileResourceLoader( File root )
   {
      _root = root;
      _config = new HashMap();

      loadConfigData();

      T.debug( "-- begin dump config data ---------------------" );
      DebugUtils.dumpMap( _config );
      T.debug( "-- end dump config data -----------------------" );
   }

   /**
    *  as a convention for now return all files in startup directory which is a
    *  subdirectory of root
    */
   public List getBootstrapEntries()
   {
      ArrayList bootstrapEntries = new ArrayList();

      File startup = new File( _root, "startup" );
      if( !startup.exists() )
      {
         Xul.warning( "no startup directory found in chrome package " + _root.getAbsolutePath() );
      }
      else
      {
         collectBootstrapEntries( bootstrapEntries, startup, "startup" );
      }

      return bootstrapEntries;
   }

   public Object getConfigData( String key )
   {
      return _config.get( key );
   }

   public InputStream getResourceAsStream( String name )
   {
      File file = new File( _root, name );

      if( !file.exists() )
         return null;

      try
      {
         return new FileInputStream( file );
      }
      catch( IOException ioex )
      {
         Xul.error( "failed to retrieve resource '" + name + "': " + ioex.getMessage() );
         return null;
      }
   }

   public URL getResourceAsUrl( String name )
   {
      File file = new File( _root, name );

      if( !file.exists() )
         return null;

      try
      {
         return file.toURL();
      }
      catch( MalformedURLException mex )
      {
         Xul.error( "failed to create URL for resource '" + name + "': " + mex.getMessage() );
         return null;
      }
   }

   private void collectBootstrapEntries( List entries, File root, String internalPath )
   {
      File files[] = root.listFiles();
      for( int i = 0; i < files.length; i++ )
      {
         File file = files[i];
         String newInternalPath;

         if( internalPath.equals( "" ) )
            newInternalPath = file.getName();
         else
            newInternalPath = internalPath + "/" + file.getName();

         if( file.isDirectory() )
         {
            collectBootstrapEntries( entries, file, newInternalPath );
         }
         else
         {
            entries.add( newInternalPath );
         }
      }
   }

   private void loadConfigData()
   {
      // todo: load user-defined locale and fallback locale only

      File localeDir = new File( _root, "locale" );
      if( !localeDir.exists() || !localeDir.isDirectory() )
      {
         Xul.warning( "no locale directory found in chrome package " + _root.getAbsolutePath() );
      }
      else
         loadConfigData( localeDir );
   }

   private void loadConfigData( File dir )
   {
      File files[] = dir.listFiles();
      if( files == null )
         return;

      for( int i = 0; i < files.length; i++ )
      {
         File file = files[i];

         if( file.isDirectory() )
            loadConfigData( file );
         else
         {
            if( file.getName().toLowerCase().endsWith( ".xml" ) )
            {
               try
               {
                  T.debug( "loading xml config file " + file.getName() );

                  XmlConfigLoader loader = new XmlConfigLoader( _config );
                  loader.load( new FileInputStream( file ) );

                  T.debug( "xml config file " + file.getName() + " successfully loaded" );
               }
               catch( JDOMException jex )
               {
                  Xul.error( "failed to parse xml config file '" + file.getName() + "': " + jex.toString() );
               }
               catch( IOException ioex )
               {
                  Xul.error( "failed to load xml config file '" + file.getName() + "': " + ioex.toString() );
               }
            }
            else
            {
               // assume java properties by default

               try
               {
                  T.debug( "loading properties config file " + file.getName() );

                  Properties tempProps = new Properties();
                  tempProps.load( new FileInputStream( file ) );

                  T.debug( "-- begin dump java props ----------------" );
                  DebugUtils.dumpMap( tempProps );
                  T.debug( "-- end dump java props -------------------" );

                  // merge all properties into one table
                  _config.putAll( tempProps );

                  T.debug( "properties config file " + file.getName() + " successfully loaded" );
               }
               catch( IOException ioex )
               {
                  Xul.error( "failed to load properties in file '" + file.getAbsolutePath() + "': " + ioex.getMessage() );
               }
            }
         }
      }
   }
}