1 16 17 package org.apache.taglibs.io; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.InputStreamReader ; 22 import java.io.OutputStream ; 23 import java.io.Reader ; 24 import java.io.StringReader ; 25 import java.io.Writer ; 26 27 import javax.servlet.ServletContext ; 28 import javax.servlet.jsp.JspException ; 29 import javax.servlet.jsp.tagext.BodyContent ; 30 import javax.servlet.jsp.tagext.Tag ; 31 32 39 public abstract class TransformerTagSupport extends AbstractBodyTag implements PipeConsumer { 40 41 protected static final boolean CLOSE_READER = false; 42 43 44 45 private Reader reader; 46 47 48 private Writer writer; 49 50 public TransformerTagSupport() { 51 } 52 53 54 public int doStartTag() throws JspException { 57 return EVAL_BODY_TAG; 58 } 59 60 public int doAfterBody() throws JspException { 61 BodyContent body = null; 62 if ( reader == null ) { 63 body = getBodyContent(); 64 reader = new StringReader ( body.getString() ); 65 } 66 if ( writer == null ) { 67 writer = getBodyContent().getEnclosingWriter(); 68 } 69 try { 70 transform( reader, writer ); 71 } 72 catch (IOException e) { 73 handleException(e); 74 } 75 finally { 76 if ( CLOSE_READER ) { 77 try { 78 reader.close(); 79 } 80 catch (Exception e) { 81 } 82 } 83 reader = null; 84 writer = null; 85 if ( body != null ) { 86 body.clearBody(); 87 } 88 } 89 return EVAL_PAGE; 90 } 91 92 public void release() { 93 super.release(); 94 reader = null; 95 writer = null; 96 } 97 98 public void setReader(Reader reader) { 101 this.reader = reader; 102 } 103 104 public void setWriter(Writer writer) { 107 this.writer = writer; 108 } 109 110 protected PipeConsumer getConsumer() { 113 return (PipeConsumer) findAncestorWithClass( this, PipeConsumer.class ); 114 } 115 116 117 protected abstract void transform( 118 Reader reader, 119 Writer writer 120 ) throws IOException , JspException ; 121 } 122 | Popular Tags |