KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > generator > lib > CommonHelper


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.generator.lib;
25
26 import org.objectweb.jorm.api.PException;
27 import org.objectweb.jorm.metainfo.api.Class;
28 import org.objectweb.jorm.metainfo.api.Package;
29 import org.objectweb.jorm.metainfo.api.ClassRef;
30 import org.objectweb.jorm.metainfo.api.CompositeName;
31 import org.objectweb.jorm.metainfo.api.FieldName;
32 import org.objectweb.jorm.metainfo.api.GenClassRef;
33 import org.objectweb.jorm.metainfo.api.Manager;
34 import org.objectweb.jorm.metainfo.api.MetaObject;
35 import org.objectweb.jorm.metainfo.api.NameDef;
36 import org.objectweb.jorm.metainfo.api.NameRef;
37 import org.objectweb.jorm.metainfo.api.PrimitiveElement;
38 import org.objectweb.jorm.metainfo.api.Reference;
39 import org.objectweb.jorm.metainfo.api.TypedElement;
40 import org.objectweb.jorm.metainfo.api.Mapping;
41 import org.objectweb.jorm.metainfo.api.ReferenceMapping;
42 import org.objectweb.jorm.metainfo.api.PrimitiveElementMapping;
43 import org.objectweb.jorm.type.api.PExceptionTyping;
44 import org.objectweb.jorm.type.api.PType;
45 import org.objectweb.jorm.type.api.PTypeSpace;
46 import org.objectweb.jorm.util.api.Loggable;
47 import org.objectweb.jorm.compiler.api.PExceptionCompiler;
48 import org.objectweb.util.monolog.api.BasicLevel;
49 import org.objectweb.util.monolog.api.Logger;
50 import org.objectweb.util.monolog.api.LoggerFactory;
51
52 import java.util.ArrayList JavaDoc;
53 import java.util.Iterator JavaDoc;
54 import java.util.Map JavaDoc;
55 import java.util.Collection JavaDoc;
56 import java.util.HashMap JavaDoc;
57
58 /**
59  * This class is a helper for the generation. All methods provided in this
60  * class are shortcut or facilties which simplify the generation.
61  * @author Sebastien Chassande-Barrioz
62  */

63 public class CommonHelper implements Loggable {
64
65     protected LoggerFactory loggerFactory = null;
66     protected Logger logger = null;
67     protected boolean debug = false;
68
69     public final static PType[] PN_GET_TYPES = {
70         PTypeSpace.BYTE,
71         PTypeSpace.CHAR,
72         PTypeSpace.DATE,
73         PTypeSpace.INT,
74         PTypeSpace.LONG,
75         PTypeSpace.OBJBYTE,
76         PTypeSpace.OBJCHAR,
77         PTypeSpace.OBJINT,
78         PTypeSpace.OBJLONG,
79         PTypeSpace.OBJSHORT,
80         PTypeSpace.SHORT,
81         PTypeSpace.STRING,
82         PTypeSpace.BYTEARRAY,
83         PTypeSpace.CHARARRAY,
84         PTypeSpace.BIGDECIMAL,
85         PTypeSpace.BIGINTEGER
86     };
87
88     /**
89      * This methods returns the word specified in parameter with the first
90      * character in upper-case. This method is used for the generation of
91      * accessor methods.
92      * @param word The String which the first character must be set in
93      * upper-case
94      * @return The String whith the first character in upper-case
95      */

96     public String JavaDoc upperFL(String JavaDoc word) {
97         if (word != null && word.length() > 0)
98             return word.substring(0, 1).toUpperCase() + word.substring(1);
99         else
100             return word;
101     }
102
103     /**
104      * This method allow to retrieve a primitive element from th meta object mo.
105      * if this metaobject is a primitive element, it is returned itself.
106      * if this meta object is a single namedef the primitive element composing the namedef is returned.
107      * This method does not work with composite pname.
108      * @param mo the meta object embeding the primitive element
109      * @param c the class embeding the meta object mo
110      * @return the primitive element
111      */

112
113     public PrimitiveElement getPrimitiveElement(Object JavaDoc mo,
114                                                 Class JavaDoc c,
115                                                 boolean canBeFieldName)
116             throws PException {
117         if (mo instanceof PrimitiveElement)
118             return (PrimitiveElement) mo;
119         else if (mo instanceof NameDef) {
120             NameDef nd = ((NameDef) mo);
121             if (nd.isFieldName())
122                 return (PrimitiveElement) (c.getTypedElement(nd.getFieldName()));
123             else
124                 throw new PException("This is composite name !" + mo);
125         } else
126             throw new PException(
127                     "impossible to obtain a PrimitiveElement with " + mo);
128     }
129
130     /**
131      * This method tests if a class contains a reference. A reference can be a
132      * designs a generic class or a class.
133      * @param co The meta object Class
134      * @return true if the class contains a reference.
135      */

136     public boolean containsReference(Class JavaDoc co) {
137         Iterator JavaDoc it = co.getAllFields().iterator();
138         while (it.hasNext()) {
139             if (it.next() instanceof Reference)
140                 return true;
141         }
142         return false;
143     }
144
145     /**
146      * This method tests if a class contains a composite reference. A reference can be a
147      * designs a generic class or a class.
148      * @param mo The meta object Mapping
149      * @return true if the class contains the namedef for the composite reference.
150      *
151      public boolean containsCompositeReference(Mapping mo) throws Exception {
152      Iterator it = mo.getClassMapping().getReferenceMappings().iterator();
153      while (it.hasNext()) {
154      ReferenceMapping rm = (ReferenceMapping) it.next();
155      if (((NameDef) rm.getLinkedMO()).isNameRef()) {
156      return true;
157      }
158      }
159      return false;
160      }
161      */

162
163     /**
164      * This method return a list containing the nameref implied in composite reference field
165      * @param mo The meta object Mapping to be analysed.
166      * @return an ArrayList which contains all nameref related to reference fields.
167      */

168
169     public ArrayList JavaDoc getCompositeReferences(Mapping mo) throws Exception JavaDoc {
170         ArrayList JavaDoc res = new ArrayList JavaDoc();
171         Iterator JavaDoc it = mo.getClassMapping().getReferenceMappings().iterator();
172         while (it.hasNext()) {
173             ReferenceMapping rm = (ReferenceMapping) it.next();
174             if (((NameDef) rm.getLinkedMO()).isNameRef()) {
175                 res.add(((NameDef) rm.getLinkedMO()).getNameRef());
176             }
177         }
178         return res;
179     }
180
181     /**
182      * This method return a SET containing the full name of compositename implied
183      * in the reference field of the class.
184      * @param moc The meta object Class
185      * @param cp the compiler parameter
186      * @return an iterator which contains all pname related to the Reference.
187      *
188      public Set getCompositeNames(Class moc, JormCompilerParameter cp) throws Exception {
189
190      Set res = new HashSet();
191      NameDef nd = moc.getNameDef(cp.projectName);
192      if (nd.isNameRef()) {
193      String cn = nd.getNameRef().getCompositeName().getFQName();
194      if (res.add(cn)) {
195      res.add(cn + "PNG");
196      }
197      }
198      Iterator it = moc.iterateAllField();
199      while (it.hasNext()) {
200      TypedElement te = (TypedElement) it.next();
201      if (te instanceof Reference) {
202      nd = ((Reference) te).getRefNameDef(cp.projectName);
203      if (nd.isNameRef()) {
204      res.add(nd.getNameRef().getCompositeName().getFQName()
205      + "PNG");
206      }
207      }
208      }
209      return res;
210      }
211      */

212
213     /**
214      * This method retrieve the package name for the meta object passed as parameter.
215      * The meta object must be a child of a Package MetaObject.
216      * @param mo The meta object
217      * @return the name of the package if it exist, null otherwise.
218      */

219     public String JavaDoc getCNPackageName(MetaObject mo) {
220         if (mo instanceof Manager) return null;
221         while (!(mo instanceof Package JavaDoc)) {
222             mo = mo.getParent();
223         }
224         if (mo instanceof Package JavaDoc)
225             return ((Package JavaDoc) mo).getName();
226         else
227             return null;
228     }
229
230     /**
231      * This method tests if a class contains a serialized field.
232      * @param co The meta object Class
233      * @return true if the class contains a serialized field.
234      */

235     public boolean containsSerializedField(Class JavaDoc co) {
236         Iterator JavaDoc it = co.getAllFields().iterator();
237         while (it.hasNext()) {
238             if (((TypedElement) it.next()).getType().getTypeCode()
239                     == PType.TYPECODE_SERIALIZED)
240                 return true;
241         }
242         return false;
243     }
244
245     /**
246      * This method tests if a class contains a char[] field.
247      * @param m The meta object Mapping
248      * @return true if the class contains a serialized field.
249      */

250     public boolean containsCharArrayField(Mapping m) {
251         Class JavaDoc co = (Class JavaDoc) m.getClassMapping().getLinkedMO();
252         Iterator JavaDoc it = co.getAllFields().iterator();
253         while (it.hasNext()) {
254             TypedElement te = (TypedElement) it.next();
255             if (te == null) {
256                 throw new NullPointerException JavaDoc(
257                         "A field of the class '" + co.getFQName() + "' is null.");
258             }
259             if (te.getType() == null) {
260                 throw new NullPointerException JavaDoc(
261                         "the type of field '" + te.getName()
262                         + "' of the class '" + co.getFQName() + "' is null.");
263             }
264             if (te.getType().getTypeCode() == PType.TYPECODE_CHARARRAY)
265                 return true;
266         }
267         it = co.getAllHiddenFields().iterator();
268         while (it.hasNext()) {
269             if (((TypedElement) it.next()).getType().getTypeCode()
270                     == PType.TYPECODE_CHARARRAY)
271                 return true;
272         }
273         NameDef nd = (NameDef)
274                 m.getClassMapping().getIdentifierMapping().getLinkedMO();
275         if (nd.isNameRef()) {
276             it = nd.getNameRef().getCompositeName().getAllFields().iterator();
277             while (it.hasNext()) {
278                 if (((TypedElement) it.next()).getType().getTypeCode()
279                         == PType.TYPECODE_CHARARRAY)
280                     return true;
281             }
282         }
283         return false;
284     }
285
286     /**
287      * This method tests if a class contains a serialized field.
288      * @param co The meta object Class
289      * @return true if the class contains a serialized field.
290      */

291     public boolean containsFieldWhichCanBeNull(Class JavaDoc co) throws PException {
292         for (Iterator JavaDoc it = co.getAllFields().iterator(); it.hasNext();) {
293             if (canBeNullValue(((TypedElement) it.next()).getType()))
294                 return true;
295         }
296         for (Iterator JavaDoc it = co.getAllHiddenFields().iterator(); it.hasNext();) {
297             if (canBeNullValue(((TypedElement) it.next()).getType()))
298                 return true;
299         }
300         return false;
301     }
302
303     /**
304      * This method tests if a String is valid. A String is valid if it is not
305      * null and if the length is greater than 0.
306      * @param str The string which must be checked
307      * @return true if the string is valid
308      */

309     public boolean isValidString(String JavaDoc str) {
310         return str != null && str.length() > 0;
311     }
312
313     /**
314      * This method tests if a class references to a generic class.
315      * @param co The meta object Class
316      * @return true if the class references a generic class.
317      */

318     public boolean containsGenClassRef(Class JavaDoc co) {
319         Iterator JavaDoc it = co.getAllFields().iterator();
320         while (it.hasNext()) {
321             if (it.next() instanceof GenClassRef)
322                 return true;
323         }
324         return false;
325     }
326
327     /**
328      * This method checks if the NameDef is composed by several fields.
329      * @param nd the meta object NameDef which must be tested
330      * @return true if the NameDef is composed by several fields.
331      */

332     public boolean isCompositeName(NameDef nd) {
333         return nd.isNameRef();
334     }
335
336     /**
337      * This method provides an iterator over the map entries which associated
338      * a composite field name (key) to the class field name (value). These map
339      * elements describe the projection of a name definition.
340      *
341      * @param mo the meta object NameDef which must be used
342      */

343
344     public Collection JavaDoc getCompositeNameDefEntries(NameDef mo) throws Exception JavaDoc {
345         if (mo == null)
346             throw new PException("Null NameDef Parameter");
347         else if (mo.isNameRef())
348             return mo.getNameRef().getProjection().entrySet();
349         else if (mo.isFieldName())
350             throw new PException("The NameDef is a FieldName ("
351                                  + mo.getFieldName() + ")");
352         else if (mo.isSystem())
353             throw new PException("System identifier unmanaged");
354         else
355             throw new PException("Unknown NameDef: " + mo);
356     }
357
358     public String JavaDoc getFQNOfCompositeName(Object JavaDoc o) {
359         CompositeName cn = null;
360         if (o instanceof CompositeName)
361             cn = (CompositeName) o;
362         else if (o instanceof NameDef)
363             cn = ((NameDef) o).getNameRef().getCompositeName();
364         else
365             return "";
366
367         String JavaDoc packName = ((Package JavaDoc) cn.getParent()).getName();
368         if (packName == null || packName.length() == 0) {
369             return cn.getName();
370         } else {
371             return packName + "." + cn.getName();
372         }
373     }
374
375     /**
376      * This method allows to know if a class represented by its Class meta
377      * object, contains a composite PName. This method analyzes the NameDef of
378      * the @c class parameter and the NameDef of all reference fields.
379      * @param mo the Mapping which must analyzed
380      */

381     public boolean containsCompositeName(Mapping mo) {
382         if (mo == null) {
383             return false;
384         }
385         NameDef nd = (NameDef)
386                 mo.getClassMapping().getIdentifierMapping().getLinkedMO();
387         if (nd.isNameRef())
388             return true;
389         if (logger != null)
390             logger.log(BasicLevel.DEBUG, "PName class has a single field");
391         Iterator JavaDoc it = mo.getClassMapping().getReferenceMappings().iterator();
392         while (it.hasNext()) {
393             ReferenceMapping rm = (ReferenceMapping) it.next();
394             nd = (NameDef) rm.getLinkedMO();
395             if (nd.isNameRef()) {
396                 return true;
397             }
398         }
399         return false;
400     }
401
402     /**
403      * This method checks if the parameter is an instance of
404      * org.objectweb.jorm.metainfo.api.Class
405      * @param o the object which the type must be checked
406      * @return true the parameter is an instance of Class
407      */

408     public boolean isClass(Object JavaDoc o) {
409         return o instanceof Class JavaDoc;
410     }
411
412     /**
413      * This method checks if the parameter is an instance of
414      * org.objectweb.jorm.metainfo.api.Reference
415      * @param te the object which the type must be checked
416      * @return true the parameter is an instance of Reference
417      */

418     public boolean isReference(TypedElement te) {
419         return te instanceof Reference;
420     }
421
422     /**
423      * This method checks if the parameter is an instance of
424      * org.objectweb.jorm.metainfo.api.ClassRef
425      * @param te the object which the type must be checked
426      * @return true the parameter is an instance of ClassRef
427      */

428     public boolean isClassRef(TypedElement te) {
429         return te instanceof ClassRef;
430     }
431
432     /**
433      * This method checks if the parameter is an instance of
434      * org.objectweb.jorm.metainfo.api.GenClassRef
435      * @param te the object which the type must be checked
436      * @return true the parameter is an instance of GenClassRef
437      */

438     public boolean isGenClassRef(TypedElement te) {
439         return te instanceof GenClassRef;
440     }
441
442     /**
443      * This method checks if the parameter is an instance of
444      * org.objectweb.jorm.metainfo.api.PrimitiveElement
445      * @param o the object which the type must be checked
446      * @return true the parameter is an instance of PrimitiveElement
447      */

448     public boolean isPrimitiveElement(Object JavaDoc o) {
449         return o instanceof PrimitiveElement;
450     }
451
452     /**
453      * @param val1 the variable name of the first element
454      * @param val2 the variable name of the second element
455      * @param type the type of the elements
456      * @return a String comparing two primitive element
457      */

458     public String JavaDoc comparePE(String JavaDoc val1, String JavaDoc val2, PType type) {
459         if (isObjectType(type)) {
460             return "((" + val1 + " == null && " + val2 + " == null) || ("
461                     + val1 + " !=null && " + val1 + ".equals(" + val2 + ")))";
462         } else {
463             return val1 + " == " + val2;
464         }
465     }
466
467     public boolean isObjectType(PType type) {
468         switch(type.getTypeCode()) {
469         case PType.TYPECODE_OBJBOOLEAN:
470         case PType.TYPECODE_OBJBYTE:
471         case PType.TYPECODE_OBJCHAR:
472         case PType.TYPECODE_DATE:
473         case PType.TYPECODE_CHARARRAY:
474         case PType.TYPECODE_BYTEARRAY:
475         case PType.TYPECODE_OBJSHORT:
476         case PType.TYPECODE_OBJINT:
477         case PType.TYPECODE_OBJLONG:
478         case PType.TYPECODE_OBJFLOAT:
479         case PType.TYPECODE_OBJDOUBLE:
480         case PType.TYPECODE_STRING:
481         case PType.TYPECODE_SERIALIZED:
482         case PType.TYPECODE_BIGDECIMAL:
483         case PType.TYPECODE_BIGINTEGER:
484             return true;
485         case PType.TYPECODE_BOOLEAN:
486         case PType.TYPECODE_BYTE:
487         case PType.TYPECODE_CHAR:
488         case PType.TYPECODE_SHORT:
489         case PType.TYPECODE_INT:
490         case PType.TYPECODE_LONG:
491         case PType.TYPECODE_FLOAT:
492         case PType.TYPECODE_DOUBLE:
493         default:
494             return false;
495         }
496     }
497
498     public boolean isNull(Object JavaDoc o) {
499         return o == null;
500     }
501
502     /**
503      * This method checks if the parameter is an instance of
504      * org.objectweb.jorm.metainfo.api.PrimitiveElement
505      * @param pe the object which the type must be checked
506      * @return true the parameter is an instance of PrimitiveElement
507      */

508     public boolean isHiddenField(PrimitiveElement pe) {
509         return ((Class JavaDoc) pe.getParent()).getHiddenField(pe.getName()) != null;
510     }
511
512     /**
513      * This method checks if the parameter is an instance of
514      * org.objectweb.jorm.metainfo.api.FieldName
515      * @param o the object which the type must be checked
516      * @return true the parameter is an instance of FieldName
517      */

518     public boolean isFieldName(Object JavaDoc o) {
519         return o instanceof FieldName;
520     }
521
522     /**
523      * This method checks if the parameter designs a Serialized field.
524      * @param o the object which the type must be checked
525      * @return true the parameter is an instance of PrimitiveElement and if its
526      * PType is SERIALIZED.
527      */

528     public boolean isSerializedField(Object JavaDoc o) {
529         return (o instanceof PrimitiveElement)
530                 && (((PrimitiveElement) o).getType().getTypeCode()
531                 == PType.TYPECODE_SERIALIZED);
532     }
533
534     /**
535      * This method checks if the PType parameter designs a Serialized type.
536      * @param t the PType object which the type must be checked
537      * @return true the parameter is a PType.SERIALIZED.
538      */

539     public boolean isSerializedType(PType t) {
540         return t.getTypeCode() == PType.TYPECODE_SERIALIZED;
541     }
542
543     /**
544      * This method checks if the PType parameter designs a Serialized type.
545      * @param t the PType object which the type must be checked
546      * @return true the parameter is a PType.SERIALIZED.
547      */

548     public boolean isCharArrayType(PType t) {
549         return t.getTypeCode() == PType.TYPECODE_CHARARRAY;
550     }
551
552     public PrimitiveElementMapping[] buildPemList(Collection JavaDoc pemcoll) {
553         return (PrimitiveElementMapping[]) pemcoll.toArray(new PrimitiveElementMapping[0]);
554     }
555
556     public int getPemPos(PrimitiveElementMapping[] pemlist, PrimitiveElement pe) throws PExceptionCompiler {
557         for (int i = 0; i < pemlist.length; i++) {
558             if (pemlist[i].getLinkedMO() == pe) {
559                 return i + 1;
560             }
561         }
562         throw new PExceptionCompiler("Cannot find a mapping for field: " + pe.getName());
563     }
564
565     public PrimitiveElement getPrimitiveElement(String JavaDoc fn, Class JavaDoc cl) throws PExceptionCompiler {
566         TypedElement res = cl.getTypedElement(fn);
567         if (res == null) {
568             throw new PExceptionCompiler("NameDef with field name does not correspond to any field - searched field: " + fn);
569         }
570         if (!(res instanceof PrimitiveElement)) {
571             throw new PExceptionCompiler("NameDef with field name does not correspond to any primitive field - searched field: " + fn);
572         }
573         return (PrimitiveElement) res;
574     }
575
576     public NameDef getRefNameDef(Mapping mo, Reference field) throws PException {
577         if (field == null) {
578             return null;
579         }
580         if (mo == null) {
581             return null;
582         }
583         ReferenceMapping rm = mo.getClassMapping().getReferenceMapping(field.getName());
584         NameDef nd = null;
585         if (rm != null) {
586             nd = (NameDef) rm.getLinkedMO();
587         }
588         if (nd == null) {
589             throw new PException("No NameDef found for the reference field '"
590                 + field.getName() +"' of the persistent class '"
591                 + ((Class JavaDoc) mo.getClassMapping().getLinkedMO()).getFQName()
592                 + "'.");
593         }
594         return nd;
595     }
596
597     public String JavaDoc getTupleDecoding(String JavaDoc fn, PType pt, String JavaDoc value)
598         throws PException {
599         if (pt == null) {
600             throw new PException("Unauthorized null PType");
601         }
602         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
603         sb.append(fn);
604         sb.append("Pnc.decode");
605         sb.append(pt.getCodingName());
606         sb.append("((");
607         sb.append(pt.getJavaName());
608         sb.append(") ");
609         sb.append(value);
610         sb.append(")");
611         return sb.toString();
612     }
613
614     /**
615      * Builds the string representing the expression to decode a PName wrt the
616      * relevant tuple field having the right type.
617      * @param tmpv The temporary variable that holds the tuple field value.
618      * @param fn The name of the involved application field.
619      * @param tuplevn The variable name that holds the corresponding tuple.
620      * @param pt The PType of the associated field.
621      * @param pos The position of the tuple field into this tuple.
622      * @return The string representing the getter expression.
623      */

624     public String JavaDoc getTupleDecoding(String JavaDoc tmpv, String JavaDoc fn, String JavaDoc tuplevn, PType pt, int pos)
625             throws PException {
626         if (pt == null)
627             throw new PException("Unauthorized null PType");
628         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
629         switch (pt.getTypeCode()) {
630         case PType.TYPECODE_BYTE:
631         case PType.TYPECODE_CHAR:
632         case PType.TYPECODE_SHORT:
633         case PType.TYPECODE_INT:
634         case PType.TYPECODE_LONG:
635             sb.append(fn);
636             sb.append("Pnc.decode");
637             sb.append(pt.getCodingName());
638             sb.append("(");
639             sb.append(getTupleGetter(tuplevn, pt, pos) );
640             sb.append(")");
641             return sb.toString();
642         case PType.TYPECODE_OBJBYTE:
643         case PType.TYPECODE_OBJCHAR:
644         case PType.TYPECODE_OBJSHORT:
645         case PType.TYPECODE_OBJINT:
646         case PType.TYPECODE_OBJLONG:
647         case PType.TYPECODE_STRING:
648         case PType.TYPECODE_DATE:
649         case PType.TYPECODE_BIGDECIMAL:
650         case PType.TYPECODE_BIGINTEGER:
651             sb.append("(((");
652             sb.append(tmpv);
653             sb.append(" = ");
654             sb.append(getTupleGetter(tuplevn, pt, pos) );
655             sb.append(") == null) ? ");
656             sb.append(fn);
657             sb.append("Pnc.getNull() : ");
658             sb.append(fn);
659             sb.append("Pnc.decode");
660             sb.append(pt.getCodingName());
661             sb.append("((");
662             sb.append(pt.getJavaName());
663             sb.append(") ");
664             sb.append(tmpv);
665             sb.append("))");
666             return sb.toString();
667         }
668         throw new PExceptionTyping("Unauthorized PType " + pt);
669     }
670
671     /**
672      * Builds the string representing the expression to access the relevant
673      * tuple field having the right type.
674      * @param tuplevn The variable name that holds the corresponding tuple.
675      * @param pt The PType of the associated field.
676      * @param pos The position of the tuple field into this tuple.
677      * @return The string representing the getter expression.
678      */

679     public String JavaDoc getTupleGetter(String JavaDoc tuplevn, PType pt, int pos)
680             throws PException {
681         if (pt == null)
682             throw new PException("Unauthorized null PType");
683         switch (pt.getTypeCode()) {
684         case PType.TYPECODE_BOOLEAN:
685             return tuplevn + ".getBoolean(" + pos + ")";
686         case PType.TYPECODE_OBJBOOLEAN:
687             return "(new Boolean(" + tuplevn + ".getBoolean(" + pos + ")))";
688         case PType.TYPECODE_BYTE:
689             return tuplevn + ".getByte(" + pos + ")";
690         case PType.TYPECODE_OBJBYTE:
691             return "(new Byte(" + tuplevn + ".getByte(" + pos + ")))";
692         case PType.TYPECODE_CHAR:
693             return tuplevn + ".getChar(" + pos + ")";
694         case PType.TYPECODE_OBJCHAR:
695             return "(new Character(" + tuplevn + ".getChar(" + pos + ")))";
696         case PType.TYPECODE_DATE:
697             return tuplevn + ".getDate(" + pos + ")";
698         case PType.TYPECODE_CHARARRAY:
699             return tuplevn + ".getCharArray(" + pos + ")";
700         case PType.TYPECODE_BYTEARRAY:
701             return tuplevn + ".getByteArray(" + pos + ")";
702         case PType.TYPECODE_SHORT:
703             return tuplevn + ".getShort(" + pos + ")";
704         case PType.TYPECODE_OBJSHORT:
705             return "(new Short(" + tuplevn + ".getShort(" + pos + ")))";
706         case PType.TYPECODE_INT:
707             return tuplevn + ".getInt(" + pos + ")";
708         case PType.TYPECODE_OBJINT:
709             return "(new Integer( " + tuplevn + ".getInt(" + pos + ")))";
710         case PType.TYPECODE_LONG:
711             return tuplevn + ".getLong(" + pos + ")";
712         case PType.TYPECODE_OBJLONG:
713             return "(new Long(" + tuplevn + ".getLong(" + pos + ")))";
714         case PType.TYPECODE_FLOAT:
715             return tuplevn + ".getFloat(" + pos + ")";
716         case PType.TYPECODE_OBJFLOAT:
717             return "(new Float(" + tuplevn + ".getFloat(" + pos + ")))";
718         case PType.TYPECODE_DOUBLE:
719             return tuplevn + ".getDouble(" + pos + ")";
720         case PType.TYPECODE_OBJDOUBLE:
721             return "(new Double(" + tuplevn + ".getDouble(" + pos + ")))";
722         case PType.TYPECODE_STRING:
723             return tuplevn + ".getString(" + pos + ")";
724         case PType.TYPECODE_SERIALIZED:
725             return "(Serializable) " + tuplevn + ".getObject(" + pos + ")";
726         case PType.TYPECODE_BIGDECIMAL:
727             return tuplevn + ".getBigDecimal(" + pos + ")";
728         case PType.TYPECODE_BIGINTEGER:
729             return tuplevn + ".getBigInteger(" + pos + ")";
730         }
731         throw new PExceptionTyping("Unauthorized PType " + pt);
732     }
733
734     /**
735      * This method does the conversion between a PType @pt and the getter
736      * function to use on a PAccessorGen (generic accessor).
737      * @param pt the ptype which helps to find the right paccessor method
738      * @return The string which represents the paccessor method associated
739      * to pt parameter
740      */

741     public String JavaDoc getPaGetMethod(PType pt) throws PException {
742         if (pt == null)
743             throw new PException("Unauthorized null PType");
744         switch (pt.getTypeCode()) {
745         case PType.TYPECODE_BOOLEAN:
746             return "paGetBooleanField";
747         case PType.TYPECODE_OBJBOOLEAN:
748             return "paGetObooleanField";
749         case PType.TYPECODE_BYTE:
750             return "paGetByteField";
751         case PType.TYPECODE_OBJBYTE:
752             return "paGetObyteField";
753         case PType.TYPECODE_CHAR:
754             return "paGetCharField";
755         case PType.TYPECODE_OBJCHAR:
756             return "paGetOcharField";
757         case PType.TYPECODE_DATE:
758             return "paGetDateField";
759         case PType.TYPECODE_CHARARRAY:
760             return "paGetCharArrayField";
761         case PType.TYPECODE_BYTEARRAY:
762             return "paGetByteArrayField";
763         case PType.TYPECODE_SHORT:
764             return "paGetShortField";
765         case PType.TYPECODE_OBJSHORT:
766             return "paGetOshortField";
767         case PType.TYPECODE_INT:
768             return "paGetIntField";
769         case PType.TYPECODE_OBJINT:
770             return "paGetOintField";
771         case PType.TYPECODE_LONG:
772             return "paGetLongField";
773         case PType.TYPECODE_OBJLONG:
774             return "paGetOlongField";
775         case PType.TYPECODE_FLOAT:
776             return "paGetFloatField";
777         case PType.TYPECODE_OBJFLOAT:
778             return "paGetOfloatField";
779         case PType.TYPECODE_DOUBLE:
780             return "paGetDoubleField";
781         case PType.TYPECODE_OBJDOUBLE:
782             return "paGetOdoubleField";
783         case PType.TYPECODE_STRING:
784             return "paGetStringField";
785         case PType.TYPECODE_SERIALIZED:
786             return "paGetSerializedField";
787         case PType.TYPECODE_BIGINTEGER:
788             return "paGetBigIntegerField";
789         case PType.TYPECODE_BIGDECIMAL:
790             return "paGetBigDecimalField";
791         case PType.TYPECODE_REFERENCE:
792             return "paGetRefField";
793         }
794         throw new PExceptionTyping("Unauthorized PType " + pt);
795     }
796
797     /**
798      * This method does the conversion between a PType @pt and the setter
799      * function to use on a PAccessorGen (generic accessor).
800      * @param pt the ptype which helps to find the right paccessor method
801      * @return The string which represents the paccessor method associated
802      * to pt parameter
803      */

804     public String JavaDoc getPaSetMethod(PType pt) throws PException {
805         if (pt == null)
806             throw new PException("Unauthorized null PType");
807
808         switch (pt.getTypeCode()) {
809         case PType.TYPECODE_BOOLEAN:
810             return "paSetBooleanField";
811         case PType.TYPECODE_OBJBOOLEAN:
812             return "paSetObooleanField";
813         case PType.TYPECODE_BYTE:
814             return "paSetByteField";
815         case PType.TYPECODE_OBJBYTE:
816             return "paSetObyteField";
817         case PType.TYPECODE_CHAR:
818             return "paSetCharField";
819         case PType.TYPECODE_OBJCHAR:
820             return "paSetOcharField";
821         case PType.TYPECODE_DATE:
822             return "paSetDateField";
823         case PType.TYPECODE_CHARARRAY:
824             return "paSetCharArrayField";
825         case PType.TYPECODE_BYTEARRAY:
826             return "paSetByteArrayField";
827         case PType.TYPECODE_SHORT:
828             return "paSetShortField";
829         case PType.TYPECODE_OBJSHORT:
830             return "paSetOshortField";
831         case PType.TYPECODE_INT:
832             return "paSetIntField";
833         case PType.TYPECODE_OBJINT:
834             return "paSetOintField";
835         case PType.TYPECODE_LONG:
836             return "paSetLongField";
837         case PType.TYPECODE_OBJLONG:
838             return "paSetOlongField";
839         case PType.TYPECODE_FLOAT:
840             return "paSetFloatField";
841         case PType.TYPECODE_OBJFLOAT:
842             return "paSetOfloatField";
843         case PType.TYPECODE_DOUBLE:
844             return "paSetDoubleField";
845         case PType.TYPECODE_OBJDOUBLE:
846             return "paSetOdoubleField";
847         case PType.TYPECODE_STRING:
848             return "paSetStringField";
849         case PType.TYPECODE_SERIALIZED:
850             return "paSetSerializedField";
851         case PType.TYPECODE_BIGINTEGER:
852             return "paSetBigIntegerField";
853         case PType.TYPECODE_BIGDECIMAL:
854             return "paSetBigDecimalField";
855         case PType.TYPECODE_REFERENCE:
856             return "paSetRefField";
857         }
858         throw new PExceptionTyping("Unauthorized PType " + pt);
859     }
860
861     /**
862      * This method permits to find the right decode function either the
863      * PType specified by the parameter @pt. These functions name are available
864      * on a PName interface.
865      * Be carefull the only jorm types support for the naming are the scalar
866      * types (char, byte, short, int, long and string) !
867      * @param pt the ptype which helps to find the right decode method
868      * @return The string which represents the decode method associated
869      * to pt parameter
870      */

871     public String JavaDoc getPNameDecodeFunction(PType pt) throws PException {
872         if (pt == null) {
873             throw new PExceptionTyping("The parameter must be not null");
874         }
875         switch (pt.getTypeCode()) {
876         case PType.TYPECODE_CHAR:
877             return "decodeChar";
878         case PType.TYPECODE_OBJCHAR:
879             return "decodeOchar";
880         case PType.TYPECODE_BYTE:
881             return "decodeByte";
882         case PType.TYPECODE_OBJBYTE:
883             return "decodeObyte";
884         case PType.TYPECODE_SHORT:
885             return "decodeShort";
886         case PType.TYPECODE_OBJSHORT:
887             return "decodeOshort";
888         case PType.TYPECODE_INT:
889             return "decodeInt";
890         case PType.TYPECODE_OBJINT:
891             return "decodeOint";
892         case PType.TYPECODE_LONG:
893             return "decodeLong";
894         case PType.TYPECODE_OBJLONG:
895             return "decodeOlong";
896         case PType.TYPECODE_STRING:
897             return "decodeString";
898         case PType.TYPECODE_DATE:
899             return "decodeDate";
900         case PType.TYPECODE_BYTEARRAY:
901             return "decode";
902         case PType.TYPECODE_CHARARRAY:
903             return "decodeCharArray";
904         case PType.TYPECODE_BIGINTEGER:
905             return "decodeBigInteger";
906         case PType.TYPECODE_BIGDECIMAL:
907             return "decodeBigDecimal";
908         default :
909             throw new PExceptionTyping(
910                     "Jorm does not support naming with the following PType:"
911                     + pt.getTypeCode());
912         }
913     }
914
915     public String JavaDoc getCoderName(PType t) {
916         switch (t.getTypeCode()) {
917         case PType.TYPECODE_BYTE:
918             return "Byte";
919         case PType.TYPECODE_OBJBYTE:
920             return "Obyte";
921         case PType.TYPECODE_CHAR:
922             return "Char";
923         case PType.TYPECODE_OBJCHAR:
924             return "Ochar";
925         case PType.TYPECODE_DATE:
926             return "Date";
927         case PType.TYPECODE_BIGDECIMAL:
928             return "BigDecimal";
929         case PType.TYPECODE_BIGINTEGER:
930             return "BigInteger";
931         case PType.TYPECODE_SHORT:
932             return "Short";
933         case PType.TYPECODE_OBJSHORT:
934             return "Oshort";
935         case PType.TYPECODE_INT:
936             return "Int";
937         case PType.TYPECODE_OBJINT:
938             return "Oint";
939         case PType.TYPECODE_LONG:
940             return "Long";
941         case PType.TYPECODE_OBJLONG:
942             return "Olong";
943         case PType.TYPECODE_STRING:
944             return "String";
945         case PType.TYPECODE_BYTEARRAY:
946             return "";
947         case PType.TYPECODE_CHARARRAY:
948             return "CharArray";
949         }
950         return null;
951     }
952
953     /**
954      * This method permits to find the right pngetXField function either the
955      * PType specified by the parameter @pt. These functions name are available
956      * on a PNameGetter interface.
957      * Be carefull the only jorm types support for the naming are the scalar
958      * types (char, byte, short, int, long and string) !
959      * @param pt the ptype which helps to find the right pname getter method
960      * @return The string which represents the pname getter method associated
961      * to pt parameter
962      */

963     public String JavaDoc getPNameGetterGetFunction(PType pt) throws PException {
964         if (pt == null) {
965             throw new PExceptionTyping("The parameter must be not null");
966         }
967         switch (pt.getTypeCode()) {
968         case PType.TYPECODE_CHAR:
969             return "pngetCharField";
970         case PType.TYPECODE_OBJCHAR:
971             return "pngetOcharField";
972         case PType.TYPECODE_BYTE:
973             return "pngetByteField";
974         case PType.TYPECODE_OBJBYTE:
975             return "pngetObyteField";
976         case PType.TYPECODE_SHORT:
977             return "pngetShortField";
978         case PType.TYPECODE_OBJSHORT:
979             return "pngetOshortField";
980         case PType.TYPECODE_INT:
981             return "pngetIntField";
982         case PType.TYPECODE_OBJINT:
983             return "pngetOintField";
984         case PType.TYPECODE_LONG:
985             return "pngetLongField";
986         case PType.TYPECODE_OBJLONG:
987             return "pngetOlongField";
988         case PType.TYPECODE_STRING:
989             return "pngetStringField";
990         case PType.TYPECODE_DATE:
991             return "pngetDateField";
992         case PType.TYPECODE_CHARARRAY:
993             return "pngetCharArrayField";
994         case PType.TYPECODE_BYTEARRAY:
995             return "pngetByteArrayField";
996         case PType.TYPECODE_BIGINTEGER:
997             return "pngetBigIntegerField";
998         case PType.TYPECODE_BIGDECIMAL:
999             return "pngetBigDecimalField";
1000        default :
1001            throw new PExceptionTyping(
1002                    "Jorm does not support naming with the following PType:"
1003                    + pt.getTypeCode());
1004        }
1005
1006    }
1007
1008    /**
1009     * This method permits to find the right encode function either the
1010     * PType specified by the parameter @pt. These functions name are available
1011     * on a PName interface.
1012     * Be carefull the only jorm types support for the naming are the scalar
1013     * types (char, byte, short, int, long and string) !
1014     * @param pt the ptype which helps to find the right encode method
1015     * @return The string which represents the encode method associated
1016     * to pt parameter
1017     */

1018    public String JavaDoc getPNameEncodeFunction(PType pt) throws PException {
1019        if (pt == null) {
1020            throw new PExceptionTyping("The parameter must be not null");
1021        }
1022        switch (pt.getTypeCode()) {
1023        case PType.TYPECODE_CHAR:
1024            return "encodeChar";
1025        case PType.TYPECODE_OBJCHAR:
1026            return "encodeOchar";
1027        case PType.TYPECODE_BYTE:
1028            return "encodeByte";
1029        case PType.TYPECODE_OBJBYTE:
1030            return "encodeObyte";
1031        case PType.TYPECODE_SHORT:
1032            return "encodeShort";
1033        case PType.TYPECODE_OBJSHORT:
1034            return "encodeOshort";
1035        case PType.TYPECODE_INT:
1036            return "encodeInt";
1037        case PType.TYPECODE_OBJINT:
1038            return "encodeOint";
1039        case PType.TYPECODE_LONG:
1040            return "encodeLong";
1041        case PType.TYPECODE_OBJLONG:
1042            return "encodeOlong";
1043        case PType.TYPECODE_STRING:
1044            return "encodeString";
1045        case PType.TYPECODE_DATE:
1046            return "encodeDate";
1047        case PType.TYPECODE_BYTEARRAY:
1048            return "encode";
1049        case PType.TYPECODE_CHARARRAY:
1050            return "encodeCharArray";
1051        case PType.TYPECODE_BIGINTEGER:
1052            return "encodeBigInteger";
1053        case PType.TYPECODE_BIGDECIMAL:
1054            return "encodeBigDecimal";
1055        default :
1056            throw new PExceptionTyping(
1057                    "Jorm does not support naming with the following PType:"
1058                    + pt.getTypeCode());
1059        }
1060    }
1061
1062    public String JavaDoc getCTDeclaration(PType t) {
1063        return getCTDeclaration(t.getTypeCode());
1064    }
1065
1066    public String JavaDoc getCTDeclaration(int typecode) {
1067        switch (typecode) {
1068        case PType.TYPECODE_CHAR:
1069            return "PNameCoder.CTCHAR";
1070        case PType.TYPECODE_OBJCHAR:
1071            return "PNameCoder.CTOCHAR";
1072
1073        case PType.TYPECODE_BYTE:
1074            return "PNameCoder.CTBYTE";
1075        case PType.TYPECODE_OBJBYTE:
1076            return "PNameCoder.CTOBYTE";
1077
1078        case PType.TYPECODE_SHORT:
1079            return "PNameCoder.CTSHORT";
1080        case PType.TYPECODE_OBJSHORT:
1081            return "PNameCoder.CTOSHORT";
1082
1083        case PType.TYPECODE_INT:
1084            return "PNameCoder.CTINT";
1085        case PType.TYPECODE_OBJINT:
1086            return "PNameCoder.CTOINT";
1087
1088        case PType.TYPECODE_LONG:
1089            return "PNameCoder.CTLONG";
1090        case PType.TYPECODE_OBJLONG:
1091            return "PNameCoder.CTOLONG";
1092
1093        case PType.TYPECODE_STRING:
1094            return "PNameCoder.CTSTRING";
1095
1096        case PType.TYPECODE_BYTEARRAY:
1097            return "PNameCoder.CTBYTEARRAY";
1098        case PType.TYPECODE_CHARARRAY:
1099            return "PNameCoder.CTCHARARRAY";
1100        case PType.TYPECODE_DATE:
1101            return "PNameCoder.CTDATE";
1102        case PType.TYPECODE_BIGDECIMAL:
1103            return "PNameCoder.CTBIGDECIMAL";
1104        case PType.TYPECODE_BIGINTEGER:
1105            return "PNameCoder.CTBIGINTEGER";
1106        }
1107        return null;
1108    }
1109
1110    /**
1111     * This method provides the projection of the field name belonging to the class
1112     * over a field name belonging to the composite name corresponding definition.
1113     * @param nd the meta object NameDef embeding a nameref with a projection.
1114     * @param cfn the field name belonging to the pname in the class.
1115     * @return the field name of the projection. this field name is a field belonging to the class.
1116     */

1117
1118    public String JavaDoc getProjectionField(NameDef nd, String JavaDoc cfn) throws Exception JavaDoc {
1119        if (nd.isNameRef()) {
1120            NameRef nr = nd.getNameRef();
1121            // get the projection of this composite name onto the field of the class
1122
// classfields represents the name of the field related to the name structure.
1123
// classfields are either primitive field or hiddenfield of the class.
1124
Iterator JavaDoc it = nr.getProjection().entrySet().iterator();
1125            while (it.hasNext()) {
1126                Map.Entry JavaDoc me = (Map.Entry JavaDoc) (it.next());
1127                if (me.getValue().equals(cfn))
1128                    return (String JavaDoc) (me.getKey());
1129            }
1130        }
1131        throw new PException("impossible to obtain field " + cfn + " of " + nd);
1132    }
1133
1134    public PType getPType(String JavaDoc ptn) {
1135        if (ptn.equals("boolean")) return PTypeSpace.BOOLEAN;
1136        if (ptn.equals("Boolean")) return PTypeSpace.OBJBOOLEAN;
1137        if (ptn.equals("char")) return PTypeSpace.CHAR;
1138        if (ptn.equals("Character")) return PTypeSpace.OBJCHAR;
1139        if (ptn.equals("byte")) return PTypeSpace.BYTE;
1140        if (ptn.equals("Byte")) return PTypeSpace.OBJBYTE;
1141        if (ptn.equals("short")) return PTypeSpace.SHORT;
1142        if (ptn.equals("Short")) return PTypeSpace.OBJSHORT;
1143        if (ptn.equals("int")) return PTypeSpace.INT;
1144        if (ptn.equals("Integer")) return PTypeSpace.OBJINT;
1145        if (ptn.equals("long")) return PTypeSpace.LONG;
1146        if (ptn.equals("Long")) return PTypeSpace.OBJLONG;
1147        if (ptn.equals("float")) return PTypeSpace.FLOAT;
1148        if (ptn.equals("Float")) return PTypeSpace.OBJFLOAT;
1149        if (ptn.equals("double")) return PTypeSpace.DOUBLE;
1150        if (ptn.equals("Double")) return PTypeSpace.OBJDOUBLE;
1151        if (ptn.equals("string")) return PTypeSpace.STRING;
1152        if (ptn.equals("date")) return PTypeSpace.DATE;
1153        if (ptn.equals("serialized")) return PTypeSpace.SERIALIZED;
1154        if (ptn.equals("BigInteger")) return PTypeSpace.BIGINTEGER;
1155        if (ptn.equals("BigDecimal")) return PTypeSpace.BIGDECIMAL;
1156        if (ptn.equals("byte[]")) return PTypeSpace.BYTEARRAY;
1157        if (ptn.equals("char[]")) return PTypeSpace.CHARARRAY;
1158        if (ptn.equals("reference")) return PTypeSpace.REFTOP;
1159        return null;
1160    }
1161
1162    /**
1163     * Its retrieves the PType object which represents the boolean jorm type.
1164     */

1165    public PType getPTypeBoolean() {
1166        return PTypeSpace.BOOLEAN;
1167    }
1168
1169    /**
1170     * Its retrieves the PType object which represents the Boolean jorm type.
1171     */

1172    public PType getPTypeOboolean() {
1173        return PTypeSpace.OBJBOOLEAN;
1174    }
1175
1176    /**
1177     * Its retrieves the PType object which represents the char jorm type.
1178     */

1179    public PType getPTypeChar() {
1180        return PTypeSpace.CHAR;
1181    }
1182
1183    /**
1184     * Its retrieves the PType object which represents the Character jorm type.
1185     */

1186    public PType getPTypeOchar() {
1187        return PTypeSpace.OBJCHAR;
1188    }
1189
1190    /**
1191     * Its retrieves the PType object which represents the byte jorm type.
1192     */

1193    public PType getPTypeByte() {
1194        return PTypeSpace.BYTE;
1195    }
1196
1197    /**
1198     * Its retrieves the PType object which represents the Byte jorm type.
1199     */

1200    public PType getPTypeObyte() {
1201        return PTypeSpace.OBJBYTE;
1202    }
1203
1204    /**
1205     * Its retrieves the PType object which represents the short jorm type.
1206     */

1207    public PType getPTypeShort() {
1208        return PTypeSpace.SHORT;
1209    }
1210
1211    /**
1212     * Its retrieves the PType object which represents the Short jorm type.
1213     */

1214    public PType getPTypeOshort() {
1215        return PTypeSpace.OBJSHORT;
1216    }
1217
1218    /**
1219     * Its retrieves the PType object which represents the int jorm type.
1220     */

1221    public PType getPTypeInt() {
1222        return PTypeSpace.INT;
1223    }
1224
1225    /**
1226     * Its retrieves the PType object which represents the Integer jorm type.
1227     */

1228    public PType getPTypeOint() {
1229        return PTypeSpace.OBJINT;
1230    }
1231
1232    /**
1233     * Its retrieves the PType object which represents the long jorm type.
1234     */

1235    public PType getPTypeLong() {
1236        return PTypeSpace.LONG;
1237    }
1238
1239    /**
1240     * Its retrieves the PType object which represents the Long jorm type.
1241     */

1242    public PType getPTypeOlong() {
1243        return PTypeSpace.OBJLONG;
1244    }
1245
1246    /**
1247     * Its retrieves the PType object which represents the float jorm type.
1248     */

1249    public PType getPTypeFloat() {
1250        return PTypeSpace.FLOAT;
1251    }
1252
1253    /**
1254     * Its retrieves the PType object which represents the Float jorm type.
1255     */

1256    public PType getPTypeOfloat() {
1257        return PTypeSpace.OBJFLOAT;
1258    }
1259
1260    /**
1261     * Its retrieves the PType object which represents the double jorm type.
1262     */

1263    public PType getPTypeDouble() {
1264        return PTypeSpace.DOUBLE;
1265    }
1266
1267    /**
1268     * Its retrieves the PType object which represents the Double jorm type.
1269     */

1270    public PType getPTypeOdouble() {
1271        return PTypeSpace.OBJDOUBLE;
1272    }
1273
1274    /**
1275     * Its retrieves the PType object which represents the string jorm type.
1276     */

1277    public PType getPTypeString() {
1278        return PTypeSpace.STRING;
1279    }
1280
1281    /**
1282     * Its retrieves the PType object which represents the date jorm type.
1283     */

1284    public PType getPTypeDate() {
1285        return PTypeSpace.DATE;
1286    }
1287
1288    /**
1289     * Its retrieves the PType object which represents the chararray jorm type.
1290     */

1291    public PType getPTypeCharArray() {
1292        return PTypeSpace.CHARARRAY;
1293    }
1294
1295    /**
1296     * Its retrieves the PType object which represents the bytearray jorm type.
1297     */

1298    public PType getPTypeByteArray() {
1299        return PTypeSpace.BYTEARRAY;
1300    }
1301
1302    /**
1303     * Its retrieves the PType object which represents the serialized jorm type.
1304     */

1305    public PType getPTypeSerialized() {
1306        return PTypeSpace.SERIALIZED;
1307    }
1308
1309    /**
1310     * Its retrieves the PType object which represents the BigInteger jorm type.
1311     */

1312    public PType getPTypeBigInteger() {
1313        return PTypeSpace.BIGINTEGER;
1314    }
1315
1316    /**
1317     * Its retrieves the PType object which represents the BigDecimal jorm type.
1318     */

1319    public PType getPTypeBigDecimal() {
1320        return PTypeSpace.BIGDECIMAL;
1321    }
1322
1323    /**
1324     * Its retrieves the PType object which represents a reference.
1325     */

1326    public PType getPTypeReference() {
1327        return PTypeSpace.REFTOP;
1328    }
1329
1330    public PType[] getPNGTypes() {
1331        return PN_GET_TYPES;
1332    }
1333
1334    public boolean not(boolean b) {
1335        return !b;
1336    }
1337
1338    public boolean canBeNullValue(PType type) throws PException {
1339        if (type == null) {
1340            throw new PException("canBeNullValue: Unauthorized null PType parameter");
1341        }
1342        switch (type.getTypeCode()) {
1343        case PType.TYPECODE_BOOLEAN:
1344
1345        case PType.TYPECODE_CHAR:
1346        case PType.TYPECODE_BYTE:
1347        case PType.TYPECODE_SHORT:
1348        case PType.TYPECODE_INT:
1349        case PType.TYPECODE_LONG:
1350        case PType.TYPECODE_FLOAT:
1351        case PType.TYPECODE_DOUBLE:
1352        case PType.TYPECODE_REFERENCE:
1353            return false;
1354        case PType.TYPECODE_OBJBOOLEAN:
1355        case PType.TYPECODE_OBJCHAR:
1356        case PType.TYPECODE_OBJBYTE:
1357        case PType.TYPECODE_OBJSHORT:
1358        case PType.TYPECODE_OBJINT:
1359        case PType.TYPECODE_OBJLONG:
1360        case PType.TYPECODE_OBJFLOAT:
1361        case PType.TYPECODE_OBJDOUBLE:
1362        case PType.TYPECODE_STRING:
1363        case PType.TYPECODE_DATE:
1364        case PType.TYPECODE_CHARARRAY:
1365        case PType.TYPECODE_BYTEARRAY:
1366        case PType.TYPECODE_SERIALIZED:
1367        case PType.TYPECODE_BIGINTEGER:
1368        case PType.TYPECODE_BIGDECIMAL:
1369            return true;
1370        default:
1371            throw new PExceptionTyping("Unknown type " + type.getJormName());
1372        }
1373    }
1374
1375    /**
1376     * This method is a helper to reach the getInheritedClasses(Class, HashMap)
1377     * method. Indeed in the velocity template is not possible to instanciate
1378     * an object.
1379     */

1380    public String JavaDoc getInheritedClasses(Class JavaDoc c) {
1381        Map JavaDoc m = getInheritedClasses(c, new HashMap JavaDoc(), false);
1382        StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
1383        sb.append('{');
1384        Iterator JavaDoc it = m.entrySet().iterator();
1385        String JavaDoc sep = "";
1386        while(it.hasNext()) {
1387            Map.Entry JavaDoc me = (Map.Entry JavaDoc) it.next();
1388            String JavaDoc child = (String JavaDoc) me.getKey();
1389            String JavaDoc[] parents = (String JavaDoc[]) me.getValue();
1390            for(int i=0; i<parents.length; i++) {
1391                sb.append(sep);
1392                sb.append("{\"");
1393                sb.append(child);
1394                sb.append("\", \"");
1395                sb.append(parents[i]);
1396                sb.append("\"}");
1397                sep =", ";
1398            }
1399        }
1400        sb.append('}');
1401        return sb.toString();
1402    }
1403
1404    /**
1405     * This recursive method add in the HashMap @calculated all inherited class
1406     * of the @current class parameter.
1407     * @return an hashmap where keys are class names and their values are the
1408     * direct inhertied class.
1409     */

1410    private HashMap JavaDoc getInheritedClasses(Class JavaDoc current,
1411                                        HashMap JavaDoc calculated,
1412                                        boolean addCurrent) {
1413        int parentNumber = current.getInheritedClassNumber();
1414        // Base case : No inherited classes
1415
if (parentNumber == 0)
1416            return calculated;
1417
1418        int addcurrent = (addCurrent ? 1 : 0);
1419        String JavaDoc[] list = new String JavaDoc[parentNumber + addcurrent];
1420        if (addCurrent) {
1421            // Put the current class in first position into the list
1422
list[0] = current.getFQName();
1423        }
1424        // Put the list into the structure
1425
calculated.put(current.getFQName(), list);
1426        //Full the list of inherited classes for the current class
1427
Iterator JavaDoc it = current.getSuperClasses().iterator();
1428        int i;
1429        for (i = addcurrent; it.hasNext() && i < list.length; i++) {
1430            Class JavaDoc parent = (Class JavaDoc) it.next();
1431            list[i] = parent.getFQName();
1432            if (!calculated.containsKey(parent.getFQName()))
1433                getInheritedClasses(parent, calculated, false);
1434        }
1435        if (it.hasNext() || i < list.length) {
1436            throw new RuntimeException JavaDoc("MalFormed JMI");
1437        }
1438        return calculated;
1439    }
1440
1441    public String JavaDoc log(String JavaDoc msg) {
1442        System.out.println(msg);
1443        return msg;
1444    }
1445    
1446    public String JavaDoc debug(String JavaDoc msg) {
1447        if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) {
1448            logger.log(BasicLevel.DEBUG, msg);
1449            return "//" + msg;
1450        }
1451        return "";
1452    }
1453
1454    //------------------------------------------//
1455
// IMPLEMENTATION OF THE Loggable INTERFACE //
1456
//------------------------------------------//
1457

1458    /**
1459     * Its assigns a logger factory.
1460     */

1461    public void setLoggerFactory(LoggerFactory lf) {
1462        if (logger == null && lf != null)
1463            setLogger(lf.getLogger(getClass().getName()));
1464        loggerFactory = lf;
1465    }
1466
1467    /**
1468     * Its assigns a logger.
1469     */

1470    public void setLogger(Logger l) {
1471        logger = l;
1472        debug = logger != null && logger.isLoggable(BasicLevel.DEBUG);
1473    }
1474
1475    /**
1476     * Its retrives the current logger.
1477     */

1478    public Logger getLogger() {
1479        return logger;
1480    }
1481
1482    /**
1483     * Its retrives the current logger factory.
1484     */

1485    public LoggerFactory getLoggerFactory() {
1486        return loggerFactory;
1487    }
1488}
1489
Popular Tags