KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > bean > ResourceTag


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

18
19 package org.apache.struts.taglib.bean;
20
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
27
28 import org.apache.struts.util.MessageResources;
29 import org.apache.struts.taglib.TagUtils;
30
31 /**
32  * Define a scripting variable based on the contents of the specified
33  * web application resource.
34  *
35  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
36  */

37 public class ResourceTag extends TagSupport JavaDoc {
38
39     // ------------------------------------------------------------- Properties
40

41     /**
42      * Buffer size to use when reading the input stream.
43      */

44     protected static final int BUFFER_SIZE = 256;
45
46     /**
47      * The name of the scripting variable that will be exposed as a page
48      * scope attribute.
49      */

50     protected String JavaDoc id = null;
51
52     public String JavaDoc getId() {
53         return (this.id);
54     }
55
56     public void setId(String JavaDoc id) {
57         this.id = id;
58     }
59
60     /**
61      * Return an InputStream to the specified resource if this is non-null.
62      */

63     protected String JavaDoc input = null;
64
65     public String JavaDoc getInput() {
66         return (this.input);
67     }
68
69     public void setInput(String JavaDoc input) {
70         this.input = input;
71     }
72
73     /**
74      * The message resources for this package.
75      */

76     protected static MessageResources messages =
77         MessageResources.getMessageResources(
78             "org.apache.struts.taglib.bean.LocalStrings");
79
80     /**
81      * The module-relative URI of the resource whose contents are to
82      * be exposed.
83      */

84     protected String JavaDoc name = null;
85
86     public String JavaDoc getName() {
87         return (this.name);
88     }
89
90     public void setName(String JavaDoc name) {
91         this.name = name;
92     }
93
94     // --------------------------------------------------------- Public Methods
95

96     /**
97      * Retrieve the required property and expose it as a scripting variable.
98      *
99      * @exception JspException if a JSP exception has occurred
100      */

101     public int doStartTag() throws JspException JavaDoc {
102
103         // Acquire an input stream to the specified resource
104
InputStream JavaDoc stream =
105             pageContext.getServletContext().getResourceAsStream(name);
106             
107         if (stream == null) {
108             JspException JavaDoc e =
109                 new JspException JavaDoc(messages.getMessage("resource.get", name));
110             TagUtils.getInstance().saveException(pageContext, e);
111             throw e;
112         }
113
114         // If we are returning an InputStream, do so and return
115
if (input != null) {
116             pageContext.setAttribute(id, stream);
117             return (SKIP_BODY);
118         }
119
120         // Accumulate the contents of this resource into a StringBuffer
121
try {
122             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
123             InputStreamReader JavaDoc reader = new InputStreamReader JavaDoc(stream);
124             char buffer[] = new char[BUFFER_SIZE];
125             int n = 0;
126             while (true) {
127                 n = reader.read(buffer);
128                 if (n < 1) {
129                     break;
130                 }
131                 sb.append(buffer, 0, n);
132             }
133             reader.close();
134             pageContext.setAttribute(id, sb.toString());
135             
136         } catch (IOException JavaDoc e) {
137             TagUtils.getInstance().saveException(pageContext, e);
138             throw new JspException JavaDoc(messages.getMessage("resource.get", name));
139         }
140         
141         return (SKIP_BODY);
142
143     }
144
145     /**
146      * Release all allocated resources.
147      */

148     public void release() {
149
150         super.release();
151         id = null;
152         input = null;
153         name = null;
154
155     }
156
157 }
158
Popular Tags