KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > components > Component


1 package com.opensymphony.webwork.components;
2
3 import com.opensymphony.webwork.config.Configuration;
4 import com.opensymphony.webwork.util.FastByteArrayOutputStream;
5 import com.opensymphony.xwork.util.OgnlValueStack;
6 import com.opensymphony.xwork.util.TextParseUtil;
7
8 import java.io.PrintWriter JavaDoc;
9 import java.io.Writer JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Stack JavaDoc;
14
15 /**
16  * User: plightbo
17  * Date: Jul 2, 2005
18  * Time: 5:52:26 PM
19  */

20 public class Component {
21     public static final boolean ALT_SYNTAX = "true".equals(Configuration.getString("webwork.tag.altSyntax"));
22     public static final String JavaDoc COMPONENT_STACK = "__component_stack";
23
24     protected OgnlValueStack stack;
25     protected Map JavaDoc parameters;
26     protected String JavaDoc id;
27
28     public Component(OgnlValueStack stack) {
29         this.stack = stack;
30         this.parameters = new HashMap JavaDoc();
31         getComponentStack().push(this);
32     }
33
34     public OgnlValueStack getStack() {
35         return stack;
36     }
37
38     private Stack getComponentStack() {
39         Stack componentStack = (Stack) stack.getContext().get(COMPONENT_STACK);
40         if (componentStack == null) {
41             componentStack = new Stack();
42             stack.getContext().put(COMPONENT_STACK, componentStack);
43         }
44         return componentStack;
45     }
46
47     public void start(Writer JavaDoc writer) {
48     }
49
50     public void end(Writer JavaDoc writer) {
51         getComponentStack().pop();
52     }
53
54     public Component findAncestor(Class JavaDoc clazz) {
55         Stack componentStack = getComponentStack();
56         for (Iterator JavaDoc iterator = componentStack.iterator(); iterator.hasNext();) {
57             Component component = (Component) iterator.next();
58             if (component.getClass().isAssignableFrom(clazz) && component != this) {
59                 return component;
60             }
61         }
62
63         return null;
64     }
65
66     public Component findLastAncestor(Class JavaDoc clazz) {
67         Stack componentStack = getComponentStack();
68         for (int i = componentStack.size() - 1; i >= 0; i--) {
69             Component component = (Component) componentStack.get(i);
70             if (component.getClass().isAssignableFrom(clazz) && component != this) {
71                 return component;
72             }
73         }
74
75         return null;
76     }
77
78     protected String JavaDoc findString(String JavaDoc expr) {
79         return (String JavaDoc) findValue(expr, String JavaDoc.class);
80     }
81
82     protected Object JavaDoc findValue(String JavaDoc expr) {
83         if (expr == null) {
84             return null;
85         }
86
87         if (ALT_SYNTAX) {
88             // does the expression start with %{ and end with }? if so, just cut it off!
89
if (expr.startsWith("%{") && expr.endsWith("}")) {
90                 expr = expr.substring(2, expr.length() - 1);
91             }
92         }
93
94         return getStack().findValue(expr);
95     }
96
97     protected Object JavaDoc findValue(String JavaDoc expr, Class JavaDoc toType) {
98         if (ALT_SYNTAX && toType == String JavaDoc.class) {
99             return translateVariables(expr, getStack());
100         } else {
101             if (ALT_SYNTAX) {
102                 // does the expression start with %{ and end with }? if so, just cut it off!
103
if (expr.startsWith("%{") && expr.endsWith("}")) {
104                     expr = expr.substring(2, expr.length() - 1);
105                 }
106             }
107
108             return getStack().findValue(expr, toType);
109         }
110     }
111
112     public static String JavaDoc translateVariables(String JavaDoc expression, OgnlValueStack stack) {
113         return TextParseUtil.translateVariables('%', expression, stack);
114     }
115
116     /**
117      * Pushes this component's parameter Map as well as the component itself on to the stack
118      * and then copies the supplied parameters over. Because the component's parameter Map is
119      * pushed before the component itself, any key-value pair that can't be assigned to componet
120      * will be set in the parameters Map.
121      *
122      * @param params
123      */

124     public void copyParams(Map JavaDoc params) {
125         stack.push(parameters);
126         stack.push(this);
127         try {
128             for (Iterator JavaDoc iterator = params.entrySet().iterator(); iterator.hasNext();) {
129                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
130                 String JavaDoc key = (String JavaDoc) entry.getKey();
131                 stack.setValue(key, entry.getValue());
132             }
133         } finally {
134             stack.pop();
135             stack.pop();
136         }
137     }
138
139     protected String JavaDoc toString(Throwable JavaDoc t) {
140         FastByteArrayOutputStream bout = new FastByteArrayOutputStream();
141         PrintWriter JavaDoc wrt = new PrintWriter JavaDoc(bout);
142         t.printStackTrace(wrt);
143         wrt.close();
144
145         return bout.toString();
146     }
147
148     public Map JavaDoc getParameters() {
149         return parameters;
150     }
151
152     public void addAllParameters(Map JavaDoc params) {
153         parameters.putAll(params);
154     }
155
156     public void addParameter(String JavaDoc key, Object JavaDoc value) {
157         if (key != null) {
158             Map JavaDoc params = getParameters();
159
160             if (value == null) {
161                 params.remove(key);
162             } else {
163                 params.put(key, value);
164             }
165         }
166     }
167
168     public String JavaDoc getId() {
169         return id;
170     }
171
172     public void setId(String JavaDoc id) {
173         this.id = id;
174     }
175 }
176
Popular Tags