KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > velocity > ExpressoTool


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.ext.velocity;
66
67
68 import com.jcorporate.expresso.core.controller.Block;
69 import com.jcorporate.expresso.core.controller.ControllerElement;
70 import com.jcorporate.expresso.core.controller.ControllerException;
71 import com.jcorporate.expresso.core.controller.ControllerResponse;
72 import com.jcorporate.expresso.core.db.DBException;
73 import com.jcorporate.expresso.core.i18n.Messages;
74 import com.jcorporate.expresso.services.dbobj.SchemaList;
75
76 import java.util.List JavaDoc;
77 import java.util.StringTokenizer JavaDoc;
78
79
80 /**
81  * A generic object that allows Velocity pages to be written more user friendly.
82  *
83  * @author David Lloyd
84  * <p/>
85  * <p/>
86  * <p/>
87  * $Log: ExpressoTool.java,v $
88  * Revision 1.5 2004/11/18 02:03:29 lhamel
89  * cosmetic reformat
90  *
91  * Revision 1.4 2004/11/17 20:48:16 lhamel
92  * cosmetic reformat
93  * <p/>
94  * Revision 1.3 2004/09/29 07:05:01 mtraum
95  * added license to source files
96  * <p/>
97  * Revision 1.2 2004/09/22 05:11:55 mtraum
98  * organize imports
99  * <p/>
100  * Revision 1.1 2004/05/29 16:51:31 dlloyd
101  * Initial checkin of velocity support
102  */

103
104 public class ExpressoTool {
105
106
107     /**
108      * Return a string in the messages bundles. The current schema stack is seached for the
109      * <p/>
110      * correct translation. This is a wrapper for Messages.getString(stack,locale,code,args).
111      *
112      * @param response The controllerResponse request attribute that contains the schema stack.
113      * @param code The string code being searched for.
114      * @param args Any replacement arguments; can be null.
115      * @return The code translation if found, otherwise the code itself.
116      */

117
118     public String JavaDoc getString(ControllerResponse response, String JavaDoc code, List args) throws ControllerException {
119
120
121         Object JavaDoc[] argsArray;
122
123         if (args == null) {
124             argsArray = new Object JavaDoc[0];
125         } else {
126             argsArray = args.toArray();
127         }
128
129
130         return Messages.getString(response.getSchemaStack(),
131
132                 response.getLocale(),
133
134                 code,
135
136                 argsArray);
137
138     }
139
140
141     /**
142      * Find a <code>ControllerElement</code> in the response identified by a path.
143      * <p/>
144      * This path is somewhat similar to the paths used in the taglibs. The differences
145      * <p/>
146      * are that a leading '/' means start from the controller response and a '@' means
147      * <p/>
148      * to return the string which is the attribute by the name that follows. Also, if
149      * <p/>
150      * the path item name cannot be found and the name starts with one of 'BLOCK:',
151      * <p/>
152      * 'OUTPUT:', 'INPUT:', or 'TRANSITION:', then the prefix is removed and the search
153      * <p/>
154      * is made again limited to the type of object specified.
155      * <p/>
156      * <p/>
157      * <p/>
158      * <p><pre>
159      * <p/>
160      * An example use in Velocity:
161      * <p/>
162      * #exp_element( $myelement "lists/groups/tests" )
163      * <p/>
164      * returns the element named 'tests'
165      * <p/>
166      * <p/>
167      * <p/>
168      * #exp_element( $myelement "lists/BLOCK:groups/tests" )
169      * <p/>
170      * returns the same element named 'tests' as above but useful if
171      * <p/>
172      * an object named groups exists in transitions (for example).
173      * <p/>
174      * <p/>
175      * <p/>
176      * #exp_element( $myelement "lists/groups/@style" )
177      * <p/>
178      * returns the string for attribute 'style' in 'groups'
179      * <p/>
180      * </pre>
181      *
182      * @param response The controllerResponse request attribute.
183      * @param path The path to the element or attribute to be returned.
184      * @return The found object or null.
185      */

186
187     public Object JavaDoc getElement(ControllerResponse response, String JavaDoc path) throws ControllerException {
188
189         if (path == null || path.length() == 0) {
190             return null;
191         }
192
193
194         if (path.charAt(0) == '/') {
195             if (path.length() == 1) {
196                 return null;
197             } else {
198                 path = path.substring(1);
199             }
200         }
201
202
203         Object JavaDoc element = null;
204
205
206         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(path, "/");
207
208         if (tok.hasMoreTokens()) {
209
210             String JavaDoc token = tok.nextToken().trim();
211
212
213             boolean isAttribute = false;
214
215             if (token.length() > 1) {
216
217                 if (token.charAt(0) == '@') {
218
219                     isAttribute = true;
220
221                     token = token.substring(1);
222
223                 }
224
225             }
226
227
228             if (token.length() != 0) {
229
230                 if (isAttribute) {
231
232                     return response.getAttribute(token);
233
234                 } else {
235
236                     element = response.getNestedMap().get(token);
237
238
239                     if (element == null) {
240
241                         if (token.startsWith("BLOCK:")) {
242
243                             element = response.getBlock(token.substring(6));
244
245                         } else if (token.startsWith("OUTPUT:")) {
246
247                             element = response.getBlock(token.substring(7));
248
249                         } else if (token.startsWith("INPUT:")) {
250
251                             element = response.getBlock(token.substring(6));
252
253                         } else if (token.startsWith("TRANSITION:")) {
254
255                             element = response.getBlock(token.substring(11));
256
257                         }
258
259                     }
260
261                 }
262
263             }
264
265             if (tok.hasMoreTokens()) {
266
267                 element = getElementFrom(response, (ControllerElement) element, tok);
268
269             }
270
271         }
272
273
274         return element;
275
276     }
277
278
279     /**
280      * Find a <code>ControllerElement</code> in the response identified by a path.
281      * <p/>
282      * This path is somewhat similar to the paths used in the taglibs. The differences
283      * <p/>
284      * are that a leading '/' means start from the controller response and a '@' means
285      * <p/>
286      * to return the string which is the attribute by the name that follows. Also, if
287      * <p/>
288      * the path item name cannot be found and the name starts with one of 'BLOCK:',
289      * <p/>
290      * 'OUTPUT:', 'INPUT:', or 'TRANSITION:', then the prefix is removed and the search
291      * <p/>
292      * is made again limited to the type of object specified. If the from parameter is null
293      * <p/>
294      * or the path starts with a '/' then this function behaves the same as
295      * <p/>
296      * getElement(response, path).
297      * <p/>
298      * <p/>
299      * <p/>
300      * <p><pre>
301      * <p/>
302      * An example use in Velocity:
303      * <p/>
304      * #exp_elementFrom( $myelement $fromElement "lists/groups/tests" )
305      * <p/>
306      * returns the element named 'tests'
307      * <p/>
308      * <p/>
309      * <p/>
310      * #exp_element( $myelement $fromElement "lists/BLOCK:groups/tests" )
311      * <p/>
312      * returns the same element named 'tests' as above but useful if
313      * <p/>
314      * an object named groups exists in transitions (for example).
315      * <p/>
316      * <p/>
317      * <p/>
318      * #exp_element( $myelement $fromElement "lists/groups/@style" )
319      * <p/>
320      * returns the string for attribute 'style' in 'groups'
321      * <p/>
322      * </pre>
323      *
324      * @param response The controllerResponse request attribute.
325      * @param path The path to the element or attribute to be returned.
326      * @param from The ControllerElement to start the search from.
327      * @return The found object or null.
328      */

329
330     public Object JavaDoc getElementFrom(ControllerResponse response, ControllerElement from, String JavaDoc path) throws ControllerException {
331
332         if (from == null) {
333             return getElement(response, path);
334         }
335
336
337         if (path == null || path.length() == 0) {
338             return from;
339         }
340
341
342         if (path.charAt(0) == '/') {
343             return getElement(response, path);
344         }
345
346
347         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(path, "/");
348
349
350         Object JavaDoc element = getElementFrom(response, from, tok);
351
352
353         return element;
354
355     }
356
357
358     private Object JavaDoc getElementFrom(ControllerResponse response, ControllerElement from, StringTokenizer JavaDoc tok) throws ControllerException {
359
360
361         ControllerElement element = null;
362
363
364         while (tok.hasMoreTokens()) {
365
366             String JavaDoc token = tok.nextToken().trim();
367
368
369             boolean isAttribute = false;
370
371             if (token.length() > 1) {
372
373                 if (token.charAt(0) == '@') {
374
375                     isAttribute = true;
376
377                     token = token.substring(1);
378
379                 }
380
381             }
382
383
384             if (token.length() != 0) {
385
386                 if (isAttribute) {
387
388                     return from.getAttribute(token);
389
390                 } else {
391
392                     element = (ControllerElement) from.getNestedMap().get(token);
393
394
395                     if (element == null && (from instanceof Block)) {
396
397                         if (token.startsWith("BLOCK:")) {
398
399                             element = ((Block) from).getBlock(token.substring(6));
400
401                         } else if (token.startsWith("OUTPUT:")) {
402
403                             element = ((Block) from).getBlock(token.substring(7));
404
405                         } else if (token.startsWith("INPUT:")) {
406
407                             element = ((Block) from).getBlock(token.substring(6));
408
409                         } else if (token.startsWith("TRANSITION:")) {
410
411                             element = ((Block) from).getBlock(token.substring(11));
412
413                         }
414
415                     }
416
417                     if (element == null) {
418
419                         return null;
420
421                     } else {
422
423                         from = element;
424
425                     }
426
427                 }
428
429             }
430
431         }
432
433         return from;
434
435     }
436
437
438     /**
439      * Get a value from the setup table. A simple wrapper for Setup.getValue(dataContext, key).
440      *
441      * @param dataContext The data context for the setup table.
442      * @param key The key whose value is to be retrieved.
443      * @return The found value or null
444      * @see com.jcorporate.expresso.services.dbobj.Setup
445      */

446
447     public String JavaDoc getSetupValue(String JavaDoc dataContext, String JavaDoc key) throws DBException {
448
449         return com.jcorporate.expresso.services.dbobj.Setup.getValue(dataContext, key);
450
451     }
452
453
454     /**
455      * Get the ConfigManager singleton.
456      *
457      * @return The ConfigManager singleton.
458      */

459
460     public com.jcorporate.expresso.core.misc.ConfigManager getConfigManager() {
461
462         return com.jcorporate.expresso.core.misc.ConfigManager.getInstance();
463
464     }
465
466
467     /**
468      * Returns the list of installed schemas. This would commonly be used to support the
469      * <p/>
470      * current expresso tradition of listin the installed components in the left menu.
471      *
472      * @param dataContext The data context for SchemaList (typically 'default').
473      * @return The SchemaList.
474      */

475
476     public List getSchemaList(String JavaDoc dataContext) throws DBException {
477
478         SchemaList sl = new SchemaList(SchemaList.SYSTEM_ACCOUNT);
479
480         sl.setDBName(dataContext);
481
482
483         return sl.searchAndRetrieveList();
484
485     }
486
487 }
Popular Tags