package csa.tiletools.map;

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import java.util.*;
public class  MapDataXMLHandler extends DefaultHandler
{
	private HashMap<String, MapData> mMapData;
	private MapData mCurrentData = null;
	private String mCurrentElement = null;
	private String mLayerData = "";
	private Vector<String> mLayerDatas = null;
	private String mSizeX = "";
	private String mSizeY = "";
	private String mSizeByTiles = "";
	private String mWrapHorizontal = "";
	private String mWrapVertical = "";
	private String mAllowTileOverlap = "";
	private String msizePerLayer = "";
	private String mGridSettings = "";
	private String mLayerSaveTyp = "";
	public HashMap<String, MapData> getLastHashMap()
	{
		return mMapData;
	}
	@Override public void startDocument() throws SAXException
	{
		mCurrentData = new MapData();
		mMapData = new HashMap<String, MapData>();
	}
	@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("MapData"))
		{
			mCurrentData = new MapData();
			mLayerData = "";
			mLayerDatas = new Vector<String>();
			mSizeX = "";
			mSizeY = "";
			mSizeByTiles = "";
			mWrapHorizontal = "";
			mWrapVertical = "";
			mAllowTileOverlap = "";
			msizePerLayer = "";
			mGridSettings = "";
			mLayerSaveTyp = "";
		}
	}
	@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("LayerData")) mLayerData += s;
		if (mCurrentElement.equalsIgnoreCase("sizeX")) mSizeX += s;
		if (mCurrentElement.equalsIgnoreCase("sizeY")) mSizeY += s;
		if (mCurrentElement.equalsIgnoreCase("sizeByTiles")) mSizeByTiles += s;
		if (mCurrentElement.equalsIgnoreCase("wrapHorizontal")) mWrapHorizontal += s;
		if (mCurrentElement.equalsIgnoreCase("wrapVertical")) mWrapVertical += s;
		if (mCurrentElement.equalsIgnoreCase("AllowTileOverlap")) mAllowTileOverlap += s;
		if (mCurrentElement.equalsIgnoreCase("sizePerLayer")) msizePerLayer += s;
		if (mCurrentElement.equalsIgnoreCase("GridSettings")) mGridSettings += s;
		if (mCurrentElement.equalsIgnoreCase("LayerSaveTyp")) mLayerSaveTyp += s;
	}
	@Override public void endElement(String uri, String localName, String qName) throws SAXException
	{
		if ("LayerData".equalsIgnoreCase(qName))
		{
			mLayerDatas.addElement(mLayerData);
			mLayerData="";
		}
		if ("MapData".equalsIgnoreCase(qName))
		{
			if (mCurrentData != null)
			{
				mLayerData = "";
				mCurrentData.mLayerData = mLayerDatas;
				try{
				mCurrentData.mSizeX = Integer.parseInt(mSizeX);
				}catch (Throwable e){}
				mSizeX = "";
				try{
				mCurrentData.mSizeY = Integer.parseInt(mSizeY);
				}catch (Throwable e){}
				mSizeY = "";
				try{
				mCurrentData.mSizeByTiles = Boolean.parseBoolean(mSizeByTiles);
				}catch (Throwable e){}
				mSizeByTiles = "";
				try{
				mCurrentData.mWrapHorizontal = Boolean.parseBoolean(mWrapHorizontal);
				}catch (Throwable e){}
				mWrapHorizontal = "";
				try{
				mCurrentData.mWrapVertical = Boolean.parseBoolean(mWrapVertical);
				}catch (Throwable e){}
				mWrapVertical = "";
				try{
				mCurrentData.mAllowTileOverlap = Boolean.parseBoolean(mAllowTileOverlap);
				}catch (Throwable e){}
				mAllowTileOverlap = "";
				try{
				mCurrentData.msizePerLayer = Boolean.parseBoolean(msizePerLayer);
				}catch (Throwable e){}
				msizePerLayer = "";
				mCurrentData.mGridSettings = mGridSettings;
				mGridSettings = "";
				try{
				mCurrentData.mLayerSaveTyp = Integer.parseInt(mLayerSaveTyp);
				}catch (Throwable e){}
				mLayerSaveTyp = "";
				mMapData.put(mCurrentData.mName, mCurrentData);
				mCurrentData = null;
			}
		}
		mCurrentElement = null;
	}
}
