1 18 19 package org.apache.struts.taglib.bean; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.InputStreamReader ; 24 25 import javax.servlet.jsp.JspException ; 26 import javax.servlet.jsp.tagext.TagSupport ; 27 28 import org.apache.struts.util.MessageResources; 29 import org.apache.struts.taglib.TagUtils; 30 31 37 public class ResourceTag extends TagSupport { 38 39 41 44 protected static final int BUFFER_SIZE = 256; 45 46 50 protected String id = null; 51 52 public String getId() { 53 return (this.id); 54 } 55 56 public void setId(String id) { 57 this.id = id; 58 } 59 60 63 protected String input = null; 64 65 public String getInput() { 66 return (this.input); 67 } 68 69 public void setInput(String input) { 70 this.input = input; 71 } 72 73 76 protected static MessageResources messages = 77 MessageResources.getMessageResources( 78 "org.apache.struts.taglib.bean.LocalStrings"); 79 80 84 protected String name = null; 85 86 public String getName() { 87 return (this.name); 88 } 89 90 public void setName(String name) { 91 this.name = name; 92 } 93 94 96 101 public int doStartTag() throws JspException { 102 103 InputStream stream = 105 pageContext.getServletContext().getResourceAsStream(name); 106 107 if (stream == null) { 108 JspException e = 109 new JspException (messages.getMessage("resource.get", name)); 110 TagUtils.getInstance().saveException(pageContext, e); 111 throw e; 112 } 113 114 if (input != null) { 116 pageContext.setAttribute(id, stream); 117 return (SKIP_BODY); 118 } 119 120 try { 122 StringBuffer sb = new StringBuffer (); 123 InputStreamReader reader = new InputStreamReader (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 e) { 137 TagUtils.getInstance().saveException(pageContext, e); 138 throw new JspException (messages.getMessage("resource.get", name)); 139 } 140 141 return (SKIP_BODY); 142 143 } 144 145 148 public void release() { 149 150 super.release(); 151 id = null; 152 input = null; 153 name = null; 154 155 } 156 157 } 158 | Popular Tags |