package csa.tiletools.map;

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import java.util.*;
public class  LayerDataXMLHandler extends DefaultHandler
{
	private HashMap<String, LayerData> mLayerData;
	private LayerData mCurrentData = null;
	private String mCurrentElement = null;
	private String mWrapHorizontal = "";
	private String mWrapVertical = "";
	private String mLayerName = "";
	private String mverticalSpeed = "";
	private String mhorizontalSpeed = "";
	private String mmustBePainted = "";
	private String misBackgroundCollision = "";
	private String misForgroundCollision = "";
	private String mpurposeTyp = "";
	private String misChangeable = "";
	private String mbackgroundColor = "";
	private String mLayerNumber = "";
	private String mtileX = "";
	private Vector<Integer> mtileXs = null;
	private String mtileY = "";
	private Vector<Integer> mtileYs = null;
	private String misBackground = "";
	private String mtileName = "";
	private Vector<String> mtileNames = null;
	private String misVisible = "";
	private String mscrollBehaviour = "";
	private String mallowTileOverlap = "";
	private String musedTileCollection = "";
	private Vector<String> musedTileCollections = null;
	private String moffsetX = "";
	private String moffsetY = "";
	private String misLayerOrdered = "";
	private String misLayerCompleteOrdered = "";
	private String misAllTilesSameSize = "";
	private String misRegularGrid = "";
	private String msaveTyp = "";
	private String monlyOneTileOnOnePosition = "";
	private String mISOBlockWrap = "";
	private String mSpriteFilename = "";
	private Vector<String> mSpriteFilenames = null;
	private String mSpriteName = "";
	private Vector<String> mSpriteNames = null;
	private String mSpriteStartX = "";
	private Vector<Integer> mSpriteStartXs = null;
	private String mSpriteStartY = "";
	private Vector<Integer> mSpriteStartYs = null;
	private String mSpriteStartAction = "";
	private Vector<String> mSpriteStartActions = null;
	private String mSpriteStartDirection = "";
	private Vector<String> mSpriteStartDirections = null;
	private String mSpriteCoordinator = "";
	private Vector<String> mSpriteCoordinators = null;
	private String mFogFileName = "";
	private String mFogName = "";
	public HashMap<String, LayerData> getLastHashMap()
	{
		return mLayerData;
	}
	@Override public void startDocument() throws SAXException
	{
		mCurrentData = new LayerData();
		mLayerData = new HashMap<String, LayerData>();
	}
	@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("LayerData"))
		{
			mCurrentData = new LayerData();
			mWrapHorizontal = "";
			mWrapVertical = "";
			mLayerName = "";
			mverticalSpeed = "";
			mhorizontalSpeed = "";
			mmustBePainted = "";
			misBackgroundCollision = "";
			misForgroundCollision = "";
			mpurposeTyp = "";
			misChangeable = "";
			mbackgroundColor = "";
			mLayerNumber = "";
			mtileX = "";
			mtileXs = new Vector<Integer>();
			mtileY = "";
			mtileYs = new Vector<Integer>();
			misBackground = "";
			mtileName = "";
			mtileNames = new Vector<String>();
			misVisible = "";
			mscrollBehaviour = "";
			mallowTileOverlap = "";
			musedTileCollection = "";
			musedTileCollections = new Vector<String>();
			moffsetX = "";
			moffsetY = "";
			misLayerOrdered = "";
			misLayerCompleteOrdered = "";
			misAllTilesSameSize = "";
			misRegularGrid = "";
			msaveTyp = "";
			monlyOneTileOnOnePosition = "";
			mISOBlockWrap = "";
			mSpriteFilename = "";
			mSpriteFilenames = new Vector<String>();
			mSpriteName = "";
			mSpriteNames = new Vector<String>();
			mSpriteStartX = "";
			mSpriteStartXs = new Vector<Integer>();
			mSpriteStartY = "";
			mSpriteStartYs = new Vector<Integer>();
			mSpriteStartAction = "";
			mSpriteStartActions = new Vector<String>();
			mSpriteStartDirection = "";
			mSpriteStartDirections = new Vector<String>();
			mSpriteCoordinator = "";
			mSpriteCoordinators = new Vector<String>();
			mFogFileName = "";
			mFogName = "";
		}
	}
	@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("WrapHorizontal")) mWrapHorizontal += s;
		if (mCurrentElement.equalsIgnoreCase("WrapVerticalHorizontal")) mWrapVertical += s;
		if (mCurrentElement.equalsIgnoreCase("LayerName")) mLayerName += s;
		if (mCurrentElement.equalsIgnoreCase("verticalSpeed")) mverticalSpeed += s;
		if (mCurrentElement.equalsIgnoreCase("horizontalSpeed")) mhorizontalSpeed += s;
		if (mCurrentElement.equalsIgnoreCase("mustBePainted")) mmustBePainted += s;
		if (mCurrentElement.equalsIgnoreCase("isBackgroundCollision")) misBackgroundCollision += s;
		if (mCurrentElement.equalsIgnoreCase("isForgroundCollision")) misForgroundCollision += s;
		if (mCurrentElement.equalsIgnoreCase("purposeTyp")) mpurposeTyp += s;
		if (mCurrentElement.equalsIgnoreCase("isChangeable")) misChangeable += s;
		if (mCurrentElement.equalsIgnoreCase("backgroundColor")) mbackgroundColor += s;
		if (mCurrentElement.equalsIgnoreCase("LayerNumber")) mLayerNumber += s;
		if (mCurrentElement.equalsIgnoreCase("tileX")) mtileX += s;
		if (mCurrentElement.equalsIgnoreCase("tileY")) mtileY += s;
		if (mCurrentElement.equalsIgnoreCase("isBackground")) misBackground += s;
		if (mCurrentElement.equalsIgnoreCase("tileName")) mtileName += s;
		if (mCurrentElement.equalsIgnoreCase("isVisible")) misVisible += s;
		if (mCurrentElement.equalsIgnoreCase("scrollBehaviour")) mscrollBehaviour += s;
		if (mCurrentElement.equalsIgnoreCase("allowTileOverlap")) mallowTileOverlap += s;
		if (mCurrentElement.equalsIgnoreCase("usedTileCollection")) musedTileCollection += s;
		if (mCurrentElement.equalsIgnoreCase("offsetX")) moffsetX += s;
		if (mCurrentElement.equalsIgnoreCase("offsetY")) moffsetY += s;
		if (mCurrentElement.equalsIgnoreCase("isLayerOrdered")) misLayerOrdered += s;
		if (mCurrentElement.equalsIgnoreCase("isLayerCompleteOrdered")) misLayerCompleteOrdered += s;
		if (mCurrentElement.equalsIgnoreCase("isAllTilesSameSize")) misAllTilesSameSize += s;
		if (mCurrentElement.equalsIgnoreCase("isRegularGrid")) misRegularGrid += s;
		if (mCurrentElement.equalsIgnoreCase("saveTyp")) msaveTyp += s;
		if (mCurrentElement.equalsIgnoreCase("onlyOneTileOnOnePosition")) monlyOneTileOnOnePosition += s;
		if (mCurrentElement.equalsIgnoreCase("ISOBlockWrap")) mISOBlockWrap += s;
		if (mCurrentElement.equalsIgnoreCase("SpriteFilename")) mSpriteFilename += s;
		if (mCurrentElement.equalsIgnoreCase("SpriteName")) mSpriteName += s;
		if (mCurrentElement.equalsIgnoreCase("SpriteStartX")) mSpriteStartX += s;
		if (mCurrentElement.equalsIgnoreCase("SpriteStartY")) mSpriteStartY += s;
		if (mCurrentElement.equalsIgnoreCase("SpriteStartAction")) mSpriteStartAction += s;
		if (mCurrentElement.equalsIgnoreCase("SpriteStartDirection")) mSpriteStartDirection += s;
		if (mCurrentElement.equalsIgnoreCase("SpriteCoordinator")) mSpriteCoordinator += s;
		if (mCurrentElement.equalsIgnoreCase("FogFileName")) mFogFileName += s;
		if (mCurrentElement.equalsIgnoreCase("FogName")) mFogName += s;
	}
	@Override public void endElement(String uri, String localName, String qName) throws SAXException
	{
		if ("tileX".equalsIgnoreCase(qName))
		{
			try{
			mtileXs.addElement(Integer.parseInt(mtileX));
			}catch (Throwable e){}
			mtileX="";
		}
		if ("tileY".equalsIgnoreCase(qName))
		{
			try{
			mtileYs.addElement(Integer.parseInt(mtileY));
			}catch (Throwable e){}
			mtileY="";
		}
		if ("tileName".equalsIgnoreCase(qName))
		{
			mtileNames.addElement(mtileName);
			mtileName="";
		}
		if ("usedTileCollection".equalsIgnoreCase(qName))
		{
			musedTileCollections.addElement(musedTileCollection);
			musedTileCollection="";
		}
		if ("SpriteFilename".equalsIgnoreCase(qName))
		{
			mSpriteFilenames.addElement(mSpriteFilename);
			mSpriteFilename="";
		}
		if ("SpriteName".equalsIgnoreCase(qName))
		{
			mSpriteNames.addElement(mSpriteName);
			mSpriteName="";
		}
		if ("SpriteStartX".equalsIgnoreCase(qName))
		{
			try{
			mSpriteStartXs.addElement(Integer.parseInt(mSpriteStartX));
			}catch (Throwable e){}
			mSpriteStartX="";
		}
		if ("SpriteStartY".equalsIgnoreCase(qName))
		{
			try{
			mSpriteStartYs.addElement(Integer.parseInt(mSpriteStartY));
			}catch (Throwable e){}
			mSpriteStartY="";
		}
		if ("SpriteStartAction".equalsIgnoreCase(qName))
		{
			mSpriteStartActions.addElement(mSpriteStartAction);
			mSpriteStartAction="";
		}
		if ("SpriteStartDirection".equalsIgnoreCase(qName))
		{
			mSpriteStartDirections.addElement(mSpriteStartDirection);
			mSpriteStartDirection="";
		}
		if ("SpriteCoordinator".equalsIgnoreCase(qName))
		{
			mSpriteCoordinators.addElement(mSpriteCoordinator);
			mSpriteCoordinator="";
		}
		if ("LayerData".equalsIgnoreCase(qName))
		{
			if (mCurrentData != null)
			{
				try{
				mCurrentData.mWrapHorizontal = Boolean.parseBoolean(mWrapHorizontal);
				}catch (Throwable e){}
				mWrapHorizontal = "";
				try{
				mCurrentData.mWrapVertical = Boolean.parseBoolean(mWrapVertical);
				}catch (Throwable e){}
				mWrapVertical = "";
				mCurrentData.mLayerName = mLayerName;
				mLayerName = "";
				try{
				mCurrentData.mverticalSpeed = Integer.parseInt(mverticalSpeed);
				}catch (Throwable e){}
				mverticalSpeed = "";
				try{
				mCurrentData.mhorizontalSpeed = Integer.parseInt(mhorizontalSpeed);
				}catch (Throwable e){}
				mhorizontalSpeed = "";
				try{
				mCurrentData.mmustBePainted = Boolean.parseBoolean(mmustBePainted);
				}catch (Throwable e){}
				mmustBePainted = "";
				try{
				mCurrentData.misBackgroundCollision = Boolean.parseBoolean(misBackgroundCollision);
				}catch (Throwable e){}
				misBackgroundCollision = "";
				try{
				mCurrentData.misForgroundCollision = Boolean.parseBoolean(misForgroundCollision);
				}catch (Throwable e){}
				misForgroundCollision = "";
				mCurrentData.mpurposeTyp = mpurposeTyp;
				mpurposeTyp = "";
				try{
				mCurrentData.misChangeable = Boolean.parseBoolean(misChangeable);
				}catch (Throwable e){}
				misChangeable = "";
				try{
				mCurrentData.mbackgroundColor = Integer.parseInt(mbackgroundColor);
				}catch (Throwable e){}
				mbackgroundColor = "";
				try{
				mCurrentData.mLayerNumber = Integer.parseInt(mLayerNumber);
				}catch (Throwable e){}
				mLayerNumber = "";
				mtileX = "";
				mCurrentData.mtileX = mtileXs;
				mtileY = "";
				mCurrentData.mtileY = mtileYs;
				try{
				mCurrentData.misBackground = Boolean.parseBoolean(misBackground);
				}catch (Throwable e){}
				misBackground = "";
				mtileName = "";
				mCurrentData.mtileName = mtileNames;
				try{
				mCurrentData.misVisible = Boolean.parseBoolean(misVisible);
				}catch (Throwable e){}
				misVisible = "";
				mCurrentData.mscrollBehaviour = mscrollBehaviour;
				mscrollBehaviour = "";
				try{
				mCurrentData.mallowTileOverlap = Boolean.parseBoolean(mallowTileOverlap);
				}catch (Throwable e){}
				mallowTileOverlap = "";
				musedTileCollection = "";
				mCurrentData.musedTileCollection = musedTileCollections;
				try{
				mCurrentData.moffsetX = Integer.parseInt(moffsetX);
				}catch (Throwable e){}
				moffsetX = "";
				try{
				mCurrentData.moffsetY = Integer.parseInt(moffsetY);
				}catch (Throwable e){}
				moffsetY = "";
				try{
				mCurrentData.misLayerOrdered = Boolean.parseBoolean(misLayerOrdered);
				}catch (Throwable e){}
				misLayerOrdered = "";
				try{
				mCurrentData.misLayerCompleteOrdered = Boolean.parseBoolean(misLayerCompleteOrdered);
				}catch (Throwable e){}
				misLayerCompleteOrdered = "";
				try{
				mCurrentData.misAllTilesSameSize = Boolean.parseBoolean(misAllTilesSameSize);
				}catch (Throwable e){}
				misAllTilesSameSize = "";
				try{
				mCurrentData.misRegularGrid = Boolean.parseBoolean(misRegularGrid);
				}catch (Throwable e){}
				misRegularGrid = "";
				try{
				mCurrentData.msaveTyp = Integer.parseInt(msaveTyp);
				}catch (Throwable e){}
				msaveTyp = "";
				try{
				mCurrentData.monlyOneTileOnOnePosition = Boolean.parseBoolean(monlyOneTileOnOnePosition);
				}catch (Throwable e){}
				monlyOneTileOnOnePosition = "";
				try{
				mCurrentData.mISOBlockWrap = Boolean.parseBoolean(mISOBlockWrap);
				}catch (Throwable e){}
				mISOBlockWrap = "";
				mSpriteFilename = "";
				mCurrentData.mSpriteFilename = mSpriteFilenames;
				mSpriteName = "";
				mCurrentData.mSpriteName = mSpriteNames;
				mSpriteStartX = "";
				mCurrentData.mSpriteStartX = mSpriteStartXs;
				mSpriteStartY = "";
				mCurrentData.mSpriteStartY = mSpriteStartYs;
				mSpriteStartAction = "";
				mCurrentData.mSpriteStartAction = mSpriteStartActions;
				mSpriteStartDirection = "";
				mCurrentData.mSpriteStartDirection = mSpriteStartDirections;
				mSpriteCoordinator = "";
				mCurrentData.mSpriteCoordinator = mSpriteCoordinators;
				mCurrentData.mFogFileName = mFogFileName;
				mFogFileName = "";
				mCurrentData.mFogName = mFogName;
				mFogName = "";
				mLayerData.put(mCurrentData.mName, mCurrentData);
				mCurrentData = null;
			}
		}
		mCurrentElement = null;
	}
}
