View Javadoc

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