import org.xml.sax.*;
import org.xml.sax.helpers.*;

/**
 * Finds a particular departure-time in a timetable
 *
 * Note: This implementation assumes that the station 
 *       preceeds the time in the stop.
 *
 * @version  1.0
 * @author   Prof. David Bernstein, James Madison University
 */
public class DepartureTimeFinder extends DefaultHandler
{
    private String    characters, dt, station, targetTrain, targetStation;
    private String    time, waitingFor;


    /**
     * Explicit value Constructor
     *
     * @param targetTrain   The train of interest (e.g., "191") 
     * @param targetStation The station of interest (e.g., "Newark, NJ") 
     */
    public DepartureTimeFinder(String targetTrain, String targetStation)
    {
       this.targetTrain   = targetTrain;
       this.targetStation = targetStation;
       time = null;
    }



    /**
     * Return the departure time (or null if the train and station were
     * not found)
     *
     * @return   The departure time
     */
    public String getDepartureTime()
    {
       return time;
    }


    /**
     * Receive notification of the beginning of the document
     *
     */
    public void startDocument()
    {
       waitingFor = "train";
    }



    /**
     * Receive notification of the start of an element
     *
     * @param uri        The namespace URI 
     * @param localName  The local name or the empty string (if no namespace)
     * @param qName      The qualified name (with prefix)
     * @param attributes The specified or default attributes
     */
    public void startElement(String uri, String localName, String qName,
			     Attributes attributes)
    {
       String       number;


       if (qName.equals(waitingFor)) 
       {
          if (qName.equals("train")) 
          {
             number = attributes.getValue("number");
             if ((number != null) && (number.equals(targetTrain))) 
             {
                waitingFor = "stop";
             }
          } 
          else if (qName.equals("stop")) 
          {
             waitingFor = "station";
          } 
          else if (qName.equals("station")) 
          {
             // Need the characters
          }
       }
    }



    /**
     * Receive notification of character data inside an element
     *
     * @param ch     The characters
     * @param start  The start position in the character array
     * @param length The number of characters to use from the character array
     */
    public void characters(char[] ch, int start, int length)
    {
       if (waitingFor.equals("station")) 
       {
          characters = new String(ch, start, length).trim();
       } 
       else if (waitingFor.equals("time")) 
       {
          characters = new String(ch, start, length).trim();
       }
    }



    /**
     * Receive notification of the start of an element
     *
     * @param uri        The namespace URI 
     * @param localName  The local name or the empty string (if no namespace)
     * @param qName      The qualified name (with prefix)
     */
    public void endElement(String uri, String localName, String qName)
    {
       if (waitingFor.equals("station")) 
       {
          if ((characters != null) && characters.equals(targetStation)) 
          {
             waitingFor = "time";
          }
          // Otherwise wait for another station
       } 
       else if (waitingFor.equals("time")) 
       {
          // This only happens when the train and station
          // have already been confirmed
          time = characters;
          waitingFor = "";
       }
    }
    
}

