package csa.tiletools.map;

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import java.util.*;
public class  GridSettingsDataXMLHandler extends DefaultHandler
{
	private HashMap<String, GridSettingsData> mGridSettingsData;
	private GridSettingsData mCurrentData = null;
	private String mCurrentElement = null;
	private String meveryX = "";
	private Vector<Integer> meveryXs = null;
	private String mXOffset = "";
	private Vector<Integer> mXOffsets = null;
	private String meveryY = "";
	private Vector<Integer> meveryYs = null;
	private String mYOffset = "";
	private Vector<Integer> mYOffsets = null;
	private String mDrawTyp = "";
	private String mSHowHotSpot = "";
	private String mGridColor = "";
	private String mXAlternating = "";
	private String mYAlternating = "";
	public HashMap<String, GridSettingsData> getLastHashMap()
	{
		return mGridSettingsData;
	}
	@Override public void startDocument() throws SAXException
	{
		mCurrentData = new GridSettingsData();
		mGridSettingsData = new HashMap<String, GridSettingsData>();
	}
	@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("GridSettingsData"))
		{
			mCurrentData = new GridSettingsData();
			meveryX = "";
			meveryXs = new Vector<Integer>();
			mXOffset = "";
			mXOffsets = new Vector<Integer>();
			meveryY = "";
			meveryYs = new Vector<Integer>();
			mYOffset = "";
			mYOffsets = new Vector<Integer>();
			mDrawTyp = "";
			mSHowHotSpot = "";
			mGridColor = "";
			mXAlternating = "";
			mYAlternating = "";
		}
	}
	@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("everyX")) meveryX += s;
		if (mCurrentElement.equalsIgnoreCase("XOffset")) mXOffset += s;
		if (mCurrentElement.equalsIgnoreCase("everyY")) meveryY += s;
		if (mCurrentElement.equalsIgnoreCase("YOffset")) mYOffset += s;
		if (mCurrentElement.equalsIgnoreCase("DrawTyp")) mDrawTyp += s;
		if (mCurrentElement.equalsIgnoreCase("SHowHotSpot")) mSHowHotSpot += s;
		if (mCurrentElement.equalsIgnoreCase("GridColor")) mGridColor += s;
		if (mCurrentElement.equalsIgnoreCase("XAlternating")) mXAlternating += s;
		if (mCurrentElement.equalsIgnoreCase("YAlternating")) mYAlternating += s;
	}
	@Override public void endElement(String uri, String localName, String qName) throws SAXException
	{
		if ("everyX".equalsIgnoreCase(qName))
		{
			try{
			meveryXs.addElement(Integer.parseInt(meveryX));
			}catch (Throwable e){}
			meveryX="";
		}
		if ("XOffset".equalsIgnoreCase(qName))
		{
			try{
			mXOffsets.addElement(Integer.parseInt(mXOffset));
			}catch (Throwable e){}
			mXOffset="";
		}
		if ("everyY".equalsIgnoreCase(qName))
		{
			try{
			meveryYs.addElement(Integer.parseInt(meveryY));
			}catch (Throwable e){}
			meveryY="";
		}
		if ("YOffset".equalsIgnoreCase(qName))
		{
			try{
			mYOffsets.addElement(Integer.parseInt(mYOffset));
			}catch (Throwable e){}
			mYOffset="";
		}
		if ("GridSettingsData".equalsIgnoreCase(qName))
		{
			if (mCurrentData != null)
			{
				meveryX = "";
				mCurrentData.meveryX = meveryXs;
				mXOffset = "";
				mCurrentData.mXOffset = mXOffsets;
				meveryY = "";
				mCurrentData.meveryY = meveryYs;
				mYOffset = "";
				mCurrentData.mYOffset = mYOffsets;
				try{
				mCurrentData.mDrawTyp = Integer.parseInt(mDrawTyp);
				}catch (Throwable e){}
				mDrawTyp = "";
				try{
				mCurrentData.mSHowHotSpot = Boolean.parseBoolean(mSHowHotSpot);
				}catch (Throwable e){}
				mSHowHotSpot = "";
				try{
				mCurrentData.mGridColor = Integer.parseInt(mGridColor);
				}catch (Throwable e){}
				mGridColor = "";
				try{
				mCurrentData.mXAlternating = Boolean.parseBoolean(mXAlternating);
				}catch (Throwable e){}
				mXAlternating = "";
				try{
				mCurrentData.mYAlternating = Boolean.parseBoolean(mYAlternating);
				}catch (Throwable e){}
				mYAlternating = "";
				mGridSettingsData.put(mCurrentData.mName, mCurrentData);
				mCurrentData = null;
			}
		}
		mCurrentElement = null;
	}
}
