// H2Replace.java
//
// You may use and distribute this example. This program is 
// provided WITHOUT WARRANTY either expressed or implied.
//
// Copyright Henrik Björkman 1997
//
// This program uses Piped Input and Output Streams to send a 
// stream of data through a number of filter programs. Each of 
// these filter programs is a thread reading from an input and 
// writing to an output stream. For the first filter the input 
// is a file and for the last the output is system out.
//
// History:
// 0.0 Created by Henrik Björkman 1997-11-25 
//     (using code from TxtToHtml.java)
// 0.1 Added package. Henrik 1998-01-25
// 0.2 Using se.beod.henrik.filter.*. Henrik 1998-06-06



import java.io.*;
import se.beod.henrik.filter.*;



public class H2Replace
{
  static PipedInputStream in;
  static PipedOutputStream out;
  static PipedInputStream next_in;
  static FilterDB db;

  static void error(String str)
  {
    System.err.println("H2Replace: " + str);
  }

  static void debug(String str)
  {
    //error(str);
  }

  // This function is called before each filter thread is created
  // To create an input output pair of piped stream and set up the
  // in and out to be used by the filter thread to be started.
  static void setupInOut() throws IOException
  {
    in=next_in;
    out=new PipedOutputStream();
    next_in=new PipedInputStream(out);
  }

  public static void main(String args[]) 
  {
    if (args.length < 2 )
    {
      System.out.println("Usage: java H2Replace <name data file> <in file>");
      System.exit(0);
    }
        
    try 
    {
       db=new FilterDB(args[0]);

       setupInOut();
       new WordParse(new FileInputStream(args[1]),out);

       setupInOut();
       new MultiReplacer(in,out,db);

       setupInOut();
       new LineMerge(in,System.out);

    }
    catch (IOException e) {error(e.toString());}
    debug("end.");
  }
}


