package csa.tiletools.particle;

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import java.util.*;
public class  ParticleEngineDataXMLHandler extends DefaultHandler
{
	private HashMap<String, ParticleEngineData> mParticleEngineData;
	private ParticleEngineData mCurrentData = null;
	private String mCurrentElement = null;
	private String mEmitters = "";
	private Vector<String> mEmitterss = null;
	private String mWidth = "";
	private String mHeight = "";
	private String mTickResolution = "";
	public HashMap<String, ParticleEngineData> getLastHashMap()
	{
		return mParticleEngineData;
	}
	@Override public void startDocument() throws SAXException
	{
		mCurrentData = new ParticleEngineData();
		mParticleEngineData = new HashMap<String, ParticleEngineData>();
	}
	@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("ParticleEngineData"))
		{
			mCurrentData = new ParticleEngineData();
			mEmitters = "";
			mEmitterss = new Vector<String>();
			mWidth = "";
			mHeight = "";
			mTickResolution = "";
		}
	}
	@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("Emitters")) mEmitters += s;
		if (mCurrentElement.equalsIgnoreCase("Width")) mWidth += s;
		if (mCurrentElement.equalsIgnoreCase("Height")) mHeight += s;
		if (mCurrentElement.equalsIgnoreCase("TickResolution")) mTickResolution += s;
	}
	@Override public void endElement(String uri, String localName, String qName) throws SAXException
	{
		if ("Emitters".equalsIgnoreCase(qName))
		{
			mEmitterss.addElement(mEmitters);
			mEmitters="";
		}
		if ("ParticleEngineData".equalsIgnoreCase(qName))
		{
			if (mCurrentData != null)
			{
				mEmitters = "";
				mCurrentData.mEmitters = mEmitterss;
				try{
				mCurrentData.mWidth = Integer.parseInt(mWidth);
				}catch (Throwable e){}
				mWidth = "";
				try{
				mCurrentData.mHeight = Integer.parseInt(mHeight);
				}catch (Throwable e){}
				mHeight = "";
				try{
				mCurrentData.mTickResolution = Integer.parseInt(mTickResolution);
				}catch (Throwable e){}
				mTickResolution = "";
				mParticleEngineData.put(mCurrentData.mName, mCurrentData);
				mCurrentData = null;
			}
		}
		mCurrentElement = null;
	}
}
