/*
** 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 java.util.jar.*;
import javax.swing.*;
import org.jdom.*;
import luxor.core.*;
import luxor.core.*;
import luxor.core.config.*;
import luxor.spi.*;
import luxor.status.*;
import luxor.util.*;

public class XulJarResourceLoaderHelper implements XulResourceLoader
{
   static Logger T = Logger.getLogger( XulJarResourceLoaderHelper.class );

   private HashMap _config;

   private File _file;
   private JarFile _jarFile;

   public XulJarResourceLoaderHelper( File file, JarFile jarFile )
   {
      _file = file;
      _jarFile = jarFile;

      // load locale/config data on "startup" before any resources are retrieved
      _config = new HashMap();
      loadConfigData();
   }

   public List getBootstrapEntries()
   {
      // for now add all files in startup directory tree
      //  with the exception of directories, of course

      ArrayList bootstrapEntries = new ArrayList();

      Enumeration it = _jarFile.entries();
      while( it.hasMoreElements() )
      {
         JarEntry entry = ( JarEntry ) it.nextElement();

         if( !entry.isDirectory() )
         {
            String entryName = entry.getName();
            String entryNameLowerCase = entryName.toLowerCase();
            // make sure it doesn't contain any path separators
            if( entryNameLowerCase.startsWith( "startup/" )
                   || entryNameLowerCase.startsWith( "startup\\" ) )
            {
               bootstrapEntries.add( entryName );
            }
         }
      }

      return bootstrapEntries;
   }

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

   public InputStream getResourceAsStream( String name )
   {
      T.debug( "getResourceAsStream( name=" + name + " )" );

      try
      {
         JarEntry entry = ( JarEntry ) _jarFile.getEntry( name );
         if( entry == null )
            return null;

         return _jarFile.getInputStream( entry );
      }
      catch( IOException ioex )
      {
         Xul.error( "failed to retrieve resource '" + name + "' inside jar: " + ioex.getMessage() );
         return null;
      }
   }

   public URL getResourceAsUrl( String name )
   {
      T.debug( "getResourceAsUrl( name=" + name + " )" );

      try
      {
         // check if entry exists
         JarEntry entry = ( JarEntry ) _jarFile.getEntry( name );
         if( entry == null )
            return null;

         URL jarURL = new URL(
               "jar:" + _file.toURL() + "!/" + name );

         T.debug( "jarURL=" + jarURL.toExternalForm() );

         return jarURL;
      }
      catch( IOException ioex )
      {
         Xul.error( "failed to retrieve resource '" + name + "' inside jar: " + ioex.getMessage() );
         return null;
      }
   }

   private List getConfigEntries()
   {
      // for now add all files in locale directory tree
      //   with the exception of directories

      ArrayList configEntries = new ArrayList();

      Enumeration it = _jarFile.entries();
      while( it.hasMoreElements() )
      {
         JarEntry entry = ( JarEntry ) it.nextElement();

         if( !entry.isDirectory() )
         {
            String entryName = entry.getName();
            String entryNameLowerCase = entryName.toLowerCase();
            // make sure it doesn't contain any path separators
            if( entryNameLowerCase.startsWith( "locale/" )
                   || entryNameLowerCase.startsWith( "locale\\" ) )
            {
               configEntries.add( entryName );
            }
         }
      }

      return configEntries;
   }

   private void loadConfigData()
   {
      // todo: add support for locale dependent config data loading
      //  a la Java resource bundles
      //  use directories, however, instead of file name/class name mangling
      Iterator it = getConfigEntries().iterator();

      while( it.hasNext() )
         loadConfigData( ( String ) it.next() );
   }

   /**
    *  supports two formats: - strongly-typed xml config data (including string
    *  arrays) - plain-vanilla java properties (name/value pairs)
    */
   private void loadConfigData( String name )
   {

      JarEntry entry = ( JarEntry ) _jarFile.getEntry( name );
      if( entry == null )
      {
         // todo: should i use an assert instead of a null check?
         Xul.error( "*** failed to find config data entry '" + name + "' in jar" );
         return;
      }

      if( name.toLowerCase().endsWith( ".xml" ) )
      {
         try
         {
            T.debug( "loading xml config file " + name );

            XmlConfigLoader loader = new XmlConfigLoader( _config );

            loader.load( _jarFile.getInputStream( entry ) );

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

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

            Properties tempProps = new Properties();
            tempProps.load( _jarFile.getInputStream( entry ) );

            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 " + name + " successfully loaded" );
         }
         catch( IOException ioex )
         {
            Xul.error( "failed to load properties for jar entry '" + name + "': " + ioex.getMessage() );
         }
      }
   }
}