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

/**  An enumeration of the links in a BufferedReader */
public class LinkEnumeration implements Enumeration {
   private BufferedReader br; // The BufferedReader to be read
   private TagEnumeration te= null;
   private String link;        // The next link to return --the last tag formed
                               // but not yet processed (null if no more)

    /** Constructor: an enumeration of the links in br.
        Precondition: br != null. */
    public LinkEnumeration(BufferedReader br) {
        te= new TagEnumeration(br);
        getReadyForNext();
    }

    /** Store next link in variable link, if the link exists.
        Set link to null if the link does not exist. */
    private void getReadyForNext() {
        link= null; // It will remain null as long as a link is not found
        while (te.hasMoreElements()) {
            String s= (String)te.nextElement();
            link= getLink(s);
            if (link != null)
                return;
        }
        return;
    }
    
    // = the link in tag (which must be a tag), if there is one; null otherwise
    private static String getLink(String tag) {
        // System.out.print("New tag: " + tag);
        // If tag contains "href" or "src", then strip everything up
            // to and including it from tag and set k &gt;= 0; else, set k to -1
                String tagLower= tag.toLowerCase();
                int k= tagLower.indexOf("href");
                if (k &gt;= 0) 
                    tag= tag.substring(k+4);
                else {
                    k= tagLower.indexOf("src");
                    if (k &gt;= 0)
                        tag= tag.substring(k+3);
                }

            if (k == -1) {
                // System.out.println("Tag doesn't have href or src in it");
                return null;
            }
        // Eliminate preceding blanks and '=' from tag --if no '=', return null
            tag= tag.trim();
            if (tag.charAt(0) != '=') {
                // System.out.println("Tag doesn't =");
                return null;
            }
            tag= tag.substring(1);

        // Eliminate preceding blanks and '\"' from tag --if not there, return null
            tag= tag.trim();
            if (tag.charAt(0) != '\"') {
                // System.out.println("Tag doesn't \"");
                return null;
            }
            tag= tag.substring(1);

        // Eliminate next "\"" and anything following it --if not there, return null
            k= tag.indexOf("\"");
            if (k == -1) {
                // System.out.println("Tag doesn't two \"");
                return null;
            }
            tag= tag.substring(0,k);

        // Eliminate "#..." from the tag (if present)
            k= tag.indexOf("#");
            if (k != -1) {
                tag= tag.substring(0,k);
            }
            
        if (tag.length() == 0)
            return null;
            
        return tag;
    }

    /**  = "the buffered reader contains another links to process" */
    public boolean hasMoreElements() {
        return link != null;
    }

    /** = the next link in the buffered reader */
    public Object nextElement() {
        String temp= link;
        getReadyForNext();
        return temp;
    }
}
</pre></body></html>