package utils.common.parser;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import utils.common.trace.Debug;
public class AddressParser{
	public	Document		ROOTDOCUMENT;
	public	Element			ROOTELEMENT;
	public	Element			MAILELEMENT;
	public	SAXBuilder		saxBuilder;
	public	String 			config 				=	"E:\\Tomcat\\webapps\\shan\\WEB-INF\\addressConfig.xml";
	public	String 			DELIMITER			= 	"!"; // Used to build the hash table delimeter <shan@ksshan.com!shan>
	public	String 			ELEMENT_ATTRIBUTE 	= 	"mailId";
	public	List			list;
	public	Iterator		iter;
	public 	Debug			log;
	private	ResourceBundle	bundle;
	public AddressParser	obj;

	public AddressParser(String fileName) throws JDOMException, IOException{
	    config 			=	fileName;
		init(); // method which does the stuff
	}
    /**
     * @throws JDOMException
     * @throws IOException
     */
    public AddressParser() throws JDOMException, IOException{
    	log	 = Debug.getInstance();
    	init();
    }
    public void init(){
    	try{
    	log.debug("Inside the Address Parser ..");
 	    saxBuilder		= 	new SAXBuilder();
 		ROOTDOCUMENT	=	saxBuilder.build(config);
 		ROOTELEMENT		=	ROOTDOCUMENT.getRootElement();
 		list			=	ROOTELEMENT.getChildren();
 		iter			= 	list.iterator();
    	}catch(Exception exe){
    		log.debug("Exception "+exe);
    		System.out.println("Exception : "+exe);
    	}
    }
    public Hashtable doParse() {
    	log.debug("Inside the AddressParser.doParser()Start");
		Element 	element;
		Hashtable	hash 	= 	new Hashtable();
		int 		mailId	=	1;

		while(iter.hasNext()) {
		    element 		= (Element)iter.next();
		    String hashData = element.getText() + DELIMITER + element.getAttributeValue(ELEMENT_ATTRIBUTE);
		    hash.put(mailId+"",hashData);
		    mailId++;
		}
		log.debug("Inside the AddressParser.doParser()End");
		return hash;
    }
    public static void main(String args[]) {
        try {
            AddressParser obj	= new AddressParser();
        } catch (Exception e) {
        	e.printStackTrace();
        }
    }
    /**************************************************************

    This is sample xml file contents. save as addressConfig.xml

	<ADDRESS>
		<mail mailId="shan@shan.com">Shan.K.S</mail>
		<mail mailId="kumar@value.com">Kumar.S</mail>
		<mail mailId="admin@park.com">ADMIN_NEW</mail>
	</ADDRESS>



    ***************************************************************/
}