////////////////////////////////////////////////////////////////////////////////
// TunnelX -- Cave Drawing Program  
// Copyright (C) 2002  Julian Todd.  
//
// 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 Tunnel;

import java.io.File; 
import java.io.IOException;  

import org.xml.sax.*;

import javax.xml.parsers.SAXParserFactory;  
import javax.xml.parsers.ParserConfigurationException;  
import javax.xml.parsers.SAXParser;  


/////////////////////////////////////////////
// local class of the parser.  
class TunnelXML extends HandlerBase // DefaultHandler
{
	SAXParserFactory factory;
	TunnelXMLparse txp;


	/////////////////////////////////////////////
	void emitError(String mess, IOException e) throws IOException
	{
		TN.emitError(mess); 
		throw e; 
	}

	/////////////////////////////////////////////
	TunnelXML()
	{
		factory = SAXParserFactory.newInstance();
	}

	/////////////////////////////////////////////
	void ParseFile(TunnelXMLparse ltxp, File sfile)
	{
	  	try
		{
	  		txp = ltxp;
		    SAXParser saxParser = factory.newSAXParser();
	  		saxParser.parse(sfile, this);
		}
		catch (SAXException e)
		{
			System.out.println(e.toString()); 		
			e.printStackTrace();
	    }
		catch (ParserConfigurationException e) 
		{
			System.out.println(e.toString()); 		
            e.printStackTrace();
		} 
		catch (IOException e) 
		{
			System.out.println(e.toString()); 		
            e.printStackTrace();
		} 
	}


	/////////////////////////////////////////////
	public void startElement(String name, AttributeList attrs) throws SAXException  
	{
		// roll forward the stack and introduce the attributes 
		txp.iposstack[txp.istack] = (txp.istack == 0 ? 0 : txp.iposstack[txp.istack - 1]);
		txp.elemstack[txp.istack] = name;
		for (int i = 0; i < attrs.getLength(); i++)  
		{
			txp.attnamestack[txp.iposstack[txp.istack]] = attrs.getName(i);
			txp.attvalstack[txp.iposstack[txp.istack]] = attrs.getValue(i);
			txp.iposstack[txp.istack]++;
		}
		txp.istack++;

		txp.startElementAttributesHandled(name);
	}

	/////////////////////////////////////////////
	public void characters(char[] ch, int start, int length) throws SAXException 
	{
		txp.characters(null, ch, start, length);
	}


	/////////////////////////////////////////////
	public void endElement(String name) throws SAXException
	{
		// roll back the stack 
		txp.istack--;
		txp.endElementAttributesHandled(name);
	}
};


