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