Source code: com/thermidor/xml/output/StringHandler.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 import java.io.ByteArrayOutputStream;
8 /**
9 * The purpose of the StringHandler is to manage/collect output into a buffer.
10 * This allows output to be collected for subsequent processesing in the parse.
11 * @author Edward Turnock
12 * @version 1.0
13 */
14 public class StringHandler extends OutputHandler {
15 /**
16 * The output buffer.
17 */
18 private ByteArrayOutputStream _baos=new ByteArrayOutputStream();
19 /**
20 * Construct a new default instance of a StringHandler.
21 */
22 public StringHandler() {
23 super();
24 }
25 /**
26 * <code>start</code> is called on the output handler to initialize it
27 * ready to start processing output.
28 * @param data the initialization data.
29 * @throws InvalidInitializationParameter is raised if the initialization
30 * data was invalid for this instance.
31 * @throws IOException is raised if there was an IO related error.
32 */
33 public void start(Object initializationData)
34 throws InvalidInitializationParameter,
35 IOException {
36 // try {
37 // Assert.isNull(initializationData,
38 // "initialization parameter for handler type [" +
39 // StringHandler.class +
40 // "] must be null");
41 // }
42 // catch(Assert.AssertException ae ) {
43 // throw new InvalidInitializationParameter(ae);
44 // }
45 _writer = new PrintWriter(_baos,true);
46 }
47 /**
48 * Handle the provided string data .
49 * @param data the text data that is to be output.
50 */
51 public void handle(String data) throws IOException{
52 _writer.print(data);
53 }
54 /**
55 * The responsibilty of the finished member function is to handle clean up
56 * when the output is popped from the output stack.
57 * @return any relevant string data that was collected will the handler
58 * is in operation.
59 */
60 public String finish() throws IOException {
61 _writer.flush();
62 _writer.close();
63 return _baos.toString();
64 }
65 }