package csa.tiletools.tiles;

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import java.util.*;
public class  AttributeBundleDataXMLHandler extends DefaultHandler
{
	private HashMap<String, AttributeBundleData> mAttributeBundleData;
	private AttributeBundleData mCurrentData = null;
	private String mCurrentElement = null;
	private String mAtrributeName = "";
	private Vector<String> mAtrributeNames = null;
	private String mAttributeType = "";
	private Vector<String> mAttributeTypes = null;
	private String mAttributeValue = "";
	private Vector<String> mAttributeValues = null;
	public HashMap<String, AttributeBundleData> getLastHashMap()
	{
		return mAttributeBundleData;
	}
	@Override public void startDocument() throws SAXException
	{
		mCurrentData = new AttributeBundleData();
		mAttributeBundleData = new HashMap<String, AttributeBundleData>();
	}
	@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("AttributeBundleData"))
		{
			mCurrentData = new AttributeBundleData();
			mAtrributeName = "";
			mAtrributeNames = new Vector<String>();
			mAttributeType = "";
			mAttributeTypes = new Vector<String>();
			mAttributeValue = "";
			mAttributeValues = 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("ATTRIBUTNAME")) mAtrributeName += s;
		if (mCurrentElement.equalsIgnoreCase("ATTRIBUTETYPE")) mAttributeType += s;
		if (mCurrentElement.equalsIgnoreCase("ATTRIBUTEVALUE")) mAttributeValue += 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 ("AttributeBundleData".equalsIgnoreCase(qName))
		{
			if (mCurrentData != null)
			{
				mAtrributeName = "";
				mCurrentData.mAtrributeName = mAtrributeNames;
				mAttributeType = "";
				mCurrentData.mAttributeType = mAttributeTypes;
				mAttributeValue = "";
				mCurrentData.mAttributeValue = mAttributeValues;
				mAttributeBundleData.put(mCurrentData.mName, mCurrentData);
				mCurrentData = null;
			}
		}
		mCurrentElement = null;
	}
}
