import becker.io.*;

public class XMLTokenizer
{
  QueueInterface channel; // the queue that will be used to return tokens
  TextInput taggedText;   // the file of tagged XML text

  public XMLTokenizer(QueueInterface pipeline, TextInput fileIn)
  //pre: pipline is not null and file is not null
  //post: initializes a new tokenizer that reads XML text from fileIn and
  //      uses pipeline to send extracted tokens
  {
   channel = pipeline; // save the queue to use
   taggedText = fileIn;  // save the location of the input text
  }

  public void tokenize()
  //pre:  taggedText contains a sequence of well-formed XML data, 
  //      each terminated by an empty tag (i.e., "<>")
  //post: read a tree from the file and put tokens in the channel
  //      if no more data to read, put nothing into the queue
  {
   // need to separate input using "<" and ">" delimiters
  channel.dequeueAll();
  String source = this.taggedText.readLine();
	int marker = 0;
	for (int i=0; i<source.length(); i++)
	{
		if ( source.charAt(i) == '<')	
		{	String substr = source.substring(marker, i);
			if (!substr.equals(""))
			{	this.channel.enqueue(source.substring(marker,i));
				marker = i;
			}
		}
		else if (source.charAt(i) == '>')
		{	this.channel.enqueue(source.substring(marker,i+1));
			marker = i+1;
		}		
	}
  }

} // end XMLTokenizer
