1 5 6 package org.infohazard.maverick.transform; 7 8 import java.io.IOException ; 9 import java.io.Reader ; 10 import java.io.StringWriter ; 11 12 import javax.servlet.ServletException ; 13 import javax.xml.transform.OutputKeys ; 14 import javax.xml.transform.Result ; 15 import javax.xml.transform.Source ; 16 import javax.xml.transform.Transformer ; 17 import javax.xml.transform.TransformerConfigurationException ; 18 import javax.xml.transform.TransformerException ; 19 import javax.xml.transform.TransformerFactory ; 20 import javax.xml.transform.sax.SAXTransformerFactory ; 21 import javax.xml.transform.sax.TransformerHandler ; 22 import javax.xml.transform.stream.StreamResult ; 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 ; 28 29 33 public abstract class StringTransformStep extends AbstractTransformStep 34 { 35 38 private static Log log = LogFactory.getLog(StringTransformStep.class); 39 40 45 public StringTransformStep(TransformContext tctx) throws ServletException 46 { 47 super(tctx); 48 } 49 50 53 public ContentHandler getSAXHandler() throws IOException , ServletException 54 { 55 log.debug("Creating TransformerHandler which sends to next step output stream."); 56 57 try 58 { 59 SAXTransformerFactory saxTFact = (SAXTransformerFactory )TransformerFactory.newInstance(); 60 TransformerHandler tHandler = saxTFact.newTransformerHandler(); 61 62 Result res = new StreamResult (this.getResponse().getOutputStream()); 63 tHandler.setResult(res); 64 65 return tHandler; 66 } 67 catch (TransformerConfigurationException ex) 68 { 69 throw new ServletException (ex); 70 } 71 } 72 73 77 public void done() throws IOException , ServletException 78 { 79 log.debug("Done being written to"); 80 81 if (this.fakeResponse == null) 82 { 83 throw new IllegalStateException ("done() called illegally"); 84 } 85 else 86 { 87 this.go(this.fakeResponse.getOutputAsString()); 88 this.fakeResponse = null; 89 } 90 } 91 92 96 public void go(Source input) throws IOException , ServletException 97 { 98 log.debug("Building String from Source"); 99 100 try 101 { 102 TransformerFactory tFactory = TransformerFactory.newInstance(); 103 Transformer trans = tFactory.newTransformer(); 104 105 trans.setOutputProperty(OutputKeys.INDENT, "no"); 106 107 StringWriter output = new StringWriter (); 108 Result res = new javax.xml.transform.stream.StreamResult (output); 109 110 trans.transform(input, res); 111 112 this.go(output.toString()); 113 } 114 catch (TransformerException ex) 115 { 116 throw new ServletException (ex); 117 } 118 } 119 120 124 public void go(Reader input) throws IOException , ServletException 125 { 126 log.debug("Building String from Reader"); 127 128 StringWriter output = new StringWriter (); 129 while (input.ready()) 130 output.write(input.read()); 131 132 this.go(output.toString()); 133 } 134 135 139 public abstract void go(String input) throws IOException , ServletException ; 140 } 141 | Popular Tags |