root/trunk/src/xml/xsl/libcommon.xsl

Revision 1646, 95.5 kB (checked in by amandel, 13 months ago)

Generate a is<Foo> method for boolean type members of value Objects.

  • Property svn:mime-type set to text/xml
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1<?xml version="1.0" encoding="UTF-8"?>
2<!--
3   $Id$
4
5   Collects common XSL templates.
6
7   Author: Michael Griffel, Andreas Mandel
8  -->
9<xsl:stylesheet
10   version="1.0"
11   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
12   xmlns:xs="http://www.w3.org/2001/XMLSchema">
13
14<!-- ===============================================================
15     C O N S T A N T S
16     =============================================================== -->
17<xsl:variable name="lowercase-a_z" select="'abcdefghijklmnopqrstuvwxyz'"/>
18<xsl:variable name="uppercase-a_z" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
19<xsl:variable name="magic-hashes"  select="'##########################'"/>
20<xsl:variable name="java-letter-or-digit"
21  select="'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$01234567890'"/>
22
23<!-- ===============================================================
24          _        _                             _   _
25      ___| |_ _ __(_)_ __   __ _   ___  ___  ___| |_(_) ___  _ __
26     / __| __| '__| | '_ \ / _` | / __|/ _ \/ __| __| |/ _ \| '_ \
27     \__ \ |_| |  | | | | | (_| | \__ \  __/ (__| |_| | (_) | | | |
28     |___/\__|_|  |_|_| |_|\__, | |___/\___|\___|\__|_|\___/|_| |_|
29                           |___/
30     =============================================================== -->
31<!-- converts $s to upper case characters -->
32<xsl:template name="toUpperCase">
33   <xsl:param name="s"/>
34   <xsl:value-of select="translate($s,
35      $lowercase-a_z,
36      $uppercase-a_z)"/>
37</xsl:template>
38
39<!-- converts $s to lower case characters -->
40<xsl:template name="toLowerCase">
41   <xsl:param name="s"/>
42   <xsl:value-of select="translate($s,
43      $uppercase-a_z,
44      $lowercase-a_z)"/>
45</xsl:template>
46
47<!--
48   Replaces the character 'char' w/ the string 'new'
49  -->
50<xsl:template name="replace-char">
51   <xsl:param name="s" select="''"/>
52   <xsl:param name="char" select="''"/>
53   <xsl:param name="new" select="''"/>
54   <xsl:param name="pos" select="1"/>
55   <xsl:if test="$pos &lt;= string-length($s)">
56         <!-- Contains upper case character at position $pos? -->
57         <xsl:choose>
58            <xsl:when test="substring($s, $pos, 1) = $char">
59               <xsl:value-of select="$new"/>
60            </xsl:when>
61            <xsl:otherwise>
62               <xsl:value-of select="substring($s, $pos, 1)"/>
63            </xsl:otherwise>
64         </xsl:choose>
65         <xsl:call-template name="replace-char">
66            <xsl:with-param name="s" select="$s"/>
67            <xsl:with-param name="char" select="$char"/>
68            <xsl:with-param name="new" select="$new"/>
69            <xsl:with-param name="pos" select="$pos + 1"/>
70         </xsl:call-template>
71   </xsl:if>
72</xsl:template>
73
74<!--
75   Replaces the non java symbol characters
76  -->
77<xsl:template name="replace-not-java-letters-or-digit">
78   <xsl:param name="s" select="''"/>
79   <xsl:param name="new" select="'_'"/>
80   <xsl:param name="pos" select="1"/>
81   <xsl:if test="$pos &lt;= string-length($s)">
82     <xsl:variable name="current-char" select="substring($s, $pos, 1)"/>
83         <!-- Contains upper case character at position $pos? -->
84         <xsl:choose>
85            <xsl:when test="not(contains($java-letter-or-digit, $current-char))">
86               <xsl:value-of select="$new"/>
87            </xsl:when>
88            <xsl:otherwise>
89               <xsl:value-of select="$current-char"/>
90            </xsl:otherwise>
91         </xsl:choose>
92         <xsl:call-template name="replace-not-java-letters-or-digit">
93            <xsl:with-param name="s" select="$s"/>
94            <xsl:with-param name="new" select="$new"/>
95            <xsl:with-param name="pos" select="$pos + 1"/>
96         </xsl:call-template>
97   </xsl:if>
98</xsl:template>
99
100<!--
101   Replaces the string 'old' w/ the string 'new'
102   Note: 'old' must not be a substring of 'new'!
103  -->
104<xsl:template name="replace-string">
105   <xsl:param name="s" select="''"/>
106   <xsl:param name="old" select="''"/>
107   <xsl:param name="new" select="''"/>
108   <xsl:choose>
109      <xsl:when test="contains($s, $old)">
110         <xsl:variable name="next"><xsl:value-of select="substring-before($s, $old)"/>
111         <xsl:value-of select="$new"/>
112         <xsl:value-of select="substring-after($s, $old)"/>
113         </xsl:variable>
114         <xsl:call-template name="replace-string">
115            <xsl:with-param name="s" select="$next"/>
116            <xsl:with-param name="old" select="$old"/>
117            <xsl:with-param name="new" select="$new"/>
118         </xsl:call-template>
119      </xsl:when>
120      <xsl:otherwise>
121         <xsl:value-of select="$s"/>
122      </xsl:otherwise>
123   </xsl:choose>
124</xsl:template>
125
126
127<!-- ===============================================================
128        _                                  _   _
129       (_) __ ___   ____ _   ___  ___  ___| |_(_) ___  _ __
130       | |/ _` \ \ / / _` | / __|/ _ \/ __| __| |/ _ \| '_ \
131       | | (_| |\ V / (_| | \__ \  __/ (__| |_| | (_) | | | |
132      _/ |\__,_| \_/ \__,_| |___/\___|\___|\__|_|\___/|_| |_|
133     |__/
134     =============================================================== -->
135
136<!-- ===============================================================
137     Apply jCoderZ classname rule, e.g.e FooID -> FooId
138     =============================================================== -->
139<xsl:template name="asCamelCase">
140   <xsl:param name="s"/>
141   <xsl:param name="pos" select="'1'"/>
142   <xsl:param name="c_last" select="'a'"/>
143   <xsl:variable name="c" select="substring($s, $pos, 1)"/>
144   <xsl:variable name="c_next" select="substring(concat($s, 'A'), $pos + 1, 1)"/>
145      <xsl:if test="$c">
146         <xsl:variable name="c_new">
147            <xsl:choose>
148               <xsl:when test="translate($c_last, $lowercase-a_z, $magic-hashes) = '#'">
149                  <xsl:value-of select="$c"/>
150               </xsl:when>
151               <xsl:when test="translate($c_next, $lowercase-a_z, $magic-hashes) = '#'">
152                  <xsl:value-of select="$c"/>
153               </xsl:when>
154               <xsl:otherwise>
155                  <xsl:value-of select="translate($c, $uppercase-a_z, $lowercase-a_z)"/>
156               </xsl:otherwise>
157            </xsl:choose>
158         </xsl:variable>
159         <xsl:value-of select="$c_new"/>
160         <xsl:call-template name="asCamelCase">
161            <xsl:with-param name="s" select="$s"/>
162            <xsl:with-param name="pos" select="$pos + 1"/>
163            <xsl:with-param name="c_last" select="$c"/>
164         </xsl:call-template>
165   </xsl:if>
166</xsl:template>
167
168<!-- ===============================================================
169     Converts a abbr. to a Java string, e.g. 'RTE' -> 'Rte'
170     =============================================================== -->
171<xsl:template name="shortnameToJava">
172   <xsl:param name="s"/>
173   <xsl:value-of select="substring($s, 1, 1)"/><xsl:call-template
174      name="toLowerCase"><xsl:with-param name="s"><xsl:value-of
175         select="substring($s, 2)"/></xsl:with-param></xsl:call-template>
176</xsl:template>
177
178<!--
179   Converts a string to a Java constant name
180   examples:
181      red to RED
182      BingoBongoFooBar to BINGO_BONGO_FOO_BAR
183      E-LBX-EXPIRED to E_LBX_EXPIRED
184      EL CORTE INGLES to EL_CORTE_INGLES
185      Visa Electron to VISA_ELECTRON
186-->
187<xsl:template name="asJavaConstantName">
188   <xsl:param name="value"/>
189   <xsl:variable name="the-clean-value">
190     <xsl:call-template name="replace-not-java-letters-or-digit">
191       <xsl:with-param name="s" select="$value"/>
192     </xsl:call-template>
193   </xsl:variable>
194   <xsl:call-template name="asJavaConstantName-clean">
195     <xsl:with-param name="value" select="$the-clean-value"/>
196   </xsl:call-template>
197</xsl:template>
198
199<xsl:template name="asJavaConstantName-clean">
200   <xsl:param name="value"/>
201
202   <xsl:if test="contains('0123456789', substring($value, 1, 1))">
203      <xsl:text>V_</xsl:text>
204   </xsl:if>
205   <xsl:choose>
206      <!-- special for ABXtoXYZBingoRequest -->
207      <xsl:when test="substring($value, 4, 2) = 'to'
208         and not(contains($value, 'ProtocolEngine'))
209         and not(contains($value, 'Customer'))">
210         <xsl:variable name="mangled-value"><xsl:value-of
211            select="substring($value,1,1)"/><xsl:call-template name="toLowerCase">
212               <xsl:with-param name="s"><xsl:value-of
213                  select="substring($value,2,2)"/></xsl:with-param>
214            </xsl:call-template><xsl:text>To</xsl:text><xsl:value-of
215               select="substring($value,6,1)"/><xsl:call-template
216                  name="toLowerCase">
217               <xsl:with-param name="s"><xsl:value-of
218                  select="substring($value,7,2)"/></xsl:with-param>
219            </xsl:call-template><xsl:value-of select="substring($value, 9)"/>
220         </xsl:variable>
221         <xsl:call-template name="asJavaConstantName-clean">
222            <xsl:with-param name="value"><xsl:value-of
223               select="$mangled-value"></xsl:value-of></xsl:with-param>
224         </xsl:call-template>
225      </xsl:when>
226      <!-- no lowercase characters? -->
227      <xsl:when test="not(contains(translate($value,
228                           $lowercase-a_z,
229                           $magic-hashes), '#'))">
230         <!-- replace '-', '.' or ' ' with '_' -->
231         <xsl:value-of select="translate($value, '- .', '___')"/>
232      </xsl:when>
233      <!-- whitespaces, '.' or '-' ? -->
234      <xsl:when test="contains(translate($value, ' .-', '###'), '#')">
235         <!-- replace special chars with '_' and insert Underscore + toUpperCase -->
236         <xsl:variable name="foo"><xsl:call-template name="insertUnderscoreBeforeUpperCaseCharacter">
237            <xsl:with-param name="s" select="$value"/>
238         </xsl:call-template></xsl:variable>
239         <xsl:call-template name="toUpperCase">
240            <xsl:with-param name="s"><xsl:value-of
241               select="translate($foo, ' -.', '___')"/></xsl:with-param>
242         </xsl:call-template>
243      </xsl:when>
244      <xsl:otherwise>
245         <xsl:call-template name="insertUnderscoreBeforeUpperCaseCharacter">
246            <xsl:with-param name="s" select="$value"/>
247         </xsl:call-template>
248      </xsl:otherwise>
249   </xsl:choose>
250</xsl:template>
251
252<!--
253   Converts a CamelCase string to a human readable name.
254   examples:
255      red to red
256      BingoBongoFooBar to bingo bongo foo bar
257      E-LBX-EXPIRED to e lbx expired
258      EL CORTE INGLES to el corte ingles
259      Visa Electron to visa electron
260      Visa_Electron to visa electron
261  -->
262<xsl:template name="asDisplayName">
263   <xsl:param name="name"/>
264   <xsl:param name="pos">1</xsl:param>
265   <xsl:variable name="s" select="translate($name, '_- ', '   ')"/>
266   <xsl:if test="$pos &lt;= string-length($s)">
267         <!-- Contains upper case character at position $pos? -->
268         <xsl:if test="contains(translate(substring($s, $pos, 1),
269                              $uppercase-a_z,
270                              $magic-hashes),
271                                 '#') and $pos != 1">
272            <!-- ... and previous character is not uppercase -->
273            <xsl:if test="not(contains(translate(substring($s, $pos - 1, 1),
274                              $uppercase-a_z,
275                              $magic-hashes),
276                                 '#')) and not(contains(translate(substring($s, $pos - 1, 1),
277                                             ' ', '#'), '#'))">
278               <xsl:text> </xsl:text>
279            </xsl:if>
280         </xsl:if>
281         <xsl:call-template name="toLowerCase">
282            <xsl:with-param name="s" select="substring($s, $pos, 1)"/>
283         </xsl:call-template>
284      <xsl:call-template name="asDisplayName">
285         <xsl:with-param name="name" select="$name"/>
286         <xsl:with-param name="pos" select="$pos + 1"/>
287      </xsl:call-template>
288   </xsl:if>
289</xsl:template>
290
291<!--
292   Converts a string to a Java identifier.
293   examples:
294      FOO_BAR to FooBar
295  -->
296<xsl:template name="asJavaIdentifier">
297   <xsl:param name="name"/>
298   <xsl:param name="pos">1</xsl:param>
299   <!-- normalize -->
300   <xsl:variable name="s" select="translate($name, '-_ ', '   ')"/>
301   <xsl:if test="$pos &lt;= string-length($s)">
302      <xsl:choose>
303         <xsl:when test="$pos = 1"> <!-- First char: force to upper case -->
304            <xsl:value-of select="translate(substring($s, 1, 1),
305                  $lowercase-a_z, $uppercase-a_z)"/>
306         </xsl:when>
307         <xsl:when test="substring($s, $pos, 1) = ' '"> <!-- whitespace? -->
308         </xsl:when>
309         <xsl:when test="substring($s, $pos - 1, 1) = ' '"> <!-- previous whitespace? -->
310            <xsl:value-of select="translate(substring($s, $pos, 1),
311                  $lowercase-a_z, $uppercase-a_z)"/>
312         </xsl:when>
313         <xsl:when test="contains($lowercase-a_z, substring($s, $pos - 1, 1))"> <!-- previous lowercase? -->
314            <xsl:value-of select="substring($s, $pos, 1)"/>
315         </xsl:when>
316         <xsl:otherwise>
317            <xsl:value-of select="translate(substring($s, $pos, 1),
318                  $uppercase-a_z, $lowercase-a_z)"/>
319         </xsl:otherwise>
320      </xsl:choose>
321      <xsl:call-template name="asJavaIdentifier">
322         <xsl:with-param name="name" select="$name"/>
323         <xsl:with-param name="pos" select="$pos + 1"/>
324      </xsl:call-template>
325   </xsl:if>
326</xsl:template>
327
328
329<!--
330   Converts a string to a Java parameter.
331   examples:
332      FOO_BAR to fooBar
333  -->
334<xsl:template name="asJavaParameter">
335   <xsl:param name="name"/>
336   <xsl:param name="pos">1</xsl:param>
337   <!-- normalize -->
338   <xsl:variable name="s" select="translate($name, '-_ ', '   ')"/>
339   <xsl:if test="$pos &lt;= string-length($s)">
340      <xsl:if test="$pos = 1"> <!-- First char: force to lower case -->
341         <xsl:value-of select="translate(substring($s, 1, 1),
342               $uppercase-a_z, $lowercase-a_z)"/>
343      </xsl:if>
344      <xsl:call-template name="asJavaIdentifier">
345         <xsl:with-param name="name" select="$name"/>
346         <xsl:with-param name="pos" select="$pos + 1"/>
347      </xsl:call-template>
348   </xsl:if>
349</xsl:template>
350
351
352<xsl:template name="insertUnderscoreBeforeUpperCaseCharacter">
353   <xsl:param name="s"/>
354   <xsl:param name="pos">1</xsl:param>
355   <xsl:if test="$pos &lt;= string-length($s)">
356      <xsl:choose>
357         <!-- Contains upper case character at position $pos? -->
358         <xsl:when test="contains(translate(substring($s, $pos, 1),
359                              $uppercase-a_z,
360                              $magic-hashes),
361                                 '#') and $pos != 1">
362            <!-- ... and previous character is not uppercase nor ' ', '-', '.' or '_' -->
363            <xsl:if test="not(contains(translate(substring($s, $pos - 1, 1),
364                              $uppercase-a_z,
365                              $magic-hashes),
366                                 '#'))
367                          and not(contains(translate(substring($s, $pos - 1, 1),
368                              ' -._', '####'), '#'))">
369               <xsl:text>_</xsl:text>
370            </xsl:if>
371            <xsl:value-of select="substring($s, $pos, 1)"/>
372         </xsl:when>
373         <xsl:otherwise>
374            <xsl:call-template name="toUpperCase">
375               <xsl:with-param name="s" select="substring($s, $pos, 1)"/>
376            </xsl:call-template>
377         </xsl:otherwise>
378      </xsl:choose>
379      <xsl:call-template name="insertUnderscoreBeforeUpperCaseCharacter">
380         <xsl:with-param name="s" select="$s"/>
381         <xsl:with-param name="pos" select="$pos + 1"/>
382      </xsl:call-template>
383   </xsl:if>
384</xsl:template>
385
386<!-- ===============================================================
387     Generates a list of 'string constants' for each node in $items
388     =============================================================== -->
389<xsl:template name="java-string-constants">
390   <xsl:param name="items"/>
391   <xsl:param name="javadoc-prefix"/>
392
393   <xsl:for-each select="$items">
394   <xsl:sort select="."/>
395   /**
396    * <xsl:value-of select="$javadoc-prefix"/><xsl:text> &lt;code&gt;</xsl:text><xsl:value-of select="."/><xsl:text>&lt;/code&gt;.</xsl:text>
397    */
398   public static final String <xsl:call-template name="asJavaConstantName">
399      <xsl:with-param name="value" select="."/>
400   </xsl:call-template>
401         = "<xsl:value-of select="."/>";
402  </xsl:for-each>
403</xsl:template>
404
405
406<!-- ===============================================================
407     escapes string to a java string.
408     =============================================================== -->
409<xsl:template name="java-string-escape">
410   <xsl:param name="s" select="''"/>
411   <xsl:variable name="stepOne">
412   <xsl:call-template name="replace-char">
413      <xsl:with-param name="s" select="$s"/>
414      <xsl:with-param name="char" select="'\'"/>
415      <xsl:with-param name="new" select="'\\'"/>
416   </xsl:call-template>
417   </xsl:variable>
418   <xsl:call-template name="replace-char">
419      <xsl:with-param name="s" select="$stepOne"/>
420      <xsl:with-param name="char" select="'&quot;'"/>
421      <xsl:with-param name="new" select="'\&quot;'"/>
422   </xsl:call-template>
423</xsl:template>
424
425
426<!-- ===============================================================
427     generates a single constant with string constructor
428     =============================================================== -->
429<xsl:template name="java-constant">
430   <xsl:param name="type"/>
431   <xsl:param name="name"/>
432   <xsl:param name="value"/>
433   <xsl:param name="comment"/>
434   <xsl:param name="quote-char" select="'&quot;'"/>
435   /**<xsl:text> </xsl:text><xsl:value-of select="normalize-space($comment)"/><xsl:text> </xsl:text>*/
436   public static final <xsl:value-of select="$type"/><xsl:text> </xsl:text><xsl:value-of select="$name"/>
437         = new <xsl:value-of select="$type"/>(<xsl:value-of select="$quote-char"/><xsl:value-of select="$value"/><xsl:value-of select="$quote-char"/>);
438</xsl:template>
439
440<!-- ===============================================================
441     generates a single constant with fromString factory
442     =============================================================== -->
443<xsl:template name="java-constant-from-string">
444   <xsl:param name="type"/>
445   <xsl:param name="name"/>
446   <xsl:param name="value"/>
447   <xsl:param name="comment"/>
448   <xsl:param name="quote-char" select="'&quot;'"/>
449   /**<xsl:text> </xsl:text><xsl:value-of select="normalize-space($comment)"/><xsl:text> </xsl:text>*/
450   public static final <xsl:value-of select="$type"/><xsl:text> </xsl:text><xsl:value-of select="$name"/>
451         = <xsl:value-of select="$type"/>.fromString(<xsl:value-of select="$quote-char"/><xsl:value-of select="$value"/><xsl:value-of select="$quote-char"/>);
452</xsl:template>
453
454<!-- ===============================================================
455     Value Object Generator
456     =============================================================== -->
457<xsl:template name="value-object-generator">
458   <xsl:param name="classname"/>
459   <xsl:param name="package"/>
460   <xsl:param name="object"/>
461
462   <xsl:variable name="name"><xsl:call-template
463      name="asDisplayName"><xsl:with-param
464         name="name" select="$classname"/></xsl:call-template></xsl:variable>
465<xsl:call-template name="java-copyright-header"/>
466package <xsl:value-of select="$package"/>;
467
468import org.jcoderz.commons.util.HashCodeUtil;
469import org.jcoderz.commons.util.ObjectUtil;
470
471/**
472 * <xsl:if test="not($object/description)"><xsl:value-of select="$name"/> value object.</xsl:if>
473 * <xsl:value-of select="$object/description"/><xsl:call-template name="generate-xdoclet">
474   <xsl:with-param name="doc-text" select="$object/xdoclet" />
475   <xsl:with-param name="indent"><xsl:text> </xsl:text></xsl:with-param>
476</xsl:call-template>
477 * @author generated
478 */
479public <xsl:if test="$object/@final = 'true'">final </xsl:if>class <xsl:value-of select="$classname"/><xsl:if test="$object/@baseclass">
480  extends <xsl:value-of select="$object/@baseclass"/></xsl:if><xsl:if test="$object/@serializable or $object/@implements">
481      implements <xsl:if test="$object/@serializable">java.io.Serializable<xsl:if test="$object/@implements">, </xsl:if></xsl:if><xsl:value-of select="$object/@implements"/></xsl:if>
482{
483<xsl:if test="$object/@serializable">
484   private static final long serialVersionUID = 1L;</xsl:if>
485   <xsl:for-each select="$object/member">
486   private <xsl:if test="@final = 'true' or ../@final = 'true'">final </xsl:if>
487      <xsl:value-of select="@type"/> m<xsl:call-template
488      name="asJavaIdentifier">
489         <xsl:with-param name="name" select="@name"/></xsl:call-template>;</xsl:for-each>
490
491   <xsl:variable name="minimum-argument-count"><xsl:copy-of
492      select="count($object/member[not(@initial-value)][@final = 'true' or ../@final = 'true'])"/>
493   </xsl:variable>
494   <xsl:variable name="maximum-argument-count"><xsl:copy-of
495      select="count($object/member[not(@initial-value)])"/>
496   </xsl:variable>
497   <xsl:if test="$minimum-argument-count != $maximum-argument-count">
498
499
500   /**
501    * Constructs a <xsl:value-of
502      select="$classname"/> with the minimum arguments.<xsl:for-each
503      select="$object/member[not(@initial-value)][@final = 'true' or ../@final = 'true']">
504   <xsl:variable name="identifier"><xsl:call-template
505      name="asJavaIdentifier">
506         <xsl:with-param name="name" select="@name"/></xsl:call-template>
507   </xsl:variable>
508    * @param a<xsl:value-of select="$identifier"/> The <xsl:value-of
509    select="normalize-space(.)"/>.<xsl:if test="@copyValue = 'clone'">
510    *   The value is cloned before being stored.</xsl:if><xsl:if test="@copyValue = 'constructor'">
511    *   The value is copied using the copy constructor before being stored.</xsl:if></xsl:for-each>
512    */
513    public <xsl:value-of select="$classname"/> (<xsl:for-each
514      select="$object/member[not(@initial-value)][@final = 'true' or ../@final = 'true']">
515       <xsl:variable name="identifier"><xsl:call-template
516          name="asJavaIdentifier">
517             <xsl:with-param name="name" select="./@name"/></xsl:call-template>
518       </xsl:variable>
519       <xsl:value-of select="@type"/> a<xsl:value-of select="$identifier"/>
520       <xsl:if test="position() != last()">,
521       </xsl:if>
522       </xsl:for-each>)
523    {  <xsl:for-each select="$object/member[not(@initial-value)][@final = 'true' or ../@final = 'true']">
524       <xsl:variable name="identifier"><xsl:call-template
525          name="asJavaIdentifier">
526             <xsl:with-param name="name" select="@name"/></xsl:call-template>
527       </xsl:variable>
528       m<xsl:value-of select="$identifier"/> = <xsl:choose>
529       <xsl:when test="@copyValue = 'clone'">a<xsl:value-of select="$identifier"/> == null
530         ? null : (<xsl:value-of select="@type"/>) a<xsl:value-of select="$identifier"/>.clone();</xsl:when>
531       <xsl:when test="@copyValue = 'constructor'">a<xsl:value-of select="$identifier"/> == null
532         ? null : new <xsl:value-of select="@type"/>(a<xsl:value-of select="$identifier"/>);</xsl:when>
533     <xsl:otherwise>a<xsl:value-of select="$identifier"/>;</xsl:otherwise></xsl:choose></xsl:for-each>
534     <xsl:for-each select="$object/member[@initial-value]">
535       m<xsl:call-template name="asJavaIdentifier">
536         <xsl:with-param name="name" select="@name"/>
537       </xsl:call-template> = <xsl:value-of select="@initial-value"/>;</xsl:for-each>
538    }</xsl:if>
539
540   /**
541    * Constructs a <xsl:value-of
542      select="$classname"/> with all arguments.<xsl:for-each
543      select="$object/member[not(@initial-value)]">
544   <xsl:variable name="identifier"><xsl:call-template
545      name="asJavaIdentifier">
546         <xsl:with-param name="name" select="@name"/></xsl:call-template>
547   </xsl:variable>
548    * @param a<xsl:value-of select="$identifier"/> The <xsl:value-of
549    select="normalize-space(.)"/>.<xsl:if test="@copyValue = 'clone'">
550    *   The value is cloned before being stored.</xsl:if><xsl:if test="@copyValue = 'constructor'">
551    *   The value is copied using the copy constructor before being stored.</xsl:if></xsl:for-each>
552    */
553    public <xsl:value-of select="$classname"/> (<xsl:for-each
554      select="$object/member[not(@initial-value)]">
555       <xsl:variable name="identifier"><xsl:call-template
556          name="asJavaIdentifier">
557             <xsl:with-param name="name" select="@name"/></xsl:call-template>
558       </xsl:variable>
559       <xsl:value-of select="@type"/> a<xsl:value-of select="$identifier"/>
560       <xsl:if test="position() != last()">,
561       </xsl:if>
562       </xsl:for-each>)
563    {  <xsl:for-each select="$object/member[not(@initial-value)]">
564       <xsl:variable name="identifier"><xsl:call-template
565          name="asJavaIdentifier">
566             <xsl:with-param name="name" select="@name"/></xsl:call-template>
567       </xsl:variable>
568       m<xsl:value-of select="$identifier"/> = <xsl:choose>
569         <xsl:when test="@copyValue = 'clone'">a<xsl:value-of select="$identifier"/> == null
570         ? null : (<xsl:value-of select="@type"/>) a<xsl:value-of select="$identifier"/>.clone();</xsl:when>
571         <xsl:when test="@copyValue = 'constructor'">a<xsl:value-of select="$identifier"/> == null
572         ? null : new <xsl:value-of select="@type"/>(a<xsl:value-of select="$identifier"/>);</xsl:when>
573         <xsl:otherwise>a<xsl:value-of select="$identifier"/>;</xsl:otherwise>
574     </xsl:choose></xsl:for-each>
575     <xsl:for-each select="$object/member[@initial-value]">
576       m<xsl:call-template name="asJavaIdentifier">
577         <xsl:with-param name="name" select="@name"/>
578        </xsl:call-template> = <xsl:value-of select="@initial-value"/>;</xsl:for-each>
579    }
580
581  <!-- Do not provide the copy constructor if we if we overide a baseclass.  -->
582  <xsl:if test="not($object/@baseclass)">
583   /**
584    * Copy constructor for <xsl:value-of
585      select="$classname"/>. No deep copy is performed.
586    * @param a<xsl:value-of select="$classname"/> The <xsl:value-of select="$classname"/>
587    *   to be copied.
588    */
589    public <xsl:value-of select="$classname"/> (<xsl:value-of
590    select="$classname"/> a<xsl:value-of select="$classname"/>)
591    {  <xsl:for-each select="$object/member">
592       <xsl:variable name="identifier"><xsl:call-template
593          name="asJavaIdentifier">
594             <xsl:with-param name="name" select="@name"/></xsl:call-template>
595       </xsl:variable>
596       m<xsl:value-of select="$identifier"/>
597         = a<xsl:value-of select="$classname"/>.get<xsl:value-of
598            select="$identifier"/>();</xsl:for-each>
599    }
600  </xsl:if>
601
602   <xsl:for-each select="$object/member">
603   <xsl:variable name="display-name"><xsl:call-template name="asDisplayName"><xsl:with-param
604         name="name" select="@name"/></xsl:call-template>
605   </xsl:variable>
606   <xsl:variable name="identifier"><xsl:call-template
607      name="asJavaIdentifier">
608         <xsl:with-param name="name" select="@name"/></xsl:call-template>
609   </xsl:variable>
610   <xsl:variable name="doc-plain">
611     <xsl:value-of select="normalize-space(current())"/>
612   </xsl:variable>
613   <xsl:variable name="doc">
614     <xsl:choose>
615       <xsl:when test="normalize-space(current())"><xsl:value-of select="$doc-plain"/></xsl:when>
616       <xsl:otherwise><xsl:value-of select="$display-name"/></xsl:otherwise>
617     </xsl:choose>
618   </xsl:variable>
619   /**
620    * Returns the <xsl:value-of select="$doc"/>. <xsl:call-template name="generate-xdoclet">
621   <xsl:with-param name="doc-text" select="current()/xdoclet" />
622   <xsl:with-param name="indent"><xsl:text>    </xsl:text></xsl:with-param>
623</xsl:call-template><xsl:if test="@copyValue = 'clone'">
624    * The value is cloned before being returned.</xsl:if><xsl:if test="@copyValue = 'constructor'">
625    * The value is copied using the copy constructor before being returned.</xsl:if>
626    * @return the <xsl:value-of select="$doc"/>.
627    */
628   public <xsl:value-of select="@type"/> get<xsl:value-of select="$identifier"/> ()
629   {
630     return <xsl:choose>
631       <xsl:when test="@copyValue = 'clone'">m<xsl:value-of select="$identifier"/> == null
632         ? null : (<xsl:value-of select="@type"/>) m<xsl:value-of select="$identifier"/>.clone();
633       </xsl:when>
634       <xsl:when test="@copyValue = 'constructor'">m<xsl:value-of select="$identifier"/> == null
635         ? null : new <xsl:value-of select="@type"/>(m<xsl:value-of select="$identifier"/>);
636       </xsl:when>
637     <xsl:otherwise>m<xsl:value-of select="$identifier"/>;
638     </xsl:otherwise>
639   </xsl:choose>
640   }
641   <xsl:if test="@type = 'boolean' or @type = 'Boolean' or @type = 'java.lang.Boolean'">
642   /**
643    * Returns the <xsl:value-of select="$doc"/>. <xsl:call-template name="generate-xdoclet">
644   <xsl:with-param name="doc-text" select="current()/xdoclet" />
645   <xsl:with-param name="indent"><xsl:text>    </xsl:text></xsl:with-param>
646</xsl:call-template><xsl:if test="@copyValue = 'clone'">
647    * The value is cloned before being returned.</xsl:if><xsl:if test="@copyValue = 'constructor'">
648    * The value is copied using the copy constructor before being returned.</xsl:if>
649    * @return the <xsl:value-of select="$doc"/>.
650    */
651   public <xsl:value-of select="@type"/> is<xsl:value-of select="$identifier"/> ()
652   {
653     return get<xsl:value-of select="$identifier"/>();
654   }
655   </xsl:if>
656   <xsl:if test="not(@final = 'true' or ../@final = 'true')">
657
658   <xsl:variable name="setter-visibility"><xsl:choose><xsl:when
659    test="@setter-visibility"><xsl:value-of select="@setter-visibility"/></xsl:when>
660    <xsl:otherwise>public</xsl:otherwise></xsl:choose></xsl:variable>
661
662   /**
663    * Sets the <xsl:value-of select="$doc"/>.<xsl:if test="@copyValue = 'clone'">
664    * The value is cloned before being stored.</xsl:if><xsl:if test="@copyValue = 'constructor'">
665    * The value is copied using the copy constructor before being stored.</xsl:if>
666    * @param a<xsl:value-of select="$identifier"/> the <xsl:value-of select="$doc"/> to be set.
667    */
668   <xsl:value-of select="$setter-visibility"/> void set<xsl:value-of select="$identifier"/> (<xsl:value-of select="@type"/> a<xsl:value-of select="$identifier"/>)
669   {
670      m<xsl:value-of select="$identifier"/> = <xsl:choose>
671       <xsl:when test="@copyValue = 'clone'">a<xsl:value-of select="$identifier"/> == null
672         ? null : (<xsl:value-of select="./@type"/>) a<xsl:value-of select="$identifier"/>.clone();
673       </xsl:when>
674       <xsl:when test="@copyValue = 'constructor'">a<xsl:value-of select="$identifier"/> == null
675         ? null : new <xsl:value-of select="@type"/>(a<xsl:value-of select="$identifier"/>);
676       </xsl:when>
677     <xsl:otherwise>a<xsl:value-of select="$identifier"/>;
678     </xsl:otherwise>
679   </xsl:choose>
680   }</xsl:if>
681   </xsl:for-each>
682
683   /**
684    * Creates a String representation of this object holding the
685    * state of all members.
686    * @return a String representation of this object.
687    */
688   public String toString()
689   {
690      final StringBuffer buffer = new StringBuffer();
691      buffer.append("[<xsl:value-of select="$classname"/>:");<xsl:if
692       test="$object/@baseclass">
693      buffer.append(" super: ");
694      buffer.append(super.toString());</xsl:if><xsl:for-each
695         select="$object/member"><xsl:variable
696      name="identifier"><xsl:call-template
697         name="asJavaIdentifier">
698            <xsl:with-param name="name" select="@name"/></xsl:call-template>
699      </xsl:variable>
700      buffer.append(" m<xsl:value-of select="$identifier"/>: ");
701      buffer.append(m<xsl:value-of select="$identifier"/>);</xsl:for-each>
702      buffer.append("]");
703      return buffer.toString();
704   }
705
706   /**
707    * Override hashCode.
708    *
709    * @return the Objects hashcode.
710    */
711   public int hashCode()
712   {
713      int hashCode = <xsl:choose>
714        <xsl:when test="$object/@baseclass">super.hashCode()</xsl:when>
715        <xsl:otherwise>HashCodeUtil.SEED</xsl:otherwise>
716      </xsl:choose>;
717      <xsl:for-each
718      select="$object/member[not(@identity-independent)]">
719      <xsl:variable name="identifier"><xsl:call-template
720         name="asJavaIdentifier">
721            <xsl:with-param name="name" select="@name"/></xsl:call-template>
722      </xsl:variable>
723      hashCode = HashCodeUtil.hash(hashCode, get<xsl:value-of
724            select="$identifier"/>());</xsl:for-each>
725      return hashCode;
726   }
727
728   /**
729    * Returns &lt;code>true&lt;/code> if this &lt;code><xsl:value-of select="$classname"/>&lt;/code>
730    * is equal to &lt;tt>object&lt;/tt>.
731    * @param object the object to compare to.
732    * @return &lt;code>true&lt;/code> if this &lt;code><xsl:value-of select="$classname"/>&lt;/code>
733    *       is equal to &lt;tt>object&lt;/tt>.
734    */
735   public boolean equals (Object object)
736   {
737      final boolean result;
738      if (this == object)
739      {
740         result = true;
741      }
742      else if (object instanceof <xsl:value-of select="$classname"/>)
743      {
744         final <xsl:value-of select="$classname"/> o = (<xsl:value-of select="$classname"/>) object;
745         result = <xsl:choose>
746        <xsl:when test="$object/@baseclass">super.equals(object)</xsl:when>
747        <xsl:otherwise>true</xsl:otherwise>
748      </xsl:choose><xsl:for-each select="$object/member[not(@identity-independent)]">
749            <xsl:variable name="identifier"><xsl:call-template
750                  name="asJavaIdentifier"><xsl:with-param
751                  name="name" select="@name"/></xsl:call-template>
752            </xsl:variable>
753               &amp;&amp; ObjectUtil.equals(get<xsl:value-of
754                  select="$identifier"/>(), o.get<xsl:value-of
755                  select="$identifier"/>())</xsl:for-each>;
756      }
757      else
758      {
759         result = false;
760      }
761      return result;
762   }
763}
764
765</xsl:template>
766
767<!-- ===============================================================
768     Type-safe Enumeration Generator
769     =============================================================== -->
770<xsl:template name="simple-enum-generator">
771   <xsl:param name="classname"/>
772   <xsl:param name="package"/>
773   <xsl:param name="values"/>
774   <xsl:param name="javadoc"/>
775   <xsl:param name="implements" select="''"/>
776   <xsl:variable name="name"><xsl:call-template
777      name="asDisplayName"><xsl:with-param
778         name="name" select="$classname"/></xsl:call-template></xsl:variable>
779   <xsl:variable name="class-javadoc"><xsl:choose>
780      <xsl:when test="$javadoc"><xsl:value-of select="$javadoc"/></xsl:when>
781      <xsl:otherwise>Enumerated type of a <xsl:value-of select="$name"/>.</xsl:otherwise>
782      </xsl:choose></xsl:variable>
783   <xsl:variable name="numeric" select="boolean($values/@numeric)"/>
784<xsl:call-template name="java-copyright-header"/>
785package <xsl:value-of select="$package"/>;
786
787
788import java.io.Serializable;
789import java.util.Arrays;
790import java.util.Collections;
791import java.util.Map;
792import java.util.HashMap;
793import java.util.List;
794
795<xsl:call-template name="simple-enum-generator-import-hook"/>
796
797/**
798 * <xsl:value-of select="$class-javadoc"/>
799 *
800 * Instances of this class are immutable.
801 *
802 * The following <xsl:value-of select="$name"/>s are defined:
803 * &lt;ul&gt;<xsl:for-each select="$values">
804 *    &lt;li&gt;<xsl:value-of select="$classname"/>.<xsl:call-template name="pic-symbolic-name"><xsl:with-param name="value" select="."/></xsl:call-template><xsl:if test="@numeric"> = <xsl:value-of select="@numeric"/></xsl:if> = '<xsl:value-of select="."/>'&lt;/li&gt;</xsl:for-each>
805 * &lt;/ul&gt;
806 *
807 * <xsl:if test="$numeric">The values of this enum have beside the internal
808 * sequential integer representation that is used for serialization
809 * dedicated assigned numeric values that are used in the
810 * &lt;code>toInt()&lt;/code> and &lt;code>fromInt()&lt;/code> methods.</xsl:if>
811 * <xsl:if test="not($numeric)">The values of this enum have a internal
812 * sequential integer representation starting with '0'.</xsl:if>
813 *
814 * @author generated
815 */
816public final class <xsl:value-of select="$classname"/>
817        implements Serializable, org.jcoderz.commons.EnumType<xsl:if test="$implements">,
818            <xsl:value-of select="$implements"/></xsl:if>
819{
820   /**
821    * The name of this type.
822    */
823   public static final String TYPE_NAME = "<xsl:value-of select="$classname"/>";
824
825   /** Ordinal of next <xsl:value-of select="$name"/> to be created. */
826   private static int sNextOrdinal = 0;
827
828   /** Maps a string representation to an enumerated value. */
829   private static final Map
830      FROM_STRING = new HashMap();
831<xsl:for-each select="$values"><xsl:variable name="constant-name"><xsl:call-template name="pic-symbolic-name"><xsl:with-param
832      name="value" select="."/></xsl:call-template></xsl:variable><xsl:if test="$numeric">
833   /** Numeric representation for <xsl:value-of select="$classname"/><xsl:text> </xsl:text><xsl:value-of select="."/>. */
834   public static final int <xsl:value-of select="$constant-name"/>_NUMERIC = <xsl:value-of select="@numeric"/>;
835<xsl:if test="not(@numeric)">// FIXME: No Numeric defined in input file for this value.<xsl:message
836          >No numeric representation defined for <xsl:value-of select="."/> in enumeration type <xsl:value-of select="$classname"/>.</xsl:message></xsl:if>
837</xsl:if><xsl:choose><xsl:when test="not(@description)">
838   /** The <xsl:value-of select="$classname"/><xsl:text> </xsl:text><xsl:value-of select="."/>. */</xsl:when><xsl:otherwise>
839   /** <xsl:value-of select="./@description"/> (value: <xsl:value-of select="."/>). */</xsl:otherwise></xsl:choose>
840   public static final <xsl:value-of select="$classname"/><xsl:text> </xsl:text><xsl:call-template name="pic-symbolic-name"><xsl:with-param name="value" select="."/></xsl:call-template>
841      = new <xsl:value-of select="$classname"/>("<xsl:value-of select="."/>"<xsl:if
842         test="$numeric">, <xsl:value-of select="$constant-name"/>_NUMERIC</xsl:if>);
843</xsl:for-each>
844
845   /** The serialVersionUID used for serialization. */
846   static final long serialVersionUID = 1;
847
848   /** Internal list of all available <xsl:value-of select="$classname"/>s */
849   private static final <xsl:value-of select="$classname"/>[] PRIVATE_VALUES
850         =
851            {
852               <xsl:for-each select="$values">
853                  <xsl:value-of select="$classname"/>.<xsl:call-template name="pic-symbolic-name"><xsl:with-param name="value" select="."/></xsl:call-template>
854               <xsl:if test="position() != last()">
855               <xsl:text>,
856               </xsl:text>
857               </xsl:if></xsl:for-each>
858            };
859
860   /** Immutable list of the <xsl:value-of select="$classname"/>s. */
861   public static final List VALUES
862         = Collections.unmodifiableList(Arrays.asList(PRIVATE_VALUES));
863
864   /**
865    * Immutable map using the name string as key holding the
866    * <xsl:value-of select="$classname"/>s as values.
867    */
868   public static final Map VALUE_MAP
869         = Collections.unmodifiableMap(FROM_STRING);
870
871   /** Assign a ordinal to this <xsl:value-of select="$name"/> */
872   private final int mOrdinal = sNextOrdinal++;
873
874   /** The name of the <xsl:value-of select="$name"/> */
875   private final transient String mName;
876<xsl:if test="$numeric">
877
878   /** The numeric representation of the <xsl:value-of select="$name"/> */
879   private final transient int mNumeric;
880</xsl:if>
881   /** Private Constructor */
882   private <xsl:value-of select="$classname"/> (String name<xsl:if
883    test="$numeric">, int numeric</xsl:if>)
884   {
885      mName = name;<xsl:if test="$numeric">
886      mNumeric = numeric;</xsl:if>
887      FROM_STRING.put(mName, this);
888   }
889
890<xsl:if test="not($numeric)">
891   /**
892    * Creates a <xsl:value-of select="$classname"/> object from its int representation.
893    *
894    * @param i the integer representation of the <xsl:value-of select="$name"/>.
895    * @return the <xsl:value-of select="$classname"/> object represented by this int.
896    * @throws ArgumentMalformedException If the assigned int value isn't
897    *       listed in the internal <xsl:value-of select="$name"/> table.
898    */
899   public static <xsl:value-of select="$classname"/> fromInt (int i)
900         throws ArgumentMalformedException
901   {
902      try
903      {
904         return PRIVATE_VALUES[i];
905      }
906      catch (ArrayIndexOutOfBoundsException e)
907      {
908         throw new ArgumentMalformedException(
909               "<xsl:value-of select="$classname"/>",
910               new Integer(i),
911               "Illegal int representation of <xsl:value-of select="$classname"/>.");
912      }
913   }
914</xsl:if>
915
916<xsl:if test="$numeric">
917
918   /**
919    * Creates a <xsl:value-of select="$classname"/> object from its numeric
920    * representation.
921    *
922    * @param i the integer representation of the <xsl:value-of select="$name"/>.
923    * @return the <xsl:value-of select="$classname"/> object represented by this int.
924    * @throws ArgumentMalformedException If the assigned int value isn't
925    *       listed in the internal <xsl:value-of select="$name"/> table.
926    */
927   public static <xsl:value-of select="$classname"/> fromInt (int i)
928         throws ArgumentMalformedException
929   {
930       final <xsl:value-of select="$classname"/> result;
931       switch (i)
932       {<xsl:for-each select="$values"><xsl:variable name="constant-name"><xsl:call-template name="pic-symbolic-name"><xsl:with-param
933          name="value" select="."/></xsl:call-template></xsl:variable>
934           case <xsl:value-of select="$constant-name"/>_NUMERIC:
935               result = <xsl:value-of select="$constant-name"/>;
936               break;</xsl:for-each>
937           default:
938               throw new ArgumentMalformedException(
939                     "<xsl:value-of select="$classname"/>",
940                     new Integer(i),
941                     "Illegal int representation of <xsl:value-of select="$classname"/>.");
942      }
943      return result;
944   }
945</xsl:if>
946
947   /**
948    * Creates a <xsl:value-of select="$classname"/> object from its String representation.
949    *
950    * @param str the string representation of the
951    *       <xsl:value-of select="$name"/>.
952    * @return the <xsl:value-of select="$classname"/> object represented by this str.
953    * @throws ArgumentMalformedException If the given str value isn't
954    *       listed in the internal <xsl:value-of select="$name"/> table.
955    */
956   public static <xsl:value-of select="$classname"/> fromString (String str)
957         throws ArgumentMalformedException
958   {
959      final <xsl:value-of select="$classname"/> result
960            = (<xsl:value-of select="$classname"/>) FROM_STRING.get(str);
961      if (result == null)
962      {
963         throw new ArgumentMalformedException(
964               "<xsl:value-of select="$classname"/>",
965               str,
966               "Illegal string representation of <xsl:value-of select="$classname"/>, only "
967                  + VALUES + " are allowed.");
968      }
969      return result;
970   }
971
972   /**
973    * Returns the int representation of this <xsl:value-of select="$name"/>.
974    *
975    * @return the int representation of this <xsl:value-of select="$name"/>.
976    */
977   public int toInt ()
978   {<xsl:if test="$numeric">
979        return mNumeric;</xsl:if><xsl:if test="not($numeric)">
980        return mOrdinal;</xsl:if>
981   }
982
983   /**
984    * Returns the String representation of this <xsl:value-of select="$name"/>.
985    *
986    * @return the String representation of this <xsl:value-of select="$name"/>.
987    */
988   public String toString ()
989   {
990      return mName;
991   }
992
993   /**
994    * Resolves instances being deserialized to a single instance
995    * per <xsl:value-of select="$name"/>.
996    */
997   private Object readResolve ()
998   {
999      return PRIVATE_VALUES[mOrdinal];
1000   }
1001}
1002</xsl:template>
1003
1004<xsl:template name="simple-enum-generator-import-hook" priority="-1">
1005import org.jcoderz.commons.ArgumentMalformedException;
1006</xsl:template>
1007
1008<!-- find the best name for the symbol -->
1009<xsl:template name="pic-symbolic-name">
1010  <xsl:param name="value"/>
1011  <xsl:choose>
1012    <xsl:when test="$value/@symbol">
1013      <xsl:call-template name="asJavaConstantName"><xsl:with-param
1014      name="value" select="$value/@symbol"/></xsl:call-template>
1015    </xsl:when>
1016    <xsl:otherwise>
1017      <xsl:call-template name="asJavaConstantName"><xsl:with-param
1018      name="value" select="."/></xsl:call-template>
1019    </xsl:otherwise>
1020  </xsl:choose>
1021</xsl:template>
1022
1023<!-- ===============================================================
1024     Restricted string generator
1025     =============================================================== -->
1026<xsl:template name="restricted-string">
1027   <xsl:param name="classname"/>
1028   <xsl:param name="package"/>
1029   <xsl:param name="min-length"/>
1030   <xsl:param name="max-length"/>
1031   <xsl:param name="constants"/>
1032   <xsl:param name="token-type" select="''"/>
1033   <xsl:param name="regex" select="''"/>
1034   <xsl:param name="implements" select="''"/>
1035   <xsl:variable name="classname-constant">TYPE_NAME</xsl:variable>
1036   <xsl:call-template name="java-copyright-header"/>
1037package <xsl:value-of select="$package"/>;
1038
1039
1040import java.io.Serializable;
1041<xsl:call-template name="restricted-string-import-hook">
1042   <xsl:with-param name="token-type" select="$token-type"/>
1043   <xsl:with-param name="regex" select="$regex"/>
1044</xsl:call-template>
1045
1046/**
1047 * Holds the <xsl:value-of select="$classname"/>.
1048 * &lt;pre&gt;
1049 * String.type[<xsl:value-of select="$min-length"/>..<xsl:value-of select="$max-length"/>].
1050<xsl:if test="$regex"> * regular expression: <xsl:value-of select="$regex"/>
1051</xsl:if> * &lt;/pre&gt;
1052 * Instances of this class are immutable.
1053 *
1054 * &lt;p>This class implements the Comparable interface based on the natural
1055 * order of the String representation of its instances.&lt;/p>
1056 *
1057 * @author generated via stylesheet
1058 */
1059public final class <xsl:value-of select="$classname"/>
1060      implements Serializable, org.jcoderz.commons.RestrictedString, Comparable<xsl:if test="$implements">,
1061            <xsl:value-of select="$implements"/></xsl:if>
1062{
1063   /**
1064    * <xsl:value-of select="$classname"/> - the name of this type as string constant.
1065    */
1066   public static final String TYPE_NAME = "<xsl:value-of select="$classname"/>";
1067
1068   /** The minimal length of <xsl:value-of select="$classname"/> (<xsl:value-of select="$min-length"/>). */
1069   public static final int MIN_LENGTH = <xsl:value-of select="$min-length"/>;
1070
1071   /** The maximal length of <xsl:value-of select="$classname"/> (<xsl:value-of select="$max-length"/>). */
1072   public static final int MAX_LENGTH = <xsl:value-of select="$max-length"/>;
1073<xsl:if test="$regex">
1074
1075   /** The regular expression matching <xsl:value-of select="$classname"/>. */
1076   public static final String REGULAR_EXPRESSION
1077         = "<xsl:call-template name="java-string-escape"><xsl:with-param name="s" select="$regex"/></xsl:call-template>";
1078
1079   /** The compiled pattern for the regular expression. */
1080   public static final Pattern REGULAR_EXPRESSION_PATTERN
1081         = Pattern.compile(REGULAR_EXPRESSION);
1082</xsl:if>
1083
1084<xsl:for-each select="$constants">
1085   <xsl:call-template name="java-constant">
1086      <xsl:with-param name="type" select="$classname"/>
1087      <xsl:with-param name="name" select="./@name"/>
1088      <xsl:with-param name="value" select="./@value"/>
1089      <xsl:with-param name="comment" select="./@comment"/>
1090   </xsl:call-template>
1091</xsl:for-each>
1092   /** The serialVersionUID used for serialization. */
1093   static final long serialVersionUID = 1;
1094
1095   /** Holds the <xsl:value-of select="$classname"/>. */
1096   private final String m<xsl:value-of select="$classname"/>;
1097
1098   /**
1099    * Creates a new instance of a <xsl:value-of select="$classname"/>.
1100    *
1101    * @param str the <xsl:value-of select="$classname"/> as string representation.
1102    * @throws ArgumentMalformedException If the given string &lt;code>str&lt;/code>
1103    *         violates the restriction of this type.
1104    */
1105   private <xsl:value-of select="$classname"/> (final String str)
1106         throws ArgumentMalformedException
1107   {
1108      Assert.notNull(str, TYPE_NAME);<xsl:if test="$min-length != 0">
1109      if (str.length() &lt; MIN_LENGTH)
1110      {
1111         throw new ArgumentMinLengthViolationException(
1112            <xsl:value-of select="$classname-constant"/>,
1113            str, new Integer(str.length()), new Integer(MIN_LENGTH),
1114            <xsl:value-of select="$classname"/>.class);
1115      }</xsl:if>
1116      if (str.length() &gt; MAX_LENGTH)
1117      {
1118         throw new ArgumentMaxLengthViolationException(
1119            <xsl:value-of select="$classname-constant"/>,
1120            str, new Integer(str.length()), new Integer(MAX_LENGTH),
1121            <xsl:value-of select="$classname"/>.class);
1122      }<xsl:if test="$regex">
1123      if (!REGULAR_EXPRESSION_PATTERN.matcher(str).matches())
1124      {
1125         throw new ArgumentPatternViolationException(
1126            <xsl:value-of select="$classname-constant"/>,
1127            str, REGULAR_EXPRESSION,
1128            <xsl:value-of select="$classname"/>.class);
1129      }</xsl:if><xsl:if test="$token-type">
1130      if (!XsdUtil.isValidToken(str))
1131      {
1132         throw new ArgumentMalformedException(
1133            <xsl:value-of select="$classname-constant"/>,
1134            str, "Token format restrictions violated.");
1135      }</xsl:if>
1136      m<xsl:value-of select="$classname"/> = str;
1137   }
1138
1139   /**
1140    * Creates a <xsl:value-of select="$classname"/> object from the String representation.
1141    *
1142    * @param str The str representation of the <xsl:value-of select="$classname"/> to be returned.
1143    * @return The <xsl:value-of select="$classname"/> object represented by this str.
1144    * @throws ArgumentMalformedException If the given string &lt;code>str&lt;/code>
1145    *         violates the restriction of this type.
1146    */
1147   public static <xsl:value-of select="$classname"/> fromString (String str)
1148         throws ArgumentMalformedException
1149   {
1150      return new <xsl:value-of select="$classname"/>(str);
1151   }
1152
1153   /**
1154    * Returns the String representation of this <xsl:value-of select="$classname"/>.
1155    *
1156    * @return The String representation of this <xsl:value-of select="$classname"/>.
1157    */
1158   public String toString ()
1159   {
1160      return m<xsl:value-of select="$classname"/>;
1161   }
1162
1163   /**
1164    * Indicates whether some other object is "equal to" this one.
1165    *
1166    * @param obj the object to compare to.
1167    * @return true if this object is the same as the obj argument; false
1168    *         otherwise.
1169    */
1170   public boolean equals (Object obj)
1171   {
1172      return (this == obj)
1173        || (obj instanceof <xsl:value-of select="$classname"/>
1174            &amp;&amp; ((<xsl:value-of select="$classname"/>) obj).m<xsl:value-of select="$classname"/>.equals(
1175               m<xsl:value-of select="$classname"/>));
1176   }
1177
1178   /**
1179    * Returns the hash code for the <xsl:value-of select="$classname"/>.
1180    *
1181    * @return the hash code for the <xsl:value-of select="$classname"/>.
1182    */
1183   public int hashCode ()
1184   {
1185      return m<xsl:value-of select="$classname"/>.hashCode();
1186   }
1187
1188   /**
1189    * Compares this <xsl:value-of select="$classname"/> with an other.
1190    * The order is based on the natural order of the String representation
1191    * of the compared instances.
1192    *
1193    * @param o the Object to be compared.   
1194    * @return a negative integer, zero, or a positive integer as this
1195    *   object is less than, equal to, or greater than the specified object.
1196    * @throws ClassCastException if the specified object's type prevents
1197    *   it from being compared to this Object.
1198    * @see String#compareTo(Object)
1199    */
1200   public int compareTo(Object o)
1201   {
1202      return toString().compareTo(
1203        ((<xsl:value-of select="$classname"/>) o).toString());
1204   }
1205}
1206</xsl:template>
1207
1208<xsl:template name="restricted-string-user-type">
1209   <xsl:param name="classname"/>
1210   <xsl:param name="type-classname"/>
1211   <xsl:param name="package"/>
1212   <xsl:param name="min-length"/>
1213   <xsl:param name="max-length"/>
1214   <xsl:variable name="classname-constant">TYPE_NAME</xsl:variable>
1215   <xsl:call-template name="java-copyright-header"/>
1216package <xsl:value-of select="$package"/>;
1217
1218/**
1219 * Hibernate user type for the  <xsl:value-of select="$type-classname"/>.
1220 *
1221 * @author generated via stylesheet
1222 */
1223public final class <xsl:value-of select="$classname"/>
1224      extends org.jcoderz.commons.util.StringUserTypeBase
1225{
1226   /** The serialVersionUID used for serialization. */
1227   static final long serialVersionUID = 1;
1228
1229   /** <xsl:choose>
1230        <xsl:when
1231          test="$min-length = 0">Holds the empty string representation of the type.</xsl:when>
1232          <xsl:otherwise>Holds null as empty representation of this type.</xsl:otherwise>
1233      </xsl:choose> */
1234   private static final <xsl:value-of select="$type-classname"/> EMPTY_OR_NULL
1235        = <xsl:choose><xsl:when test="$min-length = 0">
1236          <xsl:value-of select="$type-classname"/>.fromString("");</xsl:when>
1237          <xsl:otherwise>null;</xsl:otherwise>
1238          </xsl:choose>
1239
1240  /**
1241   * Hibernate <tt><xsl:value-of select="$type-classname"/></tt> type as mapped
1242   * from this UserType.
1243   * @return this UserType as org.hibernate.type.Type.
1244   */
1245  public static org.hibernate.type.Type getType ()
1246  {
1247    return TypeHolder.TYPE;
1248  }
1249
1250  /**
1251   * Creates a <xsl:value-of select="$type-classname"/> from its String
1252   * database representation.
1253   * @param value a string holding the database representation of the
1254   *    <xsl:value-of select="$type-classname"/>.
1255   * @return a <xsl:value-of select="$type-classname"/> representing the
1256   *    given string.
1257   * @see <xsl:value-of select="$type-classname"/>#fromString(String)
1258   */
1259  public Object fromString(String value)
1260  {
1261    return <xsl:value-of select="$type-classname"/>.fromString(value);
1262  }
1263
1264  /** <xsl:choose>
1265        <xsl:when
1266          test="$min-length = 0">@return the empty string as null representation of the type.</xsl:when>
1267          <xsl:otherwise>@return &lt;code>null&lt;/code> as null representation of the type.</xsl:otherwise>
1268      </xsl:choose> */
1269  public Object getEmptyOrNull()
1270  {
1271    return EMPTY_OR_NULL;
1272  }
1273
1274  /**
1275   * @return <xsl:value-of select="$type-classname"/>.class as the supported
1276   * class of this user type.
1277   */
1278  public Class returnedClass()
1279  {
1280    return <xsl:value-of select="$type-classname"/>.class;
1281  }
1282
1283  /**
1284   * Class to lazy initialize the Hibernate Type adapter.
1285   */
1286   private static class TypeHolder
1287   {
1288      private static final org.hibernate.type.Type TYPE
1289        = new org.hibernate.type.CustomType(<xsl:value-of select="$classname"/>.class, null);
1290   }
1291}
1292</xsl:template>
1293
1294<xsl:template name="restricted-string-import-hook" priority="-1">
1295<xsl:param name="token-type" select="''"/>
1296<xsl:param name="regex" select="''"/><xsl:if test="$regex">
1297import java.util.regex.Pattern;
1298</xsl:if>
1299import org.jcoderz.commons.ArgumentMinLengthViolationException;
1300import org.jcoderz.commons.ArgumentMaxLengthViolationException;
1301import org.jcoderz.commons.ArgumentMalformedException;<xsl:if test="$regex">
1302import org.jcoderz.commons.ArgumentPatternViolationException;</xsl:if><xsl:if test="$token-type">
1303import org.jcoderz.commons.util.XsdUtil;</xsl:if>
1304import org.jcoderz.commons.util.Assert;
1305</xsl:template>
1306
1307<!-- ===============================================================
1308     Restricted long generator
1309     =============================================================== -->
1310<xsl:template name="restricted-long">
1311   <xsl:param name="classname"/>
1312   <xsl:param name="package"/>
1313   <xsl:param name="min-value"/>
1314   <xsl:param name="max-value"/>
1315   <xsl:param name="constants"/>
1316   <xsl:param name="implements"/>
1317   <xsl:variable name="classname-constant">TYPE_NAME</xsl:variable>
1318   <xsl:call-template name="java-copyright-header"/>
1319package <xsl:value-of select="$package"/>;
1320
1321import java.io.Serializable;
1322
1323import org.jcoderz.commons.util.HashCodeUtil;
1324<xsl:call-template name="restricted-long-import-hook" />
1325
1326/**
1327 * Holds the <xsl:value-of select="$classname"/>.
1328 * &lt;pre&gt;
1329 * long[<xsl:value-of select="$min-value"/>..<xsl:value-of select="$max-value"/>].
1330 * &lt;/pre&gt;
1331 * Instances of this class are immutable.
1332 *
1333 * &lt;p>This class implements the Comparable interface based on the numeric
1334 * order of its instances.&lt;/p>
1335 *
1336 * @author generated via stylesheet
1337 */
1338public final class <xsl:value-of select="$classname"/>
1339      implements Serializable, org.jcoderz.commons.RestrictedLong, Comparable<xsl:if test="$implements">,
1340            <xsl:value-of select="$implements"/></xsl:if>
1341{
1342   /**
1343    * <xsl:value-of select="$classname"/> - the name of this type as string constant.
1344    */
1345   public static final String TYPE_NAME = "<xsl:value-of select="$classname"/>";
1346
1347   /**
1348    * The minimum value of a <xsl:value-of select="$classname"/> (<xsl:value-of select="$min-value"/>).
1349    */
1350   public static final long MIN_VALUE = <xsl:value-of select="$min-value"/>;
1351
1352   /**
1353    * The maximum value of a <xsl:value-of select="$classname"/> (<xsl:value-of select="$max-value"/>).
1354    */
1355   public static final long MAX_VALUE = <xsl:value-of select="$max-value"/>;
1356
1357<xsl:for-each select="$constants">
1358   <xsl:call-template name="java-constant">
1359      <xsl:with-param name="type" select="$classname"/>
1360      <xsl:with-param name="name" select="./@name"/>
1361      <xsl:with-param name="value" select="./@value"/>
1362      <xsl:with-param name="comment" select="./@comment"/>
1363      <xsl:with-param name="quote-char" select="''"/>
1364   </xsl:call-template>
1365</xsl:for-each>
1366   /** The serialVersionUID used for serialization. */
1367   static final long serialVersionUID = 1;
1368
1369   /** Holds the <xsl:value-of select="$classname"/>. */
1370   private final long m<xsl:value-of select="$classname"/>;
1371   /** Lazy initialized long object value. */
1372   private transient Long m<xsl:value-of select="$classname"/>LongObject;
1373   /** Lazy initialized hash code value. */
1374   private transient int mHashCode = 0;
1375
1376   /**
1377    * Creates a new instance of a <xsl:value-of select="$classname"/>.
1378    *
1379    * @param id the <xsl:value-of select="$classname"/> as long representation
1380    * @throws ArgumentMalformedException If the given long &lt;code>id&lt;/code>
1381    *         violates the restriction of the type
1382    *         <xsl:value-of select="$classname"/>.
1383    * @throws ArgumentMinValueViolationException If the value of the given
1384    *         long &lt;code>id&lt;/code> is below {@link #MIN_VALUE}.
1385    * @throws ArgumentMaxValueViolationException If the value of the given
1386    *         long &lt;code>id&lt;/code> is above {@link #MAX_VALUE}.
1387    */
1388   private <xsl:value-of select="$classname"/> (long id)
1389   {
1390      if (id &lt; MIN_VALUE)
1391      {
1392         throw new ArgumentMinValueViolationException(
1393               TYPE_NAME, new Long(id), new Long(MIN_VALUE),
1394               <xsl:value-of select="$classname"/>.class);
1395      }
1396      if (id &gt; MAX_VALUE)
1397      {
1398         throw new ArgumentMaxValueViolationException(
1399               TYPE_NAME, new Long(id), new Long(MAX_VALUE),
1400               <xsl:value-of select="$classname"/>.class);
1401      }
1402      m<xsl:value-of select="$classname"/> = id;
1403   }
1404
1405   /**
1406    * Construct a <xsl:value-of select="$classname"/> object from its long representation.
1407    * @param id the long representation of the <xsl:value-of select="$classname"/>
1408    * @return the <xsl:value-of select="$classname"/> object represented by the given long
1409    * @throws ArgumentMalformedException If the given long &lt;code>id&lt;/code>
1410    *         violates the restriction of the type
1411    *         <xsl:value-of select="$classname"/>.
1412    * @throws ArgumentMinValueViolationException If the value of the given
1413    *         long &lt;code>id&lt;/code> is below {@link #MIN_VALUE}.
1414    * @throws ArgumentMaxValueViolationException If the value of the given
1415    *         long &lt;code>id&lt;/code> is above {@link #MAX_VALUE}.
1416    */
1417   public static <xsl:value-of select="$classname"/> fromLong (long id)
1418         throws ArgumentMalformedException
1419   {
1420      return new <xsl:value-of select="$classname"/>(id);
1421   }
1422
1423   /**
1424    * Construct a <xsl:value-of select="$classname"/> object from its string representation.
1425    * @param s the string representation of the <xsl:value-of select="$classname"/>
1426    * @return the <xsl:value-of select="$classname"/> object represented by the given string
1427    * @throws ArgumentMalformedException If the given string &lt;code>s&lt;/code>
1428    *         violates the restriction of the type
1429    *         <xsl:value-of select="$classname"/>.
1430    * @throws ArgumentMinValueViolationException If the value of the given
1431    *         long &lt;code>id&lt;/code> is below {@link #MIN_VALUE}.
1432    * @throws ArgumentMaxValueViolationException If the value of the given
1433    *         long &lt;code>id&lt;/code> is above {@link #MAX_VALUE}.
1434    */
1435   public static <xsl:value-of select="$classname"/> fromString (String s)
1436         throws ArgumentMalformedException
1437   {
1438      final long id;
1439      try
1440      {
1441         id = Long.parseLong(s);
1442      }
1443      catch (NumberFormatException e)
1444      {
1445         throw new ArgumentMalformedException(
1446               TYPE_NAME, s, "Invalid string representation", e);
1447      }
1448      return new <xsl:value-of select="$classname"/>(id);
1449   }
1450
1451   /**
1452    * Construct a <xsl:value-of select="$classname"/> object from its Long representation.
1453    * @param id the Long representation of the <xsl:value-of select="$classname"/>
1454    * @return the <xsl:value-of select="$classname"/> object represented by the given Long
1455    * @throws ArgumentMalformedException If the given Long &lt;code>id&lt;/code>
1456    *         is null or violates the restriction of the type
1457    *         <xsl:value-of select="$classname"/>.
1458    * @throws ArgumentMinValueViolationException If the value of the given
1459    *         long &lt;code>id&lt;/code> is below {@link #MIN_VALUE}.
1460    * @throws ArgumentMaxValueViolationException If the value of the given
1461    *         long &lt;code>id&lt;/code> is above {@link #MAX_VALUE}.
1462    */
1463   public static <xsl:value-of select="$classname"/> fromLong (Long id)
1464         throws ArgumentMalformedException
1465   {
1466      Assert.notNull(id, "id");
1467      return new <xsl:value-of select="$classname"/>(id.longValue());
1468   }
1469
1470   /**
1471    * Generates a random <xsl:value-of select="$classname"/> object.
1472    * @return a random <xsl:value-of select="$classname"/> object.
1473    */
1474   public static <xsl:value-of select="$classname"/> random ()
1475   {
1476      return new <xsl:value-of select="$classname"/>(RandomUtil.random(MIN_VALUE, MAX_VALUE));
1477   }
1478
1479   /**
1480    * Returns the long representation of this <xsl:value-of select="$classname"/> object.
1481    * @return the long representation of this <xsl:value-of select="$classname"/> object.
1482    */
1483   public long toLong ()
1484   {
1485      return m<xsl:value-of select="$classname"/>;
1486   }
1487
1488   /**
1489    * Returns the Long representation of this <xsl:value-of select="$classname"/> object.
1490    * @return the Long representation of this <xsl:value-of select="$classname"/> object.
1491    */
1492   public Long toLongObject ()
1493   {
1494      if (m<xsl:value-of select="$classname"/>LongObject == null)
1495      {
1496         m<xsl:value-of select="$classname"/>LongObject = new Long(m<xsl:value-of select="$classname"/>);
1497      }
1498      return m<xsl:value-of select="$classname"/>LongObject;
1499   }
1500
1501   /** {@inheritDoc} */
1502   public String toString ()
1503   {
1504      return Long.toString(m<xsl:value-of select="$classname"/>);
1505   }
1506
1507   /** {@inheritDoc} */
1508   public boolean equals (Object obj)
1509   {
1510      return (this == obj)
1511        || (obj instanceof <xsl:value-of select="$classname"/>
1512            &amp;&amp; ((<xsl:value-of select="$classname"/>) obj).m<xsl:value-of select="$classname"/>
1513               == m<xsl:value-of select="$classname"/>);
1514   }
1515
1516   /** {@inheritDoc} */
1517   public int hashCode ()
1518   {
1519      if (mHashCode == 0)
1520      {
1521         mHashCode = HashCodeUtil.hash(HashCodeUtil.SEED, m<xsl:value-of select="$classname"/>);
1522      }
1523      return mHashCode;
1524   }
1525
1526   /**
1527    * Compares this <xsl:value-of select="$classname"/> with an other.
1528    * The order is based on the numeric order of the
1529    * of the compared instances.
1530    *
1531    * @return a negative integer, zero, or a positive integer as this
1532    *   object is less than, equal to, or greater than the specified object.
1533    * @throws ClassCastException if the specified object's type prevents
1534    *   it from being compared to this Object.
1535    * @see Long#compareTo(Object)
1536    */
1537   public int compareTo(Object o)
1538   {
1539     final long thisVal = toLong();
1540     final long anotherVal = ((<xsl:value-of select="$classname"/>) o).toLong();
1541     return (thisVal &lt; anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1));
1542   }
1543
1544}
1545</xsl:template>
1546
1547<xsl:template name="restricted-int-user-type">
1548   <xsl:param name="classname"/>
1549   <xsl:param name="type-classname"/>
1550   <xsl:param name="package"/>
1551   <xsl:param name="min-length"/>
1552   <xsl:param name="max-length"/>
1553   <xsl:variable name="classname-constant">TYPE_NAME</xsl:variable>
1554   <xsl:call-template name="java-copyright-header"/>
1555package <xsl:value-of select="$package"/>;
1556
1557/**
1558 * Hibernate user type for the  <xsl:value-of select="$type-classname"/>.
1559 *
1560 * @author generated via stylesheet
1561 */
1562public final class <xsl:value-of select="$classname"/>
1563      extends org.jcoderz.commons.util.IntUserTypeBase
1564{
1565   /** The serialVersionUID used for serialization. */
1566   static final long serialVersionUID = 1;
1567
1568   /**
1569    * Hibernate <tt><xsl:value-of select="$type-classname"/></tt> type as mapped
1570    * from this UserType.
1571    * @return this UserType as org.hibernate.type.Type.
1572    */
1573   public static org.hibernate.type.Type getType ()
1574   {
1575      return TypeHolder.TYPE;
1576   }
1577
1578  /**
1579   * Creates a <xsl:value-of select="$type-classname"/> from its numeric
1580   * int database representation.
1581   * @param value a int holding the database representation of the
1582   *    <xsl:value-of select="$type-classname"/>.
1583   * @return a <xsl:value-of select="$type-classname"/> representing the
1584   *    given int.
1585   * @see <xsl:value-of select="$type-classname"/>#fromInt(int)
1586   */
1587  public Object fromInt(int value)
1588  {
1589    return <xsl:value-of select="$type-classname"/>.fromInt(value);
1590  }
1591
1592  /**
1593   * Converts the <xsl:value-of select="$type-classname"/> to its numeric
1594   * int database representation.
1595   * @param value the <xsl:value-of select="$type-classname"/> to be
1596   *    converted.
1597   * @return a int representing the
1598   *    given <xsl:value-of select="$type-classname"/>.
1599   * @see <xsl:value-of select="$type-classname"/>#toInt()
1600   */
1601  public int toInt(Object value)
1602  {
1603    return ((<xsl:value-of select="$type-classname"/>) value).toInt();
1604  }
1605
1606  /**
1607   * @return <xsl:value-of select="$type-classname"/>.class as the supported
1608   * class of this user type.
1609   */
1610  public Class returnedClass()
1611  {
1612    return <xsl:value-of select="$type-classname"/>.class;
1613  }
1614
1615
1616  /**
1617   * Class to lazy initialize the Hibernate Type adapter.
1618   */
1619   private static class TypeHolder
1620   {
1621      private static final org.hibernate.type.Type TYPE
1622        = new org.hibernate.type.CustomType(<xsl:value-of select="$classname"/>.class, null);
1623   }
1624}
1625</xsl:template>
1626
1627<xsl:template name="restricted-long-user-type">
1628   <xsl:param name="classname"/>
1629   <xsl:param name="type-classname"/>
1630   <xsl:param name="package"/>
1631   <xsl:param name="min-length"/>
1632   <xsl:param name="max-length"/>
1633   <xsl:variable name="classname-constant">TYPE_NAME</xsl:variable>
1634   <xsl:call-template name="java-copyright-header"/>
1635package <xsl:value-of select="$package"/>;
1636
1637/**
1638 * Hibernate user type for the  <xsl:value-of select="$type-classname"/>.
1639 *
1640 * @author generated via stylesheet
1641 */
1642public final class <xsl:value-of select="$classname"/>
1643      extends org.jcoderz.commons.util.LongUserTypeBase
1644{
1645   /** The serialVersionUID used for serialization. */
1646   static final long serialVersionUID = 1;
1647
1648   /**
1649    * Hibernate <tt><xsl:value-of select="$type-classname"/></tt> type as mapped
1650    * from this UserType.
1651    * @return this UserType as org.hibernate.type.Type.
1652    */
1653   public static org.hibernate.type.Type getType ()
1654   {
1655      return TypeHolder.TYPE;
1656   }
1657
1658  /**
1659   * Creates a <xsl:value-of select="$type-classname"/> from its numeric
1660   * long database representation.
1661   * @param value a long holding the database representation of the
1662   *    <xsl:value-of select="$type-classname"/>.
1663   * @return a <xsl:value-of select="$type-classname"/> representing the
1664   *    given long.
1665   * @see <xsl:value-of select="$type-classname"/>#fromLong(long)
1666   */
1667  public Object fromLong(long value)
1668  {
1669    return <xsl:value-of select="$type-classname"/>.fromLong(value);
1670  }
1671
1672  /**
1673   * Converts the <xsl:value-of select="$type-classname"/> to its numeric
1674   * long database representation.
1675   * @param value the <xsl:value-of select="$type-classname"/> to be
1676   *    converted.
1677   * @return a long representing the
1678   *    given <xsl:value-of select="$type-classname"/>.
1679   * @see <xsl:value-of select="$type-classname"/>#toLong()
1680   */
1681  public long toLong(Object value)
1682  {
1683    return ((<xsl:value-of select="$type-classname"/>) value).toLong();
1684  }
1685
1686  /**
1687   * @return <xsl:value-of select="$type-classname"/>.class as the supported
1688   * class of this user type.
1689   */
1690  public Class returnedClass()
1691  {
1692    return <xsl:value-of select="$type-classname"/>.class;
1693  }
1694
1695  /**
1696   * Class to lazy initialize the Hibernate Type adapter.
1697   */
1698   private static class TypeHolder
1699   {
1700      private static final org.hibernate.type.Type TYPE
1701        = new org.hibernate.type.CustomType(<xsl:value-of select="$classname"/>.class, null);
1702   }
1703}
1704</xsl:template>
1705
1706<xsl:template name="restricted-long-import-hook" priority="-1">
1707import org.jcoderz.commons.ArgumentMalformedException;
1708import org.jcoderz.commons.ArgumentMinValueViolationException;
1709import org.jcoderz.commons.ArgumentMaxValueViolationException;
1710import org.jcoderz.commons.util.RandomUtil;
1711import org.jcoderz.commons.util.Assert;
1712</xsl:template>
1713
1714<!-- ===============================================================
1715     String that must match a regex.
1716     =============================================================== -->
1717<xsl:template name="regex-string">
1718   <xsl:param name="classname"/>
1719   <xsl:param name="package"/>
1720   <xsl:param name="constants" select="''"/>
1721   <xsl:param name="regex" select="'FIXME'"/>
1722
1723   <xsl:variable name="classname-constant"><xsl:text>TYPE_NAME</xsl:text></xsl:variable>
1724
1725   <xsl:call-template name="java-copyright-header"/>
1726package <xsl:value-of select="$package"/>;
1727
1728
1729import java.io.Serializable;
1730import java.util.regex.Pattern;
1731<xsl:call-template name="regex-string-import-hook"/>
1732
1733/**
1734 * Type-safe string type.
1735 * &lt;pre&gt;
1736 * regular expression: <xsl:value-of select="$regex"/>
1737 * &lt;/pre&gt;
1738 * Instances of this class are immutable.
1739 *
1740 * @author generated
1741 */
1742public final class <xsl:value-of select="$classname"/>
1743      implements Serializable, org.jcoderz.commons.StrongType
1744{
1745   /**
1746    * The name of this type.
1747    */
1748   public static final String TYPE_NAME = "<xsl:value-of select="$classname"/>";
1749
1750   /** The regular expression matching <xsl:value-of select="$classname"/>. */
1751   public static final String REGULAR_EXPRESSION
1752         = "<xsl:call-template name="java-string-escape"><xsl:with-param name="s" select="$regex"/></xsl:call-template>";
1753
1754   /** The compiled pattern for the regular expression. */
1755   public static final Pattern REGULAR_EXPRESSION_PATTERN
1756         = Pattern.compile(REGULAR_EXPRESSION);
1757<xsl:for-each select="$constants">
1758   <xsl:call-template name="java-constant">
1759      <xsl:with-param name="type" select="$classname"/>
1760      <xsl:with-param name="name" select="./@name"/>
1761      <xsl:with-param name="value" select="./@value"/>
1762      <xsl:with-param name="comment" select="./@comment"/>
1763   </xsl:call-template>
1764</xsl:for-each>
1765   /** The serialVersionUID used for serialization. */
1766   static final long serialVersionUID = 1;
1767
1768   /** Holds the <xsl:value-of select="$classname"/>. */
1769   private final String m<xsl:value-of select="$classname"/>;
1770
1771   /**
1772    * Creates a new instance of a <xsl:value-of select="$classname"/>.
1773    *
1774    * @param str the <xsl:value-of select="$classname"/> as string representation
1775    * @throws ArgumentMalformedException If the given string &lt;code>str&lt;/code>
1776    *         does not conform to the Simpay Scheme representation of the
1777    *         <xsl:value-of select="$classname"/>.
1778    */
1779   private <xsl:value-of select="$classname"/> (final String str)
1780         throws ArgumentMalformedException
1781   {
1782      Assert.notNull(str, TYPE_NAME);
1783      if (!REGULAR_EXPRESSION_PATTERN.matcher(str).matches())
1784      {
1785         throw new ArgumentMalformedException(
1786            <xsl:value-of select="$classname-constant"/>,
1787            str,
1788            "Value must match regular expression " + REGULAR_EXPRESSION + ".");
1789      }
1790
1791      m<xsl:value-of select="$classname"/> = str;
1792   }
1793
1794   /**
1795    * Creates a <xsl:value-of select="$classname"/> object from SXP String representation.
1796    *
1797    * @param str The str representation of the <xsl:value-of select="$classname"/> to be returned.
1798    * @return The <xsl:value-of select="$classname"/> object represented by this str.
1799    * @throws ArgumentMalformedException If the given string &lt;code>s&lt;/code>
1800    *         does not conform to the Simpay Interface representation of
1801    *         <xsl:value-of select="$classname"/>.
1802    */
1803   public static <xsl:value-of select="$classname"/> fromString (String str)
1804         throws ArgumentMalformedException
1805   {
1806      return new <xsl:value-of select="$classname"/>(str);
1807   }
1808
1809   /**
1810    * Returns the SXP String representation of this <xsl:value-of select="$classname"/>.
1811    *
1812    * @return The SXP String representation of this <xsl:value-of select="$classname"/>.
1813    */
1814   public String toString ()
1815   {
1816      return m<xsl:value-of select="$classname"/>;
1817   }
1818
1819   /**
1820    * Indicates whether some other object is "equal to" this one.
1821    *
1822    * @param obj the object to compare to.
1823    * @return true if this object is the same as the obj argument; false
1824    *         otherwise.
1825    */
1826   public boolean equals (Object obj)
1827   {
1828      return (this == obj)
1829        || (obj instanceof <xsl:value-of select="$classname"/>
1830            &amp;&amp; ((<xsl:value-of select="$classname"/>) obj).m<xsl:value-of select="$classname"/>.equals(
1831               m<xsl:value-of select="$classname"/>));
1832   }
1833
1834   /**
1835    * Returns the hash code for the <xsl:value-of select="$classname"/>.
1836    *
1837    * @return the hash code for the <xsl:value-of select="$classname"/>.
1838    */
1839   public int hashCode ()
1840   {
1841      return m<xsl:value-of select="$classname"/>.hashCode();
1842   }
1843}
1844</xsl:template>
1845
1846<xsl:template name="regex-string-import-hook" priority="-1">
1847import org.jcoderz.commons.ArgumentMalformedException;
1848import org.jcoderz.commons.util.Assert;
1849</xsl:template>
1850
1851<!-- ===============================================================
1852     Fix Point Number generator.
1853     =============================================================== -->
1854<xsl:template name="fix-point-number">
1855   <xsl:param name="classname"/>
1856   <xsl:param name="package"/>
1857   <xsl:param name="constants" select="''"/>
1858   <xsl:param name="implements" select="''"/>
1859   <xsl:param name="fraction-digits" select="'FIXME'"/>
1860   <xsl:param name="total-digits" select="'FIXME'"/>
1861   <xsl:param name="min-value" select="''"/>
1862   <xsl:param name="max-value" select="'FIXME'"/>
1863
1864  <xsl:variable name="DECIMAL_SCALE">1<xsl:call-template name="repeat">
1865       <xsl:with-param name="pattern" select="'0'"/>
1866       <xsl:with-param name="count" select="$fraction-digits"/>
1867    </xsl:call-template>
1868  </xsl:variable>
1869  <xsl:variable name="MIN_INTERNAL">-<xsl:call-template name="repeat">
1870       <xsl:with-param name="pattern" select="'9'"/>
1871       <xsl:with-param
1872            name="count" select="$total-digits - $fraction-digits"/>
1873       </xsl:call-template>.<xsl:call-template name="repeat">
1874         <xsl:with-param name="pattern" select="'9'"/>
1875         <xsl:with-param name="count" select="$fraction-digits"/>
1876       </xsl:call-template></xsl:variable>
1877  <xsl:variable name="MAX_INTERNAL"><xsl:call-template name="repeat">
1878        <xsl:with-param name="pattern" select="'9'"/>
1879        <xsl:with-param
1880          name="count" select="$total-digits - $fraction-digits"/>
1881      </xsl:call-template>.<xsl:call-template name="repeat">
1882        <xsl:with-param name="pattern" select="'9'"/>
1883        <xsl:with-param name="count" select="$fraction-digits"/>
1884      </xsl:call-template></xsl:variable>
1885
1886  <xsl:variable name="MAX_VALUE">
1887    <xsl:choose>
1888      <xsl:when test="not($max-value)"><xsl:value-of select="$MAX_INTERNAL"/>
1889      </xsl:when>
1890      <xsl:otherwise>
1891        <xsl:if test="$MAX_INTERNAL &lt; $max-value">
1892          <xsl:message terminate="yes">
1893             WARNING: Max value can not be represented by <xsl:value-of select="$classname"/>
1894          </xsl:message>
1895        </xsl:if>
1896        <xsl:value-of select="$max-value"/>
1897      </xsl:otherwise>
1898    </xsl:choose>
1899  </xsl:variable>
1900  <xsl:variable name="MIN_VALUE">
1901    <xsl:choose>
1902      <xsl:when test="not($min-value)"><xsl:value-of select="$MIN_INTERNAL"/>
1903      </xsl:when>
1904      <xsl:otherwise>
1905        <xsl:if test="$MIN_INTERNAL &gt; $min-value">
1906          <xsl:message terminate="yes">
1907             WARNING: Min value can not be represented by <xsl:value-of select="$classname"/>
1908          </xsl:message>
1909        </xsl:if>
1910        <xsl:value-of select="$min-value"/>
1911      </xsl:otherwise>
1912    </xsl:choose>
1913  </xsl:variable>
1914
1915  <!-- TODO: Assert that given max and min-value are in the total-digits
1916             range supported -->
1917  <!-- TODO might dynamicly switch to int / BD? -->
1918  <xsl:variable name="backing-type">long</xsl:variable>
1919  <xsl:variable name="backing-class">Long</xsl:variable>
1920  <xsl:variable name="backing-char">L</xsl:variable>
1921
1922<xsl:call-template name="java-copyright-header"/>
1923package <xsl:value-of select="$package"/>;
1924
1925
1926import java.io.Serializable;
1927import java.math.BigDecimal;
1928
1929import org.jcoderz.commons.ArgumentMalformedException;
1930import org.jcoderz.commons.ArgumentMaxValueViolationException;
1931import org.jcoderz.commons.ArgumentMinValueViolationException;
1932import org.jcoderz.commons.ArgumentFractionDigitsViolationException;
1933import org.jcoderz.commons.FixPointNumber;
1934import org.jcoderz.commons.util.Assert;
1935import org.jcoderz.commons.util.HashCodeUtil;
1936import org.jcoderz.commons.util.NumberUtil;
1937
1938
1939/**
1940 * Fix point numeric to represent <xsl:value-of select="$classname"/>.
1941 *
1942 * &lt;p>Permitted values range from <xsl:value-of select="$MIN_VALUE"/> to
1943 * <xsl:value-of select="$MAX_VALUE"/>. It the number of decimal
1944 * digits supported is <xsl:value-of select="$fraction-digits"/> and
1945 * the total number of digits is <xsl:value-of select="$total-digits"/>&lt;/p>
1946 *
1947 * Instances of this class are immutable.
1948 *
1949 * @author generated
1950 */
1951public final class <xsl:value-of select="$classname"/>
1952    extends Number
1953    implements Comparable, FixPointNumber, Serializable<xsl:if test="$implements">,
1954            <xsl:value-of select="$implements"/></xsl:if>
1955{
1956    /**
1957     * <xsl:value-of select="$classname"/> - the name of this type as String constant.
1958     */
1959    public static final String TYPE_NAME = "<xsl:value-of select="$classname"/>";
1960
1961    /** The preffered database representation of this type. */
1962    public static final String PREFERED_DATABASE_TYPE
1963        = "NUMBER(<xsl:value-of select="$total-digits"/>,<xsl:value-of select="$fraction-digits"/>)";
1964
1965    /** The minimal unscaled value of <xsl:value-of select="$classname"/> ({@value}). */
1966    public static final <xsl:value-of select="$backing-type"/> MIN_VALUE_UNSCALED
1967        = <xsl:call-template name="power10precise">
1968          <xsl:with-param name="number" select="$MIN_VALUE"/>
1969          <xsl:with-param name="exp" select="$fraction-digits"/>
1970        </xsl:call-template><xsl:value-of select="$backing-char"/>;
1971
1972    /** The maximal value of <xsl:value-of select="$classname"/> ({@value}). */
1973    public static final <xsl:value-of select="$backing-type"/> MAX_VALUE_UNSCALED
1974        = <xsl:call-template name="power10precise">
1975          <xsl:with-param name="number" select="$MAX_VALUE"/>
1976          <xsl:with-param name="exp" select="$fraction-digits"/>
1977        </xsl:call-template><xsl:value-of select="$backing-char"/>;
1978
1979    /** The number of fraction digits (<xsl:value-of select="$fraction-digits"/>). */
1980    public static final int FRACTION_DIGITS
1981        = <xsl:value-of select="$fraction-digits"/>;
1982
1983    /** The number of fraction digits (<xsl:value-of select="$fraction-digits"/>). */
1984    public static final int SCALE
1985        = FRACTION_DIGITS;
1986
1987    /** The scale (<xsl:value-of select="$DECIMAL_SCALE"/>). */
1988    public static final <xsl:value-of select="$backing-type"/> DECIMAL_SCALE
1989        = <xsl:value-of select="$DECIMAL_SCALE"/>;
1990
1991    /** The minimal scaled value of <xsl:value-of select="$classname"/> ({@value}). */
1992    public static final <xsl:value-of select="$backing-type"/> MIN_VALUE_SCALED
1993        = MIN_VALUE_UNSCALED / DECIMAL_SCALE;
1994
1995    /** The maximal scaled value of <xsl:value-of select="$classname"/> ({@value}). */
1996    public static final <xsl:value-of select="$backing-type"/> MAX_VALUE_SCALED
1997        = MAX_VALUE_UNSCALED / DECIMAL_SCALE;
1998
1999    /** The number of fraction digits as integer (<xsl:value-of select="$fraction-digits"/>).  */
2000    public static final Integer FRACTION_DIGITS_AS_INTEGER
2001        = new Integer(FRACTION_DIGITS);
2002
2003    /** The maximum number of digits (<xsl:value-of select="$total-digits"/>). */
2004    public static final int TOTAL_DIGITS
2005        = <xsl:value-of select="$total-digits"/>;
2006
2007    /** The maximum number of digits as Integer (<xsl:value-of select="$total-digits"/>).  */
2008    public static final Integer TOTAL_DIGITS_AS_INTEGER
2009        = new Integer(TOTAL_DIGITS);
2010
2011    /** The minimal value of <xsl:value-of select="$classname"/> (<xsl:value-of select="$MIN_VALUE"/>). */
2012    public static final <xsl:value-of select="$classname"/> MIN_VALUE
2013        = new <xsl:value-of select="$classname"/>(MIN_VALUE_UNSCALED);
2014
2015    /** The maximal value of <xsl:value-of select="$classname"/> (<xsl:value-of select="$MAX_VALUE"/>). */
2016    public static final <xsl:value-of select="$classname"/> MAX_VALUE
2017        = new <xsl:value-of select="$classname"/>(MAX_VALUE_UNSCALED);
2018<!-- It is important to define the constants at the very end of the statics
2019     -->
2020<xsl:for-each select="$constants">
2021   <xsl:call-template name="java-constant-from-string">
2022      <xsl:with-param name="type" select="$classname"/>
2023      <xsl:with-param name="name" select="./@name"/>
2024      <xsl:with-param name="value" select="./@value"/>
2025      <xsl:with-param name="comment" select="./@comment"/>
2026   </xsl:call-template>
2027</xsl:for-each>
2028    /** The serialVersionUID used for serialization. */
2029    static final long serialVersionUID = 1;
2030
2031
2032    /** Holds the <xsl:value-of select="$classname"/> in a unscaled <xsl:value-of select="$backing-type"/>. */
2033    private final <xsl:value-of select="$backing-type"/> mUnscaled;
2034
2035    /** Lazy initialized internal String representation. */
2036    private volatile String mStringRepresentation = null;
2037
2038    /** Lazy initialized internal BigDecimal representation. */
2039    private volatile BigDecimal mBigDecimal;
2040
2041
2042    /**
2043     * Creates a new instance of a <xsl:value-of select="$classname"/>.
2044     *
2045     * @param unscaledValue unscaled <xsl:value-of select="$backing-type"/> representation
2046     * @throws ArgumentMalformedException If the given
2047     *   unscaledValue violates the restriction
2048     *   of the <xsl:value-of select="$classname"/> type.
2049     */
2050    private <xsl:value-of select="$classname"/> (final <xsl:value-of select="$backing-type"/> unscaledValue)
2051          throws ArgumentMalformedException
2052    {
2053       if (unscaledValue &lt; MIN_VALUE_UNSCALED)
2054       {
2055          throw new ArgumentMinValueViolationException(
2056             TYPE_NAME,
2057             unscaledValue + "/" + DECIMAL_SCALE, MIN_VALUE,
2058             <xsl:value-of select="$classname"/>.class);
2059       }
2060       if (unscaledValue &gt; MAX_VALUE_UNSCALED)
2061       {
2062          throw new ArgumentMaxValueViolationException(
2063             TYPE_NAME, unscaledValue + "/" + DECIMAL_SCALE,
2064             MAX_VALUE, <xsl:value-of select="$classname"/>.class);
2065       }
2066       mUnscaled = unscaledValue;
2067    }
2068
2069    /**
2070     * Creates a <xsl:value-of select="$classname"/> object from the String
2071     * representation.
2072     *
2073     * @param str The str representation of the <xsl:value-of select="$classname"/>
2074     *   to be returned.
2075     * @return The <xsl:value-of select="$classname"/> object represented by this str.
2076     * @throws ArgumentMalformedException If the given
2077     *   String violates the restriction
2078     *   of the <xsl:value-of select="$classname"/> type.
2079     */
2080    public static <xsl:value-of select="$classname"/> fromString (String str)
2081          throws ArgumentMalformedException
2082    {
2083        Assert.notNull(str, TYPE_NAME);
2084
2085        final <xsl:value-of select="$classname"/> result;
2086        try
2087        {
2088            result = valueOf(new BigDecimal(str));
2089        }
2090        catch (NumberFormatException ex)
2091        {
2092            throw new ArgumentMalformedException(
2093                TYPE_NAME, str, "Invalid string representation", ex);
2094        }
2095        return result;
2096
2097    }
2098
2099    /**
2100     * Translates a &lt;tt>BigDecimal&lt;/tt> value into a
2101     * &lt;tt><xsl:value-of select="$classname"/>&lt;/tt>.
2102     *
2103     * @param bd the &lt;tt>BigDecimal&lt;/tt>.
2104     * @return a &lt;tt><xsl:value-of select="$classname"/>&lt;/tt> whose value is equal
2105     *  to bd.
2106     * @throws ArgumentMalformedException If the given
2107     *   &lt;tt>BigDecimal&lt;/tt> violates the restriction
2108     *   of the <xsl:value-of select="$classname"/> type.
2109     */
2110    public static <xsl:value-of select="$classname"/> valueOf (BigDecimal bd)
2111    {
2112        if (bd.scale() &gt; SCALE)
2113        {
2114            throw new ArgumentFractionDigitsViolationException(
2115                TYPE_NAME, bd, new Integer(bd.scale()),
2116                FRACTION_DIGITS_AS_INTEGER,
2117                <xsl:value-of select="$classname"/>.class);
2118        }
2119        return new <xsl:value-of select="$classname"/>(
2120            bd.setScale(SCALE).unscaledValue().<xsl:value-of select="$backing-type"/>Value());
2121    }
2122
2123    /**
2124     * Translates a &lt;tt>long&lt;/tt> value into a
2125     * &lt;tt><xsl:value-of select="$classname"/>&lt;/tt>.
2126     *
2127     * @param val the &lt;tt>long&lt;/tt>.
2128     * @return a &lt;tt><xsl:value-of select="$classname"/>&lt;/tt> whose value is equal
2129     *  to the given long.
2130     * @throws ArgumentMalformedException If the given
2131     *   &lt;tt>long&lt;/tt> violates the restriction
2132     *   of the <xsl:value-of select="$classname"/> type.
2133     * @see BigDecimal#valueOf(long)
2134     */
2135    public static <xsl:value-of select="$classname"/> valueOf (long val)
2136    {
2137        if (val &lt; MIN_VALUE_SCALED)
2138        {
2139           throw new ArgumentMinValueViolationException(
2140              TYPE_NAME, new <xsl:value-of select="$backing-class"/>(val), MIN_VALUE,
2141              <xsl:value-of select="$classname"/>.class);
2142        }
2143        if (val &gt; MAX_VALUE_SCALED)
2144        {
2145           throw new ArgumentMaxValueViolationException(
2146              TYPE_NAME, new <xsl:value-of select="$backing-class"/>(val), MAX_VALUE,
2147              <xsl:value-of select="$classname"/>.class);
2148        }
2149        return new <xsl:value-of select="$classname"/>(val * DECIMAL_SCALE);
2150    }
2151
2152    /**
2153     * Translates a &lt;tt>long&lt;/tt> with the given scale into a
2154     * &lt;tt><xsl:value-of select="$classname"/>&lt;/tt>.
2155     *
2156     * @param unscaledVal the unscaled value.
2157     * @param scale the scale to be applied.
2158     * @return a &lt;tt><xsl:value-of select="$classname"/>&lt;/tt> whose value is
2159     *         &lt;tt>(unscaledVal &amp;times; 10&lt;sup>-scale&lt;/sup>)&lt;/tt>.
2160     * @throws ArgumentMalformedException If the given
2161     *   long and scale violates the restriction
2162     *   of the <xsl:value-of select="$classname"/> type.
2163     * @see BigDecimal#valueOf(long, int)
2164     */
2165    public static <xsl:value-of select="$classname"/> valueOf (long unscaledVal, int scale)
2166    {
2167        return valueOf(BigDecimal.valueOf(unscaledVal, scale));
2168    }
2169
2170    /**
2171     * Returns the unscaled long value of this <xsl:value-of select="$classname"/>.
2172     * The actual value is the returned value / DECIMAL_SCALE.
2173     * @return The unscaled long value of this <xsl:value-of select="$classname"/>.
2174     */
2175    public long unscaledLongValue ()
2176    {
2177        return mUnscaled;
2178    }
2179
2180    /**
2181     * Returns the String representation of this <xsl:value-of select="$classname"/>.
2182     * The implementation does not apply any localization rules.
2183     * @return The String representation of this <xsl:value-of select="$classname"/>.
2184     * @see BigDecimal#toString()
2185     */
2186    public String toString ()
2187    {
2188        if (mStringRepresentation == null)
2189        {
2190            mStringRepresentation
2191                = NumberUtil.toString(mUnscaled, SCALE);
2192        }
2193       return mStringRepresentation;
2194    }
2195
2196    /**
2197     * Returns the BigDecimal representation of this <xsl:value-of select="$classname"/>.
2198     * @return The BigDecimal representation of this <xsl:value-of select="$classname"/>.
2199     */
2200    public BigDecimal toBigDecimal ()
2201    {
2202        if (mBigDecimal == null)
2203        {
2204            mBigDecimal = BigDecimal.valueOf(mUnscaled, SCALE);
2205        }
2206       return mBigDecimal;
2207    }
2208
2209    /** {@inheritDoc} */
2210    public boolean equals (Object obj)
2211    {
2212        return (this == obj)
2213          || (obj instanceof <xsl:value-of select="$classname"/>
2214             &amp;&amp; ((<xsl:value-of select="$classname"/>) obj).mUnscaled == mUnscaled);
2215    }
2216
2217    /** {@inheritDoc} */
2218    public int hashCode ()
2219    {
2220        return HashCodeUtil.hash(HashCodeUtil.SEED, mUnscaled);
2221    }
2222
2223    /** {@inheritDoc} */
2224    public double doubleValue ()
2225    {
2226        return toBigDecimal().doubleValue();
2227    }
2228
2229    /** {@inheritDoc} */
2230    public float floatValue ()
2231    {
2232        return toBigDecimal().floatValue();
2233    }
2234
2235    /** {@inheritDoc} */
2236    public int intValue ()
2237    {
2238        return toBigDecimal().intValue();
2239    }
2240
2241    /** {@inheritDoc} */
2242    public long longValue ()
2243    {
2244        return toBigDecimal().longValue();
2245    }
2246
2247    /** {@inheritDoc} */
2248    public int compareTo (Object o)
2249    {
2250        final long thisVal = mUnscaled;
2251        final long anotherVal = ((<xsl:value-of select="$classname"/>) o).mUnscaled;
2252        return (thisVal &lt; anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1));
2253    }
2254}
2255</xsl:template> <!-- fix-point-number -->
2256
2257<!-- TODO: We might map this to Long on DB??? as an option? -->
2258<xsl:template name="fix-point-user-type">
2259   <xsl:param name="classname"/>
2260   <xsl:param name="type-classname"/>
2261   <xsl:param name="package"/>
2262   <xsl:variable name="classname-constant">TYPE_NAME</xsl:variable>
2263   <xsl:call-template name="java-copyright-header"/>
2264package <xsl:value-of select="$package"/>;
2265
2266
2267import java.math.BigDecimal;
2268
2269/**
2270 * Hibernate user type for the  <xsl:value-of select="$type-classname"/>.
2271 *
2272 * @author generated via stylesheet
2273 */
2274public final class <xsl:value-of select="$classname"/>
2275      extends org.jcoderz.commons.util.BigDecimalUserTypeBase
2276{
2277   /** The serialVersionUID used for serialization. */
2278   static final long serialVersionUID = 1;
2279
2280   /**
2281    * Hibernate <tt><xsl:value-of select="$type-classname"/></tt> type as mapped
2282    * from this UserType.
2283    * @return this UserType as org.hibernate.type.Type.
2284    */
2285   public static org.hibernate.type.Type getType ()
2286   {
2287      return TypeHolder.TYPE;
2288   }
2289
2290  /**
2291   * Creates a <xsl:value-of select="$type-classname"/> from its numeric
2292   * BigDecimal database representation.
2293   * @param value a BigDecimal holding the database representation of the
2294   *    <xsl:value-of select="$type-classname"/>.
2295   * @return a <xsl:value-of select="$type-classname"/> representing the
2296   *    given BigDecimal.
2297   * @see <xsl:value-of select="$type-classname"/>#valueOf(BigDecimal)
2298   */
2299  public Object fromBigDecimal(BigDecimal value)
2300  {
2301    return <xsl:value-of select="$type-classname"/>.valueOf(value);
2302  }
2303
2304  /**
2305   * Converts the <xsl:value-of select="$type-classname"/> to its numeric
2306   * BigDecimal database representation.
2307   * @param value the <xsl:value-of select="$type-classname"/> to be
2308   *    converted.
2309   * @return a BigDecimal representing the
2310   *    given <xsl:value-of select="$type-classname"/>.
2311   * @see <xsl:value-of select="$type-classname"/>#toBigDecimal()
2312   */
2313  public BigDecimal toBigDecimal(Object value)
2314  {
2315    return ((<xsl:value-of select="$type-classname"/>) value).toBigDecimal();
2316  }
2317
2318  /**
2319   * @return <xsl:value-of select="$type-classname"/>.class as the supported class of this user type.
2320   */
2321  public Class returnedClass()
2322  {
2323    return <xsl:value-of select="$type-classname"/>.class;
2324  }
2325
2326
2327   /**
2328    * Class to lazy initialize the Hibernate Type adapter.
2329    */
2330   private static class TypeHolder
2331   {
2332      private static final org.hibernate.type.Type TYPE
2333        = new org.hibernate.type.CustomType(<xsl:value-of select="$classname"/>.class, null);
2334   }
2335}
2336</xsl:template>
2337
2338<xsl:template name="repeat">
2339   <xsl:param name="pattern"/>
2340   <xsl:param name="count"/>
2341  <xsl:if test="$count > 0">
2342    <xsl:call-template name="repeat">
2343      <xsl:with-param name="pattern" select="$pattern"/>
2344      <xsl:with-param name="count" select="$count - 1"/>
2345    </xsl:call-template>
2346    <xsl:value-of select="$pattern"/>
2347  </xsl:if>
2348</xsl:template>
2349
2350<xsl:template name="power10precise">
2351   <xsl:param name="number"/>
2352   <xsl:param name="exp"/>
2353  <xsl:choose>
2354    <xsl:when test="$exp > 0">
2355      <xsl:call-template name="power10precise">
2356        <xsl:with-param name="number">
2357          <xsl:call-template name="times10precize">
2358            <xsl:with-param name="number" select="$number"/>
2359          </xsl:call-template>
2360        </xsl:with-param>
2361        <xsl:with-param name="exp" select="$exp - 1"/>
2362      </xsl:call-template>
2363    </xsl:when>
2364    <xsl:otherwise><xsl:value-of select="$number"/></xsl:otherwise>
2365    </xsl:choose>
2366</xsl:template>
2367
2368<xsl:template name="times10precize">
2369   <xsl:param name="number"/>
2370  <xsl:choose>
2371    <xsl:when test="contains($number, '.')">
2372      <xsl:variable name="pos" select="string-length(substring-before($number, '.'))"/>
2373      <xsl:value-of
2374        select="concat(substring($number, 0, $pos + 1), substring($number, $pos + 2, 1))"/>
2375      <xsl:if test="string-length(substring($number, $pos + 3)) &gt; 0"
2376        >.<xsl:value-of select="substring($number, $pos + 3)"/>
2377      </xsl:if>
2378    </xsl:when>
2379    <xsl:otherwise><xsl:value-of select="$number"/>0</xsl:otherwise>
2380  </xsl:choose>
2381</xsl:template>
2382
2383<!-- ===============================================================
2384     Outputs the jCoderZ Java copyright header
2385     =============================================================== -->
2386<xsl:template name="java-copyright-header" priority="-1">
2387<xsl:text>/*
2388 * Generated source file, not in CVS/SVN repository
2389 */</xsl:text>
2390</xsl:template>
2391
2392<!--
2393  ** This template modifies an EJB-QL query replacing the abstract
2394  ** schema name with the given parameter $schemaName.
2395  ** The abstract schema name in the query is identified as the
2396  ** string between the 'FROM' and 'AS' keywords, like
2397  ** in "SELECT OBJECT(a) FROM abstractSchema AS a".
2398  -->
2399<xsl:template name="replace-schema-in-query">
2400   <xsl:param name="schemaName"/>
2401   <xsl:param name="query"/>
2402   <xsl:variable name="queryLowerCase">
2403      <xsl:call-template name="toLowerCase">
2404         <xsl:with-param name="s" select="$query"/>
2405      </xsl:call-template>
2406   </xsl:variable>
2407   <xsl:variable name="beforeFromIndex">
2408      <xsl:value-of select="string-length(substring-before($queryLowerCase, 'from'))"/>
2409   </xsl:variable>
2410   <xsl:variable name="beforeAsIndex">
2411      <xsl:value-of select="string-length(substring-before($queryLowerCase, 'as'))"/>
2412   </xsl:variable>
2413   <xsl:value-of select="substring($query, 1, $beforeFromIndex)"/> FROM <xsl:value-of select="$schemaName"/> <xsl:value-of select="substring($query, $beforeAsIndex)"/>
2414</xsl:template>
2415
2416<!--
2417  ** This template extracts the package from a fully qualified class
2418  ** name.
2419  -->
2420<xsl:template name="package-from-class">
2421   <xsl:param name="class"/>
2422   <xsl:param name="count" select="0"/>
2423   <xsl:if test="contains($class, '.')">
2424      <xsl:if test="$count &gt; 0">
2425         <xsl:text>.</xsl:text>
2426      </xsl:if>
2427      <xsl:value-of select="substring-before($class, '.')"/>
2428      <xsl:call-template name="package-from-class">
2429         <xsl:with-param name="class" select="substring-after($class, '.')"/>
2430         <xsl:with-param name="count" select="$count + 1"/>
2431      </xsl:call-template>
2432   </xsl:if>
2433</xsl:template>
2434
2435<!--
2436  ** Generate complex javadoc structure that might contain xdoclet tags
2437  ** as sublelements / attributes
2438  -->
2439<xsl:template name="generate-xdoclet">
2440  <xsl:param name="doc-text"/>
2441  <xsl:param name="indent"/>
2442  <xsl:apply-templates select="$doc-text" mode="generate-javadoc-">
2443    <xsl:with-param name="indent" select="$indent"/>
2444  </xsl:apply-templates>
2445</xsl:template>
2446
2447<xsl:template match="*" mode="generate-javadoc-">
2448  <xsl:param name="indent"/>
2449  <xsl:apply-templates mode="generate-javadoc-content"
2450    select="*|comment()|processing-instruction()">
2451      <xsl:with-param name="indent" select="$indent"/>
2452  </xsl:apply-templates>
2453</xsl:template>
2454
2455<xsl:template match="node()" mode="generate-javadoc-content">
2456  <xsl:param name="indent"/>
2457<xsl:text>
2458</xsl:text><xsl:value-of select="$indent"/>* @<xsl:value-of select="name(.)" />
2459  <xsl:apply-templates
2460    select="@*" mode="generate-javadoc-attributes"/>
2461</xsl:template>
2462
2463<xsl:template match="@*" mode="generate-javadoc-attributes">
2464 <xsl:text> </xsl:text><xsl:value-of select="name(.)"/><xsl:if
2465   test="string-length(.) != 0">="<xsl:value-of select="." />"</xsl:if>
2466</xsl:template>
2467
2468</xsl:stylesheet>
Note: See TracBrowser for help on using the browser.