KickJava   Java API By Example, From Geeks To Geeks.

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


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.objectweb.modfact.corba.generator.BracketGenerator;
23 import org.objectweb.modfact.corba.helper.JavaCommon;
24 import org.objectweb.modfact.corba.helper.MOFCommon;
25 import org.objectweb.modfact.corba.logging.ModFactLogger;
26
27 import java.util.Vector JavaDoc;
28
29 import org.omg.CORBA.TypeCodePackage.BadKind JavaDoc;
30 import org.omg.CORBA.TypeCodePackage.Bounds JavaDoc;
31 import org.omg.mof.Model.Import;
32 import org.omg.mof.Model.Namespace;
33 import org.omg.mof.Model.PackageHelper;
34 import org.omg.mof.Model.ScopeKind;
35 import org.omg.mof.Model.VisibilityKind;
36
37 public class PackageImplementationGenerator extends BracketGenerator {
38
39     ModFactLogger logger = null;
40
41     org.omg.mof.Model.Package[] input;
42
43     /**
44      * Set Input
45      */

46     public void setInput(org.omg.mof.Model.ModelElement[] elt) {
47         input = new org.omg.mof.Model.Package[elt.length];
48         for (int i = 0; i < input.length; i++) {
49             input[i] = (org.omg.mof.Model.Package) elt[i];
50         }
51     }
52
53     /**
54      * Set Trace
55      */

56     public void setLogger(ModFactLogger log) {
57         logger = log;
58     }
59
60     //Helper
61
JavaCommon javaHelper;
62     MOFCommon mofHelper;
63
64     //Log facility
65
// Logger logger = Logger.getLogger("fr.lip6.src.mdafactory.generation.idl2javaImpl.PackageImplementation");
66

67     public void generate()
68         throws
69             org.omg.mof.Reflective.NotSet,
70             org.omg.mof.Reflective.MofError,
71             org.omg.CORBA.TypeCodePackage.BadKind JavaDoc,
72             org.omg.CORBA.TypeCodePackage.Bounds JavaDoc {
73         outputln(
74             "// le Code généré du Package: " + input[0].name() + "Package");
75         packageImpl(input[0]);
76         flushFile();
77     }
78
79     public void constructeurPackage(org.omg.mof.Model.Package _package)
80         throws
81             org.omg.mof.Reflective.MofError,
82             org.omg.mof.Reflective.NotSet,
83             BadKind JavaDoc,
84             Bounds JavaDoc {
85         String JavaDoc className =
86             javaHelper.format1Idl2JavaConflict(_package.name() + "Package")
87                 + "Impl";
88         String JavaDoc _nformat1 = javaHelper.format1Idl2JavaConflict(_package.name());
89         String JavaDoc _pformat1 = javaHelper.javaQualifiedName(_package);
90         String JavaDoc _pformat2 = javaHelper.format2(_package.name());
91
92         Vector JavaDoc _attributes = new Vector JavaDoc();
93         org.omg.mof.Model.Class[] classes =
94             mofHelper.classesOfPackage(_package);
95         for (int i = 0; i < classes.length; i++) {
96             org.omg.mof.Model.MofAttribute[] attributes =
97                 mofHelper.attributesOfClass(
98                     classes[i],
99                     ScopeKind.classifier_level,
100                     true);
101             for (int k = 0; k < attributes.length; k++)
102                 _attributes.addElement(attributes[k]);
103
104         }
105
106         java.util.Vector JavaDoc _attributes_name =
107             new java.util.Vector JavaDoc(_attributes.size());
108         for (int i = 0; i < _attributes.size(); i++) {
109             org.omg.mof.Model.MofAttribute _current =
110                 (org.omg.mof.Model.MofAttribute) _attributes.elementAt(i);
111             String JavaDoc _attributeqname = " ";
112             for (int j = 0; j < _current.qualified_name().length; j++) {
113                 if (j > 0)
114                     _attributeqname =
115                         javaHelper.format2(_current.qualified_name()[j])
116                             + "_"
117                             + _attributeqname;
118                 else
119                     _attributeqname =
120                         javaHelper.format2(_current.qualified_name()[j]);
121             }
122             _attributes_name.addElement(_attributeqname);
123         }
124
125         output("public " + className + "(");
126         for (int i = 0; i < _attributes.size(); i++) {
127             org.omg.mof.Model.MofAttribute _current =
128                 (org.omg.mof.Model.MofAttribute) _attributes.elementAt(i);
129             String JavaDoc _current_type = javaHelper.javaType(_current.type());
130             String JavaDoc _attributeqname = (String JavaDoc) _attributes_name.elementAt(i);
131
132             if (!((i != 0) || (i != (_attributes.size() - 1))))
133                 output(", ");
134             if ((_current.multiplicity().upper > 1)
135                 || (_current.multiplicity().upper == -1))
136                 output(_current_type + "[] " + _attributeqname);
137             else
138                 output(_current_type + " " + _attributeqname);
139         }
140         outputln(") {");
141         for (int i = 0; i < _attributes.size(); i++) {
142             org.omg.mof.Model.MofAttribute _current =
143                 (org.omg.mof.Model.MofAttribute) _attributes.elementAt(i);
144             String JavaDoc _aformat2 = javaHelper.format2(_current.name());
145             String JavaDoc _attributeqname = (String JavaDoc) _attributes_name.elementAt(i);
146             output("_" + _aformat2 + "_val = " + _attributeqname);
147             output("; ");
148
149         }
150         outputln("} ");
151     }
152
153     public void packageImpl(org.omg.mof.Model.Package _package)
154         throws
155             org.omg.mof.Reflective.NotSet,
156             org.omg.mof.Reflective.MofError,
157             BadKind JavaDoc,
158             Bounds JavaDoc {
159
160         //Déclaration du Package
161
output("package ");
162
163         // les deux formats du nom du package
164
String JavaDoc className =
165             javaHelper.format1Idl2JavaConflict(_package.name() + "Package")
166                 + "Impl";
167         String JavaDoc superClass =
168             javaHelper.format1Idl2JavaConflict(_package.name() + "Package")
169                 + "POA";
170         String JavaDoc _pformat1 = javaHelper.javaQualifiedName(_package);
171         String JavaDoc _pformat2 = javaHelper.format2(_package.name());
172
173         // le nom du package
174
outputln(javaHelper.javaPackage(_package) + " ;");
175
176         outputln(
177             "public class " + className + " extends " + superClass + " {");
178
179         // le code du RefBaseObject et du RefPackageObject
180
output(javaHelper.refBaseObjectTemplate());
181         outputln();
182         output(javaHelper.refPackageTemplate());
183
184         // Création de classe
185
org.omg.mof.Model.Class classes[] =
186             mofHelper.classesOfPackage(_package);
187
188         for (int k = 0; k < classes.length; k++) {
189             if (classes[k]
190                 .visibility()
191                 .equals(org.omg.mof.Model.VisibilityKind.public_vis)) {
192                 String JavaDoc _cformat1 = javaHelper.javaQualifiedName(classes[k]);
193                 String JavaDoc _cformat2 = javaHelper.format2(javaHelper.javaClassName(classes[k]));
194
195                 //Déclaration des attributs statiques
196
org.omg.mof.Model.MofAttribute[] attributes =
197                     mofHelper.allAttributesOfClass(
198                         classes[k],
199                         org.omg.mof.Model.ScopeKind.classifier_level,
200                         true);
201                 for (int i = 0; i < attributes.length; i++) {
202                     String JavaDoc _type_format1 =
203                         javaHelper.javaType(attributes[i].type());
204                     String JavaDoc _aformat2 = javaHelper.format2(attributes[i].name());
205                     if ((attributes[i].multiplicity().upper > 1)
206                         || (attributes[i].multiplicity().upper == -1))
207                         output(
208                             _type_format1
209                                 + "[] _"
210                                 + _aformat2
211                                 + "_val ;");
212                     else
213                         output(
214                             _type_format1 + " _" + _aformat2 + "_val ;");
215                 }
216
217                 outputln(
218                     "private "
219                         + _cformat1
220                         + "Class _"
221                         + _cformat2
222                         + "_ref = null ;");
223                 outputln(
224                     "public " + _cformat1 + "Class " + _cformat2 + "_ref(){");
225                 outputln(" if( _" + _cformat2 + "_ref == null ) {");
226                 output(
227                     _cformat1
228                         + "ClassImpl _"
229                         + _cformat2
230                         + "_temp = new "
231                         + _cformat1
232                         + "ClassImpl(");
233                 for (int i = 0; i < attributes.length; i++) {
234                     String JavaDoc _aformat2 = javaHelper.format2(attributes[i].name());
235                     output("_" + _aformat2 + "_val");
236                     output(", ");
237                 }
238                 outputln("_this());");
239                 outputln("try {");
240                 outputln(
241                     "_poa().activate_object(_" + _cformat2 + "_temp);");
242                 outputln(
243                     "org.omg.CORBA.Object obj = _poa().servant_to_reference(_"
244                         + _cformat2
245                         + "_temp);");
246                 outputln(
247                     "_"
248                         + _cformat2
249                         + "_ref = "
250                         + _cformat1
251                         + "ClassHelper.narrow(obj);");
252                 outputln("}");
253                 outputln("catch (java.lang.Exception ex) {");
254                 outputln("ex.printStackTrace();");
255                 outputln("System.exit(0);");
256                 outputln("}");
257                 outputln("}");
258                 outputln("return _" + _cformat2 + "_ref;");
259                 outputln("}");
260             }
261
262         }
263
264         //Association
265
org.omg.mof.Model.Association[] associations =
266             mofHelper.associationsOfPackage(_package);
267
268         for (int k = 0; k < associations.length; k++) {
269             if (associations[k]
270                 .visibility()
271                 .equals(org.omg.mof.Model.VisibilityKind.public_vis)) {
272                 String JavaDoc _aformat1 =
273                     javaHelper.javaQualifiedName(associations[k]);
274                 String JavaDoc _aformat2 = javaHelper.format2(associations[k].name());
275                 outputln(
276                     "private "
277                         + _aformat1
278                         + " _"
279                         + _aformat2
280                         + "_ref = null ;");
281                 outputln(
282                     "public " + _aformat1 + " " + _aformat2 + "_ref(){");
283                 outputln(" if( _" + _aformat2 + "_ref == null ) {");
284                 outputln(
285                     _aformat1
286                         + "Impl _"
287                         + _aformat2
288                         + "_temp = new "
289                         + _aformat1
290                         + "Impl();");
291                 outputln("try {");
292                 outputln(
293                     "_poa().activate_object(_" + _aformat2 + "_temp);");
294                 outputln(
295                     "org.omg.CORBA.Object obj = _poa().servant_to_reference(_"
296                         + _aformat2
297                         + "_temp);");
298                 outputln(
299                     "_"
300                         + _aformat2
301                         + "_ref = "
302                         + _aformat1
303                         + "Helper.narrow(obj);");
304                 outputln("}");
305                 outputln("catch (java.lang.Exception ex){");
306                 outputln("ex.printStackTrace();");
307                 outputln("System.exit(0);");
308                 outputln("}");
309                 outputln("}");
310                 outputln("return _" + _aformat2 + "_ref ;");
311                 outputln("}");
312             }
313         }
314
315         //nested packages
316

317         org.omg.mof.Model.Import[] imports =
318             mofHelper.importsOfPackage(_package);
319         for (int i = 0; i < imports.length; i++) {
320             Import _import = imports[i];
321             Namespace imported = _import.imported_namespace();
322             if (imported._is_a(PackageHelper.id())) {
323                 org.omg.mof.Model.Package cluteredPackage =
324                     PackageHelper.narrow(imported);
325                 if ((_import.visibility().equals(VisibilityKind.public_vis))
326                     && (_import.is_clustered())
327                     && (cluteredPackage
328                         .visibility()
329                         .equals(VisibilityKind.public_vis))) {
330
331                     Namespace importedNamespace =
332                         imports[i].imported_namespace();
333                     if (importedNamespace._is_a(PackageHelper.id())) {
334                         org.omg.mof.Model.Package importedPackage =
335                             PackageHelper.narrow(importedNamespace);
336                         String JavaDoc _aformat1 =
337                             javaHelper.javaPackage(importedPackage)+"."+javaHelper.format1Idl2JavaConflict(importedPackage.name() + "Package");
338                         String JavaDoc _aformat2 = javaHelper.format2(importedPackage.name());
339                         outputln(
340                             "private "
341                                 + _aformat1
342                                 + " _"
343                                 + _aformat2
344                                 + "_ref = null ;");
345                         outputln(
346                             "public "
347                                 + _aformat1
348                                 + " "
349                                 + _aformat2
350                                 + "_ref(){");
351                         outputln(
352                             " if( _" + _aformat2 + "_ref == null ) {");
353                         outputln(
354                             _aformat1
355                                 + "Impl _"
356                                 + _aformat2
357                                 + "_temp = new "
358                                 + _aformat1
359                                 + "Impl();");
360                         outputln("try {");
361                         outputln(
362                             "_poa().activate_object(_"
363                                 + _aformat2
364                                 + "_temp);");
365                         outputln(
366                             "org.omg.CORBA.Object obj = _poa().servant_to_reference(_"
367                                 + _aformat2
368                                 + "_temp);");
369                         outputln(
370                             "_"
371                                 + _aformat2
372                                 + "_ref = "
373                                 + _aformat1
374                                 + "Helper.narrow(obj);");
375                         outputln("}");
376                         outputln("catch (java.lang.Exception ex){");
377                         outputln("ex.printStackTrace();");
378                         outputln("System.exit(0);");
379                         outputln("}");
380                         outputln("}");
381                         outputln("return _" + _aformat2 + "_ref ;");
382                         outputln("}");
383                     }
384                 }
385             }
386         }
387
388         org.omg.mof.Model.Package[] packages =
389             mofHelper.packagesOfPackage(_package);
390         for (int i = 0; i < packages.length; i++) {
391             org.omg.mof.Model.Package nestedPackage = packages[i];
392             if (nestedPackage.visibility().equals(VisibilityKind.public_vis)) {
393                 String JavaDoc _aformat1 = javaHelper.javaQualifiedName(packages[i])+"."+javaHelper.format1Idl2JavaConflict(packages[i].name() + "Package");
394                 String JavaDoc _aformat2 = javaHelper.format2(packages[i].name());
395                 outputln(
396                     "private "
397                         + _aformat1
398                         + " _"
399                         + _aformat2
400                         + "_ref = null ;");
401                 outputln(
402                     "public " + _aformat1 + " " + _aformat2 + "_ref(){");
403                 outputln(" if( _" + _aformat2 + "_ref == null ) {");
404                 outputln(
405                     _aformat1
406                         + "Impl _"
407                         + _aformat2
408                         + "_temp = new "
409                         + _aformat1
410                         + "Impl();");
411                 outputln("try {");
412                 outputln(
413                     "_poa().activate_object(_" + _aformat2 + "_temp);");
414                 outputln(
415                     "org.omg.CORBA.Object obj = _poa().servant_to_reference(_"
416                         + _aformat2
417                         + "_temp);");
418                 outputln(
419                     "_"
420                         + _aformat2
421                         + "_ref = "
422                         + _aformat1
423                         + "Helper.narrow(obj);");
424                 outputln("}");
425                 outputln("catch (java.lang.Exception ex){");
426                 outputln("ex.printStackTrace();");
427                 outputln("System.exit(0);");
428                 outputln("}");
429                 outputln("}");
430                 outputln("return _" + _aformat2 + "_ref ;");
431                 outputln("}");
432
433             }
434         }
435
436         constructeurPackage(_package);
437         outputln("}");
438     }
439     /**
440      * Returns the javaHelper.
441      * @return JavaCommon
442      */

443     public JavaCommon getJavaHelper() {
444         return javaHelper;
445     }
446
447     /**
448      * Returns the mofHelper.
449      * @return MOFCommon
450      */

451     public MOFCommon getMofHelper() {
452         return mofHelper;
453     }
454
455     /**
456      * Sets the javaHelper.
457      * @param javaHelper The javaHelper to set
458      */

459     public void setJavaHelper(JavaCommon javaHelper) {
460         this.javaHelper = javaHelper;
461     }
462
463     /**
464      * Sets the mofHelper.
465      * @param mofHelper The mofHelper to set
466      */

467     public void setMofHelper(MOFCommon mofHelper) {
468         this.mofHelper = mofHelper;
469     }
470
471 }
472
Popular Tags