package csa.tiletools.map;

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import java.util.*;
public class  TileGrowthDataXMLHandler extends DefaultHandler
{
	private HashMap<String, TileGrowthData> mTileGrowthData;
	private TileGrowthData mCurrentData = null;
	private String mCurrentElement = null;
	private String mTileCollectionFilename = "";
	private Vector<String> mTileCollectionFilenames = null;
	private String mTileCollectionClass = "";
	private Vector<String> mTileCollectionClasss = null;
	private String mTypesToGrowFrom = "";
	private Vector<String> mTypesToGrowFroms = null;
	private String mTypesToGrow = "";
	private Vector<String> mTypesToGrows = null;
	public HashMap<String, TileGrowthData> getLastHashMap()
	{
		return mTileGrowthData;
	}
	@Override public void startDocument() throws SAXException
	{
		mCurrentData = new TileGrowthData();
		mTileGrowthData = new HashMap<String, TileGrowthData>();
	}
	@Override public void endDocument () throws SAXException
	{
	}
	@Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
	{
		mCurrentElement = qName;
		if (qName.equalsIgnoreCase("TileGrowthData"))
		{
			mCurrentData = new TileGrowthData();
			mTileCollectionFilename = "";
			mTileCollectionFilenames = new Vector<String>();
			mTileCollectionClass = "";
			mTileCollectionClasss = new Vector<String>();
			mTypesToGrowFrom = "";
			mTypesToGrowFroms = new Vector<String>();
			mTypesToGrow = "";
			mTypesToGrows = new Vector<String>();
		}
	}
	@Override public void characters(char[] ch, int start, int length)
	{
		String s = new String( ch, start, length );
		if (mCurrentElement == null) return;
		if (mCurrentElement.equalsIgnoreCase("Class")) mCurrentData.mClass += s;
		if (mCurrentElement.equalsIgnoreCase("Name")) mCurrentData.mName += s;
		if (mCurrentElement.equalsIgnoreCase("TileCollectionFilename")) mTileCollectionFilename += s;
		if (mCurrentElement.equalsIgnoreCase("TileCollectionClass")) mTileCollectionClass += s;
		if (mCurrentElement.equalsIgnoreCase("TypesToGrowFrom")) mTypesToGrowFrom += s;
		if (mCurrentElement.equalsIgnoreCase("TypesToGrow")) mTypesToGrow += s;
	}
	@Override public void endElement(String uri, String localName, String qName) throws SAXException
	{
		if ("TileCollectionFilename".equalsIgnoreCase(qName))
		{
			mTileCollectionFilenames.addElement(mTileCollectionFilename);
			mTileCollectionFilename="";
		}
		if ("TileCollectionClass".equalsIgnoreCase(qName))
		{
			mTileCollectionClasss.addElement(mTileCollectionClass);
			mTileCollectionClass="";
		}
		if ("TypesToGrowFrom".equalsIgnoreCase(qName))
		{
			mTypesToGrowFroms.addElement(mTypesToGrowFrom);
			mTypesToGrowFrom="";
		}
		if ("TypesToGrow".equalsIgnoreCase(qName))
		{
			mTypesToGrows.addElement(mTypesToGrow);
			mTypesToGrow="";
		}
		if ("TileGrowthData".equalsIgnoreCase(qName))
		{
			if (mCurrentData != null)
			{
				mTileCollectionFilename = "";
				mCurrentData.mTileCollectionFilename = mTileCollectionFilenames;
				mTileCollectionClass = "";
				mCurrentData.mTileCollectionClass = mTileCollectionClasss;
				mTypesToGrowFrom = "";
				mCurrentData.mTypesToGrowFrom = mTypesToGrowFroms;
				mTypesToGrow = "";
				mCurrentData.mTypesToGrow = mTypesToGrows;
				mTileGrowthData.put(mCurrentData.mName, mCurrentData);
				mCurrentData = null;
			}
		}
		mCurrentElement = null;
	}
}
