1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.views.jsp;
19
20 import java.io.PrintWriter;
21
22 import javax.servlet.jsp.tagext.BodyTagSupport;
23
24 import org.apache.struts2.util.FastByteArrayOutputStream;
25 import org.apache.struts2.views.util.ContextUtil;
26
27 import com.opensymphony.xwork2.util.ValueStack;
28
29
30 /***
31 * Contains common functonalities for Struts JSP Tags.
32 *
33 */
34 public class StrutsBodyTagSupport extends BodyTagSupport {
35
36 private static final long serialVersionUID = -1201668454354226175L;
37
38 /***
39 * @s.tagattribute required="false" type="String"
40 * description="The id of the tag element."
41 */
42 public void setId(String string) {
43 super.setId(string);
44 }
45
46 protected boolean altSyntax() {
47 return ContextUtil.isUseAltSyntax(getStack().getContext());
48 }
49
50 protected ValueStack getStack() {
51 return TagUtils.getStack(pageContext);
52 }
53
54 protected String findString(String expr) {
55 return (String) findValue(expr, String.class);
56 }
57
58 protected Object findValue(String expr) {
59 if (altSyntax()) {
60
61 if (expr.startsWith("%{") && expr.endsWith("}")) {
62 expr = expr.substring(2, expr.length() - 1);
63 }
64 }
65
66 return getStack().findValue(expr);
67 }
68
69 protected Object findValue(String expr, Class toType) {
70 if (altSyntax() && toType == String.class) {
71 return translateVariables(expr, getStack());
72 } else {
73 if (altSyntax()) {
74
75 if (expr.startsWith("%{") && expr.endsWith("}")) {
76 expr = expr.substring(2, expr.length() - 1);
77 }
78 }
79
80 return getStack().findValue(expr, toType);
81 }
82 }
83
84 protected String toString(Throwable t) {
85 FastByteArrayOutputStream bout = new FastByteArrayOutputStream();
86 PrintWriter wrt = new PrintWriter(bout);
87 t.printStackTrace(wrt);
88 wrt.close();
89
90 return bout.toString();
91 }
92
93 protected String getBody() {
94 if (bodyContent == null) {
95 return "";
96 } else {
97 return bodyContent.getString().trim();
98 }
99 }
100
101 public static String translateVariables(String expression, ValueStack stack) {
102 while (true) {
103 int x = expression.indexOf("%{");
104 int y = expression.indexOf("}", x);
105
106 if ((x != -1) && (y != -1)) {
107 String var = expression.substring(x + 2, y);
108
109 Object o = stack.findValue(var, String.class);
110
111 if (o != null) {
112 expression = expression.substring(0, x) + o + expression.substring(y + 1);
113 } else {
114
115 expression = expression.substring(0, x) + expression.substring(y + 1);
116 }
117 } else {
118 break;
119 }
120 }
121
122 return expression;
123 }
124 }