KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > jsp > WebWorkTagSupport


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.views.jsp;
6
7 import com.opensymphony.webwork.util.FastByteArrayOutputStream;
8 import com.opensymphony.webwork.config.Configuration;
9 import com.opensymphony.xwork.util.OgnlValueStack;
10
11 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
12 import java.io.PrintWriter JavaDoc;
13
14
15 /**
16  * WebWork base class for defining new tag handlers.
17  */

18 public abstract class WebWorkTagSupport extends TagSupport JavaDoc {
19     public static final boolean ALT_SYNTAX = "true".equals(Configuration.getString("webwork.tag.altSyntax"));
20
21     public static String JavaDoc translateVariables(String JavaDoc expression, OgnlValueStack stack) {
22         while (true) {
23             int x = expression.indexOf("%{");
24             int y = expression.indexOf("}", x);
25
26             if ((x != -1) && (y != -1)) {
27                 String JavaDoc var = expression.substring(x + 2, y);
28
29                 Object JavaDoc o = stack.findValue(var, String JavaDoc.class);
30
31                 if (o != null) {
32                     expression = expression.substring(0, x) + o + expression.substring(y + 1);
33                 } else {
34                     // the variable doesn't exist, so don't display anything
35
expression = expression.substring(0, x) + expression.substring(y + 1);
36                 }
37             } else {
38                 break;
39             }
40         }
41
42         return expression;
43     }
44
45     protected OgnlValueStack getStack() {
46         return TagUtils.getStack(pageContext);
47     }
48
49     protected String JavaDoc findString(String JavaDoc expr) {
50         return (String JavaDoc) findValue(expr, String JavaDoc.class);
51     }
52
53     protected Object JavaDoc findValue(String JavaDoc expr) {
54         if (ALT_SYNTAX) {
55             // does the expression start with %{ and end with }? if so, just cut it off!
56
if (expr.startsWith("%{") && expr.endsWith("}")) {
57                 expr = expr.substring(2, expr.length() - 1);
58             }
59         }
60
61         return getStack().findValue(expr);
62     }
63
64     protected Object JavaDoc findValue(String JavaDoc expr, Class JavaDoc toType) {
65         if (ALT_SYNTAX && toType == String JavaDoc.class) {
66             return translateVariables(expr, getStack());
67         } else {
68             if (ALT_SYNTAX) {
69                 // does the expression start with %{ and end with }? if so, just cut it off!
70
if (expr.startsWith("%{") && expr.endsWith("}")) {
71                     expr = expr.substring(2, expr.length() - 1);
72                 }
73             }
74
75             return getStack().findValue(expr, toType);
76         }
77     }
78
79     protected String JavaDoc toString(Throwable JavaDoc t) {
80         FastByteArrayOutputStream bout = new FastByteArrayOutputStream();
81         PrintWriter JavaDoc wrt = new PrintWriter JavaDoc(bout);
82         t.printStackTrace(wrt);
83         wrt.close();
84
85         return bout.toString();
86     }
87 }
88
Popular Tags