<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 tags in a BufferedReader */
public class TagEnumeration implements Enumeration {
   private BufferedReader br; // The BufferedReader to be read
   private String line= "";   // Input that has been read but not yet processed
   private String tag;        // The next tag to return --the last tag formed
                              // but not yet processed (null if no more)

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

    /** Store next tag in 'tag', if it exists.
        Set tag to null if it does not exist. */
    private void getReadyForNext() {
        try {
            tag= null; // It will stay null if there isn't a next.
            
            // Read input until a line with "&lt;" in it is found --begins a tag
            // Return false if no such line
                while (line != null &amp;&amp; line.indexOf("&lt;") == -1) {
                    line= br.readLine();
                }

            if (line == null) { // end of file reached
                tag = null;
                return;
            }

            // Remove the material from line that is before "&lt;"
                line= line.substring(line.indexOf("&lt;"));

            // Store in k the index of the next "&gt;", reading input lines if necessary.
            // Return false if not possible
                int k= line.indexOf("&gt;");
                // invariant: k = index of "&gt;" in line (-1 if none)
                while (k == -1) {
                    String nextLine= br.readLine();
                    if (nextLine == null)  // end of file, so no more tags
                        return;
                    line= line + nextLine;
                    k= line.indexOf("&gt;");
                }

            tag=  line.substring(0,k+1);
            line= line.substring(k+1);
           
        } catch (IOException e) {
            // Nothing to do here. tag is already null.
        }
    }

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

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