package csa.jmom.unit;

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import java.util.*;
public class  UnitDataXMLHandler extends DefaultHandler
{
	private HashMap<String, UnitData> mUnitData;
	private UnitData mCurrentData = null;
	private String mCurrentElement = null;
	private String mPeople = "";
	private String mBattlefieldCompositeTileName = "";
	private Vector<String> mBattlefieldCompositeTileNames = null;
	private String mBattlefieldCompositeFileName = "";
	private Vector<String> mBattlefieldCompositeFileNames = null;
	private String mWorldCompositeTileName = "";
	private Vector<String> mWorldCompositeTileNames = null;
	private String mWorldCompositeFileName = "";
	private Vector<String> mWorldCompositeFileNames = null;
	private String mNumberOfSingleUnits = "";
	private String mNameOfSingleBattlefieldUnitTile = "";
	private String mBaseMeleeAttack = "";
	private String mBaseMeleeDefense = "";
	private String mBaseRangeAttack = "";
	private String mBaseRangeDefense = "";
	private String mBaseSpellPoints = "";
	private String mBaseSpellHit = "";
	private String mBaseSpellResistence = "";
	private String mKnownSpells = "";
	private Vector<String> mKnownSpellss = null;
	private String mStackPriority = "";
	private String mEndurance = "";
	private String mAbilities = "";
	private Vector<String> mAbilitiess = null;
	public HashMap<String, UnitData> getLastHashMap()
	{
		return mUnitData;
	}
	@Override public void startDocument() throws SAXException
	{
		mCurrentData = new UnitData();
		mUnitData = new HashMap<String, UnitData>();
	}
	@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("UnitData"))
		{
			mCurrentData = new UnitData();
			mPeople = "";
			mBattlefieldCompositeTileName = "";
			mBattlefieldCompositeTileNames = new Vector<String>();
			mBattlefieldCompositeFileName = "";
			mBattlefieldCompositeFileNames = new Vector<String>();
			mWorldCompositeTileName = "";
			mWorldCompositeTileNames = new Vector<String>();
			mWorldCompositeFileName = "";
			mWorldCompositeFileNames = new Vector<String>();
			mNumberOfSingleUnits = "";
			mNameOfSingleBattlefieldUnitTile = "";
			mBaseMeleeAttack = "";
			mBaseMeleeDefense = "";
			mBaseRangeAttack = "";
			mBaseRangeDefense = "";
			mBaseSpellPoints = "";
			mBaseSpellHit = "";
			mBaseSpellResistence = "";
			mKnownSpells = "";
			mKnownSpellss = new Vector<String>();
			mStackPriority = "";
			mEndurance = "";
			mAbilities = "";
			mAbilitiess = 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("People")) mPeople += s;
		if (mCurrentElement.equalsIgnoreCase("BattlefieldCompositeTileName")) mBattlefieldCompositeTileName += s;
		if (mCurrentElement.equalsIgnoreCase("BattlefieldCompositeFileName")) mBattlefieldCompositeFileName += s;
		if (mCurrentElement.equalsIgnoreCase("WorldCompositeTileName")) mWorldCompositeTileName += s;
		if (mCurrentElement.equalsIgnoreCase("WorldCompositeFileName")) mWorldCompositeFileName += s;
		if (mCurrentElement.equalsIgnoreCase("NumberOfSingleUnits")) mNumberOfSingleUnits += s;
		if (mCurrentElement.equalsIgnoreCase("NameOfSingleBattlefieldUnitTile")) mNameOfSingleBattlefieldUnitTile += s;
		if (mCurrentElement.equalsIgnoreCase("NameOfSingleBattlefieldUnitTile")) mBaseMeleeAttack += s;
		if (mCurrentElement.equalsIgnoreCase("BaseMeleeDefense")) mBaseMeleeDefense += s;
		if (mCurrentElement.equalsIgnoreCase("BaseRangeAttack")) mBaseRangeAttack += s;
		if (mCurrentElement.equalsIgnoreCase("BaseRangeDefense")) mBaseRangeDefense += s;
		if (mCurrentElement.equalsIgnoreCase("BaseSpellPoints")) mBaseSpellPoints += s;
		if (mCurrentElement.equalsIgnoreCase("BaseSpellHit")) mBaseSpellHit += s;
		if (mCurrentElement.equalsIgnoreCase("BaseSpellResistence")) mBaseSpellResistence += s;
		if (mCurrentElement.equalsIgnoreCase("KnownSpells")) mKnownSpells += s;
		if (mCurrentElement.equalsIgnoreCase("StackPriority")) mStackPriority += s;
		if (mCurrentElement.equalsIgnoreCase("Endurance")) mEndurance += s;
		if (mCurrentElement.equalsIgnoreCase("Abilities")) mAbilities += s;
	}
	@Override public void endElement(String uri, String localName, String qName) throws SAXException
	{
		if ("BattlefieldCompositeTileName".equalsIgnoreCase(qName))
		{
			mBattlefieldCompositeTileNames.addElement(mBattlefieldCompositeTileName);
			mBattlefieldCompositeTileName="";
		}
		if ("BattlefieldCompositeFileName".equalsIgnoreCase(qName))
		{
			mBattlefieldCompositeFileNames.addElement(mBattlefieldCompositeFileName);
			mBattlefieldCompositeFileName="";
		}
		if ("WorldCompositeTileName".equalsIgnoreCase(qName))
		{
			mWorldCompositeTileNames.addElement(mWorldCompositeTileName);
			mWorldCompositeTileName="";
		}
		if ("WorldCompositeFileName".equalsIgnoreCase(qName))
		{
			mWorldCompositeFileNames.addElement(mWorldCompositeFileName);
			mWorldCompositeFileName="";
		}
		if ("KnownSpells".equalsIgnoreCase(qName))
		{
			mKnownSpellss.addElement(mKnownSpells);
			mKnownSpells="";
		}
		if ("Abilities".equalsIgnoreCase(qName))
		{
			mAbilitiess.addElement(mAbilities);
			mAbilities="";
		}
		if ("UnitData".equalsIgnoreCase(qName))
		{
			if (mCurrentData != null)
			{
				mCurrentData.mPeople = mPeople;
				mPeople = "";
				mBattlefieldCompositeTileName = "";
				mCurrentData.mBattlefieldCompositeTileName = mBattlefieldCompositeTileNames;
				mBattlefieldCompositeFileName = "";
				mCurrentData.mBattlefieldCompositeFileName = mBattlefieldCompositeFileNames;
				mWorldCompositeTileName = "";
				mCurrentData.mWorldCompositeTileName = mWorldCompositeTileNames;
				mWorldCompositeFileName = "";
				mCurrentData.mWorldCompositeFileName = mWorldCompositeFileNames;
				try{
				mCurrentData.mNumberOfSingleUnits = Integer.parseInt(mNumberOfSingleUnits);
				}catch (Throwable e){}
				mNumberOfSingleUnits = "";
				mCurrentData.mNameOfSingleBattlefieldUnitTile = mNameOfSingleBattlefieldUnitTile;
				mNameOfSingleBattlefieldUnitTile = "";
				try{
				mCurrentData.mBaseMeleeAttack = Integer.parseInt(mBaseMeleeAttack);
				}catch (Throwable e){}
				mBaseMeleeAttack = "";
				try{
				mCurrentData.mBaseMeleeDefense = Integer.parseInt(mBaseMeleeDefense);
				}catch (Throwable e){}
				mBaseMeleeDefense = "";
				try{
				mCurrentData.mBaseRangeAttack = Integer.parseInt(mBaseRangeAttack);
				}catch (Throwable e){}
				mBaseRangeAttack = "";
				try{
				mCurrentData.mBaseRangeDefense = Integer.parseInt(mBaseRangeDefense);
				}catch (Throwable e){}
				mBaseRangeDefense = "";
				try{
				mCurrentData.mBaseSpellPoints = Integer.parseInt(mBaseSpellPoints);
				}catch (Throwable e){}
				mBaseSpellPoints = "";
				try{
				mCurrentData.mBaseSpellHit = Integer.parseInt(mBaseSpellHit);
				}catch (Throwable e){}
				mBaseSpellHit = "";
				try{
				mCurrentData.mBaseSpellResistence = Integer.parseInt(mBaseSpellResistence);
				}catch (Throwable e){}
				mBaseSpellResistence = "";
				mKnownSpells = "";
				mCurrentData.mKnownSpells = mKnownSpellss;
				try{
				mCurrentData.mStackPriority = Integer.parseInt(mStackPriority);
				}catch (Throwable e){}
				mStackPriority = "";
				try{
				mCurrentData.mEndurance = Integer.parseInt(mEndurance);
				}catch (Throwable e){}
				mEndurance = "";
				mAbilities = "";
				mCurrentData.mAbilities = mAbilitiess;
				mUnitData.put(mCurrentData.mName, mCurrentData);
				mCurrentData = null;
			}
		}
		mCurrentElement = null;
	}
}
