1 18 19 20 package org.apache.struts.taglib.bean; 21 22 23 import javax.servlet.jsp.JspException ; 24 import javax.servlet.jsp.PageContext ; 25 import javax.servlet.jsp.tagext.BodyTagSupport ; 26 27 import org.apache.struts.taglib.TagUtils; 28 import org.apache.struts.util.MessageResources; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 33 39 40 public class DefineTag extends BodyTagSupport { 41 42 45 private static final Log log = LogFactory.getLog(DefineTag.class); 46 47 49 52 protected static MessageResources messages = 53 MessageResources.getMessageResources 54 ("org.apache.struts.taglib.bean.LocalStrings"); 55 56 57 60 protected String body = null; 61 62 63 65 66 70 protected String id = null; 71 72 public String getId() { 73 return (this.id); 74 } 75 76 public void setId(String id) { 77 this.id = id; 78 } 79 80 81 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 95 98 protected String property = null; 99 100 public String getProperty() { 101 return (this.property); 102 } 103 104 public void setProperty(String property) { 105 this.property = property; 106 } 107 108 109 112 protected String scope = null; 113 114 public String getScope() { 115 return (this.scope); 116 } 117 118 public void setScope(String scope) { 119 this.scope = scope; 120 } 121 122 123 126 protected String toScope = null; 127 128 public String getToScope() { 129 return (this.toScope); 130 } 131 132 public void setToScope(String toScope) { 133 this.toScope = toScope; 134 } 135 136 137 140 protected String type = null; 141 142 public String getType() { 143 return (this.type); 144 } 145 146 public void setType(String type) { 147 this.type = type; 148 } 149 150 151 154 protected String value = null; 155 156 public String getValue() { 157 return (this.value); 158 } 159 160 public void setValue(String value) { 161 this.value = value; 162 } 163 164 165 167 168 173 public int doStartTag() throws JspException { 174 175 return (EVAL_BODY_TAG); 176 177 } 178 179 180 186 public int doAfterBody() throws JspException { 187 188 if (bodyContent != null) { 189 body = bodyContent.getString(); 190 if (body != null) { 191 body = body.trim(); 192 } 193 if (body.length() < 1) { 194 body = null; 195 } 196 } 197 return (SKIP_BODY); 198 199 } 200 201 202 207 public int doEndTag() throws JspException { 208 209 int n = 0; 211 if (this.body != null) { 212 n++; 213 } 214 if (this.name != null) { 215 n++; 216 } 217 if (this.value != null) { 218 n++; 219 } 220 if (n != 1) { 221 JspException e = 222 new JspException (messages.getMessage("define.value")); 223 TagUtils.getInstance().saveException(pageContext, e); 224 throw e; 225 } 226 227 Object value = this.value; 229 if ((value == null) && (name != null)) { 230 value = TagUtils.getInstance().lookup(pageContext, name, property, scope); 231 } 232 if ((value == null) && (body != null)) { 233 value = body; 234 } 235 if (value == null) { 236 JspException e = 237 new JspException (messages.getMessage("define.null")); 238 TagUtils.getInstance().saveException(pageContext, e); 239 throw e; 240 } 241 242 int inScope = PageContext.PAGE_SCOPE; 244 try { 245 if (toScope != null) { 246 inScope = TagUtils.getInstance().getScope(toScope); 247 } 248 } catch (JspException e) { 249 log.warn("toScope was invalid name so we default to PAGE_SCOPE",e); 250 } 251 252 pageContext.setAttribute(id, value, inScope); 253 254 return (EVAL_PAGE); 256 257 } 258 259 262 public void release() { 263 264 super.release(); 265 body = null; 266 id = null; 267 name = null; 268 property = null; 269 scope = null; 270 toScope = "page"; 271 type = null; 272 value = null; 273 274 } 275 276 277 } 278 | Popular Tags |