View Javadoc

1   /*
2    * $Id: StrutsBodyTagSupport.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.views.jsp;
23  
24  import java.io.PrintWriter;
25  
26  import javax.servlet.jsp.tagext.BodyTagSupport;
27  
28  import org.apache.struts2.util.FastByteArrayOutputStream;
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      protected boolean altSyntax() {
43          return ContextUtil.isUseAltSyntax(getStack().getContext());
44      }
45  
46      protected ValueStack getStack() {
47          return TagUtils.getStack(pageContext);
48      }
49  
50      protected String findString(String expr) {
51          return (String) findValue(expr, String.class);
52      }
53  
54      protected Object findValue(String expr) {
55          if (altSyntax()) {
56              // does the expression start with %{ and end with }? if so, just cut it off!
57              if (expr.startsWith("%{") && expr.endsWith("}")) {
58                  expr = expr.substring(2, expr.length() - 1);
59              }
60          }
61  
62          return getStack().findValue(expr);
63      }
64  
65      protected Object findValue(String expr, Class toType) {
66          if (altSyntax() && toType == String.class) {
67              return translateVariables(expr, getStack());
68          } else {
69              if (altSyntax()) {
70                  // does the expression start with %{ and end with }? if so, just cut it off!
71                  if (expr.startsWith("%{") && expr.endsWith("}")) {
72                      expr = expr.substring(2, expr.length() - 1);
73                  }
74              }
75  
76              return getStack().findValue(expr, toType);
77          }
78      }
79  
80      protected String toString(Throwable t) {
81          FastByteArrayOutputStream bout = new FastByteArrayOutputStream();
82          PrintWriter wrt = new PrintWriter(bout);
83          t.printStackTrace(wrt);
84          wrt.close();
85  
86          return bout.toString();
87      }
88  
89      protected String getBody() {
90          if (bodyContent == null) {
91              return "";
92          } else {
93              return bodyContent.getString().trim();
94          }
95      }
96  
97      public static String translateVariables(String expression, ValueStack stack) {
98          while (true) {
99              int x = expression.indexOf("%{");
100             int y = expression.indexOf("}", x);
101 
102             if ((x != -1) && (y != -1)) {
103                 String var = expression.substring(x + 2, y);
104 
105                 Object o = stack.findValue(var, String.class);
106 
107                 if (o != null) {
108                     expression = expression.substring(0, x) + o + expression.substring(y + 1);
109                 } else {
110                     // the variable doesn't exist, so don't display anything
111                     expression = expression.substring(0, x) + expression.substring(y + 1);
112                 }
113             } else {
114                 break;
115             }
116         }
117 
118         return expression;
119     }
120 }