KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > examples > taglib > FileTag


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.examples.taglib;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import java.io.Reader JavaDoc;
23
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
26
27 /**
28  * <p>Tag handler for &lt;file&gt;
29  *
30  * @author Pierre Delisle
31  * @version $Revision: 1.3 $ $Date: 2004/02/28 01:01:41 $
32  */

33 public class FileTag extends TagSupport JavaDoc {
34     
35     //*********************************************************************
36
// Instance variables
37

38     private String JavaDoc id;
39     private String JavaDoc file;
40     
41     private Reader JavaDoc reader;
42     
43     //*********************************************************************
44
// Constructors
45

46     public FileTag() {
47         super();
48         init();
49     }
50     
51     private void init() {
52         id = null;
53         file = null;
54     }
55     
56     //*********************************************************************
57
// Tag's properties
58

59     /**
60      * Tag's 'id' attribute
61      */

62     public void setId(String JavaDoc id) {
63         this.id = id;
64     }
65     
66     /**
67      * Tag's 'file' attribute
68      */

69     public void setfile(String JavaDoc file) {
70         this.file = file;
71     }
72     
73     //*********************************************************************
74
// TagSupport methods
75

76     public int doStartTag() throws JspException JavaDoc {
77         reader = getReaderFromFile(file);
78         exposeVariable(reader);
79         return EVAL_BODY_INCLUDE;
80     }
81     
82     public int doEndTag() throws JspException JavaDoc {
83         try {
84             reader.close();
85         } catch (IOException JavaDoc ex) {}
86         reader = null;
87         return EVAL_PAGE;
88     }
89     
90     /**
91      * Releases any files we may have (or inherit)
92      */

93     public void release() {
94         super.release();
95         init();
96     }
97     
98     //*********************************************************************
99
// Tag's specific behavior methods
100

101     public Reader JavaDoc getReaderFromFile(String JavaDoc name) throws JspException JavaDoc {
102         InputStream JavaDoc in = pageContext.getServletContext().
103             getResourceAsStream(name);
104         if (in == null) {
105             throw new JspException JavaDoc("Could not access " + name);
106         }
107
108         return new InputStreamReader JavaDoc(in);
109     }
110
111     
112     //*********************************************************************
113
// Utility methods
114

115     private void exposeVariable(Reader JavaDoc reader) {
116         if (id != null) {
117             pageContext.setAttribute(id, reader);
118         }
119     }
120 }
121
Popular Tags