1 16 17 package org.springframework.web.servlet.tags; 18 19 import java.beans.PropertyEditor ; 20 import java.io.IOException ; 21 22 import javax.servlet.jsp.JspException ; 23 import javax.servlet.jsp.tagext.TagSupport ; 24 25 import org.springframework.web.util.ExpressionEvaluationUtils; 26 import org.springframework.web.util.HtmlUtils; 27 import org.springframework.web.util.TagUtils; 28 29 42 public class TransformTag extends HtmlEscapingAwareTag { 43 44 45 private Object value; 46 47 48 private String var; 49 50 51 private String scope = TagUtils.SCOPE_PAGE; 52 53 54 66 public void setValue(Object value) { 67 this.value = value; 68 } 69 70 76 public void setVar(String var) { 77 this.var = var; 78 } 79 80 87 public void setScope(String scope) { 88 this.scope = scope; 89 } 90 91 92 protected final int doStartTagInternal() throws JspException { 93 Object resolvedValue = this.value; 94 if (this.value instanceof String ) { 95 String strValue = (String ) this.value; 96 resolvedValue = ExpressionEvaluationUtils.evaluate("value", strValue, pageContext); 97 } 98 if (resolvedValue != null) { 99 BindTag tag = (BindTag) TagSupport.findAncestorWithClass(this, BindTag.class); 101 if (tag == null) { 102 throw new JspException ("TransformTag can only be used within BindTag"); 104 } 105 PropertyEditor editor = tag.getEditor(); 107 String result = null; 108 if (editor != null) { 109 editor.setValue(resolvedValue); 111 result = editor.getAsText(); 112 } 113 else { 114 result = resolvedValue.toString(); 116 } 117 result = isHtmlEscape() ? HtmlUtils.htmlEscape(result) : result; 118 String resolvedVar = ExpressionEvaluationUtils.evaluateString("var", this.var, pageContext); 119 if (resolvedVar != null) { 120 String resolvedScope = ExpressionEvaluationUtils.evaluateString("scope", this.scope, pageContext); 121 pageContext.setAttribute(resolvedVar, result, TagUtils.getScope(resolvedScope)); 122 } 123 else { 124 try { 125 pageContext.getOut().print(result); 127 } 128 catch (IOException ex) { 129 throw new JspException (ex); 130 } 131 } 132 } 133 return SKIP_BODY; 134 } 135 136 } 137 | Popular Tags |