1 16 17 package org.apache.taglibs.standard.examples.taglib; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.InputStreamReader ; 22 import java.io.Reader ; 23 24 import javax.servlet.jsp.JspException ; 25 import javax.servlet.jsp.tagext.TagSupport ; 26 27 33 public class FileTag extends TagSupport { 34 35 38 private String id; 39 private String file; 40 41 private Reader reader; 42 43 46 public FileTag() { 47 super(); 48 init(); 49 } 50 51 private void init() { 52 id = null; 53 file = null; 54 } 55 56 59 62 public void setId(String id) { 63 this.id = id; 64 } 65 66 69 public void setfile(String file) { 70 this.file = file; 71 } 72 73 76 public int doStartTag() throws JspException { 77 reader = getReaderFromFile(file); 78 exposeVariable(reader); 79 return EVAL_BODY_INCLUDE; 80 } 81 82 public int doEndTag() throws JspException { 83 try { 84 reader.close(); 85 } catch (IOException ex) {} 86 reader = null; 87 return EVAL_PAGE; 88 } 89 90 93 public void release() { 94 super.release(); 95 init(); 96 } 97 98 101 public Reader getReaderFromFile(String name) throws JspException { 102 InputStream in = pageContext.getServletContext(). 103 getResourceAsStream(name); 104 if (in == null) { 105 throw new JspException ("Could not access " + name); 106 } 107 108 return new InputStreamReader (in); 109 } 110 111 112 115 private void exposeVariable(Reader reader) { 116 if (id != null) { 117 pageContext.setAttribute(id, reader); 118 } 119 } 120 } 121 | Popular Tags |