KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.xalan.extensions.XSLProcessorContext;
25 import org.apache.xalan.extensions.ExpressionContext;
26 import org.apache.xalan.templates.ElemExtensionCall;
27 import org.apache.xml.utils.WrappedRuntimeException;
28
29 /**
30  * Counter utility (instanciable).
31  * <p>
32  * <table class="usage">
33  * <tr><th class="usage">Usage (XSL stylesheet)</th></tr>
34  * <tr><td class="usage"><pre>
35  *
36  * &lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
37  *
38  * &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
39  * ...
40  * <b>xmlns:cnt="org.ejen.ext.Counter"
41  * extension-element-prefixes="cnt ..."
42  * exclude-result-prefixes="cnt ..."</b>
43  * version="1.0"&gt;
44  *
45  * &lt;xsl:output method="xml" encoding="iso-8859-1"/&gt;
46  *
47  * &lt;xsl:template match="ejen"&gt;
48  *
49  * &lt;xsl:variable name="cnt1" select="cnt:{@link #Counter() new}()"/&gt;
50  * &lt;xsl:variable name="cnt2" select="cnt:{@link #Counter(int) new}(-5)"/&gt;
51  * &lt;xsl:variable name="cnt3" select="cnt:{@link #Counter(Counter) new}([$cnt2])"/&gt;
52  * &lt;cnt:{@link #reset(XSLProcessorContext,ElemExtensionCall) reset} [value="0"] [instance="$cnt2"]/&gt;
53  * &lt;cnt:{@link #incr(XSLProcessorContext,ElemExtensionCall) incr} [step="-2"] [instance="$cnt2"]/&gt;
54  * &lt;cnt:{@link #read(XSLProcessorContext,ElemExtensionCall) read} [instance="$cnt2"]/&gt;
55  * &lt;xsl:param name="i" select="cnt:{@link #read(ExpressionContext) read}([$cnt2])"/&gt;
56  * &lt;cnt:{@link #incrAndRead(XSLProcessorContext,ElemExtensionCall) incrAndRead} [step="-2"] [instance="$cnt2"]/&gt;
57  * &lt;xsl:param name="i" select="cnt:{@link #incrAndRead(ExpressionContext) incrAndRead}([$cnt2])"/&gt;
58  * &lt;xsl:param name="i" select="cnt:{@link #incrAndRead(ExpressionContext,int) incrAndRead}([$cnt2,] -2)"/&gt;
59  * &lt;cnt:{@link #readAndIncr(XSLProcessorContext,ElemExtensionCall) readAndIncr} [step="-2"] [instance="$cnt2"]/&gt;
60  * &lt;xsl:param name="i" select="cnt:{@link #readAndIncr(ExpressionContext) readAndIncr}([$cnt2])"/&gt;
61  * &lt;xsl:param name="i" select="cnt:{@link #readAndIncr(ExpressionContext,int) readAndIncr}([$cnt2,] -2)"/&gt;
62  *
63  * &lt;/xsl:template&gt;
64  *
65  * &lt;/xsl:stylesheet&gt;
66  * </pre></td></tr></table>
67  * @author F. Wolff
68  * @version 1.0
69  */

70 public class Counter {
71
72     /** Counter default initial value: 1 */
73     private static final int DEFAULT_INITIAL_VALUE = 1;
74
75     /** Current value of this Counter */
76     private int _value;
77
78     /**
79      * Constructs a new Counter object with initial value set to 1. If this
80      * constructor is not explicitly used, the Xalan extension mechanism constructs
81      * a default instance.
82      * <p>
83      * <table class="usage"><tr><td class="usage"><pre>
84      *
85      * &lt;xsl:variable name="cnt1" select="cnt:new()"/&gt;
86      * </pre></td></tr></table>
87      */

88     public Counter() {
89         _value = DEFAULT_INITIAL_VALUE;
90     }
91
92     /**
93      * Constructs a new Counter object whose initial value is the value parameter.
94      * <p>
95      * <table class="usage"><tr><td class="usage"><pre>
96      *
97      * &lt;xsl:variable name="cnt2" select="cnt:new(0)"/&gt;
98      * </pre></td></tr></table>
99      * <p>
100      * <dd><dl><dt><b>XSLT parameters:</b>
101      * <dd><b>[Mandatory]</b> initial value of this Counter.
102      * </dl></dd>
103      * <p>
104      * @param value initial value of this Counter.
105      */

106     public Counter(int value) {
107         _value = value;
108     }
109
110     /**
111      * Constructs a new Counter object whose initial value is the current
112      * value of the cnt parameter.
113      * <p>
114      * <table class="usage"><tr><td class="usage"><pre>
115      *
116      * &lt;xsl:variable name="cnt3" select="cnt:new($cnt1)"/&gt;
117      * </pre></td></tr></table>
118      * <p>
119      * <dd><dl><dt><b>XSLT parameters:</b>
120      * <dd><b>[Mandatory]</b> Counter instance.
121      * </dl></dd>
122      * <p>
123      * @param cnt a Counter object.
124      */

125     public Counter(Counter cnt) {
126         _value = cnt._value;
127     }
128
129     /**
130      * Resets the Counter value.
131      * <p>
132      * <table class="usage"><tr><td class="usage"><pre>
133      *
134      * &lt;cnt:reset [value="0"] [instance="$cnt2"]/&gt;
135      * </pre></td></tr></table>
136      * <p>
137      * <dd><dl><dt><b>XSLT Attributes:</b>
138      * <dd>instance <b>[Optional]</b> Counter instance (if not set,
139      * default instance is used).
140      * <dd>value <b>[Optional/AVT]</b> new Counter value (if not set, the counter
141      * will be reset to <code>DEFAULT_INITIAL_VALUE</code>).
142      * </dl></dd>
143      * <p>
144      * @param context automatically passed by the xalan extension mechanism.
145      * @param elem automatically passed by the xalan extension mechanism.
146      * @throws org.apache.xml.utils.WrappedRuntimeException with a XSL Exception
147      * or a java.lang.NumberFormatException.
148      */

149     public void reset(XSLProcessorContext context, ElemExtensionCall elem) {
150         Object JavaDoc o = XSLUtil.getOAttribute(context, elem, "instance",
151                 Counter.class, false, false);
152         String JavaDoc value = null;
153
154         try {
155             value = elem.getAttribute("value", context.getContextNode(),
156                     context.getTransformer());
157             int iValue = (value != null)
158                     ? Integer.parseInt(value)
159                     : DEFAULT_INITIAL_VALUE;
160
161             if (o == null) {
162                 _value = iValue;
163             } else {
164                 ((Counter) o)._value = iValue;
165             }
166         } catch (Exception JavaDoc e) {
167             throw new WrappedRuntimeException(Counter.class.getName()
168                     + ".reset : exception while getting or parsing node attribute 'value' or 'instance'",
169                     e);
170         }
171     }
172
173     /**
174      * Increments the value of this Counter by 'step'.
175      * <p>
176      * <table class="usage"><tr><td class="usage"><pre>
177      *
178      * &lt;cnt:incr [step="-2"] [instance="$cnt2"]/&gt;
179      * </pre></td></tr></table>
180      * <p>
181      * <dd><dl><dt><b>XSLT Attributes:</b>
182      * <dd>instance <b>[Optional]</b> Counter instance (if not set,
183      * default instance is used).
184      * <dd>step <b>[Optional/AVT]</b> step used for incrementation (if not set,
185      * the counter will be incremented by 1).
186      * </dl></dd>
187      * <p>
188      * @param context automatically passed by the xalan extension mechanism.
189      * @param elem automatically passed by the xalan extension mechanism.
190      * @throws org.apache.xml.utils.WrappedRuntimeException with a XSL Exception
191      * or a java.lang.NumberFormatException.
192      */

193     public void incr(XSLProcessorContext context, ElemExtensionCall elem) {
194         Object JavaDoc o = XSLUtil.getOAttribute(context, elem, "instance",
195                 Counter.class, false, false);
196         String JavaDoc step = null;
197
198         try {
199             step = elem.getAttribute("step", context.getContextNode(),
200                     context.getTransformer());
201             int iStep = (step != null) ? Integer.parseInt(step) : 1;
202
203             if (o == null) {
204                 _value += iStep;
205             } else {
206                 ((Counter) o)._value += iStep;
207             }
208         } catch (Exception JavaDoc e) {
209             throw new WrappedRuntimeException(Counter.class.getName()
210                     + ".incr : exception while getting or parsing node attribute 'step'",
211                     e);
212         }
213     }
214
215     /**
216      * Returns the current value of this Counter.
217      * <p>
218      * <table class="usage"><tr><td class="usage"><pre>
219      *
220      * &lt;cnt:read [instance="$cnt2"]/&gt;
221      * </pre></td></tr></table>
222      * <p>
223      * <dd><dl><dt><b>XSLT Attributes:</b>
224      * <dd>instance <b>[Optional]</b> Counter instance (if not set,
225      * default instance is used).
226      * </dl></dd>
227      * <p>
228      * @param context automatically passed by the xalan extension mechanism.
229      * @param elem automatically passed by the xalan extension mechanism.
230      * @return the current value of this Counter.
231      */

232     public int read(XSLProcessorContext context, ElemExtensionCall elem) {
233         Object JavaDoc o = XSLUtil.getOAttribute(context, elem, "instance",
234                 Counter.class, false, false);
235
236         if (o == null) {
237             return _value;
238         } else {
239             return ((Counter) o)._value;
240         }
241     }
242
243     /**
244      * Returns the current value of this Counter.
245      * <p>
246      * <table class="usage"><tr><td class="usage"><pre>
247      *
248      * &lt;xsl:param name="i" select="cnt:read([$cnt2])"/&gt;
249      * </pre></td></tr></table>
250      * <p>
251      * <dd><dl><dt><b>XSLT parameters:</b>
252      * <dd><b>[Optional]</b> Counter instance (if not set, default instance
253      * is used).
254      * </dl></dd>
255      * <p>
256      * @param context automatically passed by the xalan extension mechanism.
257      * @return the current value of this Counter.
258      */

259     public int read(ExpressionContext context) {
260         return _value;
261     }
262
263     /**
264      * Increments the value of this Counter, then returns it.
265      * <p>
266      * <table class="usage"><tr><td class="usage"><pre>
267      *
268      * &lt;cnt:incrAndRead [step="-2"] [instance="$cnt2"]/&gt;
269      * </pre></td></tr></table>
270      * <p>
271      * <dd><dl><dt><b>XSLT Attributes:</b>
272      * <dd>instance <b>[Optional]</b> Counter instance (if not set,
273      * default instance is used).
274      * <dd>step <b>[Optional/AVT]</b> step used for incrementation (if not set,
275      * the counter will be incremented by 1).
276      * </dl></dd>
277      * <p>
278      * @param context automatically passed by the xalan extension mechanism.
279      * @param elem automatically passed by the xalan extension mechanism.
280      * @return the current value of this Counter (after incrementation).
281      * @throws org.apache.xml.utils.WrappedRuntimeException with a XSL Exception
282      * or a java.lang.NumberFormatException.
283      */

284     public int incrAndRead(XSLProcessorContext context, ElemExtensionCall elem) {
285         Object JavaDoc o = XSLUtil.getOAttribute(context, elem, "instance",
286                 Counter.class, false, false);
287         String JavaDoc step = null;
288
289         try {
290             step = elem.getAttribute("step", context.getContextNode(),
291                     context.getTransformer());
292             int iStep = (step != null) ? Integer.parseInt(step) : 1;
293
294             if (o == null) {
295                 _value += iStep;
296             } else {
297                 ((Counter) o)._value += iStep;
298             }
299         } catch (Exception JavaDoc e) {
300             throw new WrappedRuntimeException(Counter.class.getName()
301                     + ".incrAndRead : exception while getting or parsing node attribute 'step' or 'instance'",
302                     e);
303         }
304         return _value;
305     }
306
307     /**
308      * Increments the value of this Counter by 1, then returns it.
309      * <p>
310      * <table class="usage"><tr><td class="usage"><pre>
311      *
312      * &lt;xsl:param name="i" select="cnt:incrAndRead([$cnt2])"/&gt;
313      * </pre></td></tr></table>
314      * <p>
315      * <dd><dl><dt><b>XSLT parameters:</b>
316      * <dd><b>[Optional]</b> Counter instance (if not set, default instance
317      * is used).
318      * </dl></dd>
319      * <p>
320      * @param context automatically passed by the xalan extension mechanism.
321      * @return the current value of this Counter (after incrementation).
322      */

323     public int incrAndRead(ExpressionContext context) {
324         return ++_value;
325     }
326
327     /**
328      * Increments the value of this Counter by 'step', then returns it.
329      * <p>
330      * <table class="usage"><tr><td class="usage"><pre>
331      *
332      * &lt;xsl:param name="i" select="cnt:incrAndRead([$cnt2,] -2)"/&gt;
333      * </pre></td></tr></table>
334      * <p>
335      * <dd><dl><dt><b>XSLT parameters:</b>
336      * <dd><b>[Optional]</b> Counter instance (if not set, default instance
337      * is used).
338      * <dd><b>[Mandatory]</b> step used for incrementation (if not set,
339      * the counter will be incremented by 1).
340      * </dl></dd>
341      * <p>
342      * @param context automatically passed by the xalan extension mechanism.
343      * @param step incrementation step.
344      * @return the current value of this Counter (after incrementation).
345      */

346     public int incrAndRead(ExpressionContext context, int step) {
347         return (_value += step);
348     }
349
350     /**
351      * Returns the value of this Counter, then increments it.
352      * <p>
353      * <table class="usage"><tr><td class="usage"><pre>
354      *
355      * &lt;cnt:readAndIncr [step="-2"] [instance="$cnt2"]/&gt;
356      * </pre></td></tr></table>
357      * <p>
358      * <dd><dl><dt><b>XSLT Attributes:</b>
359      * <dd>instance <b>[Optional]</b> Counter instance (if not set,
360      * default instance is used).
361      * <dd>step <b>[Optional/AVT]</b> step used for incrementation (if not set,
362      * the counter will be incremented by 1).
363      * </dl></dd>
364      * <p>
365      * @param context automatically passed by the xalan extension mechanism.
366      * @param elem automatically passed by the xalan extension mechanism.
367      * @return the current value of this Counter (before incrementation).
368      * @throws org.apache.xml.utils.WrappedRuntimeException with a XSL Exception
369      * or a java.lang.NumberFormatException.
370      */

371     public int readAndIncr(XSLProcessorContext context, ElemExtensionCall elem) {
372         Object JavaDoc o = XSLUtil.getOAttribute(context, elem, "instance",
373                 Counter.class, false, false);
374         int oldValue = _value;
375         String JavaDoc step = null;
376
377         try {
378             step = elem.getAttribute("step", context.getContextNode(),
379                     context.getTransformer());
380             int iStep = (step != null) ? Integer.parseInt(step) : 1;
381
382             if (o == null) {
383                 _value += iStep;
384             } else {
385                 ((Counter) o)._value += iStep;
386             }
387         } catch (Exception JavaDoc e) {
388             throw new WrappedRuntimeException(Counter.class.getName()
389                     + ".readAndIncr : exception while getting or parsing node attribute 'step' or 'instance'",
390                     e);
391         }
392         return oldValue;
393     }
394
395     /**
396      * Returns the value of this Counter, then increments it by 1.
397      * <p>
398      * <table class="usage"><tr><td class="usage"><pre>
399      *
400      * &lt;xsl:param name="i" select="cnt:readAndIncr([$cnt2])"/&gt;
401      * </pre></td></tr></table>
402      * <p>
403      * <dd><dl><dt><b>XSLT parameters:</b>
404      * <dd><b>[Optional]</b> Counter instance (if not set, default instance
405      * is used).
406      * </dl></dd>
407      * <p>
408      * @param context automatically passed by the xalan extension mechanism.
409      * @return the current value of this Counter (before incrementation).
410      */

411     public int readAndIncr(ExpressionContext context) {
412         return _value++;
413     }
414
415     /**
416      * Returns the value of this Counter, then increments it by 'step'.
417      * <p>
418      * <table class="usage"><tr><td class="usage"><pre>
419      *
420      * &lt;xsl:param name="i" select="cnt:readAndIncr([$cnt2,] -2)"/&gt;
421      * </pre></td></tr></table>
422      * <p>
423      * <dd><dl><dt><b>XSLT parameters:</b>
424      * <dd><b>[Optional]</b> Counter instance (if not set, default instance
425      * is used).
426      * <dd><b>[Mandatory]</b> step used for incrementation (if not set,
427      * the counter will be incremented by 1).
428      * </dl></dd>
429      * <p>
430      * @param context automatically passed by the xalan extension mechanism.
431      * @param step incrementation step.
432      * @return the current value of this Counter (before incrementation).
433      */

434     public int readAndIncr(ExpressionContext context, int step) {
435         int oldValue = _value;
436
437         _value += step;
438         return oldValue;
439     }
440 }
441
Popular Tags