/*
 *
 * Copyright (C)  15-sept-2003 Jean-Yves Beaujean
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,  59
 * Tem0ple Place - Suite 330, Boston, MA 02111-1307, USA.

 */
package jxtray.connection;


import org.xml.sax.ContentHandler;
import org.xml.sax.XMLReader;
import org.xml.sax.InputSource;


import org.jdom.output.*;
import org.jdom.*;

import org.xml.sax.helpers.XMLReaderFactory;

import java.util.Arrays;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import java.io.File;
import java.io.FileOutputStream;

import jxtray.Jxtray;
import jxtray.JxtrayConstants;
import jxtray.Messages;

/**
 *  This class is a drivers container.
 *  It contains all the informations drivers defined in the XML files
 */
public class DriverCache extends Hashtable 
{

	private static boolean isInstanceAlive = false;
	
	public static DriverCache getInstance()
	{
		if ( isInstanceAlive )
			return null;
		else
		{
			isInstanceAlive = true;
			return new DriverCache();
		}
	}

	public void update( DriverInfo newDriver) throws Exception
	{
		this.put(newDriver.getName(),newDriver);
	}
	
	public Object[] getAllDriverName() throws Exception
	{
		
		Vector driverName = new Vector();
		Enumeration enumEl = elements();
		DriverInfo info = null;
		
		while ( enumEl.hasMoreElements())
			driverName.add(((DriverInfo) enumEl.nextElement()).getName()) ;
		
		Object elmt[] = driverName.toArray();
		Arrays.sort(elmt);
		return elmt;
		
	}

	/** Load from the XML file */
	public void loadDrivers() throws Exception
	{

		XMLReader reader = XMLReaderFactory.createXMLReader(JxtrayConstants.XMLPARSER);
		ContentHandler driverHandler = new DriverContentHandler(this);
		reader.setContentHandler(driverHandler);
		InputSource inputSource = new 	InputSource("file:///"+Jxtray.HOME+"conf"+File.separator+"dbdrivers.xml");
		reader.parse(inputSource);
	}

	/** Print the contain of the Hastable */
	public void printState() throws Exception
	{
			Enumeration enumEl = elements();
			DriverInfo info = null;
			while ( enumEl.hasMoreElements())
			{
				info = (DriverInfo) enumEl.nextElement() ;
				System.out.println("");
				System.out.println("--> driver info");
				System.out.println("name : \t" +info.getName());
				System.out.println("classe : \t" +info.getClassname());
				System.out.println("url : \t" +info.getUrl());
				System.out.println("website: \t" +info.getWebsite());
			}
			System.out.println("+--+ NO MORE DRIVERS IN CACHE +--+");
	}
	
	/** Enregistre les connexions dans le fichier XML */
		public void writeXMLFile()
		{
				Element root = new Element("driverdb");
				Document doconnection = new Document(root);
				
				Enumeration enumEl = elements();
				DriverInfo info = null;
				while ( enumEl.hasMoreElements())
				{
						 
					info = (DriverInfo) enumEl.nextElement() ;
					Element element = new Element("driver");
					
					Element subelement = new Element("name");
					subelement.setAttribute(new Attribute("value",info.getName())); 
					element.addContent(subelement);
					  
					subelement = new Element("url");
					subelement.setAttribute(new Attribute("value",info.getUrl()));
					element.addContent(subelement);
					
					subelement = new Element("classname");
					subelement.setAttribute(new Attribute("value", info.getClassname()));
					element.addContent(subelement);

					subelement = new Element("website");
					subelement.setAttribute(new Attribute("value",info.getWebsite() ));
					element.addContent(subelement);
					
					subelement = new Element("pathfile");
					subelement.setAttribute(new Attribute("value",info.getPathfile()));
					element.addContent(subelement);										
				
					root.addContent(element);
				}
				
				
				XMLOutputter outputter = new XMLOutputter();
				try
				{
					outputter.output(doconnection, new FileOutputStream(Jxtray.HOME+"conf"+File.separator+"dbdrivers.xml"));
				}
				catch( Exception e )
				{
					Messages.error("DriverCache.writeXMLFile() :" + e);
				}
		}
		
		/** Memory Managnement */
		public void finalize() 
		{
			this.clear();
		}		
		

	
	
	

}
