001: /*
002: * $Id: StrutsBodyTagSupport.java 540552 2007-05-22 12:13:00Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.views.jsp;
022:
023: import java.io.PrintWriter;
024:
025: import javax.servlet.jsp.tagext.BodyTagSupport;
026:
027: import org.apache.struts2.util.FastByteArrayOutputStream;
028: import org.apache.struts2.views.annotations.StrutsTagAttribute;
029: import org.apache.struts2.views.util.ContextUtil;
030:
031: import com.opensymphony.xwork2.util.ValueStack;
032:
033: /**
034: * Contains common functonalities for Struts JSP Tags.
035: *
036: */
037: public class StrutsBodyTagSupport extends BodyTagSupport {
038:
039: private static final long serialVersionUID = -1201668454354226175L;
040:
041: @StrutsTagAttribute(required=false,description="The id of the tag element")
042: public void setId(String string) {
043: super .setId(string);
044: }
045:
046: protected boolean altSyntax() {
047: return ContextUtil.isUseAltSyntax(getStack().getContext());
048: }
049:
050: protected ValueStack getStack() {
051: return TagUtils.getStack(pageContext);
052: }
053:
054: protected String findString(String expr) {
055: return (String) findValue(expr, String.class);
056: }
057:
058: protected Object findValue(String expr) {
059: if (altSyntax()) {
060: // does the expression start with %{ and end with }? if so, just cut it off!
061: if (expr.startsWith("%{") && expr.endsWith("}")) {
062: expr = expr.substring(2, expr.length() - 1);
063: }
064: }
065:
066: return getStack().findValue(expr);
067: }
068:
069: protected Object findValue(String expr, Class toType) {
070: if (altSyntax() && toType == String.class) {
071: return translateVariables(expr, getStack());
072: } else {
073: if (altSyntax()) {
074: // does the expression start with %{ and end with }? if so, just cut it off!
075: if (expr.startsWith("%{") && expr.endsWith("}")) {
076: expr = expr.substring(2, expr.length() - 1);
077: }
078: }
079:
080: return getStack().findValue(expr, toType);
081: }
082: }
083:
084: protected String toString(Throwable t) {
085: FastByteArrayOutputStream bout = new FastByteArrayOutputStream();
086: PrintWriter wrt = new PrintWriter(bout);
087: t.printStackTrace(wrt);
088: wrt.close();
089:
090: return bout.toString();
091: }
092:
093: protected String getBody() {
094: if (bodyContent == null) {
095: return "";
096: } else {
097: return bodyContent.getString().trim();
098: }
099: }
100:
101: public static String translateVariables(String expression,
102: 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
114: + expression.substring(y + 1);
115: } else {
116: // the variable doesn't exist, so don't display anything
117: expression = expression.substring(0, x)
118: + expression.substring(y + 1);
119: }
120: } else {
121: break;
122: }
123: }
124:
125: return expression;
126: }
127: }
|