Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/thermidor/xml/output/FileHandler.java


1   package com.thermidor.xml.output;
2   import java.io.IOException;
3   import java.io.File;
4   import java.io.FileOutputStream;
5   import java.io.PrintWriter;
6   import com.thermidor.util.Assert;
7   public class FileHandler extends OutputHandler {
8     /**
9      * Construct a new default instance of a FileHandler.
10     */
11    public FileHandler() {
12      super();
13    }
14    /**
15     * <code>start</code> is called on the output handler to initialize it
16     * ready to start processing output.
17     * @param data the initialization data.
18     * @throws InvalidInitializationParameter is raised if the initialization 
19     * data was invalid for this instance.
20     * @throws IOException is raised if there was an IO related error.
21     */
22    public void start(Object initializationData) 
23      throws InvalidInitializationParameter,
24             IOException {
25      System.out.println("FileHandler::start(Object "+initializationData+") ");
26      try {
27        Assert.notNull(initializationData,
28                       "initialization parameter for handler type [" +
29                       FileHandler.class +
30                       "] must be non null");
31      }
32      catch(Assert.AssertException ae ) {
33        throw new InvalidInitializationParameter(ae);
34      }
35      File outputFile = null;
36      if(initializationData instanceof String) {
37        outputFile = new File((String)initializationData);
38      }
39      else if (initializationData instanceof File) {
40        outputFile = (File)initializationData;
41      }
42      else {
43        throw new InvalidInitializationParameter
44          ("initialization parameter for handler type [" +
45           FileHandler.class +
46           "] must be java.lang.String or java.io.File, but was [" +
47           initializationData.getClass()+"]");
48      }
49      FileOutputStream fos = new FileOutputStream(outputFile);
50      _writer = new PrintWriter(fos,true);
51    }    
52    /**
53     * Handle the provided string data .
54     * @param data the text data that is to be output.
55     */
56    public void handle(String data) throws IOException{
57      //System.out.println("FileHandler::handle(String "+data+")");
58      _writer.print(data);
59    }
60    /**
61     * The responsibilty of the  finished member function is to handle clean up
62     * when the output is popped from the output stack.
63     * @return any relevant string data that was collected will the handler 
64     * is in operation.
65     */
66    public String finish() throws IOException {
67      System.out.println("FileHandler.finish()");
68      _writer.flush();
69      _writer.close();
70      return EMPTY;
71    }    
72      
73  }