1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.taglib.bean;
19
20 import org.apache.struts.taglib.TagUtils;
21 import org.apache.struts.util.MessageResources;
22
23 import javax.servlet.jsp.JspException;
24 import javax.servlet.jsp.tagext.TagSupport;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29
30 /***
31 * Define a scripting variable based on the contents of the specified web
32 * application resource.
33 *
34 * @version $Rev: 376840 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
35 * $
36 */
37 public class ResourceTag extends TagSupport {
38
39
40 /***
41 * Buffer size to use when reading the input stream.
42 */
43 protected static final int BUFFER_SIZE = 256;
44
45 /***
46 * The message resources for this package.
47 */
48 protected static MessageResources messages =
49 MessageResources.getMessageResources(
50 "org.apache.struts.taglib.bean.LocalStrings");
51
52 /***
53 * The name of the scripting variable that will be exposed as a page scope
54 * attribute.
55 */
56 protected String id = null;
57
58 /***
59 * Return an InputStream to the specified resource if this is non-null.
60 */
61 protected String input = null;
62
63 /***
64 * The module-relative URI of the resource whose contents are to be
65 * exposed.
66 */
67 protected String name = null;
68
69 public String getId() {
70 return (this.id);
71 }
72
73 public void setId(String id) {
74 this.id = id;
75 }
76
77 public String getInput() {
78 return (this.input);
79 }
80
81 public void setInput(String input) {
82 this.input = input;
83 }
84
85 public String getName() {
86 return (this.name);
87 }
88
89 public void setName(String name) {
90 this.name = name;
91 }
92
93
94
95 /***
96 * Retrieve the required property and expose it as a scripting variable.
97 *
98 * @throws JspException if a JSP exception has occurred
99 */
100 public int doStartTag() throws JspException {
101
102 InputStream stream =
103 pageContext.getServletContext().getResourceAsStream(name);
104
105 if (stream == null) {
106 JspException e =
107 new JspException(messages.getMessage("resource.get", name));
108
109 TagUtils.getInstance().saveException(pageContext, e);
110 throw e;
111 }
112
113
114 if (input != null) {
115 pageContext.setAttribute(id, stream);
116
117 return (SKIP_BODY);
118 }
119
120
121 try {
122 StringBuffer sb = new StringBuffer();
123 InputStreamReader reader = new InputStreamReader(stream);
124 char[] buffer = new char[BUFFER_SIZE];
125 int n = 0;
126
127 while (true) {
128 n = reader.read(buffer);
129
130 if (n < 1) {
131 break;
132 }
133
134 sb.append(buffer, 0, n);
135 }
136
137 reader.close();
138 pageContext.setAttribute(id, sb.toString());
139 } catch (IOException e) {
140 TagUtils.getInstance().saveException(pageContext, e);
141 throw new JspException(messages.getMessage("resource.get", name));
142 }
143
144 return (SKIP_BODY);
145 }
146
147 /***
148 * Release all allocated resources.
149 */
150 public void release() {
151 super.release();
152 id = null;
153 input = null;
154 name = null;
155 }
156 }