KickJava   Java API By Example, From Geeks To Geeks.

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


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 ClassImplementationGenerator 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 Class: " + input[0].name());
66         classImpl(input[0]);
67         flushFile();
68     }
69
70     public void constructeurClass(org.omg.mof.Model.Class _class)
71         throws org.omg.mof.Reflective.MofError, org.omg.mof.Reflective.NotSet , BadKind JavaDoc , Bounds JavaDoc {
72
73         String JavaDoc _nformat1 = javaHelper.format1Idl2JavaConflict(javaHelper.javaClassName(_class));
74         String JavaDoc _cformat1 = javaHelper.javaQualifiedName(_class);
75         String JavaDoc _cformat2 = javaHelper.format2(javaHelper.javaClassName(_class));
76
77         if (_class
78             .visibility()
79             .equals(org.omg.mof.Model.VisibilityKind.public_vis)) {
80             output("public " + _nformat1 + "Impl(");
81
82             // trouver toutes les attributs classifier_level de cette classe
83
org.omg.mof.Model.MofAttribute[] attributesND =
84                 mofHelper.allAttributesOfClass(
85                     _class,
86                     org.omg.mof.Model.ScopeKind.instance_level,
87                     false);
88
89             for (int i = 0; i < attributesND.length; i++) {
90                 String JavaDoc _type_format1 = javaHelper.javaType(attributesND[i].type());
91                 String JavaDoc _aformat2 = javaHelper.format2(attributesND[i].name());
92                 if ((attributesND[i].multiplicity().upper > 1)
93                     || (attributesND[i].multiplicity().upper == -1))
94                     output(_type_format1 + "[] " + _aformat2 + "s");
95                 else
96                     output(_type_format1 + " " + _aformat2);
97                 output(", ");
98             }
99             outputln(_cformat1 + "Class _proxy){");
100             for (int i = 0; i < attributesND.length; i++) {
101                 String JavaDoc _aformat2 = javaHelper.format2(attributesND[i].name());
102                 int _lower = attributesND[i].multiplicity().lower;
103                 int _upper = attributesND[i].multiplicity().upper;
104                 if ((_upper > 1) || (_upper == -1)) {
105                     output(
106                         "for(int i=0;i<" + _aformat2 + "s.length; i++) ");
107                     output(
108                         " _"
109                             + _aformat2
110                             + "s.addElement("
111                             + _aformat2
112                             + "s[i]);");
113                 } else {
114                     outputln("_" + _aformat2 + " = " + _aformat2 + " ;");
115                     if ((_lower == 0) && (_upper == 1))
116                         outputln("_exist_" + _aformat2 + " = true;");
117                 }
118             }
119             outputln("_class_proxy = _proxy ;");
120             outputln(" }");
121             outputln("}");
122
123         }
124     }
125
126     public void allOfType(org.omg.mof.Model.Class _class)
127         throws org.omg.mof.Reflective.MofError , NotSet{
128         String JavaDoc _cformat1 = javaHelper.javaQualifiedName(_class);
129         String JavaDoc _cformat2 = javaHelper.format2(javaHelper.javaClassName(_class));
130         outputln(
131             "public " + _cformat1 + "[] all_of_type_" + _cformat2 + "() {");
132         outputln("return _class_proxy.all_of_type_" + _cformat2 + "();");
133         outputln("}");
134     }
135
136     public void allOfClasses(org.omg.mof.Model.Class _class)
137         throws org.omg.mof.Reflective.MofError , NotSet{
138         String JavaDoc _cformat1 = javaHelper.javaQualifiedName(_class);
139         String JavaDoc _cformat2 = javaHelper.format2(javaHelper.javaClassName(_class));
140         outputln(
141             "public " + _cformat1 + "[] all_of_class_" + _cformat2 + "() {");
142         outputln(
143             "return _class_proxy.all_of_class_" + _cformat2 + "();");
144         outputln("}");
145     }
146
147     public void createClass(org.omg.mof.Model.Class _class)
148         throws org.omg.mof.Reflective.MofError , BadKind JavaDoc , Bounds JavaDoc , NotSet{
149         String JavaDoc _cformat1 = javaHelper.javaQualifiedName(_class);
150         String JavaDoc _cformat2 = javaHelper.format2(javaHelper.javaClassName(_class));
151         if (_class
152             .visibility()
153             .equals(org.omg.mof.Model.VisibilityKind.public_vis)) {
154             output("public " + _cformat1 + " create_" + _cformat2 + "(");
155             org.omg.mof.Model.MofAttribute[] attributesND =
156                 mofHelper.allAttributesOfClass(
157                     _class,
158                     org.omg.mof.Model.ScopeKind.instance_level,
159                     false);
160             for (int i = 0; i < attributesND.length; i++) {
161                 String JavaDoc _type_format1 = javaHelper.javaType(attributesND[i].type());
162                 String JavaDoc _aformat2 = javaHelper.format2(attributesND[i].name());
163                 if ((attributesND[i].multiplicity().upper > 1)
164                     || (attributesND[i].multiplicity().upper == -1))
165                     output(_type_format1 + "[] " + _aformat2);
166                 else
167                     output(_type_format1 + " " + _aformat2);
168                 if (i != (attributesND.length - 1))
169                     output(", ");
170             }
171             outputln(") throws org.omg.mof.Reflective.MofError {");
172             output("return _class_proxy.create_" + _cformat2 + "(");
173             for (int i = 0; i < attributesND.length; i++) {
174                 String JavaDoc _aformat2 = javaHelper.format2(attributesND[i].name());
175                 output(_aformat2);
176                 if (i != (attributesND.length - 1))
177                     output(", ");
178
179             }
180             outputln(");");
181             outputln("}");
182         }
183     }
184
185     public void classImpl(org.omg.mof.Model.Class _class)
186         throws org.omg.mof.Reflective.NotSet, org.omg.mof.Reflective.MofError , BadKind JavaDoc , Bounds JavaDoc{
187
188         //les formats du nom de la classe
189
String JavaDoc _nformat1 = javaHelper.format1Idl2JavaConflict(javaHelper.javaClassName(_class));
190         String JavaDoc _cformat1 = javaHelper.javaQualifiedName(_class);
191         String JavaDoc _cformat2 = javaHelper.format2(javaHelper.javaClassName(_class));
192
193         //Décalration du Package
194
output("package ");
195
196         // Affichage du nom du package
197
outputln(javaHelper.javaPackage(PackageHelper.narrow(_class.container())) + " ;");
198
199         outputln(
200             "public class "
201                 + _nformat1
202                 + "Impl extends "
203                 + _cformat1
204                 + "POA {");
205         outputln();
206
207         //genérer du code pour RefBaseObject et RefObject
208
output(javaHelper.refBaseObjectTemplate());
209         outputln();
210         output(javaHelper.refObjectTemplate());
211
212         //Déclaration de classe proxy
213
outputln("private " + _cformat1 + "Class _class_proxy;");
214
215         // Tous les attributs classifier_level
216
org.omg.mof.Model.MofAttribute[] attributesC =
217             mofHelper.allAttributesOfClass(
218                 _class,
219                 org.omg.mof.Model.ScopeKind.classifier_level,
220                 true);
221         for (int i = 0; i < attributesC.length; i++) {
222             if (attributesC[i]
223                 .visibility()
224                 .equals(org.omg.mof.Model.VisibilityKind.public_vis)) {
225                 String JavaDoc _code_attribute =
226                     javaHelper.attributeTemplate(attributesC[i]);
227                 outputln(_code_attribute);
228             }
229         }
230
231         // Toutes les opérations classifier_level
232
org.omg.mof.Model.Operation[] operationsC =
233             mofHelper.allOperationsOfClass(
234                 _class,
235                 org.omg.mof.Model.ScopeKind.classifier_level);
236         for (int i = 0; i < operationsC.length; i++) {
237             if (operationsC[i]
238                 .visibility()
239                 .equals(org.omg.mof.Model.VisibilityKind.public_vis)) {
240                 String JavaDoc _code_operation =
241                     javaHelper.operationTemplate(operationsC[i]);
242                 outputln(_code_operation);
243             }
244         }
245
246         // Traitement des CreateClass, all_of_type et all_of_classes de la classe et de ses superclasses
247
java.util.Vector JavaDoc _sup_classes = mofHelper.superClassesOrdered(_class);
248
249         //Générer le code de la méthode Create et de la méthode all_of_classes
250
for (int i = 0; i < _sup_classes.size(); i++) {
251             org.omg.mof.Model.Class _current =
252                 (org.omg.mof.Model.Class) _sup_classes.elementAt(i);
253             if (!_current.is_abstract()) {
254                 createClass(_current);
255                 allOfClasses(_current);
256             }
257             allOfType(_current);
258         }
259
260         // Tous les attributs Instance_level
261
org.omg.mof.Model.MofAttribute[] attributes =
262             mofHelper.allAttributesOfClass(
263                 _class,
264                 org.omg.mof.Model.ScopeKind.instance_level,
265                 true);
266         for (int i = 0; i < attributes.length; i++) {
267             if (attributes[i]
268                 .visibility()
269                 .equals(org.omg.mof.Model.VisibilityKind.public_vis)) {
270                 String JavaDoc _code_attribute =
271                     javaHelper.attributeTemplate(attributes[i]);
272                 outputln(_code_attribute);
273             }
274         }
275
276         // Toutes les opérations Instance_level
277
org.omg.mof.Model.Operation[] operations =
278             mofHelper.allOperationsOfClass(
279                 _class,
280                 org.omg.mof.Model.ScopeKind.instance_level);
281         for (int i = 0; i < operations.length; i++) {
282             if (operations[i]
283                 .visibility()
284                 .equals(org.omg.mof.Model.VisibilityKind.public_vis)) {
285                 String JavaDoc _code_operation =
286                     javaHelper.operationTemplate(operations[i]);
287                 outputln(_code_operation);
288             }
289         }
290
291         // Toutes les références Instance et Classifier level
292
org.omg.mof.Model.Reference[] references =
293             mofHelper.allReferencesOfClass(_class);
294         for (int i = 0; i < references.length; i++) {
295             if (references[i]
296                 .visibility()
297                 .equals(org.omg.mof.Model.VisibilityKind.public_vis)) {
298                 String JavaDoc _code_reference =
299                     javaHelper.referenceTemplate(references[i]);
300                 outputln(_code_reference);
301             }
302         }
303         constructeurClass(_class);
304     }
305
306     /**
307      * Returns the javaHelper.
308      * @return JavaCommon
309      */

310     public JavaCommon getJavaHelper() {
311         return javaHelper;
312     }
313
314     /**
315      * Returns the mofHelper.
316      * @return MOFCommon
317      */

318     public MOFCommon getMofHelper() {
319         return mofHelper;
320     }
321
322     /**
323      * Sets the javaHelper.
324      * @param javaHelper The javaHelper to set
325      */

326     public void setJavaHelper(JavaCommon javaHelper) {
327         this.javaHelper = javaHelper;
328     }
329
330     /**
331      * Sets the mofHelper.
332      * @param mofHelper The mofHelper to set
333      */

334     public void setMofHelper(MOFCommon mofHelper) {
335         this.mofHelper = mofHelper;
336     }
337
338 }
Popular Tags