package csa.tiletools.tiles;

import csa.util.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;

public class  AttributeBundleData
{
	public String mClass="";
	public String mName="";
	public Vector<String> mAtrributeName=new Vector<String>();
	public Vector<String> mAttributeType=new Vector<String>();
	public Vector<String> mAttributeValue=new Vector<String>();
	public Vector<String> getAtrributeName()
	{
		return mAtrributeName;
	}
	public void setAtrributeName(Vector<String> AtrributeName)
	{
		mAtrributeName=AtrributeName;
	}
	public Vector<String> getAttributeType()
	{
		return mAttributeType;
	}
	public void setAttributeType(Vector<String> AttributeType)
	{
		mAttributeType=AttributeType;
	}
	public Vector<String> getAttributeValue()
	{
		return mAttributeValue;
	}
	public void setAttributeValue(Vector<String> AttributeValue)
	{
		mAttributeValue=AttributeValue;
	}
	private String exportXML()
	{
		String s = new String();
		s += "\t<AttributeBundleData>\n";
		s += "\t\t<Class>"+UtilityString.toXML(mClass)+"</Class>\n";
		s += "\t\t<Name>"+UtilityString.toXML(mName)+"</Name>\n";
		s += "\t\t<ATTRIBUTNAMEs>\n";
		for (int i=0;i<mAtrributeName.size(); i++)
		{
			s += "\t\t\t<ATTRIBUTNAME>"+UtilityString.toXML(mAtrributeName.elementAt(i))+"</ATTRIBUTNAME>\n";
		}
		s += "\t\t</ATTRIBUTNAMEs>\n";
		s += "\t\t<ATTRIBUTETYPEs>\n";
		for (int i=0;i<mAttributeType.size(); i++)
		{
			s += "\t\t\t<ATTRIBUTETYPE>"+UtilityString.toXML(mAttributeType.elementAt(i))+"</ATTRIBUTETYPE>\n";
		}
		s += "\t\t</ATTRIBUTETYPEs>\n";
		s += "\t\t<ATTRIBUTEVALUEs>\n";
		for (int i=0;i<mAttributeValue.size(); i++)
		{
			s += "\t\t\t<ATTRIBUTEVALUE>"+UtilityString.toXML(mAttributeValue.elementAt(i))+"</ATTRIBUTEVALUE>\n";
		}
		s += "\t\t</ATTRIBUTEVALUEs>\n";
		s += "\t</AttributeBundleData>\n";
		return s;
	}
	@Override public String toString()
	{
		return mName;
	}
	private static AttributeBundleDataXMLHandler XMLHANDLER = new AttributeBundleDataXMLHandler();
	public static AttributeBundleDataXMLHandler getXMLParseHandler()
	{
		return XMLHANDLER;
	}
	public static boolean saveCollectionAsXML(String filename, Collection<AttributeBundleData> col)
	{
		try
		{
			PrintWriter pw = new PrintWriter(csa.Global.mBaseDir+filename, "UTF-8");
			pw.print("<?xml version=\"1.0\"?>\n");
			pw.print("<AllAttributeBundleData>\n");
			Iterator<AttributeBundleData> iter = col.iterator();
			while (iter.hasNext())
			{
				AttributeBundleData item = iter.next();
				pw.print(item.exportXML());
			}
			pw.print("</AllAttributeBundleData>\n");
			pw.close();
		}
		catch (IOException e)
		{
			System.err.println(e.toString());
			return false;
		}
		return true;
	}
	public static HashMap<String, AttributeBundleData> getHashMapFromXML(String filename)
	{
		HashMap<String, AttributeBundleData> filters = new HashMap<String, AttributeBundleData>();
		try
		{
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			AttributeBundleDataXMLHandler h = AttributeBundleData.getXMLParseHandler();
			saxParser.parse(csa.Global.mBaseDir+filename, h);
			filters = h.getLastHashMap();
		}
		catch (Throwable e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.toString() ,"AttributeBundleData Load Error...",  JOptionPane.INFORMATION_MESSAGE);
		}
		return filters;
	}
}
