KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > corba > idl2java > ClassProxyImplementationGenerator


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.idl2java;
21
22 import org.omg.CORBA.TypeCodePackage.BadKind JavaDoc;
23 import org.omg.CORBA.TypeCodePackage.Bounds JavaDoc;
24 import org.omg.mof.Model.PackageHelper;
25 import org.omg.mof.Reflective.NotSet;
26
27 import org.objectweb.modfact.corba.generator.BracketGenerator;
28 import org.objectweb.modfact.corba.helper.JavaCommon;
29 import org.objectweb.modfact.corba.helper.MOFCommon;
30 import org.objectweb.modfact.corba.logging.ModFactLogger;
31
32 public class ClassProxyImplementationGenerator extends BracketGenerator {
33
34     ModFactLogger logger = null;
35
36     org.omg.mof.Model.Class[] input;
37
38     /**
39      * Set Input
40      */

41     public void setInput(org.omg.mof.Model.ModelElement[] elt) {
42         input = new org.omg.mof.Model.Class[elt.length];
43         for (int i = 0; i < input.length; i++) {
44             input[i] = (org.omg.mof.Model.Class) elt[i];
45         }
46     }
47
48     /**
49      * Set Trace
50      */

51     public void setLogger(ModFactLogger log) {
52         logger = log;
53     }
54
55     //Helper
56
JavaCommon javaHelper;
57     MOFCommon mofHelper;
58
59     public void generate()
60         throws
61             org.omg.mof.Reflective.NotSet,
62             org.omg.mof.Reflective.MofError,
63             org.omg.CORBA.TypeCodePackage.BadKind JavaDoc,
64             org.omg.CORBA.TypeCodePackage.Bounds JavaDoc {
65         outputln("// le Code généré de la ProxyClass: " + input[0].name());
66         classProxyImpl(input[0]);
67         flushFile();
68     }
69
70     // La partie gnération du code
71
public void allOfType(org.omg.mof.Model.Class _class)
72         throws org.omg.mof.Reflective.MofError, org.omg.mof.Reflective.NotSet {
73
74         String JavaDoc _cformat1 = javaHelper.javaQualifiedName(_class);
75         String JavaDoc _cformat2 = javaHelper.format2(javaHelper.javaClassName(_class));
76
77         outputln(
78             "public " + _cformat1 + "[] all_of_type_" + _cformat2 + "() {");
79
80         // ce vecteur et tableau sont crées pour toutes les classes
81
outputln(
82             "java.util.Vector all_type_vector = new java.util.Vector();");
83         org.omg.mof.Model.Class[] _sub_classes =
84             mofHelper.subClassesOfClass(_class);
85
86         for (int i = 0; i < _sub_classes.length; i++) {
87             org.omg.mof.Model.Class _class_temp = _sub_classes[i];
88             //Only if class is in the same extends
89
if (!_class_temp.is_abstract()
90                 && mofHelper.isInSameExtent(_class_temp, _class)) {
91                 String JavaDoc _sformat1 = javaHelper.javaQualifiedName(_class_temp);
92                 String JavaDoc _sformat2 = javaHelper.format2(javaHelper.javaClassName(_class_temp));
93                 /* If the class is contained by another package */
94                 String JavaDoc _sformat2_ref = getRefMethod(_class_temp, _class.container());
95                 
96                 //mettre tous les retours de la méthode all_classes des sous classes
97
outputln(
98                     _sformat1
99                         + "[] "
100                         + "_"
101                         + _sformat2
102                         + " =_model_package."
103                         + _sformat2_ref
104                         + ".all_of_class_"
105                         + _sformat2
106                         + "();");
107                 outputln(
108                     "for(int i=0; i<"
109                         + "_"
110                         + _sformat2
111                         + ".length ; i++) all_type_vector.addElement("
112                         + "_"
113                         + _sformat2
114                         + "[i]);");
115             }
116         }
117         if (!_class.is_abstract())
118             outputln(
119                 "for(int i=0; i< all_class_vector_"
120                     + _cformat2
121                     + ".size() ; i++) all_type_vector.addElement(("
122                     + _cformat1
123                     + ")all_class_vector_"
124                     + _cformat2
125                     + ".elementAt(i));");
126         outputln(
127             _cformat1
128                 + "[] all_type = new "
129                 + _cformat1
130                 + "[all_type_vector.size()];");
131         outputln(
132             "for (int i=0; i< all_type_vector.size(); i++) all_type[i]= ("
133                 + _cformat1
134                 + ")all_type_vector.elementAt(i);");
135         outputln("return all_type; ");
136         outputln("} ");
137
138     }
139
140     public void allOfClasses(org.omg.mof.Model.Class _class)
141         throws org.omg.mof.Reflective.MofError , NotSet{
142         String JavaDoc _cformat1 = javaHelper.javaQualifiedName(_class);
143         String JavaDoc _cformat2 = javaHelper.format2(javaHelper.javaClassName(_class));
144         outputln(
145             "java.util.Vector all_class_vector_"
146                 + _cformat2
147                 + " = new java.util.Vector();");
148         outputln(
149             "public " + _cformat1 + "[] all_of_class_" + _cformat2 + "() {");
150         outputln(
151             _cformat1
152                 + "[] _"
153                 + _cformat2
154                 + " = new "
155                 + _cformat1
156                 + "[all_class_vector_"
157                 + _cformat2
158                 + ".size()];");
159         outputln(
160             "for (int i=0; i< all_class_vector_"
161                 + _cformat2
162                 + ".size(); i++) _"
163                 + _cformat2
164                 + "[i]= ("
165                 + _cformat1
166                 + ")all_class_vector_"
167                 + _cformat2
168                 + ".elementAt(i);");
169         outputln("return _" + _cformat2 + "; ");
170         outputln("} ");
171     }
172
173     public void createClass(org.omg.mof.Model.Class _class)
174         throws org.omg.mof.Reflective.MofError, BadKind JavaDoc, Bounds JavaDoc , NotSet{
175         String JavaDoc _cformat1 = javaHelper.javaQualifiedName(_class);
176         String JavaDoc _cformat2 = javaHelper.format2(javaHelper.javaClassName(_class));
177
178         //if (_class.visibility().equals(org.omg.mof.Model.VisibilityKind.public_vis)){
179
output("public " + _cformat1 + " create_" + _cformat2 + "(");
180
181         // trouver toutes les attributs de cette classe
182
org.omg.mof.Model.MofAttribute[] attributesND =
183             mofHelper.allAttributesOfClass(
184                 _class,
185                 org.omg.mof.Model.ScopeKind.instance_level,
186                 false);
187         for (int i = 0; i < attributesND.length; i++) {
188             String JavaDoc _type_format1 = javaHelper.javaType(attributesND[i].type());
189             String JavaDoc _aformat2 = javaHelper.format2(attributesND[i].name());
190             if ((attributesND[i].multiplicity().upper > 1)
191                 || (attributesND[i].multiplicity().upper == -1)) {
192                 output(_type_format1 + "[] " + _aformat2);
193             } else
194                 output(_type_format1 + " " + _aformat2);
195             if (i != (attributesND.length - 1))
196                 output(", ");
197         }
198         outputln(") throws org.omg.mof.Reflective.MofError {");
199         output(
200             _cformat1
201                 + "Impl _"
202                 + _cformat2
203                 + " = new "
204                 + _cformat1
205                 + "Impl(");
206         for (int i = 0; i < attributesND.length; i++) {
207             String JavaDoc _aformat2 = javaHelper.format2(attributesND[i].name());
208             output(_aformat2);
209             output(", ");
210         }
211         outputln("_this());");
212         outputln("try {");
213         outputln("_poa().activate_object(_" + _cformat2 + ");");
214         outputln(
215             "org.omg.CORBA.Object obj = _poa().servant_to_reference(_"
216                 + _cformat2
217                 + ");");
218         outputln(
219             _cformat1
220                 + " _ref = "
221                 + _cformat1
222                 + "Helper.narrow(obj);");
223         outputln("all_class_vector_" + _cformat2 + ".addElement(_ref);");
224         outputln("return _ref;");
225         outputln("}");
226         outputln("catch(java.lang.Exception ex){");
227         outputln("ex.printStackTrace();");
228         outputln("System.exit(0);");
229         outputln("}");
230         outputln("return null;");
231         outputln("}");
232     }
233
234     public void constructeurClass(org.omg.mof.Model.Class _class)
235         throws
236             org.omg.mof.Reflective.MofError,
237             org.omg.mof.Reflective.NotSet,
238             BadKind JavaDoc,
239             Bounds JavaDoc,
240             Bounds JavaDoc {
241
242         //String _nformat1 = helper.format1Idl2JavaConflict(_class.name());
243
String JavaDoc _nformat1 = javaHelper.javaClassName(_class);
244         String JavaDoc _cformat1 = javaHelper.javaQualifiedName(_class);
245         String JavaDoc _cformat2 = javaHelper.format2(javaHelper.javaClassName(_class));
246
247         if (_class
248             .visibility()
249             .equals(org.omg.mof.Model.VisibilityKind.public_vis)) {
250             output("public " + _nformat1 + "ClassImpl(");
251
252             // trouver toutes les attributs classifier_level de cette classe
253
org.omg.mof.Model.MofAttribute[] attributesND =
254                 mofHelper.allAttributesOfClass(
255                     _class,
256                     org.omg.mof.Model.ScopeKind.classifier_level,
257                     true);
258
259             for (int i = 0; i < attributesND.length; i++) {
260                 String JavaDoc _type_format1 =
261                     javaHelper.javaType(attributesND[i].type());
262                 String JavaDoc _aformat2 = javaHelper.format2(attributesND[i].name());
263                 if ((attributesND[i].multiplicity().upper > 1)
264                     || (attributesND[i].multiplicity().upper == -1))
265                     output(_type_format1 + "[] " + _aformat2 + "s");
266                 else
267                     output(_type_format1 + " " + _aformat2);
268                 output(", ");
269             }
270             outputln(
271                 javaHelper.format1Idl2JavaConflict(
272                     _class.container().name() + "Package")
273                     + " _model_package) {");
274             for (int i = 0; i < attributesND.length; i++) {
275                 String JavaDoc _aformat2 = javaHelper.format2(attributesND[i].name());
276                 int _lower = attributesND[i].multiplicity().lower;
277                 int _upper = attributesND[i].multiplicity().upper;
278                 if ((_upper > 1) || (_upper == -1)) {
279                     output(
280                         "for(int i=0;i<" + _aformat2 + "s.length; i++) ");
281                     output(
282                         " _"
283                             + _aformat2
284                             + "s.addElement("
285                             + _aformat2
286                             + "s[i]);");
287                 } else {
288                     outputln("_" + _aformat2 + " = " + _aformat2 + " ;");
289                     if ((_lower == 0) && (_upper == 1))
290                         outputln("_exist_" + _aformat2 + " = true;");
291                 }
292             }
293             outputln("this._model_package = _model_package ;");
294             outputln("}");
295             outputln("}");
296
297         }
298     }
299
300     public void classProxyImpl(org.omg.mof.Model.Class _class)
301         throws
302             org.omg.mof.Reflective.NotSet,
303             org.omg.mof.Reflective.MofError,
304             BadKind JavaDoc,
305             Bounds JavaDoc {
306
307         //les formats du nom de la classe
308
//String _nformat1 = helper.format1Idl2JavaConflict(_class.name());
309
String JavaDoc _nformat1 = javaHelper.javaClassName(_class);
310         String JavaDoc _cformat1 = javaHelper.javaQualifiedName(_class);
311         String JavaDoc _cformat2 = javaHelper.format2(javaHelper.javaClassName(_class));
312
313         //Décalration du Package
314
output("package ");
315
316         // Affichage du nom du package
317
outputln(
318             javaHelper.javaPackage(PackageHelper.narrow(_class.container()))
319                 + ";");
320
321         outputln(
322             "public class "
323                 + _nformat1
324                 + "ClassImpl extends "
325                 + _cformat1
326                 + "ClassPOA {");
327         outputln(
328             javaHelper.format1Idl2JavaConflict(
329                 _class.container().name() + "Package")
330                 + " _model_package;");
331
332         //genérer du code pour RefBaseObject et RefObject
333
output(javaHelper.refBaseObjectTemplate());
334         outputln();
335         output(javaHelper.refObjectTemplate());
336
337         // Tous les attributs classifier_level
338
org.omg.mof.Model.MofAttribute[] attributes =
339             mofHelper.allAttributesOfClass(
340                 _class,
341                 org.omg.mof.Model.ScopeKind.classifier_level,
342                 false);
343         for (int i = 0; i < attributes.length; i++) {
344             if (attributes[i]
345                 .visibility()
346                 .equals(org.omg.mof.Model.VisibilityKind.public_vis)) {
347                 String JavaDoc _code_attribute =
348                     javaHelper.attributeTemplate(attributes[i]);
349                 outputln(_code_attribute);
350             }
351         }
352
353         // Toutes les opérations classifier_level
354
org.omg.mof.Model.Operation[] operations =
355             mofHelper.allOperationsOfClass(
356                 _class,
357                 org.omg.mof.Model.ScopeKind.classifier_level);
358         for (int i = 0; i < operations.length; i++) {
359             if (operations[i]
360                 .visibility()
361                 .equals(org.omg.mof.Model.VisibilityKind.public_vis)) {
362                 String JavaDoc _code_operation =
363                     javaHelper.operationTemplate(operations[i]);
364                 outputln(_code_operation);
365             }
366         }
367
368         // Traitement des CreateClass, all_of_type et all_of_classes de la classe et de ses superclasses
369
//org.omg.mof.Model.GeneralizableElement[] _all_superclasses =
370
// _class.all_supertypes();
371
//java.util.Vector _sup_classes = new java.util.Vector();
372
//for (int i = 0; i < _all_superclasses.length; i++)
373
// _sup_classes.addElement(
374
// org.omg.mof.Model.ClassHelper.narrow(_all_superclasses[i]));
375
java.util.Vector JavaDoc _sup_classes = mofHelper.superClassesOrdered(_class);
376         java.util.Iterator JavaDoc it = _sup_classes.iterator();
377         while (it.hasNext()) {
378             org.omg.CORBA.Object JavaDoc obj = (org.omg.CORBA.Object JavaDoc)it.next();
379             if (obj._is_equivalent(_class))
380                 it.remove();
381         }
382
383         for (int i = 0; i < _sup_classes.size(); i++) {
384             org.omg.mof.Model.Class _current =
385                 (org.omg.mof.Model.Class) _sup_classes.elementAt(i);
386             if (_current
387                 .visibility()
388                 .equals(org.omg.mof.Model.VisibilityKind.public_vis)) {
389                 String JavaDoc _cu_format1 = javaHelper.javaQualifiedName(_current);
390                 String JavaDoc _cu_format2 = javaHelper.format2(javaHelper.className(_current));
391                 /* If the class is contained by another package */
392                 String JavaDoc _cu_format2_ref = getRefMethod(_current, _class.container());
393
394                 //Générer le code de la méthode Create et de la méthode all_of_classes
395
if (!_current.is_abstract()) {
396
397                     //CreateClass des SuperClasses
398
output(
399                         "public "
400                             + _cu_format1
401                             + " create_"
402                             + _cu_format2
403                             + "(");
404
405                     // trouver toutes les attributs de cette classe
406
org.omg.mof.Model.MofAttribute attributesND[] =
407                         mofHelper.allAttributesOfClass(
408                             _current,
409                             org.omg.mof.Model.ScopeKind.instance_level,
410                             false);
411                     for (int j = 0; j < attributesND.length; j++) {
412                         String JavaDoc _type_format1 =
413                             javaHelper.javaType(attributesND[j].type());
414                         String JavaDoc _aformat2 =
415                             javaHelper.format2(attributesND[j].name());
416                         if ((attributesND[j].multiplicity().upper > 1)
417                             || (attributesND[j].multiplicity().upper == -1)) {
418                             output(_type_format1 + "[] " + _aformat2);
419                         } else
420                             output(_type_format1 + " " + _aformat2);
421                         if (j != (attributesND.length - 1))
422                             output(", ");
423                     }
424                     outputln(") throws org.omg.mof.Reflective.MofError {");
425                     if (mofHelper.isInSameExtent(_current, _class)) {
426                         //supertype closure rule
427
output(
428                             _cu_format1
429                                 + " _"
430                                 + _cu_format2
431                                 + " = _model_package."
432                                 + _cu_format2_ref
433                                 + ".create_"
434                                 + _cu_format2
435                                 + "(");
436                         for (int j = 0; j < attributesND.length; j++) {
437                             String JavaDoc _aformat2 =
438                                 javaHelper.format2(attributesND[j].name());
439                             output(_aformat2);
440                             if (j != (attributesND.length - 1))
441                                 output(", ");
442                         }
443                         outputln(");");
444                         outputln("return _" + _cu_format2 + "; ");
445                     } else {
446                         outputln(
447                             "throw new org.omg.mof.Reflective.MofError();");
448                     }
449                     outputln("} ");
450
451                     //AllofClasses des SuperClasses
452
outputln(
453                         "public "
454                             + _cu_format1
455                             + "[] all_of_class_"
456                             + _cu_format2
457                             + "() {");
458                     outputln(
459                         _cu_format1 + "[] _" + _cu_format2 + ";");
460                     if (mofHelper.isInSameExtent(_current, _class)) {
461                         outputln(
462                             "_"
463                                 + _cu_format2
464                                 + " = _model_package."
465                                 + _cu_format2_ref
466                                 + ".all_of_class_"
467                                 + _cu_format2
468                                 + "();");
469                     } else {
470                         outputln(
471                             "_"
472                                 + _cu_format2
473                                 + " = new "
474                                 + _cu_format1
475                                 + "[0];");
476                     }
477                     outputln("return _" + _cu_format2 + "; ");
478                     outputln("} ");
479                     //END allOfClass
480
}
481
482                 //Générer le code de la méthode AllOfType de la classe courante
483
outputln(
484                     "public "
485                         + _cu_format1
486                         + "[] all_of_type_"
487                         + _cu_format2
488                         + "() {");
489                 outputln(_cu_format1 + "[] _" + _cu_format2 + ";");
490                 if (mofHelper.isInSameExtent(_current, _class)) {
491                     outputln(
492                         "_"
493                             + _cu_format2
494                             + " = _model_package."
495                             + _cu_format2_ref
496                             + ".all_of_type_"
497                             + _cu_format2
498                             + "();");
499                 } else {
500                     outputln(
501                         "_"
502                             + _cu_format2
503                             + " = new "
504                             + _cu_format1
505                             + "[0];");
506                 }
507                 outputln("return _" + _cu_format2 + "; ");
508                 outputln("} ");
509                 //END allOfType
510
}
511         }
512
513         //Générer le code de la méthode AllOfType et AllofClasses et create Class Ade la classe courante
514
if (!_class.is_abstract()) {
515             createClass(_class);
516             allOfClasses(_class);
517         }
518
519         allOfType(_class);
520
521         constructeurClass(_class);
522     }
523     
524     /**
525      * Returns the ref method of a class contained in a package.
526      * And tests if the ref method is present in the package or in an imported package.
527      * @param clazz The class
528      * @param parent The pacakge
529      * @return The string representation of the ref method calling
530      */

531     private String JavaDoc getRefMethod(org.omg.mof.Model.Class clazz, org.omg.mof.Model.Namespace parent)
532         throws org.omg.mof.Reflective.MofError, org.omg.mof.Reflective.NotSet {
533         String JavaDoc _format2_ref = javaHelper.format2(javaHelper.javaClassName(clazz)) + "_ref()";
534         try {
535             if (!clazz.container()._is_equivalent(parent)) {
536                 _format2_ref = javaHelper.format2(clazz.container().name()) + "_ref()." + _format2_ref;
537             }
538         } catch (org.omg.mof.Reflective.NotSet notSet) {
539             // Nothing to do
540
}
541         return _format2_ref;
542     }
543                 
544     /**
545      * Returns the javaHelper.
546      * @return JavaCommon
547      */

548     public JavaCommon getJavaHelper() {
549         return javaHelper;
550     }
551
552     /**
553      * Returns the mofHelper.
554      * @return MOFCommon
555      */

556     public MOFCommon getMofHelper() {
557         return mofHelper;
558     }
559
560     /**
561      * Sets the javaHelper.
562      * @param javaHelper The javaHelper to set
563      */

564     public void setJavaHelper(JavaCommon javaHelper) {
565         this.javaHelper = javaHelper;
566     }
567
568     /**
569      * Sets the mofHelper.
570      * @param mofHelper The mofHelper to set
571      */

572     public void setMofHelper(MOFCommon mofHelper) {
573         this.mofHelper = mofHelper;
574     }
575
576 }
577
Popular Tags