KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > corba > provider > JMIProvider


1 /**
2  * copyright 2002 2003 Laboratoire d'Informatique Paris 6 (LIP6)
3  *
4  * This file is part of ModFact.
5  *
6  * ModFact is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * at your option) any later version.
10  *
11  * ModFact 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with ModFact; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20 package org.objectweb.modfact.corba.provider;
21
22 import org.omg.CORBA.TCKind JavaDoc;
23 import org.omg.CORBA.TypeCode JavaDoc;
24 import org.omg.CORBA.TypeCodePackage.BadKind JavaDoc;
25 import org.omg.mof.Model.Association;
26 import org.omg.mof.Model.ClassHelper;
27 import org.omg.mof.Model.Classifier;
28 import org.omg.mof.Model.DataType;
29 import org.omg.mof.Model.DataTypeHelper;
30 import org.omg.mof.Model.Import;
31 import org.omg.mof.Model.MofAttribute;
32 import org.omg.mof.Model.MultiplicityType;
33 import org.omg.mof.Model.Namespace;
34 import org.omg.mof.Model.PackageHelper;
35 import org.omg.mof.Reflective.MofError;
36 import org.omg.mof.Reflective.NotSet;
37
38 import org.objectweb.modfact.corba.helper.JMICommon;
39 import org.objectweb.modfact.corba.helper.MOFCommon;
40
41 /**
42  * @author Xavier
43  *
44  */

45 public class JMIProvider implements JMICommon {
46
47     MOFCommon mofHelper;
48
49     public JMIProvider(MOFCommon mofHelper) {
50         this.mofHelper = mofHelper;
51     }
52
53     /**
54      * @see fr.lip6.modfact.helper.JMICommon#jmiPackageName(Package)
55      */

56     public String JavaDoc jmiPackageName(org.omg.mof.Model.Package _package)
57         throws MofError {
58         return _package.name().toLowerCase();
59     }
60
61     /**
62      * @see fr.lip6.modfact.helper.JMICommon#jmiQualifiedPackageName(Package)
63      */

64     public String JavaDoc jmiPackageQualifiedName(org.omg.mof.Model.Package _package)
65         throws MofError, NotSet {
66         //String[] names = _package.qualified_name();
67
String JavaDoc res = "";
68
69         String JavaDoc[] names = _package.qualified_name();
70         for (int i = 0; i < names.length; i++) {
71             res = names[i].toLowerCase() + res;
72             if (i != (names.length - 1))
73                 res = "." + res;
74         }
75
76         org.omg.mof.Model.Package outerPackage =
77             mofHelper.outermostPackageOfModelElement(_package);
78         //can be itself no matter
79
String JavaDoc packagePrefix = mofHelper.jmiPrefixForPackage(_package);
80         if (packagePrefix.compareTo("") != 0)
81             res = packagePrefix + "." + res;
82
83         return res;
84     }
85
86     /**
87      * @see fr.lip6.modfact.helper.JMICommon#jmiClassName(Class)
88      */

89     public String JavaDoc jmiClassName(org.omg.mof.Model.Class clazz)
90         throws MofError, NotSet {
91         if (mofHelper.jmiSubstituteIdentifierForClass(clazz).compareTo("")
92             != 0)
93             return mofHelper.jmiSubstituteIdentifierForClass(clazz);
94         char[] res = clazz.name().toCharArray();
95         if (res.length != 0) {
96             res[0] = Character.toUpperCase(res[0]);
97         }
98         return new String JavaDoc(res);
99     }
100
101     /**
102      * @see fr.lip6.modfact.helper.JMICommon#jmiClassName(Class)
103      */

104     public String JavaDoc jmiClassQualifiedName(org.omg.mof.Model.Class clazz)
105         throws MofError, NotSet {
106         String JavaDoc res = jmiClassName(clazz);
107
108         org.omg.mof.Model.Package _package =
109             PackageHelper.narrow(clazz.container());
110         return jmiPackageQualifiedName(_package) + "." + res;
111     }
112
113     /**
114      * @see fr.lip6.modfact.helper.JMICommon#jmiAssociationName(Association)
115      */

116     public String JavaDoc jmiAssociationName(Association association)
117         throws MofError, NotSet {
118         char[] res = association.name().toCharArray();
119         if (res.length != 0) {
120             res[0] = Character.toUpperCase(res[0]);
121         }
122         return new String JavaDoc(res);
123     }
124
125     /**
126      * @see fr.lip6.modfact.helper.JMICommon#jmiAssociationName(Association)
127      */

128     public String JavaDoc jmiAssociationQualifiedName(Association association)
129         throws MofError, NotSet {
130         char[] res = association.name().toCharArray();
131         if (res.length != 0) {
132             res[0] = Character.toUpperCase(res[0]);
133         }
134         org.omg.mof.Model.Package _package =
135             PackageHelper.narrow(association.container());
136         return jmiPackageQualifiedName(_package) + "." + new String JavaDoc(res);
137     }
138
139     /**
140      * @see fr.lip6.modfact.helper.JMICommon#jmiAttributeName(MofAttribute)
141      */

142     public String JavaDoc jmiAttributeName(MofAttribute attribute) throws MofError {
143         char[] res = attribute.name().toCharArray();
144         if (res.length != 0) {
145             res[0] = Character.toLowerCase(res[0]);
146         }
147         return new String JavaDoc(res);
148     }
149
150     /**
151      * @see fr.lip6.modfact.helper.JMICommon#jmiTypeCodeName(TypeCode)
152      */

153     public String JavaDoc jmiTypeCodeName(TypeCode JavaDoc _type) throws BadKind JavaDoc {
154         switch (_type.kind().value()) {
155             case org.omg.CORBA.TCKind._tk_boolean :
156                 {
157                     return "boolean";
158
159                 }
160             case org.omg.CORBA.TCKind._tk_octet :
161                 {
162                     return "byte";
163
164                 }
165             case org.omg.CORBA.TCKind._tk_char :
166                 {
167                     return "char";
168
169                 }
170             case org.omg.CORBA.TCKind._tk_wchar :
171                 {
172                     return "char";
173
174                 }
175             case org.omg.CORBA.TCKind._tk_short :
176                 {
177                     return "short";
178
179                 }
180             case org.omg.CORBA.TCKind._tk_ushort :
181                 {
182                     return "short";
183
184                 }
185             case org.omg.CORBA.TCKind._tk_long :
186                 {
187                     return "int";
188
189                 }
190             case org.omg.CORBA.TCKind._tk_ulong :
191                 {
192                     return "int";
193
194                 }
195             case org.omg.CORBA.TCKind._tk_longlong :
196                 {
197                     return "long";
198
199                 }
200             case org.omg.CORBA.TCKind._tk_ulonglong :
201                 {
202                     return "long";
203
204                 }
205             case org.omg.CORBA.TCKind._tk_double :
206                 {
207                     return "double";
208
209                 }
210             case org.omg.CORBA.TCKind._tk_float :
211                 {
212                     return "float";
213
214                 }
215             case org.omg.CORBA.TCKind._tk_string :
216                 {
217                     return "String";
218
219                 }
220             case org.omg.CORBA.TCKind._tk_wstring :
221                 {
222                     return "String";
223
224                 }
225             case org.omg.CORBA.TCKind._tk_TypeCode :
226                 {
227                     return "org.omg.CORBA.TypeCode";
228
229                 }
230             case org.omg.CORBA.TCKind._tk_any :
231                 {
232                     return "org.omg.CORBA.Any";
233
234                 }
235             case org.omg.CORBA.TCKind._tk_objref :
236                 {
237                     return "org.omg.CORBA.Object";
238
239                 }
240         }
241         return _type.name();
242     }
243
244     /**
245      *
246      */

247     public String JavaDoc jmiNullableTypeCodeName(TypeCode JavaDoc _type) throws BadKind JavaDoc {
248         switch (_type.kind().value()) {
249             case org.omg.CORBA.TCKind._tk_boolean :
250                 {
251                     return "Boolean";
252
253                 }
254             case org.omg.CORBA.TCKind._tk_octet :
255                 {
256                     return "Byte";
257
258                 }
259             case org.omg.CORBA.TCKind._tk_char :
260                 {
261                     return "Char";
262
263                 }
264             case org.omg.CORBA.TCKind._tk_wchar :
265                 {
266                     return "Char";
267
268                 }
269             case org.omg.CORBA.TCKind._tk_short :
270                 {
271                     return "Short";
272
273                 }
274             case org.omg.CORBA.TCKind._tk_ushort :
275                 {
276                     return "Short";
277
278                 }
279             case org.omg.CORBA.TCKind._tk_long :
280                 {
281                     return "Integer";
282
283                 }
284             case org.omg.CORBA.TCKind._tk_ulong :
285                 {
286                     return "Integer";
287
288                 }
289             case org.omg.CORBA.TCKind._tk_longlong :
290                 {
291                     return "Long";
292
293                 }
294             case org.omg.CORBA.TCKind._tk_ulonglong :
295                 {
296                     return "Long";
297
298                 }
299             case org.omg.CORBA.TCKind._tk_double :
300                 {
301                     return "Double";
302
303                 }
304             case org.omg.CORBA.TCKind._tk_float :
305                 {
306                     return "Float";
307                 }
308             case org.omg.CORBA.TCKind._tk_string :
309                 {
310                     return "String";
311
312                 }
313             case org.omg.CORBA.TCKind._tk_wstring :
314                 {
315                     return "String";
316
317                 }
318             case org.omg.CORBA.TCKind._tk_TypeCode :
319                 {
320                     return "org.omg.CORBA.TypeCode";
321
322                 }
323             case org.omg.CORBA.TCKind._tk_any :
324                 {
325                     return "org.omg.CORBA.Any";
326
327                 }
328             case org.omg.CORBA.TCKind._tk_objref :
329                 {
330                     return "org.omg.CORBA.Object";
331
332                 }
333         }
334         return _type.name();
335     }
336
337     /**
338      * @see fr.lip6.modfact.helper.JMICommon#jmiImportName(Import)
339      */

340     public String JavaDoc jmiImportName(Import _import) throws MofError, NotSet {
341         return jmiPackageQualifiedName(
342             PackageHelper.narrow(_import.imported_namespace()));
343     }
344
345     /**
346      * @see fr.lip6.modfact.helper.JMICommon#jmiClassifierName(Classifier)
347      */

348     public String JavaDoc jmiClassifierName(Classifier _classifier)
349         throws MofError, BadKind JavaDoc, NotSet {
350         if (_classifier._is_a(org.omg.mof.Model.ClassHelper.id()))
351             return jmiClassName(
352                 (org.omg.mof.Model.Class) org.omg.mof.Model.ClassHelper.narrow(
353                     _classifier));
354         else if (_classifier._is_a(org.omg.mof.Model.DataTypeHelper.id())) {
355             DataType dt = (DataType) DataTypeHelper.narrow(_classifier);
356             String JavaDoc resu = jmiTypeCodeName(dt.type_code());
357             return resu;
358         } else
359             return _classifier.name();
360     }
361
362     /**
363      * @see fr.lip6.modfact.helper.JMICommon#jmiClassifierName(Classifier)
364      */

365     public String JavaDoc jmiClassifierQualifiedName(Classifier _classifier)
366         throws MofError, BadKind JavaDoc, NotSet {
367
368         if (_classifier._is_a(org.omg.mof.Model.ClassHelper.id()))
369             return jmiClassQualifiedName(
370                 (org.omg.mof.Model.Class) org.omg.mof.Model.ClassHelper.narrow(
371                     _classifier));
372         else if (_classifier._is_a(org.omg.mof.Model.DataTypeHelper.id())) {
373             DataType dt = (DataType) DataTypeHelper.narrow(_classifier);
374             Namespace ns = dt.container();
375             String JavaDoc resu = jmiTypeCodeName(dt.type_code());
376
377             if ((dt.type_code().kind().value() == TCKind._tk_struct)
378                 || (dt.type_code().kind().value() == TCKind._tk_enum)) {
379                 if (ns._is_a(PackageHelper.id())) {
380                     org.omg.mof.Model.Package _package =
381                         PackageHelper.narrow(ns);
382                     resu = jmiPackageQualifiedName(_package) + "." + resu;
383                 } else if (ns._is_a(ClassHelper.id())) {
384                     org.omg.mof.Model.Class clazz = ClassHelper.narrow(ns);
385                     org.omg.mof.Model.Package _package =
386                         PackageHelper.narrow(ns.container());
387                     resu = jmiPackageQualifiedName(_package) + "." + resu;
388                 }
389             }
390             return resu;
391         } else
392             return _classifier.name();
393     }
394
395     /**
396                                                 * @see fr.lip6.modfact.helper.JMICommon#jmiType(String)
397                                                 */
// public String jmiType(String _type) {
398
// if (_type.equalsIgnoreCase("boolean"))
399
// return "boolean";
400
// else if (_type.equalsIgnoreCase("octet"))
401
// return "byte";
402
// else if (_type.equalsIgnoreCase("char"))
403
// return "char";
404
// else if (_type.equalsIgnoreCase("wchar"))
405
// return "char";
406
// else if (_type.equalsIgnoreCase("short"))
407
// return "Short";
408
// else if (_type.equalsIgnoreCase("unsigned short"))
409
// return "Short";
410
// else if (_type.equalsIgnoreCase("long"))
411
// return "int";
412
// else if (_type.equalsIgnoreCase("unsigned long"))
413
// return "int";
414
// else if (_type.equalsIgnoreCase("long long"))
415
// return "long";
416
// else if (_type.equalsIgnoreCase("unsigned long long"))
417
// return "long";
418
// else if (_type.equalsIgnoreCase("double"))
419
// return "double";
420
// else if (_type.equalsIgnoreCase("float"))
421
// return "float";
422
// else if (_type.equalsIgnoreCase("string"))
423
// return "String";
424
// else if (_type.equalsIgnoreCase("wstring"))
425
// return "String";
426
// else if (_type.equalsIgnoreCase("CORBA::TypeCode"))
427
// return "TypeCode";
428
// else if (_type.equalsIgnoreCase("any"))
429
// return "Any";
430
// else if (_type.equalsIgnoreCase("Object"))
431
// return "Object";
432
// // je pourrais déclancher une exception à revoir la igne ci-dessous
433
// else {
434
// return _type;
435
// }
436
// }
437
/**
438      * @see fr.lip6.modfact.helper.JMICommon#accessorName(String, TypeCode)
439      */

440     public String JavaDoc jmiFormat1(String JavaDoc name) throws MofError {
441         char[] res = name.toCharArray();
442         if (res.length != 0) {
443             res[0] = Character.toUpperCase(res[0]);
444         }
445         return new String JavaDoc(res);
446     }
447
448     /**
449     * Return a nullable type (i.e. Boolean instead of boolean)
450                                              */

451     public String JavaDoc jmiNullableClassifierName(Classifier classifier)
452         throws MofError, BadKind JavaDoc, NotSet {
453         if (classifier._is_a(org.omg.mof.Model.ClassHelper.id()))
454             return jmiClassName(
455                 (org.omg.mof.Model.Class) org.omg.mof.Model.ClassHelper.narrow(
456                     classifier));
457         else if (classifier._is_a(org.omg.mof.Model.DataTypeHelper.id())) {
458             DataType dt = (DataType) DataTypeHelper.narrow(classifier);
459             String JavaDoc resu = jmiNullableTypeCodeName(dt.type_code());
460             return resu;
461         } else
462             return classifier.name();
463     } /**
464                                                                                              * Return a nullable type (i.e. Boolean instead of boolean)
465                                                                                              */

466     public String JavaDoc jmiNullableClassifierQualifiedName(Classifier classifier)
467         throws MofError, BadKind JavaDoc, NotSet {
468         if (classifier._is_a(org.omg.mof.Model.ClassHelper.id()))
469             return jmiClassName(
470                 (org.omg.mof.Model.Class) org.omg.mof.Model.ClassHelper.narrow(
471                     classifier));
472         else if (classifier._is_a(org.omg.mof.Model.DataTypeHelper.id())) {
473             DataType dt = (DataType) DataTypeHelper.narrow(classifier);
474             Namespace ns = dt.container();
475             String JavaDoc resu = jmiNullableTypeCodeName(dt.type_code());
476
477             if ((dt.type_code().kind().value() == TCKind._tk_struct)
478                 || (dt.type_code().kind().value() == TCKind._tk_enum)) {
479                 if (ns._is_a(PackageHelper.id())) {
480                     org.omg.mof.Model.Package _package =
481                         PackageHelper.narrow(ns);
482                     resu = jmiPackageQualifiedName(_package) + "." + resu;
483                 } else if (ns._is_a(ClassHelper.id())) {
484                     org.omg.mof.Model.Class clazz = ClassHelper.narrow(ns);
485                     org.omg.mof.Model.Package _package =
486                         PackageHelper.narrow(ns.container());
487                     resu = jmiPackageQualifiedName(_package) + "." + resu;
488                 }
489             }
490             return resu;
491         } else
492             return classifier.name();
493     }
494
495     public String JavaDoc jmiAccessorName(MofAttribute attribute) throws MofError {
496         String JavaDoc accessorName = jmiFormat1(attribute.name());
497         MultiplicityType mult = attribute.multiplicity();
498         //DataType
499
boolean isBoolean = false;
500         if (attribute.type()._is_a(DataTypeHelper.id())) {
501             DataType type = DataTypeHelper.narrow(attribute.type());
502             if (type.type_code().kind().equals(TCKind.tk_boolean))
503                 isBoolean = true;
504         }
505         if (mult.lower == 0 && mult.upper == 1 && isBoolean) {
506             if (attribute.name().toLowerCase().startsWith("is"))
507                 return attribute.name();
508             else
509                 return "is" + accessorName;
510         } else
511             return "get" + accessorName;
512     }
513
514     public String JavaDoc jmiMutatorName(MofAttribute attribute) throws MofError {
515         String JavaDoc mutatorName = "";
516         MultiplicityType mult = attribute.multiplicity();
517         //DataType
518
boolean isBoolean = false;
519         if (attribute.type()._is_a(DataTypeHelper.id())) {
520             DataType type = DataTypeHelper.narrow(attribute.type());
521             if (type.type_code().kind().equals(TCKind.tk_boolean))
522                 isBoolean = true;
523         }
524         if (isBoolean
525             && (mult.lower == 0 || mult.lower == 1)
526             && mult.upper == 1
527             && attribute.name().toLowerCase().startsWith("is")) {
528             return "set" + jmiFormat1(attribute.name().substring(2));
529         } else
530             return "set" + jmiFormat1(attribute.name());
531     }
532     
533     public String JavaDoc valueOfConstant(org.omg.CORBA.Any JavaDoc _value)
534         throws org.omg.CORBA.TypeCodePackage.BadKind JavaDoc {
535             return mofHelper.valueOfConstant(_value);
536         }
537
538 }
539
Popular Tags