/*
 *
 * Copyright (C)  5-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
 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.

 * 
 */
package jxtray.connection;

import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;

import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;

import jxtray.swing.TreeConnectionElement;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;




public class JxtConnectionHandler implements ContentHandler
{

	private DefaultTreeModel treeModel;
	private DefaultMutableTreeNode current;
	private Locator locator;
	private Map namespaceMappings;
	private JxtConnectionManager manager;
	//private JxtConnection currentConnection;
	private JxtConnectionParam currentConnectionParam;
	private String driverName;
	
	public JxtConnectionHandler( DefaultTreeModel treeModel, DefaultMutableTreeNode base, JxtConnectionManager manager)
	{
			this.treeModel = treeModel;
			this.current = base;
			this.namespaceMappings = new HashMap();
			this.manager = manager;
	}
	
	
	public void setDocumentLocator( Locator locator) 
	{
			this.locator = locator;
	}
	
	public void startDocument() throws SAXException 
	{
	}
	
	public void endDocument() throws SAXException 
	{
	}
	
	public void processingInstruction ( String target, String data) throws SAXException
	{
			
	}
	
	public void startPrefixMapping(String prefix, String uri) 
	{
			namespaceMappings.put(uri,prefix);
	}
	
	public void endPrefixMapping(String prefix ) 
	{
			for ( Iterator i = namespaceMappings.keySet().iterator(); i.hasNext();)
			{
					String uri = (String) i.next();
					String thisPrefix=  (String) namespaceMappings.get(uri);
					if ( prefix.equals(thisPrefix) )
					{
							namespaceMappings.remove(uri);
							break;
					}
			}
	}
	
	public void startElement ( String namespaceURI, String localName, String qName, Attributes atts) throws SAXException
	{
			DefaultMutableTreeNode element;
			
			if ("connections".equals(localName) ) // if menubar do nothing. Object is already created
			{
			}
			else if ("connection".equals(localName) ) // if menubar do nothing. Object is already created
			{
					element = new TreeConnectionElement( atts.getValue(0),TreeConnectionElement.NODE_CONNECTION_CLOSE);
					current.add(element);
					current = element ;
					currentConnectionParam = new JxtConnectionParam();
					currentConnectionParam.setName(atts.getValue(0));	
					currentConnectionParam.setHost(atts.getValue(1));
					currentConnectionParam.setPasswordPrompt(atts.getValue(2));
			}
			else if ("driver".equals(localName) ) // if menubar do nothing. Object is already created
			{

 					element = new TreeConnectionElement( atts.getValue(0),TreeConnectionElement.NODE_DRIVER);
					current.add(element);
					current.add(new TreeConnectionElement( atts.getValue(1),TreeConnectionElement.NODE_JDBCURL));
					current = element ;
					driverName = atts.getValue(0);
					currentConnectionParam.setJdbcurl(atts.getValue(1));
			} 
			else if ("user".equals(localName) ) // if menubar do nothing. Object is already created
			{	
					element = new TreeConnectionElement( atts.getValue(0),TreeConnectionElement.NODE_USER);
					current.add(element);
					current = element ;
					currentConnectionParam.setLogin(atts.getValue(0));
					currentConnectionParam.setPassword(atts.getValue(1));
			}			
			else
			{
			}
	}
	
	public void endElement ( String namespaceURI, String localName, String qName) throws SAXException
	{
		// remonter de un étage
		current = (DefaultMutableTreeNode) current.getParent();
		if ("connection".equals(localName) ) // if menubar do nothing. Object is already created
		{
			try
			{
				manager.put(currentConnectionParam.getName(),new JxtConnection(currentConnectionParam, driverName));
			}
			catch ( Exception e )
			{
					System.err.println("JxtConnectionHandler.endElement() : exception : "+ e );
			}			
		}
		
	}
	
	public void characters ( char[] ch, int start, int length) throws SAXException
	{	
			
	}
	
	public void ignorableWhitespace ( char[] ch, int start, int length) throws SAXException
	{	
	}
	public void skippedEntity ( String name) throws SAXException
	{	
	}	
	
	
}
