package csa.tiletools.sprites;

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import java.util.*;
public class  SpriteDataXMLHandler extends DefaultHandler
{
	private HashMap<String, SpriteData> mSpriteData;
	private SpriteData 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 mForceSpriteSize = "";
	private String mActionString = "";
	private Vector<String> mActionStrings = null;
	private String mImageSetName = "";
	private Vector<String> mImageSetNames = null;
	private String mActionDirectionImageName = "";
	private Vector<String> mActionDirectionImageNames = null;
	private String mStepsAbsolut = "";
	private String mActionDirectionAnimationStepDeltaX = "";
	private Vector<Integer> mActionDirectionAnimationStepDeltaXs = null;
	private String mActionDirectionAnimationStepDeltaY = "";
	private Vector<Integer> mActionDirectionAnimationStepDeltaYs = null;
	private String mSubsequentAction = "";
	private Vector<String> mSubsequentActions = null;
	private String mLoopAction = "";
	private Vector<Boolean> mLoopActions = null;
	private String mPoolName = "";
	private String mAutomaticMovment = "";
	private String mUpdateWhenNotInViewport = "";
	private String mBackShadow = "";
	private String mBackThickness = "";
	private String mBackAntialiazing = "";
	private String mBack_R = "";
	private String mBack_G = "";
	private String mBack_B = "";
	public HashMap<String, SpriteData> getLastHashMap()
	{
		return mSpriteData;
	}
	@Override public void startDocument() throws SAXException
	{
		mCurrentData = new SpriteData();
		mSpriteData = new HashMap<String, SpriteData>();
	}
	@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("SpriteData"))
		{
			mCurrentData = new SpriteData();
			mWidth = "";
			mHeight = "";
			mType = "";
			mAtrributeName = "";
			mAtrributeNames = new Vector<String>();
			mAttributeType = "";
			mAttributeTypes = new Vector<String>();
			mAttributeValue = "";
			mAttributeValues = new Vector<String>();
			mID = "";
			mDescription = "";
			mForceSpriteSize = "";
			mActionString = "";
			mActionStrings = new Vector<String>();
			mImageSetName = "";
			mImageSetNames = new Vector<String>();
			mActionDirectionImageName = "";
			mActionDirectionImageNames = new Vector<String>();
			mStepsAbsolut = "";
			mActionDirectionAnimationStepDeltaX = "";
			mActionDirectionAnimationStepDeltaXs = new Vector<Integer>();
			mActionDirectionAnimationStepDeltaY = "";
			mActionDirectionAnimationStepDeltaYs = new Vector<Integer>();
			mSubsequentAction = "";
			mSubsequentActions = new Vector<String>();
			mLoopAction = "";
			mLoopActions = new Vector<Boolean>();
			mPoolName = "";
			mAutomaticMovment = "";
			mUpdateWhenNotInViewport = "";
			mBackShadow = "";
			mBackThickness = "";
			mBackAntialiazing = "";
			mBack_R = "";
			mBack_G = "";
			mBack_B = "";
		}
	}
	@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("ForceSpriteSize")) mForceSpriteSize += s;
		if (mCurrentElement.equalsIgnoreCase("ActionString")) mActionString += s;
		if (mCurrentElement.equalsIgnoreCase("ImageSetName")) mImageSetName += s;
		if (mCurrentElement.equalsIgnoreCase("ActionDirectionImageName")) mActionDirectionImageName += s;
		if (mCurrentElement.equalsIgnoreCase("StepsAbsolut")) mStepsAbsolut += s;
		if (mCurrentElement.equalsIgnoreCase("ActionDirectionAnimationStepDeltaX")) mActionDirectionAnimationStepDeltaX += s;
		if (mCurrentElement.equalsIgnoreCase("ActionDirectionAnimationStepDeltaY")) mActionDirectionAnimationStepDeltaY += s;
		if (mCurrentElement.equalsIgnoreCase("SubsequentAction")) mSubsequentAction += s;
		if (mCurrentElement.equalsIgnoreCase("LoopAction")) mLoopAction += s;
		if (mCurrentElement.equalsIgnoreCase("PoolName")) mPoolName += s;
		if (mCurrentElement.equalsIgnoreCase("AutomaticMovment")) mAutomaticMovment += s;
		if (mCurrentElement.equalsIgnoreCase("UpdateWhenNotInViewport")) mUpdateWhenNotInViewport += s;
		if (mCurrentElement.equalsIgnoreCase("BackShadow")) mBackShadow += s;
		if (mCurrentElement.equalsIgnoreCase("BackThickness")) mBackThickness += s;
		if (mCurrentElement.equalsIgnoreCase("BackAntialiazing")) mBackAntialiazing += s;
		if (mCurrentElement.equalsIgnoreCase("Back_R")) mBack_R += s;
		if (mCurrentElement.equalsIgnoreCase("Back_G")) mBack_G += s;
		if (mCurrentElement.equalsIgnoreCase("Back_B")) mBack_B += 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 ("ActionString".equalsIgnoreCase(qName))
		{
			mActionStrings.addElement(mActionString);
			mActionString="";
		}
		if ("ImageSetName".equalsIgnoreCase(qName))
		{
			mImageSetNames.addElement(mImageSetName);
			mImageSetName="";
		}
		if ("ActionDirectionImageName".equalsIgnoreCase(qName))
		{
			mActionDirectionImageNames.addElement(mActionDirectionImageName);
			mActionDirectionImageName="";
		}
		if ("ActionDirectionAnimationStepDeltaX".equalsIgnoreCase(qName))
		{
			try{
			mActionDirectionAnimationStepDeltaXs.addElement(Integer.parseInt(mActionDirectionAnimationStepDeltaX));
			}catch (Throwable e){}
			mActionDirectionAnimationStepDeltaX="";
		}
		if ("ActionDirectionAnimationStepDeltaY".equalsIgnoreCase(qName))
		{
			try{
			mActionDirectionAnimationStepDeltaYs.addElement(Integer.parseInt(mActionDirectionAnimationStepDeltaY));
			}catch (Throwable e){}
			mActionDirectionAnimationStepDeltaY="";
		}
		if ("SubsequentAction".equalsIgnoreCase(qName))
		{
			mSubsequentActions.addElement(mSubsequentAction);
			mSubsequentAction="";
		}
		if ("LoopAction".equalsIgnoreCase(qName))
		{
			try{
			mLoopActions.addElement(Boolean.parseBoolean(mLoopAction));
			}catch (Throwable e){}
			mLoopAction="";
		}
		if ("SpriteData".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 = "";
				try{
				mCurrentData.mForceSpriteSize = Boolean.parseBoolean(mForceSpriteSize);
				}catch (Throwable e){}
				mForceSpriteSize = "";
				mActionString = "";
				mCurrentData.mActionString = mActionStrings;
				mImageSetName = "";
				mCurrentData.mImageSetName = mImageSetNames;
				mActionDirectionImageName = "";
				mCurrentData.mActionDirectionImageName = mActionDirectionImageNames;
				try{
				mCurrentData.mStepsAbsolut = Boolean.parseBoolean(mStepsAbsolut);
				}catch (Throwable e){}
				mStepsAbsolut = "";
				mActionDirectionAnimationStepDeltaX = "";
				mCurrentData.mActionDirectionAnimationStepDeltaX = mActionDirectionAnimationStepDeltaXs;
				mActionDirectionAnimationStepDeltaY = "";
				mCurrentData.mActionDirectionAnimationStepDeltaY = mActionDirectionAnimationStepDeltaYs;
				mSubsequentAction = "";
				mCurrentData.mSubsequentAction = mSubsequentActions;
				mLoopAction = "";
				mCurrentData.mLoopAction = mLoopActions;
				mCurrentData.mPoolName = mPoolName;
				mPoolName = "";
				try{
				mCurrentData.mAutomaticMovment = Boolean.parseBoolean(mAutomaticMovment);
				}catch (Throwable e){}
				mAutomaticMovment = "";
				try{
				mCurrentData.mUpdateWhenNotInViewport = Boolean.parseBoolean(mUpdateWhenNotInViewport);
				}catch (Throwable e){}
				mUpdateWhenNotInViewport = "";
				try{
				mCurrentData.mBackShadow = Boolean.parseBoolean(mBackShadow);
				}catch (Throwable e){}
				mBackShadow = "";
				try{
				mCurrentData.mBackThickness = Integer.parseInt(mBackThickness);
				}catch (Throwable e){}
				mBackThickness = "";
				try{
				mCurrentData.mBackAntialiazing = Integer.parseInt(mBackAntialiazing);
				}catch (Throwable e){}
				mBackAntialiazing = "";
				try{
				mCurrentData.mBack_R = Integer.parseInt(mBack_R);
				}catch (Throwable e){}
				mBack_R = "";
				try{
				mCurrentData.mBack_G = Integer.parseInt(mBack_G);
				}catch (Throwable e){}
				mBack_G = "";
				try{
				mCurrentData.mBack_B = Integer.parseInt(mBack_B);
				}catch (Throwable e){}
				mBack_B = "";
				mSpriteData.put(mCurrentData.mName, mCurrentData);
				mCurrentData = null;
			}
		}
		mCurrentElement = null;
	}
}
