001: /*
002: * $Id: ResourceTag.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.taglib.bean;
022:
023: import org.apache.struts.taglib.TagUtils;
024: import org.apache.struts.util.MessageResources;
025:
026: import javax.servlet.jsp.JspException;
027: import javax.servlet.jsp.tagext.TagSupport;
028:
029: import java.io.IOException;
030: import java.io.InputStream;
031: import java.io.InputStreamReader;
032:
033: /**
034: * Define a scripting variable based on the contents of the specified web
035: * application resource.
036: *
037: * @version $Rev: 471754 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
038: * $
039: */
040: public class ResourceTag extends TagSupport {
041: // ------------------------------------------------------------- Properties
042:
043: /**
044: * Buffer size to use when reading the input stream.
045: */
046: protected static final int BUFFER_SIZE = 256;
047:
048: /**
049: * The message resources for this package.
050: */
051: protected static MessageResources messages = MessageResources
052: .getMessageResources("org.apache.struts.taglib.bean.LocalStrings");
053:
054: /**
055: * The name of the scripting variable that will be exposed as a page scope
056: * attribute.
057: */
058: protected String id = null;
059:
060: /**
061: * Return an InputStream to the specified resource if this is non-null.
062: */
063: protected String input = null;
064:
065: /**
066: * The module-relative URI of the resource whose contents are to be
067: * exposed.
068: */
069: protected String name = null;
070:
071: public String getId() {
072: return (this .id);
073: }
074:
075: public void setId(String id) {
076: this .id = id;
077: }
078:
079: public String getInput() {
080: return (this .input);
081: }
082:
083: public void setInput(String input) {
084: this .input = input;
085: }
086:
087: public String getName() {
088: return (this .name);
089: }
090:
091: public void setName(String name) {
092: this .name = name;
093: }
094:
095: // --------------------------------------------------------- Public Methods
096:
097: /**
098: * Retrieve the required property and expose it as a scripting variable.
099: *
100: * @throws JspException if a JSP exception has occurred
101: */
102: public int doStartTag() throws JspException {
103: // Acquire an input stream to the specified resource
104: InputStream stream = pageContext.getServletContext()
105: .getResourceAsStream(name);
106:
107: if (stream == null) {
108: JspException e = new JspException(messages.getMessage(
109: "resource.get", name));
110:
111: TagUtils.getInstance().saveException(pageContext, e);
112: throw e;
113: }
114:
115: // If we are returning an InputStream, do so and return
116: if (input != null) {
117: pageContext.setAttribute(id, stream);
118:
119: return (SKIP_BODY);
120: }
121:
122: // Accumulate the contents of this resource into a StringBuffer
123: try {
124: StringBuffer sb = new StringBuffer();
125: InputStreamReader reader = new InputStreamReader(stream);
126: char[] buffer = new char[BUFFER_SIZE];
127: int n = 0;
128:
129: while (true) {
130: n = reader.read(buffer);
131:
132: if (n < 1) {
133: break;
134: }
135:
136: sb.append(buffer, 0, n);
137: }
138:
139: reader.close();
140: pageContext.setAttribute(id, sb.toString());
141: } catch (IOException e) {
142: TagUtils.getInstance().saveException(pageContext, e);
143: throw new JspException(messages.getMessage("resource.get",
144: name));
145: }
146:
147: return (SKIP_BODY);
148: }
149:
150: /**
151: * Release all allocated resources.
152: */
153: public void release() {
154: super.release();
155: id = null;
156: input = null;
157: name = null;
158: }
159: }
|