import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.XMLReader;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;
import java.util.*;


public class SurveyReader extends DefaultHandler
{

   //int indent = 0;

   public SurveyReader() {
      ///System.out.println("Object Created.");
   }

   public void showEvent(String name) {
      //System.out.println("Hello, " + name + "!");
   }

   public void error(SAXParseException e) {
	   System.out.println("Error parsing the file :"+e.getMessage());
   }

   public void warning(SAXParseException e){
	   System.out.println("Problem parsing the file :"+e.getMessage());
   }

   public void fatalError(SAXParseException e){
	   System.out.println("Probelm parsing the file :"+e.getMessage());
	   System.out.println("Cannot continue...");
	   //System.exit(1);
   }

   public void startDocument() throws SAXException {
   }

   String thisQuestion ="";
   String thisElement ="";

   public Vector tourVect = new Vector();
   public Vector miscVect = new Vector();
   public Vector rideVect = new Vector();
   public Vector grupVect = new Vector();
   public Vector clasVect = new Vector();
   public Vector evtdVect = new Vector();

   Hashtable tmpHst;

   public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
	   thisElement = localName;

	   if (localName.equals("TOUR")) 				tmpHst = new Hashtable();
	   if (localName.equals("MISC_DESCRIPTION")) 	tmpHst = new Hashtable();
	   if (localName.equals("RIDE_DESCRIPTION")) 	tmpHst = new Hashtable();
	   if (localName.equals("GRP_DESCRIPTION")) 	tmpHst = new Hashtable();
	   if (localName.equals("CLAS_DESCRIPTION")) 	tmpHst = new Hashtable();
	   if (localName.equals("ED_DESCRIPTION")) 		tmpHst = new Hashtable();



	   /*##################RETRIEVE ATTRIBUTES################################################*/
	       	for (int att = 0; att < atts.getLength(); att++) {
	          	//String attName = atts.getLocalName(att);
	          	//System.out.println("   " + attName + ": " + atts.getValue(attName));
   			}
   	   /*##################RETRIEVE ATTRIBUTES END############################################*/

   }

   public void characters( char ch[], int start, int length) throws SAXException {
	   if (thisElement.equals("PAX")) {
		   tmpHst.put("pax", new String(ch, start, length));
		   //System.out.println( new String(ch, start, length));
	   } else if ( thisElement.equals("FINAL_AMT")) {
		   tmpHst.put("finalamt", new String(ch, start, length));
		   //System.out.println( new String(ch, start, length));
	   } else if ( thisElement.equals("SINGLE_ROOM_SUPP")) {
		   tmpHst.put("singlers", new String(ch, start, length));
	   } else if ( thisElement.equals("M_DESCRIPTION")) {
	   		   tmpHst.put("mdesc", new String(ch, start, length));
	   } else if ( thisElement.equals("R_DESCRIPTION")) {
	   		   tmpHst.put("rdesc", new String(ch, start, length));
	   } else if ( thisElement.equals("G_DESCRIPTION")) {
	   		   tmpHst.put("gdesc", new String(ch, start, length));
	   } else if ( thisElement.equals("C_DESCRIPTION")) {
	   		   tmpHst.put("cdesc", new String(ch, start, length));
	   } else if ( thisElement.equals("EVENT_DAY")) {
	   		   tmpHst.put("evntd", new String(ch, start, length));
	   } else if ( thisElement.equals("E_DESCRIPTION")) {
	   		   tmpHst.put("edesc", new String(ch, start, length));
	   }
	}

   public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
	   //System.out.println("End Element :"+ localName );

	   if (localName.equals("TOUR"))	{tourVect.addElement((Object)tmpHst); System.out.println(tmpHst.toString());}
	   if (localName.equals("MISC_DESCRIPTION")) miscVect.addElement((Object)tmpHst);
	   if (localName.equals("RIDE_DESCRIPTION")) rideVect.addElement((Object)tmpHst);
	   if (localName.equals("GRP_DESCRIPTION")) grupVect.addElement((Object)tmpHst);
	   if (localName.equals("CLAS_DESCRIPTION")) clasVect.addElement((Object)tmpHst);
	   if (localName.equals("ED_DESCRIPTION")) evtdVect.addElement((Object)tmpHst);
	   		//System.out.println("Added : "+tmpHst.toString());
	   		//System.out.println("Size : "+tourVect.size()+ " Element : "+ tourVect.indexOf(tmpHst));

	   thisQuestion = "";
	   thisElement = "";

	   //tmpHst = null;
   }

   public void writeOutput() {
	System.out.println("Size at tour writeOutput:"+tourVect.size() );
	System.out.println("Size at misc writeOutput:"+miscVect.size() );
	System.out.println("Size at ride writeOutput:"+rideVect.size() );
	System.out.println("Size at grup writeOutput:"+grupVect.size() );
	System.out.println("Size at clas writeOutput:"+clasVect.size() );
	System.out.println("Size at evtd writeOutput:"+evtdVect.size() );

			for (int pos=0;pos<tourVect.size();pos++){
				Hashtable tHst = (Hashtable)tourVect.get(pos);
				System.out.println("pax :"+tHst.get("pax"));
				System.out.println("finalamt :"+tHst.get("finalamt"));
				System.out.println("singlers :"+tHst.get("singlers"));
				tHst = null;
			}
			for (int pos=0;pos<miscVect.size();pos++){
				Hashtable tHst = (Hashtable)miscVect.get(pos);
				System.out.println("misc : " + tHst.get("mdesc"));
				tHst = null;
			}
			for (int pos=0;pos<rideVect.size();pos++){
				Hashtable tHst = (Hashtable)rideVect.get(pos);
				System.out.println("ride : "+tHst.get("rdesc"));
				tHst = null;
			}
			for (int pos=0;pos<grupVect.size();pos++){
				Hashtable tHst = (Hashtable)grupVect.get(pos);
				System.out.println("group : "+tHst.get("gdesc"));
				tHst = null;
			}
			for (int pos=0;pos<clasVect.size();pos++){
				Hashtable tHst = (Hashtable)clasVect.get(pos);
				System.out.println("class : "+tHst.get("cdesc"));
				tHst = null;
			}
			for (int pos=0;pos<evtdVect.size();pos++){
				Hashtable tHst = (Hashtable)evtdVect.get(pos);
				System.out.println("eventday : "+tHst.get("evntd"));
				System.out.println("eventday desc : "+tHst.get("edesc"));
				tHst = null;
			}
	}



   public void doTheWork (String xmlSource) {


         XMLReader xmlReader = null;

         try {

            SAXParserFactory spfactory = SAXParserFactory.newInstance();
			spfactory.setValidating(false);

            SAXParser saxParser = spfactory.newSAXParser();

            xmlReader = saxParser.getXMLReader();

				try {
					xmlReader.setFeature( "http://xml.org/sax/features/namespaces", true);
				}
				catch (SAXException e) {
					System.err.println("could not set parser feature");
				}
			SurveyReader t = new SurveyReader();
			xmlReader.setContentHandler(t);
			xmlReader.setErrorHandler(t);

			InputSource source = new InputSource(xmlSource);
			xmlReader.parse(source);



			t.writeOutput();

         } catch (Exception e) {
               System.err.println(e);
               //System.exit(1);
         }

   }

}
