/*
 *  ==========================================================================
 *  TestIOReadFromURL.java : This Java 2 program reads a stream of numbers
 *  from a file located at a URL...
 *
 *  Written By: Mark Austin                                     February, 2006
 *  ==========================================================================
 */

import java.lang.Math;
import java.util.*;
import java.io.*;
import java.net.*;
import java.text.*;

public class TestIOReadFromURL {

    public void readDataFile( URL url ) throws IOException {
        int iNoDataItems = 0;
        String sName = null;
        String sLine;

        // Open input stream from the URL ...

        InputStream inStream = url.openStream();
        BufferedReader in = new BufferedReader( new InputStreamReader( inStream ) );

        // Read data from data.txt....

        try {

             // Get no of lines in input file ....

             sLine = in.readLine();
             StringTokenizer st = new StringTokenizer(sLine);
             if ( st.hasMoreTokens() == true ) 
                iNoDataItems = Integer.parseInt( st.nextToken() );
             else 
                System.out.println("*** ERROR in input file ");

	     for ( int i = 1; i <= iNoDataItems; i = i + 1 ) {
          
                 // Read name of item ....

                 sLine = in.readLine();
                 StringTokenizer st1 = new StringTokenizer( sLine );
                 if ( st1.hasMoreTokens() == true ) {
                      sName = st1.nextToken();
                 }

                 // Read (x,y) coordinates of each node ....

                 double dX1 = Double.parseDouble( st1.nextToken() );
                 double dY1 = Double.parseDouble( st1.nextToken() );

                 // Read data point status ....

                 boolean bActivePoint;
                 if ( st1.nextToken().equals("false") == true )
                      bActivePoint = false;
                 else
                      bActivePoint = true;

                 // Echo output ....

                 System.out.print("Name( " + sName + " ):");
                 System.out.print("(x,y) =" + "(" + dX1 + "," + dY1 + "): ");
                 System.out.println("Pole Status = " + bActivePoint );
             }
        }

        catch (FileNotFoundException e){}
        catch (EOFException e){}

        /// Close input file 

        in.close();
    }

    // Exercise methods in TestIOFile() ....

    public static void main( String args[] ) throws IOException {
        URL url = null;

        // Create object for test IO program....

        TestIOReadFromURL turl = new TestIOReadFromURL();

        // Get URL for web address....

        try {
           url = new URL("http://www.isr.umd.edu/~austin/ence200.d/data.d/data.txt");
        } catch ( MalformedURLException e) {} 

        // Read content from data.txt ....

        turl.readDataFile( url );
    }
}
