KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > digester > CallParamRule


1 /* $Id: CallParamRule.java 467222 2006-10-24 03:17:11Z markt $
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19
20 package org.apache.tomcat.util.digester;
21
22
23 import org.xml.sax.Attributes JavaDoc;
24
25
26 /**
27  * <p>Rule implementation that saves a parameter for use by a surrounding
28  * <code>CallMethodRule<code>.</p>
29  *
30  * <p>This parameter may be:
31  * <ul>
32  * <li>from an attribute of the current element
33  * See {@link #CallParamRule(int paramIndex, String attributeName)}
34  * <li>from current the element body
35  * See {@link #CallParamRule(int paramIndex)}
36  * <li>from the top object on the stack.
37  * See {@link #CallParamRule(int paramIndex, boolean fromStack)}
38  * <li>the current path being processed (separate <code>Rule</code>).
39  * See {@link PathCallParamRule}
40  * </ul>
41  * </p>
42  */

43
44 public class CallParamRule extends Rule {
45
46     // ----------------------------------------------------------- Constructors
47

48
49     /**
50      * Construct a "call parameter" rule that will save the body text of this
51      * element as the parameter value.
52      *
53      * @param digester The associated Digester
54      * @param paramIndex The zero-relative parameter number
55      *
56      * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
57      * Use {@link #CallParamRule(int paramIndex)} instead.
58      */

59     public CallParamRule(Digester digester, int paramIndex) {
60
61         this(paramIndex);
62
63     }
64
65
66     /**
67      * Construct a "call parameter" rule that will save the value of the
68      * specified attribute as the parameter value.
69      *
70      * @param digester The associated Digester
71      * @param paramIndex The zero-relative parameter number
72      * @param attributeName The name of the attribute to save
73      *
74      * @deprecated The digester instance is now set in the {@link Digester#addRule} method.
75      * Use {@link #CallParamRule(int paramIndex, String attributeName)} instead.
76      */

77     public CallParamRule(Digester digester, int paramIndex,
78                          String JavaDoc attributeName) {
79
80         this(paramIndex, attributeName);
81
82     }
83
84     /**
85      * Construct a "call parameter" rule that will save the body text of this
86      * element as the parameter value.
87      *
88      * @param paramIndex The zero-relative parameter number
89      */

90     public CallParamRule(int paramIndex) {
91
92         this(paramIndex, null);
93
94     }
95
96
97     /**
98      * Construct a "call parameter" rule that will save the value of the
99      * specified attribute as the parameter value.
100      *
101      * @param paramIndex The zero-relative parameter number
102      * @param attributeName The name of the attribute to save
103      */

104     public CallParamRule(int paramIndex,
105                          String JavaDoc attributeName) {
106
107         this.paramIndex = paramIndex;
108         this.attributeName = attributeName;
109
110     }
111
112
113     /**
114      * Construct a "call parameter" rule.
115      *
116      * @param paramIndex The zero-relative parameter number
117      * @param fromStack should this parameter be taken from the top of the stack?
118      */

119     public CallParamRule(int paramIndex, boolean fromStack) {
120     
121         this.paramIndex = paramIndex;
122         this.fromStack = fromStack;
123
124     }
125     
126     /**
127      * Constructs a "call parameter" rule which sets a parameter from the stack.
128      * If the stack contains too few objects, then the parameter will be set to null.
129      *
130      * @param paramIndex The zero-relative parameter number
131      * @param stackIndex the index of the object which will be passed as a parameter.
132      * The zeroth object is the top of the stack, 1 is the next object down and so on.
133      */

134     public CallParamRule(int paramIndex, int stackIndex) {
135     
136         this.paramIndex = paramIndex;
137         this.fromStack = true;
138         this.stackIndex = stackIndex;
139     }
140  
141     // ----------------------------------------------------- Instance Variables
142

143
144     /**
145      * The attribute from which to save the parameter value
146      */

147     protected String JavaDoc attributeName = null;
148
149
150     /**
151      * The zero-relative index of the parameter we are saving.
152      */

153     protected int paramIndex = 0;
154
155
156     /**
157      * Is the parameter to be set from the stack?
158      */

159     protected boolean fromStack = false;
160     
161     /**
162      * The position of the object from the top of the stack
163      */

164     protected int stackIndex = 0;
165
166     /**
167      * Stack is used to allow nested body text to be processed.
168      * Lazy creation.
169      */

170     protected ArrayStack bodyTextStack;
171
172     // --------------------------------------------------------- Public Methods
173

174
175     /**
176      * Process the start of this element.
177      *
178      * @param attributes The attribute list for this element
179      */

180     public void begin(Attributes JavaDoc attributes) throws Exception JavaDoc {
181
182         Object JavaDoc param = null;
183         
184         if (attributeName != null) {
185         
186             param = attributes.getValue(attributeName);
187             
188         } else if(fromStack) {
189         
190             param = digester.peek(stackIndex);
191             
192             if (digester.log.isDebugEnabled()) {
193             
194                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc("[CallParamRule]{");
195                 sb.append(digester.match);
196                 sb.append("} Save from stack; from stack?").append(fromStack);
197                 sb.append("; object=").append(param);
198                 digester.log.debug(sb.toString());
199             }
200         }
201         
202         // Have to save the param object to the param stack frame here.
203
// Can't wait until end(). Otherwise, the object will be lost.
204
// We can't save the object as instance variables, as
205
// the instance variables will be overwritten
206
// if this CallParamRule is reused in subsequent nesting.
207

208         if(param != null) {
209             Object JavaDoc parameters[] = (Object JavaDoc[]) digester.peekParams();
210             parameters[paramIndex] = param;
211         }
212     }
213
214
215     /**
216      * Process the body text of this element.
217      *
218      * @param bodyText The body text of this element
219      */

220     public void body(String JavaDoc bodyText) throws Exception JavaDoc {
221
222         if (attributeName == null && !fromStack) {
223             // We must wait to set the parameter until end
224
// so that we can make sure that the right set of parameters
225
// is at the top of the stack
226
if (bodyTextStack == null) {
227                 bodyTextStack = new ArrayStack();
228             }
229             bodyTextStack.push(bodyText.trim());
230         }
231
232     }
233     
234     /**
235      * Process any body texts now.
236      */

237     public void end(String JavaDoc namespace, String JavaDoc name) {
238         if (bodyTextStack != null && !bodyTextStack.empty()) {
239             // what we do now is push one parameter onto the top set of parameters
240
Object JavaDoc parameters[] = (Object JavaDoc[]) digester.peekParams();
241             parameters[paramIndex] = bodyTextStack.pop();
242         }
243     }
244
245     /**
246      * Render a printable version of this Rule.
247      */

248     public String JavaDoc toString() {
249
250         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("CallParamRule[");
251         sb.append("paramIndex=");
252         sb.append(paramIndex);
253         sb.append(", attributeName=");
254         sb.append(attributeName);
255         sb.append(", from stack=");
256         sb.append(fromStack);
257         sb.append("]");
258         return (sb.toString());
259
260     }
261
262
263 }
264
Popular Tags