001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.jsp;
006:
007: import com.opensymphony.webwork.util.FastByteArrayOutputStream;
008: import com.opensymphony.webwork.views.util.ContextUtil;
009: import com.opensymphony.xwork.util.OgnlValueStack;
010:
011: import javax.servlet.jsp.tagext.BodyTagSupport;
012: import java.io.PrintWriter;
013:
014: /**
015: * Contains common functonalities for WebWork JSP Tags.
016: *
017: * @author plightbo
018: * @author tm_jee
019: * Date: Oct 17, 2003
020: * Time: 7:09:15 AM
021: */
022: public class WebWorkBodyTagSupport extends BodyTagSupport {
023:
024: private static final long serialVersionUID = -1201668454354226175L;
025:
026: /**
027: * @ww.tagattribute required="false" type="String"
028: * description="The id of the tag element."
029: */
030: public void setId(String string) {
031: super .setId(string);
032: }
033:
034: protected boolean altSyntax() {
035: return ContextUtil.isUseAltSyntax(getStack().getContext());
036: }
037:
038: protected OgnlValueStack getStack() {
039: return TagUtils.getStack(pageContext);
040: }
041:
042: protected String findString(String expr) {
043: return (String) findValue(expr, String.class);
044: }
045:
046: protected Object findValue(String expr) {
047: if (altSyntax()) {
048: // does the expression start with %{ and end with }? if so, just cut it off!
049: if (expr.startsWith("%{") && expr.endsWith("}")) {
050: expr = expr.substring(2, expr.length() - 1);
051: }
052: }
053:
054: return getStack().findValue(expr);
055: }
056:
057: protected Object findValue(String expr, Class toType) {
058: if (altSyntax() && toType == String.class) {
059: return translateVariables(expr, getStack());
060: } else {
061: if (altSyntax()) {
062: // does the expression start with %{ and end with }? if so, just cut it off!
063: if (expr.startsWith("%{") && expr.endsWith("}")) {
064: expr = expr.substring(2, expr.length() - 1);
065: }
066: }
067:
068: return getStack().findValue(expr, toType);
069: }
070: }
071:
072: protected String toString(Throwable t) {
073: FastByteArrayOutputStream bout = new FastByteArrayOutputStream();
074: PrintWriter wrt = new PrintWriter(bout);
075: t.printStackTrace(wrt);
076: wrt.close();
077:
078: return bout.toString();
079: }
080:
081: protected String getBody() {
082: if (bodyContent == null) {
083: return "";
084: } else {
085: String body = bodyContent.getString();
086: if (body == null) {
087: return "";
088: } else {
089: return body.trim();
090: }
091: }
092: }
093:
094: public static String translateVariables(String expression,
095: OgnlValueStack stack) {
096: while (true) {
097: int x = expression.indexOf("%{");
098: int y = expression.indexOf("}", x);
099:
100: if ((x != -1) && (y != -1)) {
101: String var = expression.substring(x + 2, y);
102:
103: Object o = stack.findValue(var, String.class);
104:
105: if (o != null) {
106: expression = expression.substring(0, x) + o
107: + expression.substring(y + 1);
108: } else {
109: // the variable doesn't exist, so don't display anything
110: expression = expression.substring(0, x)
111: + expression.substring(y + 1);
112: }
113: } else {
114: break;
115: }
116: }
117:
118: return expression;
119: }
120: }
|