package csa.tiletools.tiles;

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import java.util.*;
public class  TileDataXMLHandler extends DefaultHandler
{
	private HashMap<String, TileData> mTileData;
	private TileData mCurrentData = null;
	private String mCurrentElement = null;
	private String mWidth = "";
	private String mHeight = "";
	private String mType = "";
	private String mAtrributeName = "";
	private Vector<String> mAtrributeNames = null;
	private String mAttributeType = "";
	private Vector<String> mAttributeTypes = null;
	private String mAttributeValue = "";
	private Vector<String> mAttributeValues = null;
	private String mID = "";
	private String mDescription = "";
	private String mImageDataFile = "";
	private String mImageDataName = "";
	private String mForceTileSize = "";
	private String mTileActionLoop = "";
	private String mTileActionCanBeBroken = "";
	private String mTileActionSound = "";
	private String mTileActionIDs = "";
	private Vector<String> mTileActionIDss = null;
	private String mTileActionTiles = "";
	private Vector<String> mTileActionTiless = null;
	private String mTileSetFileName = "";
	private String mIsTransitionSource = "";
	private String mTransitionPriority = "";
	private String mTransitionType = "";
	private String mTransitionChildrenNames = "";
	private Vector<String> mTransitionChildrenNamess = null;
	private String mVariantNames = "";
	private Vector<String> mVariantNamess = null;
	private String mVariantPercent = "";
	private String mIsCompositeTile = "";
	private String mCompositeFile = "";
	private String mCompositeClass = "";
	private String mCompositeName = "";
	private String mISOHeight = "";
	private String mISOHeightOffset = "";
	private String mISOBaseline = "";
	private String mISOInherentOffset = "";
	public HashMap<String, TileData> getLastHashMap()
	{
		return mTileData;
	}
	@Override public void startDocument() throws SAXException
	{
		mCurrentData = new TileData();
		mTileData = new HashMap<String, TileData>();
	}
	@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("TileData"))
		{
			mCurrentData = new TileData();
			mWidth = "";
			mHeight = "";
			mType = "";
			mAtrributeName = "";
			mAtrributeNames = new Vector<String>();
			mAttributeType = "";
			mAttributeTypes = new Vector<String>();
			mAttributeValue = "";
			mAttributeValues = new Vector<String>();
			mID = "";
			mDescription = "";
			mImageDataFile = "";
			mImageDataName = "";
			mForceTileSize = "";
			mTileActionLoop = "";
			mTileActionCanBeBroken = "";
			mTileActionSound = "";
			mTileActionIDs = "";
			mTileActionIDss = new Vector<String>();
			mTileActionTiles = "";
			mTileActionTiless = new Vector<String>();
			mTileSetFileName = "";
			mIsTransitionSource = "";
			mTransitionPriority = "";
			mTransitionType = "";
			mTransitionChildrenNames = "";
			mTransitionChildrenNamess = new Vector<String>();
			mVariantNames = "";
			mVariantNamess = new Vector<String>();
			mVariantPercent = "";
			mIsCompositeTile = "";
			mCompositeFile = "";
			mCompositeClass = "";
			mCompositeName = "";
			mISOHeight = "";
			mISOHeightOffset = "";
			mISOBaseline = "";
			mISOInherentOffset = "";
		}
	}
	@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("WIDTH")) mWidth += s;
		if (mCurrentElement.equalsIgnoreCase("HEIGHT")) mHeight += s;
		if (mCurrentElement.equalsIgnoreCase("TYPE")) mType += s;
		if (mCurrentElement.equalsIgnoreCase("ATTRIBUTNAME")) mAtrributeName += s;
		if (mCurrentElement.equalsIgnoreCase("ATTRIBUTETYPE")) mAttributeType += s;
		if (mCurrentElement.equalsIgnoreCase("ATTRIBUTEVALUE")) mAttributeValue += s;
		if (mCurrentElement.equalsIgnoreCase("ID")) mID += s;
		if (mCurrentElement.equalsIgnoreCase("DESCRIPTION")) mDescription += s;
		if (mCurrentElement.equalsIgnoreCase("IMAGEDATAFILE")) mImageDataFile += s;
		if (mCurrentElement.equalsIgnoreCase("IMAGEDATENAME")) mImageDataName += s;
		if (mCurrentElement.equalsIgnoreCase("ForceTileSize")) mForceTileSize += s;
		if (mCurrentElement.equalsIgnoreCase("TileActionLoop")) mTileActionLoop += s;
		if (mCurrentElement.equalsIgnoreCase("TileActionCanBeBroken")) mTileActionCanBeBroken += s;
		if (mCurrentElement.equalsIgnoreCase("TileActionSound")) mTileActionSound += s;
		if (mCurrentElement.equalsIgnoreCase("TileActionIDs")) mTileActionIDs += s;
		if (mCurrentElement.equalsIgnoreCase("TileActionTiles")) mTileActionTiles += s;
		if (mCurrentElement.equalsIgnoreCase("TileSetFileName")) mTileSetFileName += s;
		if (mCurrentElement.equalsIgnoreCase("IsTransitionSource")) mIsTransitionSource += s;
		if (mCurrentElement.equalsIgnoreCase("TransitionPriority")) mTransitionPriority += s;
		if (mCurrentElement.equalsIgnoreCase("TransitionType")) mTransitionType += s;
		if (mCurrentElement.equalsIgnoreCase("TransitionChildrenNames")) mTransitionChildrenNames += s;
		if (mCurrentElement.equalsIgnoreCase("VariantNames")) mVariantNames += s;
		if (mCurrentElement.equalsIgnoreCase("VariantPercent")) mVariantPercent += s;
		if (mCurrentElement.equalsIgnoreCase("IsCompositeTile")) mIsCompositeTile += s;
		if (mCurrentElement.equalsIgnoreCase("CompositeFile")) mCompositeFile += s;
		if (mCurrentElement.equalsIgnoreCase("CompositeClass")) mCompositeClass += s;
		if (mCurrentElement.equalsIgnoreCase("CompositeName")) mCompositeName += s;
		if (mCurrentElement.equalsIgnoreCase("ISOHeight")) mISOHeight += s;
		if (mCurrentElement.equalsIgnoreCase("ISOHeightOffset")) mISOHeightOffset += s;
		if (mCurrentElement.equalsIgnoreCase("ISOBaseline")) mISOBaseline += s;
		if (mCurrentElement.equalsIgnoreCase("ISOInherentOffset")) mISOInherentOffset += s;
	}
	@Override public void endElement(String uri, String localName, String qName) throws SAXException
	{
		if ("ATTRIBUTNAME".equalsIgnoreCase(qName))
		{
			mAtrributeNames.addElement(mAtrributeName);
			mAtrributeName="";
		}
		if ("ATTRIBUTETYPE".equalsIgnoreCase(qName))
		{
			mAttributeTypes.addElement(mAttributeType);
			mAttributeType="";
		}
		if ("ATTRIBUTEVALUE".equalsIgnoreCase(qName))
		{
			mAttributeValues.addElement(mAttributeValue);
			mAttributeValue="";
		}
		if ("TileActionIDs".equalsIgnoreCase(qName))
		{
			mTileActionIDss.addElement(mTileActionIDs);
			mTileActionIDs="";
		}
		if ("TileActionTiles".equalsIgnoreCase(qName))
		{
			mTileActionTiless.addElement(mTileActionTiles);
			mTileActionTiles="";
		}
		if ("TransitionChildrenNames".equalsIgnoreCase(qName))
		{
			mTransitionChildrenNamess.addElement(mTransitionChildrenNames);
			mTransitionChildrenNames="";
		}
		if ("VariantNames".equalsIgnoreCase(qName))
		{
			mVariantNamess.addElement(mVariantNames);
			mVariantNames="";
		}
		if ("TileData".equalsIgnoreCase(qName))
		{
			if (mCurrentData != null)
			{
				try{
				mCurrentData.mWidth = Integer.parseInt(mWidth);
				}catch (Throwable e){}
				mWidth = "";
				try{
				mCurrentData.mHeight = Integer.parseInt(mHeight);
				}catch (Throwable e){}
				mHeight = "";
				try{
				mCurrentData.mType = Integer.parseInt(mType);
				}catch (Throwable e){}
				mType = "";
				mAtrributeName = "";
				mCurrentData.mAtrributeName = mAtrributeNames;
				mAttributeType = "";
				mCurrentData.mAttributeType = mAttributeTypes;
				mAttributeValue = "";
				mCurrentData.mAttributeValue = mAttributeValues;
				mCurrentData.mID = mID;
				mID = "";
				mCurrentData.mDescription = mDescription;
				mDescription = "";
				mCurrentData.mImageDataFile = mImageDataFile;
				mImageDataFile = "";
				mCurrentData.mImageDataName = mImageDataName;
				mImageDataName = "";
				try{
				mCurrentData.mForceTileSize = Boolean.parseBoolean(mForceTileSize);
				}catch (Throwable e){}
				mForceTileSize = "";
				try{
				mCurrentData.mTileActionLoop = Boolean.parseBoolean(mTileActionLoop);
				}catch (Throwable e){}
				mTileActionLoop = "";
				try{
				mCurrentData.mTileActionCanBeBroken = Boolean.parseBoolean(mTileActionCanBeBroken);
				}catch (Throwable e){}
				mTileActionCanBeBroken = "";
				mCurrentData.mTileActionSound = mTileActionSound;
				mTileActionSound = "";
				mTileActionIDs = "";
				mCurrentData.mTileActionIDs = mTileActionIDss;
				mTileActionTiles = "";
				mCurrentData.mTileActionTiles = mTileActionTiless;
				mCurrentData.mTileSetFileName = mTileSetFileName;
				mTileSetFileName = "";
				try{
				mCurrentData.mIsTransitionSource = Boolean.parseBoolean(mIsTransitionSource);
				}catch (Throwable e){}
				mIsTransitionSource = "";
				try{
				mCurrentData.mTransitionPriority = Integer.parseInt(mTransitionPriority);
				}catch (Throwable e){}
				mTransitionPriority = "";
				try{
				mCurrentData.mTransitionType = Integer.parseInt(mTransitionType);
				}catch (Throwable e){}
				mTransitionType = "";
				mTransitionChildrenNames = "";
				mCurrentData.mTransitionChildrenNames = mTransitionChildrenNamess;
				mVariantNames = "";
				mCurrentData.mVariantNames = mVariantNamess;
				try{
				mCurrentData.mVariantPercent = Integer.parseInt(mVariantPercent);
				}catch (Throwable e){}
				mVariantPercent = "";
				try{
				mCurrentData.mIsCompositeTile = Boolean.parseBoolean(mIsCompositeTile);
				}catch (Throwable e){}
				mIsCompositeTile = "";
				mCurrentData.mCompositeFile = mCompositeFile;
				mCompositeFile = "";
				mCurrentData.mCompositeClass = mCompositeClass;
				mCompositeClass = "";
				mCurrentData.mCompositeName = mCompositeName;
				mCompositeName = "";
				try{
				mCurrentData.mISOHeight = Integer.parseInt(mISOHeight);
				}catch (Throwable e){}
				mISOHeight = "";
				try{
				mCurrentData.mISOHeightOffset = Integer.parseInt(mISOHeightOffset);
				}catch (Throwable e){}
				mISOHeightOffset = "";
				try{
				mCurrentData.mISOBaseline = Integer.parseInt(mISOBaseline);
				}catch (Throwable e){}
				mISOBaseline = "";
				try{
				mCurrentData.mISOInherentOffset = Integer.parseInt(mISOInherentOffset);
				}catch (Throwable e){}
				mISOInherentOffset = "";
				mTileData.put(mCurrentData.mName, mCurrentData);
				mCurrentData = null;
			}
		}
		mCurrentElement = null;
	}
}
