View Javadoc

1   /*
2    * $Id: StrutsBodyTagSupport.java 454565 2006-10-10 00:02:56Z jmitchell $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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              // does the expression start with %{ and end with }? if so, just cut it off!
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                  // does the expression start with %{ and end with }? if so, just cut it off!
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                     // the variable doesn't exist, so don't display anything
115                     expression = expression.substring(0, x) + expression.substring(y + 1);
116                 }
117             } else {
118                 break;
119             }
120         }
121 
122         return expression;
123     }
124 }