<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">

/* Grader for CS211 Assignment 4 -- enumerating tags and links*/

import java.io.*;
import java.net.*;

public class Main {

	public static String fileName = "graded.txt";  // Output file name
// Modify for your computer

	// The location of the 4 html testing files.  Modify the path for your computer
	public static String inputFile1 = "links1.html";
	public static String inputFile2 = "links2.html";
	public static String inputFile3 = "links3.html";
	public static String inputFile4 = "links4.html";

	public static PrintStream out= null;		// The output file
	public static URL url = null;				// The url of the current html file
	public static BufferedReader br = null;		// The BufferedReader for the current html file

	public static int numTags = 0;				// A counter for the number of tags in a file
	public static int numLinks = 0;				// A counter for the number of links in a file
    public static int pointsTE= 0;				// points deducted automatically during TagEnumerationCheck
    public static int pointsLE= 0;				// points deducted automatically during LinkEnumerationCheck

    

    public static void main(String args[]) throws IOException {
		
		createOut();    // Initialize the output file (static variable out) 
		System.out.println("createout completed");

		testTagEnumeration();
		System.out.println("TagEnumeration testing complete");
		out.println("Points deducted by automatic tester: " + pointsTE);
		out.println("");
		out.println("");
		
		testLinkEnumeration();
		System.out.println("LinkEnumeration testing complete");
		out.println("Points deducted by automatic tester: " + pointsLE);
		out.println("");
		out.println("");
    }

	



    // Test class TagEnumeration, writing output to output file out
    public static void testTagEnumeration() {
        out.println("Checking TagEnumeration Automatically. Messages printed (with points deducted) " + 
				    "if errors found.");
			// Test an empty file
			    createIn1();
			    TagEnumeration te = null;
			    try {
				    te = new TagEnumeration(br);
			    } catch (Exception e) {messTE(2, "TagEnumeration constructor did not initialize correctly"); }
                
			// Test constructor		
			    if (te == null) {messTE(2, "TagEnumeration constructor did not initialize correctly"); }
		        
			// Test the case where there are no tags			
			    try {
				    if (te.hasMoreElements()) {
				    	messTE(2, "hasMoreElements() returned true on a file with no tags");
				    }
                } catch (Exception e){		
				    messTE(2, "exception calling hasMoreElements");
			    }
			
			// Test a file that has tags, but no link
			    createIn2();
			    try {
				    te = new TagEnumeration(br);
			    } catch (Exception e) {messTE(2, "TagEnumeration constructor did not initialize correctly"); }
			    numTags = 0;
			    if (te != null) {
				    // Test that hasMoreElements() returns true when there are more tags
				        if( !te.hasMoreElements()) {
					        messTE(2, "hasMoreElements() returned false when there remained tags");
				        }

				    // Test that nextElement() returns something				
				        try {
					        String elt = (String)te.nextElement();
					        if (elt == null) {
						        messTE(2, "nextElement() returned null when there was a tag to return");
						        messTE(2, "an incorrect tag was returned");
					        }
	
					        // Test that the tags have the right form (ie &lt; blahblahblah &gt;)			
					        else {
						        if( !elt.equals("&lt;HTML&gt;") ) {
							        messTE(2, "an incorrect tag was returned" );
						        }
						    numTags++;
					    }
				        } catch (Exception e) {
					        messTE(5, "unexpected exception disabled a few automatic tests: " + e);
				        }
	
				    // Test that they get all the tags in the file			
				        while( te.hasMoreElements() ) {
					        numTags++;
					        te.nextElement();
				        }
				        if( numTags != 2 ) {		
					        messTE(2, "incorrect number of tags returned");
				        }
			            } else {
				            // they already were docked marks in this case
				            messTE(8, "Constructor returned null, so unable to check automated tests");
			            }
		    
			    // Test a file with a tag that spans more than one line
			        createIn3();
			        try {
				        te = new TagEnumeration(br);
			        } catch (Exception e)
			            {messTE(2, "TagEnumeration constructor did not initialize correctly"); }
	
			        try {te.hasMoreElements();
				    te.nextElement();
				    te.hasMoreElements();
				    String elt = (String)te.nextElement();	// Gets the tag spanning two lines (hopefully)
				    if( !elt.equals("&lt;BODY BGCOLOR=\"#400080\" TEXT=\"#FFFFFF\" LINK=\"#FFFFFF\" VLINK=\"#FFFFFF\" ALINK=\"#FFFFFF\"&gt;") ) {		
					    messTE(2, "tag longer than one line set incorrectly");
				    }
			    } catch (Exception e) {
				    messTE(5, "unexpected exception in trying tags longer than one line: " + e);
			    }
    }
	

	// Add pts to static variable pointsTE and print message m on output file out
	public static void messTE(int pts, String m) {
	    pointsTE= pointsTE + pts;
	    out.println("   -" + pts + ". " + m);
	}

	// Test class LinkEnumeration, writing output to output file out
    public static void testLinkEnumeration() {
        out.println("Checking LinkEnumeration Automatically. Messages printed (with points deducted) " +
                   "if errors found.");
		// Test a file with tags, but no links
			createIn2();
			LinkEnumeration le = null;
			try {
				le = new LinkEnumeration(br);
			} catch (Exception e) {messTE(2, "LinkEnumeration constructor did not initialize correctly"); }
		    // Test constructor
			    if (le == null) {messLE(2, "LinkEnumeration constructor did not initialize correctly"); }

		    // Test the case where there are no links		
			    try {
				    if (le.hasMoreElements()) {
					    messLE(2, "hasMoreElements() returned true on a file with no links");
				    }
			    } catch (Exception e){
				    messLE(2, "exception calling hasMoreElements");
			    }
	
		// Test  file with a tag that spans more than one line	
			createIn3();
			try {
				le = new LinkEnumeration(br);
			} catch (Exception e) {messTE(2, "LinkEnumeration constructor did not initialize correctly"); }
			numLinks = 0;
	
			try {	
				// Test that hasMoreElements() returns true when there are more links
				if( !le.hasMoreElements() ) {
					messLE(2, "hasMoreElements() returned false when there remained links");
				}

				// Test that nextElement() returns something			
				    try {
					    String elt = (String)le.nextElement();
					    if (elt == null) {
						    messLE(2, "nextElement() returned null when there was a link to return");
						    messLE(2, "an incorrect link was returned");
					    } 
	
					    // Test that the link returned was indeed correct			
					    else {
						    if( !elt.equals("http://www.runnersworld.com/")) {
						    	messLE(2, "link incorrect: " + elt);
						    }
						    numLinks++;
					    }
				    } catch (Exception e) {
					    messLE(5, "unexpected exception disabled some automatic tests: " + e);
				    }

				// Test that they get all the tags in the file
				    while( le.hasMoreElements() ) {
					    numLinks++;
					    le.nextElement();
				    }
	
				    if( numLinks != 3 ) {		
					    messLE(2, "incorrect number of links returned");
				    }
			} catch (Exception e) {
				messLE(5, "unexpected exception in trying tags longer than one line: " + e);
			}

			// Test a file with a link with a lot of whitespace in the tag		
			    createIn4();
			    try {
			    	le = new LinkEnumeration(br);
			    } catch (Exception e) {messTE(2, "LinkEnumeration constructor did not initialize correctly"); }
			    try {le.hasMoreElements(); // Gries added
				    String elt = (String)le.nextElement();
				    if( !elt.equals("http://www.runnersworld.com/") ) {
					    messLE(2, "tag with whitespace caused incorrect link to be returned");
				    }
			    } catch (Exception e) {		
				    messLE(5, "unexpected exception trying to check links with whitespace: " + e);
			    }
    }

	// Add pts to static variable pointsSV and print message m on output file out
	public static void messLE(int pts, String m) {
	    pointsLE= pointsLE + pts;
	    out.println("   -" + pts + ". " + m);
	}

	// Store the file to output in static variable out and print its name in
	// Java console. throws IOException
	public static void createOut() throws IOException {
	    File outputFile= new File(fileName);
	    out= new PrintStream(new FileOutputStream(outputFile));
	    System.out.println("Writing to file " + outputFile.getPath());   
	}

	 /**  = a reader for URL url (which must not be null). If the protocol 
           is not http or file, null is returned. */ 
    public static BufferedReader getReader() { 
        try { 
            InputStream			is  = url.openStream(); 
            InputStreamReader   isr = new InputStreamReader(is); 
            return new BufferedReader(isr);
        } catch (IOException e) { 
            return null; 
        } 
    } 

	//  Create the buffered reader for the first html file
	public static void createIn1() {
		try {
			br= new BufferedReader(new FileReader(inputFile1));
		} catch (Exception e) {
			System.out.println("Error in createIn1: " +e);
		}
	}

	// Create the buffered reader for the second html file
	public static void createIn2() {
		try {
			br= new BufferedReader(new FileReader(inputFile2));
		} catch (Exception e) {
			System.out.println("Error in createIn2: " +e);
		}
	}

	// Create the buffered reader for the third html file
	public static void createIn3() {
		try {
			br= new BufferedReader(new FileReader(inputFile3));
		} catch (Exception e) {
			System.out.println("Error in createIn3: " +e);
		}
	}

	// Create the buffered reader for the fourth html file
	public static void createIn4() {
		try {
			br= new BufferedReader(new FileReader(inputFile4));
		} catch (Exception e) {
			System.out.println("Error in createIn4: " +e);
		}
	}
}


</pre></body></html>