KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infohazard > maverick > transform > StringTransformStep


1 /*
2  * $Id: StringTransformStep.java,v 1.3 2004/06/07 20:38:14 eelco12 Exp $
3  * $Source: /cvsroot/mav/maverick/src/java/org/infohazard/maverick/transform/StringTransformStep.java,v $
4  */

5
6 package org.infohazard.maverick.transform;
7
8 import java.io.IOException JavaDoc;
9 import java.io.Reader JavaDoc;
10 import java.io.StringWriter JavaDoc;
11
12 import javax.servlet.ServletException JavaDoc;
13 import javax.xml.transform.OutputKeys JavaDoc;
14 import javax.xml.transform.Result JavaDoc;
15 import javax.xml.transform.Source JavaDoc;
16 import javax.xml.transform.Transformer JavaDoc;
17 import javax.xml.transform.TransformerConfigurationException JavaDoc;
18 import javax.xml.transform.TransformerException JavaDoc;
19 import javax.xml.transform.TransformerFactory JavaDoc;
20 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
21 import javax.xml.transform.sax.TransformerHandler JavaDoc;
22 import javax.xml.transform.stream.StreamResult JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.infohazard.maverick.flow.TransformContext;
27 import org.xml.sax.ContentHandler JavaDoc;
28
29 /**
30  * Helper class for transform steps that are basically text by nature.
31  * Funnels all method calls into go(String).
32  */

33 public abstract class StringTransformStep extends AbstractTransformStep
34 {
35     /**
36      * Logger.
37      */

38     private static Log log = LogFactory.getLog(StringTransformStep.class);
39     
40     /**
41      * Construct with transform context.
42      * @param tctx transform context
43      * @throws ServletException
44      */

45     public StringTransformStep(TransformContext tctx) throws ServletException JavaDoc
46     {
47         super(tctx);
48     }
49     
50     /**
51      * @see org.infohazard.maverick.flow.TransformStep#getSAXHandler()
52      */

53     public ContentHandler JavaDoc getSAXHandler() throws IOException JavaDoc, ServletException JavaDoc
54     {
55         log.debug("Creating TransformerHandler which sends to next step output stream.");
56         
57         try
58         {
59             SAXTransformerFactory JavaDoc saxTFact = (SAXTransformerFactory JavaDoc)TransformerFactory.newInstance();
60             TransformerHandler JavaDoc tHandler = saxTFact.newTransformerHandler();
61     
62             Result JavaDoc res = new StreamResult JavaDoc(this.getResponse().getOutputStream());
63             tHandler.setResult(res);
64             
65             return tHandler;
66         }
67         catch (TransformerConfigurationException JavaDoc ex)
68         {
69             throw new ServletException JavaDoc(ex);
70         }
71     }
72     
73     /**
74      * Funnels output to go(String).
75      * @see org.infohazard.maverick.flow.TransformStep#done()
76      */

77     public void done() throws IOException JavaDoc, ServletException JavaDoc
78     {
79         log.debug("Done being written to");
80         
81         if (this.fakeResponse == null)
82         {
83             throw new IllegalStateException JavaDoc("done() called illegally");
84         }
85         else
86         {
87             this.go(this.fakeResponse.getOutputAsString());
88             this.fakeResponse = null;
89         }
90     }
91
92     /**
93      * Funnels output to go(String).
94      * @see org.infohazard.maverick.flow.TransformStep#go(javax.xml.transform.Source)
95      */

96     public void go(Source JavaDoc input) throws IOException JavaDoc, ServletException JavaDoc
97     {
98         log.debug("Building String from Source");
99         
100         try
101         {
102             TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
103             Transformer JavaDoc trans = tFactory.newTransformer();
104             
105             trans.setOutputProperty(OutputKeys.INDENT, "no");
106
107             StringWriter JavaDoc output = new StringWriter JavaDoc();
108             Result JavaDoc res = new javax.xml.transform.stream.StreamResult JavaDoc(output);
109
110             trans.transform(input, res);
111
112             this.go(output.toString());
113         }
114         catch (TransformerException JavaDoc ex)
115         {
116             throw new ServletException JavaDoc(ex);
117         }
118     }
119
120     /**
121      * Funnels output to go(String).
122      * @see org.infohazard.maverick.flow.TransformStep#go(java.io.Reader)
123      */

124     public void go(Reader JavaDoc input) throws IOException JavaDoc, ServletException JavaDoc
125     {
126         log.debug("Building String from Reader");
127         
128         StringWriter JavaDoc output = new StringWriter JavaDoc();
129         while (input.ready())
130             output.write(input.read());
131
132         this.go(output.toString());
133     }
134
135     /**
136      * You implement this.
137      * @see org.infohazard.maverick.flow.TransformStep#go(java.lang.String)
138      */

139     public abstract void go(String JavaDoc input) throws IOException JavaDoc, ServletException JavaDoc;
140 }
141
Popular Tags