import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

public class ReaderVsStream {

  public static void main(String args[]) {
    ReaderVsStream _rvs = new ReaderVsStream();
    try {
      _rvs.run();
    } catch(Exception e) {
      e.printStackTrace();
    }
  }

  public void run()
   throws Exception {
    /* This test class transforms in.xml using style.xsl. Both are encoded
       ISO-8859-1, but the style specifies the output encoding UTF-8. out1.xml
       will contain the result when readers/writers are used, out2.xml will
       contain the result when streams are used.

       in1     input reader for in.xml
       in2     input stream for in.xml
       style1  input reader for style.xsl
       style2  input stream for style.xsl
       out1   output writer for out1.xml
       out2   output stream for out2.xml
     */

    // First, let's get to the readers/writers
    Reader in1    = new FileReader("in.xml");
    Reader style1 = new FileReader("style.xsl");
    Writer out1   = new FileWriter("out1.xml");
    // get the factory
    TransformerFactory tFactory = TransformerFactory.newInstance();
    // get the instance
    Transformer transformer = tFactory.newTransformer(new StreamSource(style1));
    // transform
    transformer.transform(new StreamSource(in1), new StreamResult(out1));
    // close the files
    out1.close();
    style1.close();
    in1.close();

    // Now let's get to the streams
    InputStream in2    = new FileInputStream("in.xml");
    InputStream style2 = new FileInputStream("style.xsl");
    OutputStream out2  = new FileOutputStream("out2.xml");
    // get the factory again (just to be sure)
    tFactory = TransformerFactory.newInstance();
    // get the instance again (just to be sure)
    transformer = tFactory.newTransformer(new StreamSource(style2));
    // transform again
    transformer.transform(new StreamSource(in2), new StreamResult(out2));
    // close the files
    out2.close();
    style2.close();
    in2.close();
  }
}
