/*
 *  Team 1
 *  CS575: Software Design
 *  Project Phase I
 *  Implicit Invocation style
 *  IIOuput.java
 */

import java.io.*;
import java.util.Vector;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PipedReader;
import java.io.PipedWriter;

/**
 *  <p>Output class for the Implicit Invocation KWIC system.</p>
 *
 *  <p><b>PRE: </b>expects to receive a message whenever it is time to output
 *  <br><b>POST:</b>after execution, all sentences will have been sent to stdout.
 * 
 *  @author Luiza da Silva
 */

public class IIOutput extends IIMaster implements Runnable
{
    /**
     * Constructor
     * <p> Starts the thread with the name "Output Thread"</p>
     */
    public IIOutput()
    {
            new Thread(this, "Output Thread").start();
    }

    /**
     * This method will output the incoming data
     */ 
    public void run()
    {
        StreamTokenizer inPipe = new StreamTokenizer(outputReadMessagePipe);
        inPipe.parseNumbers();

        try 
        {
            while( inPipe.nextToken() == inPipe.TT_NUMBER )
            {
                if( inPipe.nval == 1)
                {
                   shiftedCollection.startKWICRowIterator();
                    while( shiftedCollection.hasNextKWICRow() == true )
                    {
                            Object[] temp = shiftedCollection.getNextKWICRow();
                            int count = temp.length;
                            for( int i=0; i<count; i++ )
                            {
                                    System.out.print(temp[i].toString() + " ");
                            }
                            System.out.println();
                    }
                }                
            }
        }            
        catch( Exception e )
        {
            // SendMessage(e);
        }
    }
}