import org.w3c.dom.*;
import org.xml.sax.*;
import java.io.*;
import javax.xml.parsers.*;

/**
 * Finds and displays departure-times of trains
 *
 * @version  1.0
 * @author   Prof. David Bernstein, James Madison University
 */
public class TimeTable
{

    /**
     * The entry-point of the application
     *
     * @param args   The command line arguments (train, station)
     */
    public static void main(String[] args) throws Exception
    {
       boolean                   found; 
       Document                  tree;
       DocumentBuilder           builder;
       DocumentBuilderFactory    factory;
       Element                   root, station, stop, time, train;
       File                      amtrak;
       int                       i, n;
       Node                      text;
       NodeList                  children, stations, stops, textNodes;
       NodeList                  times, trains;
       String                    departureTime, number, stationName;



       amtrak  = new File("amtrak.xml");
       factory = DocumentBuilderFactory.newInstance();
       builder = factory.newDocumentBuilder();
       tree    = builder.parse(amtrak);


       // Get the timetable element
       root = tree.getDocumentElement();

       System.out.println("\n\nService for "+root.getAttribute("title"));
       System.out.println(root.getAttribute("subtitle"));
       System.out.println("\n");

       // Get the trains
       trains = root.getElementsByTagName("train");

       // Find the desired train
       found = false;
       train = null;
       n = trains.getLength();
       for (i=0; i < n && !found; i++) 
       {
          train  = (Element)trains.item(i);
          number = train.getAttribute("number");
          found = (number.equals(args[0]));
       }

       // Find the desired station
       departureTime = null;
       if (found) 
       {
          stops = train.getElementsByTagName("stop");
          found = false;
          n = stops.getLength();
          for (i=0; i < n && !found; i++) 
          {
             stop     = (Element)stops.item(i);

             // Get the station for this stop
             stations = stop.getElementsByTagName("station");
             station  = (Element)stations.item(0);

             // Get the text for this station
             textNodes   = station.getChildNodes();
             text        = textNodes.item(0);
             stationName = text.getNodeValue();

             // Is it the desired station?
             if (stationName.equals(args[1])) 
             {
                found = true;

                // Get the time for this stop
                times = stop.getElementsByTagName("time");
                time  = (Element)times.item(0);

                // Get the text for this time
                textNodes   = time.getChildNodes();
                text        = textNodes.item(0);
                departureTime = text.getNodeValue();
             }
          }
       }

       if (!found) System.out.println("Sorry!");
       else        System.out.println("Train "+args[0]+
                                      " departs "+args[1]+
                                      " at "+departureTime);
    }

}
