View Javadoc

1   /*
2    * $Id: StrutsBodyTagSupport.java 540552 2007-05-22 12:13:00Z mrdon $
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.annotations.StrutsTagAttribute;
29  import org.apache.struts2.views.util.ContextUtil;
30  
31  import com.opensymphony.xwork2.util.ValueStack;
32  
33  
34  /***
35   * Contains common functonalities for Struts JSP Tags.
36   *
37   */
38  public class StrutsBodyTagSupport extends BodyTagSupport {
39  
40      private static final long serialVersionUID = -1201668454354226175L;
41  
42      @StrutsTagAttribute(required=false,description="The id of the tag element")
43      public void setId(String string) {
44          super.setId(string);
45      }
46  
47      protected boolean altSyntax() {
48          return ContextUtil.isUseAltSyntax(getStack().getContext());
49      }
50  
51      protected ValueStack getStack() {
52          return TagUtils.getStack(pageContext);
53      }
54  
55      protected String findString(String expr) {
56          return (String) findValue(expr, String.class);
57      }
58  
59      protected Object findValue(String expr) {
60          if (altSyntax()) {
61              // does the expression start with %{ and end with }? if so, just cut it off!
62              if (expr.startsWith("%{") && expr.endsWith("}")) {
63                  expr = expr.substring(2, expr.length() - 1);
64              }
65          }
66  
67          return getStack().findValue(expr);
68      }
69  
70      protected Object findValue(String expr, Class toType) {
71          if (altSyntax() && toType == String.class) {
72              return translateVariables(expr, getStack());
73          } else {
74              if (altSyntax()) {
75                  // does the expression start with %{ and end with }? if so, just cut it off!
76                  if (expr.startsWith("%{") && expr.endsWith("}")) {
77                      expr = expr.substring(2, expr.length() - 1);
78                  }
79              }
80  
81              return getStack().findValue(expr, toType);
82          }
83      }
84  
85      protected String toString(Throwable t) {
86          FastByteArrayOutputStream bout = new FastByteArrayOutputStream();
87          PrintWriter wrt = new PrintWriter(bout);
88          t.printStackTrace(wrt);
89          wrt.close();
90  
91          return bout.toString();
92      }
93  
94      protected String getBody() {
95          if (bodyContent == null) {
96              return "";
97          } else {
98              return bodyContent.getString().trim();
99          }
100     }
101 
102     public static String translateVariables(String expression, ValueStack stack) {
103         while (true) {
104             int x = expression.indexOf("%{");
105             int y = expression.indexOf("}", x);
106 
107             if ((x != -1) && (y != -1)) {
108                 String var = expression.substring(x + 2, y);
109 
110                 Object o = stack.findValue(var, String.class);
111 
112                 if (o != null) {
113                     expression = expression.substring(0, x) + o + expression.substring(y + 1);
114                 } else {
115                     // the variable doesn't exist, so don't display anything
116                     expression = expression.substring(0, x) + expression.substring(y + 1);
117                 }
118             } else {
119                 break;
120             }
121         }
122 
123         return expression;
124     }
125 }