KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > ext > GlobalVariables


1 //
2
// Ejen (code generation system)
3
// Copyright (C) 2001, 2002 François Wolff (ejen@noos.fr).
4
//
5
// This file is part of Ejen.
6
//
7
// Ejen is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 2 of the License, or
10
// (at your option) any later version.
11
//
12
// Ejen is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License for more details.
16
//
17
// You should have received a copy of the GNU General Public License
18
// along with Ejen; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
//
21
package org.ejen.ext;
22
23 import org.ejen.util.XSLUtil;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import org.w3c.dom.Document JavaDoc;
27 import org.apache.xml.utils.WrappedRuntimeException;
28 import org.apache.xalan.extensions.XSLProcessorContext;
29 import org.apache.xalan.extensions.ExpressionContext;
30 import org.apache.xalan.templates.ElemExtensionCall;
31 import org.apache.xpath.objects.XObject;
32 import org.apache.xpath.objects.XString;
33 import org.apache.xpath.objects.XNodeSet;
34 import org.apache.xpath.objects.XRTreeFrag;
35 import org.apache.xpath.NodeSet;
36
37 /**
38  * Gobal variables utility (instanciable).
39  * <p>
40  * <table class="usage">
41  * <tr><th class="usage">Usage (XSL stylesheet)</th></tr>
42  * <tr><td class="usage"><pre>
43  *
44  * &lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
45  *
46  * &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
47  * ...
48  * <b>xmlns:gvs="org.ejen.ext.GlobalVariables"
49  * extension-element-prefixes="gvs ..."
50  * exclude-result-prefixes="gvs ..."</b>
51  * version="1.0"&gt;
52  *
53  * &lt;xsl:output method="xml" encoding="iso-8859-1"/&gt;
54  *
55  * &lt;xsl:template match="ejen"&gt;
56  *
57  * &lt;xls:variable name="vars1" select="gvs:{@link #GlobalVariables() new}()"/&gt;
58  * &lt;xls:variable name="vars2" select="gvs:{@link #GlobalVariables(GlobalVariables) new}($vars1)"/&gt;
59  * &lt;gvs:{@link #clear(XSLProcessorContext,ElemExtensionCall) clear}/&gt;
60  * &lt;vars1-count&gt;
61  * &lt;xsl:value-of select="gvs:{@link #size(ExpressionContext) size}([$vars1])"/&gt;
62  * &lt;/vars1-count&gt;
63  * &lt;gvs:{@link #remove(XSLProcessorContext,ElemExtensionCall) remove} name="myVar" [instance="$vars1"]/&gt;
64  * &lt;gvs:{@link #put(XSLProcessorContext,ElemExtensionCall) put} name="primary-keys"
65  * [select="column[@primary-key='true']"]
66  * [instance="$gvs2"]/&gt;
67  * <i>or</i>
68  * &lt;gvs:{@link #put(XSLProcessorContext,ElemExtensionCall) put} name="myVar"
69  * [attribute="Hello {people[$i]/@name} !"]
70  * [instance="$gvs2"]/&gt;
71  * &lt;xsl:value-of select="gvs:{@link #get(ExpressionContext,String) get}([$vars2,] 'myVar')"/&gt;
72  * &lt;xsl:for-each select="gvs:{@link #elements(ExpressionContext) elements}([$vars2])"&gt;
73  * ...
74  * &lt;/xsl:for-each&gt;
75  * &lt;xsl:for-each select="gvs:{@link #keys(ExpressionContext) keys}([$vars2])"&gt;
76  * ...
77  * &lt;/xsl:for-each&gt;
78  *
79  * &lt;/xsl:template&gt;
80  *
81  * &lt;/xsl:stylesheet&gt;
82  * </pre></td></tr></table>
83  * @author F. Wolff
84  * @version 1.0
85  */

86 public class GlobalVariables {
87
88     /** Table of variables */
89     private Hashtable JavaDoc _vars = null;
90
91     /**
92      * Constructs a new GlobalVariables object. If this constructor is
93      * not explicitly used, the Xalan extension mechanism constructs a
94      * default instance.
95      * <p>
96      * <table class="usage"><tr><td class="usage"><pre>
97      *
98      * &lt;xsl:variable name="gvs1" select="gvs:new()"/&gt;
99      * </pre></td></tr></table>
100      */

101     public GlobalVariables() {
102         _vars = new Hashtable JavaDoc();
103     }
104
105     /**
106      * Constructs a new GlobalVariables object with the same mappings as the
107      * given GlobalVariables.
108      * <p>
109      * <table class="usage"><tr><td class="usage"><pre>
110      *
111      * &lt;xsl:variable name="gvs2" select="gvs:new($gvs1)"/&gt;
112      * </pre></td></tr></table>
113      * <p>
114      * <dd><dl><dt><b>XSLT parameters:</b>
115      * <dd><b>[Mandatory]</b> GlobalVariables instance.
116      * </dl></dd>
117      * <p>
118      * @param gvs a GlobalVariables object to be copied in this GlobalVariables.
119      */

120     public GlobalVariables(GlobalVariables gvs) {
121         _vars = new Hashtable JavaDoc(gvs._vars);
122     }
123
124     /**
125      * Throws away all variables.
126      * <p>
127      * <table class="usage"><tr><td class="usage"><pre>
128      *
129      * &lt;gvs:clear [instance="$gvs2"]/&gt;
130      * </pre></td></tr></table>
131      * <p>
132      * <dd><dl><dt><b>XSLT Attributes:</b>
133      * <dd>instance <b>[Optional]</b> GlobalVariables instance (if not set,
134      * default instance is used).
135      * </dl></dd>
136      * <p>
137      * @param context automatically passed by the xalan extension mechanism.
138      * @param elem automatically passed by the xalan extension mechanism.
139      * @throws org.apache.xml.utils.WrappedRuntimeException if the instance attribute
140      * is not an instance of GlobalVariables.
141      */

142     public void clear(XSLProcessorContext context, ElemExtensionCall elem) {
143         Object JavaDoc o = XSLUtil.getOAttribute(context, elem, "instance",
144                 GlobalVariables.class, false, false);
145
146         if (o == null) {
147             _vars.clear();
148         } else {
149             ((GlobalVariables) o)._vars.clear();
150         }
151     }
152
153     /**
154      * Returns variables count.
155      * <p>
156      * <table class="usage"><tr><td class="usage"><pre>
157      *
158      * &lt;xsl:value-of select="gvs:size([$gvs2])"/&gt;
159      * </pre></td></tr></table>
160      * <p>
161      * <dd><dl><dt><b>XSLT parameters:</b>
162      * <dd><b>[Optional]</b> GlobalVariables instance (if not set, default instance
163      * is used).
164      * </dl></dd>
165      * <p>
166      * @param context automatically passed by the xalan extension mechanism.
167      * @return variables count in this <code>GlobalVariables</code>.
168      */

169     public int size(ExpressionContext context) {
170         return _vars.size();
171     }
172
173     /**
174      * Removes a variable.
175      * <p>
176      * <table class="usage"><tr><td class="usage"><pre>
177      *
178      * &lt;gvs:remove name="myVar"[ instance="$gvs2"]/&gt;
179      * </pre></td></tr></table>
180      * <p>
181      * <dd><dl><dt><b>XSLT Attributes:</b>
182      * <dd>name <b>[Mandatory/AVT]</b> - name of the variable to be removed.
183      * <dd>instance <b>[Optional]</b> - GlobalVariables instance.
184      * </dl></dd>
185      * <p>
186      * @param context automatically passed by the xalan extension mechanism.
187      * @param elem automatically passed by the xalan extension mechanism.
188      * @throws org.apache.xml.utils.WrappedRuntimeException
189      * if the 'name' attribute is missing or if the instance attribute
190      * is not an instance of GlobalVariables.
191      */

192     public void remove(XSLProcessorContext context, ElemExtensionCall elem) {
193         String JavaDoc name = XSLUtil.getAttribute(context, elem, "name", true);
194         Object JavaDoc o = XSLUtil.getOAttribute(context, elem, "instance",
195                 GlobalVariables.class, false, false);
196
197         if (o == null) {
198             _vars.remove(name);
199         } else {
200             ((GlobalVariables) o)._vars.remove(name);
201         }
202     }
203
204     /**
205      * Creates (or updates the value of) a variable.
206      * <p>
207      * <table class="usage"><tr><td class="usage"><pre>
208      *
209      * &lt;gvs:put name="myVar"
210      * [select="column[@primary-key='true']"]
211      * [instance="$gvs2"]/&gt;
212      * <i>or</i>
213      * &lt;gvs:put name="myVar"
214      * [attribute="Hello {people[$i]/@name} !"]
215      * [instance="$gvs2"]/&gt;
216      * </pre></td></tr></table>
217      * <p>
218      * <i>'select' and 'attribute' cannot be used at the same time. If none of those
219      * attributes is used, the default value "(no value)" is bound to the variable
220      * name.</i>
221      * <p>
222      * <dd><dl><dt><b>XSLT Attributes:</b>
223      * <dd>name <b>[Mandatory/AVT]</b> - name of the variable to be removed.
224      * <dd>select <b>[Optional/AVT]</b> - XPath expression (nodes set, XSL variable...).
225      * <dd>attribute <b>[Optional/AVT]</b> - a String.
226      * <dd>instance <b>[Optional]</b> - GlobalVariables instance (if not set, default instance
227      * is used).
228      * </dl></dd>
229      * <p>
230      * @param context automatically passed by the xalan extension mechanism.
231      * @param elem automatically passed by the xalan extension mechanism.
232      * @throws org.apache.xml.utils.WrappedRuntimeException with a XSL Exception
233      * if the 'name' attribute.
234      */

235     public void put(XSLProcessorContext context, ElemExtensionCall elem) {
236         XObject xo = null;
237         String JavaDoc name = XSLUtil.getAttribute(context, elem, "name", true);
238         String JavaDoc select = XSLUtil.getAttribute(context, elem, "select", false);
239         Object JavaDoc o = XSLUtil.getOAttribute(context, elem, "instance",
240                 GlobalVariables.class, false, false);
241
242         if (select != null) {
243             xo = XSLUtil.evaluate(context, elem, select);
244             if (xo == null) {
245                 throw new WrappedRuntimeException(new IllegalArgumentException JavaDoc("bad \"select\" attribute ?!"));
246             }
247         } else {
248             String JavaDoc s = XSLUtil.getAttribute(context, elem, "attribute", false);
249
250             if (s != null) {
251                 xo = new XString(s);
252             }
253         }
254         if (xo == null) {
255             xo = new XString("(no value)");
256         }
257         if (o == null) {
258             _vars.put(name, xo);
259         } else {
260             ((GlobalVariables) o)._vars.put(name, xo);
261         }
262     }
263
264     /**
265      * Gets the value of a variable.
266      * <p>
267      * <table class="usage"><tr><td class="usage"><pre>
268      *
269      * &lt;xsl:value-of select="gvs:get([$gvs2, ]'myVar')"/&gt;
270      * </pre></td></tr></table>
271      * <p>
272      * <dd><dl><dt><b>XSLT parameters:</b>
273      * <dd><b>[Optional]</b> GlobalVariables instance (if not set, default instance
274      * is used).
275      * <dd><b>[Mandatory/AVT]</b> name of the variable.
276      * </dl></dd>
277      * <p>
278      * @param context automatically passed by the xalan extension mechanism.
279      * @param name the name of the variable.
280      * @return the value of the variable or <code>null</code> if there was
281      * no such variable.
282      */

283     public XObject get(ExpressionContext context, String JavaDoc name) {
284         return (XObject) (_vars.get(XSLUtil.evaluate(context, name)));
285     }
286
287     /**
288      * Gets the value of a variable.
289      * <p>
290      * <table class="usage"><tr><td class="usage"><pre>
291      *
292      * &lt;gvs:get name="myVar"[ instance="$gvs2"]/&gt;
293      * </pre></td></tr></table>
294      * <p>
295      * <dd><dl><dt><b>XSLT Attributes:</b>
296      * <dd>name <b>[Mandatory/AVT]</b> - name of the variable to be removed.
297      * <dd>instance <b>[Optional]</b> - GlobalVariables instance (if not set, default instance
298      * is used).
299      * </dl></dd>
300      * <p>
301      * @param context automatically passed by the xalan extension mechanism.
302      * @param elem automatically passed by the xalan extension mechanism.
303      * @return the value of the variable or <code>null</code> if there was
304      * no such variable.
305      * @throws org.apache.xml.utils.WrappedRuntimeException with a XSL Exception
306      * if the 'name' attribute is missing.
307      */

308     public XObject get(XSLProcessorContext context, ElemExtensionCall elem) {
309         Object JavaDoc o = XSLUtil.getOAttribute(context, elem, "instance",
310                 GlobalVariables.class, false, false);
311         String JavaDoc name = XSLUtil.getAttribute(context, elem, "name", true);
312
313         if (o == null) {
314             return (XObject) (_vars.get(name));
315         } else {
316             return (XObject) (((GlobalVariables) o)._vars.get(name));
317         }
318     }
319
320     /**
321      * Returns a NodeSet that contains all variable values in this GlobalVariables.
322      * <p>
323      * <table class="usage"><tr><td class="usage"><pre>
324      *
325      * &lt;xsl:for-each select="gvs:elements([$gvs2])"&gt;
326      * ...
327      * &lt;/xsl:for-each&gt;
328      * </pre></td></tr></table>
329      * <p>
330      * <i>Returned NodeSet is rather an union of the values than a collection
331      * of the values: if the value of a variable is already a XNodeSet, returned
332      * NodeSet will contain all Nodes of this XNodeSet (and not a Node that
333      * contains the XNodeSet); same with XRTreeFrag values; if the
334      * value is a XString (or XBoolean, XNumber...), then returned NodeSet
335      * will contain a CDATA Node with a String representation of the value.</i>
336      * <p>
337      * <dd><dl><dt><b>XSLT parameters:</b>
338      * <dd><b>[Optional]</b> GlobalVariables instance (if not set, default instance
339      * is used).
340      * <dd><b>[Mandatory/AVT]</b> name of the variable.
341      * </dl></dd>
342      * <p>
343      * @param context automatically passed by the xalan extension mechanism.
344      * @return a NodeSet (may be empty) that contains all variable values in this
345      * GlobalVariables.
346      * @throws org.apache.xml.utils.WrappedRuntimeException DOM Exception...
347      */

348     public NodeSet elements(ExpressionContext context) {
349         Document JavaDoc doc = XSLUtil.getContextDocument(context);
350
351         try {
352             NodeSet ns = new NodeSet();
353
354             for (Enumeration JavaDoc e = _vars.elements(); e.hasMoreElements();) {
355                 XObject xo = (XObject) (e.nextElement());
356
357                 if (xo instanceof XNodeSet) {
358                     ns.addNodes(xo.nodeset());
359                 } else if (xo instanceof XRTreeFrag) {
360                     ns.addNodes(((XRTreeFrag) xo).convertToNodeset());
361                 } else {
362                     ns.addElement(doc.createCDATASection(xo.toString()));
363                 }
364             }
365             return ns;
366         } catch (Exception JavaDoc e) {
367             throw new WrappedRuntimeException(e);
368         }
369     }
370
371     /**
372      * Returns a NodeSet that contains all variable names in this GlobalVariables.
373      * <p>
374      * <table class="usage"><tr><td class="usage"><pre>
375      *
376      * &lt;xsl:for-each select="gvs:keys([$gvs2])"&gt;
377      * ...
378      * &lt;/xsl:for-each&gt;
379      * </pre></td></tr></table>
380      * <p>
381      * <i>Returned NodeSet is a collection of CDATA Nodes, each of them containing
382      * a variable name.</i>
383      * <p>
384      * <dd><dl><dt><b>XSLT parameters:</b>
385      * <dd><b>[Optional]</b> GlobalVariables instance (if not set, default instance
386      * is used).
387      * <dd><b>[Mandatory/AVT]</b> name of the variable.
388      * </dl></dd>
389      * <p>
390      * @param context automatically passed by the xalan extension mechanism.
391      * @return a NodeSet (may be empty) that contains all variable names in this
392      * GlobalVariables.
393      * @throws org.apache.xml.utils.WrappedRuntimeException DOM Exception...
394      */

395     public NodeSet keys(ExpressionContext context) {
396         Document JavaDoc doc = XSLUtil.getContextDocument(context);
397
398         try {
399             NodeSet ns = new NodeSet();
400
401             for (Enumeration JavaDoc e = _vars.keys(); e.hasMoreElements();) {
402                 ns.addElement(doc.createCDATASection((String JavaDoc) (e.nextElement())));
403             }
404             return ns;
405         } catch (Exception JavaDoc e) {
406             throw new WrappedRuntimeException(e);
407         }
408     }
409 }
410
Popular Tags