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

import java.io.*;
import java.util.*;
import luxor.config.*;
import luxor.core.*;

public class MimeUtils
{
   private static MimeUtils _instance;

   private Properties _props;

   private MimeUtils()
   {
      Properties defaultProps = new Properties();

      defaultProps.setProperty( "jnlp", "application/x-java-jnlp-file" );

      _props = new Properties( defaultProps );

      ClassLoader cl = MimeUtils.class.getClassLoader();

      try
      {
         InputStream in = cl.getResourceAsStream( Constants.MIME_PROPERTIES );
         if( in == null )
            Xul.error( "failed to find properties " + Constants.MIME_PROPERTIES );
         else
            _props.load( in );
      }
      catch( IOException ioex )
      {
         Xul.error( "failed to load properties " + Constants.MIME_PROPERTIES + ": " + ioex.toString() );
      }
   }

   private static MimeUtils getInstance()
   {
      if( _instance == null )
         _instance = new MimeUtils();
      return _instance;
   }

   public static String guessContentTypeFromName( String name )
   {
      // get file extension without dot
      String ext = FileUtils.getFileExtension( name, false );

      ext = ext.toLowerCase();

      String contentType = getInstance().lookupContentType( ext );

      if( contentType == null )
         contentType = "text/plain";

      return contentType;
   }

   public String lookupContentType( String ext )
   {
      return ( String ) _props.get( ext );
   }

}