KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import org.omg.mof.Model.PackageHelper;
24 import org.objectweb.modfact.corba.generator.BracketGenerator;
25 import org.objectweb.modfact.corba.helper.JavaCommon;
26 import org.objectweb.modfact.corba.helper.MOFCommon;
27 import org.objectweb.modfact.corba.logging.ModFactLogger;
28
29 public class AssociationImplementationGenerator extends BracketGenerator {
30
31     ModFactLogger logger = null;
32
33     org.omg.mof.Model.Association[] input;
34
35     /**
36      * Set Input
37      */

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

48     public void setLogger(ModFactLogger log) {
49         logger = log;
50     }
51
52     //Helper
53
JavaCommon javaHelper;
54     MOFCommon mofHelper;
55
56     public void generate()
57         throws org.omg.mof.Reflective.NotSet, org.omg.mof.Reflective.MofError {
58
59         //logger.log(Level.FINE, "Association Implementation startGeneration");
60
outputln("// le Code généré de l'association: " + input[0].name());
61         associationImpl(input[0]);
62         flushFile();
63     }
64
65     public void associationImpl(
66         org.omg.mof.Model.Association _association)
67         throws org.omg.mof.Reflective.NotSet, org.omg.mof.Reflective.MofError {
68
69         //les formats du nom de l'association
70
String JavaDoc _nformat1 = javaHelper.format1Idl2JavaConflict(_association.name());
71         String JavaDoc _aformat1 = javaHelper.javaQualifiedName(_association);
72         String JavaDoc _aformat2 = javaHelper.format2(_association.name());
73
74         //Décalration du Package
75
output("package ");
76
77         // Affichage du nom du package
78
outputln(javaHelper.javaPackage(PackageHelper.narrow(_association.container())) + " ;");
79
80         outputln(
81             "public class "
82                 + _nformat1
83                 + "Impl extends "
84                 + _aformat1
85                 + "POA {");
86         outputln(
87                 javaHelper.format1Idl2JavaConflict(_association.container().name()+"Package")
88                 + " _model_package;");
89
90         //Affichage de RefBaseObject et RefAssociation
91
output(javaHelper.refBaseObjectTemplate());
92         outputln();
93         output(javaHelper.refAssociationTemplate());
94
95         //Code propre à l'association
96
org.omg.mof.Model.AssociationEnd[] _ends =
97             mofHelper.associationEndsOfAssociation(_association);
98         org.omg.mof.Model.AssociationEnd _source = _ends[0];
99         org.omg.mof.Model.AssociationEnd _dest = _ends[1];
100
101         //Associationd_end1
102
String JavaDoc _sourceformat1 = javaHelper.javaQualifiedName(_source.type());
103         String JavaDoc _sourceformat2 = javaHelper.format2(_source.name());
104         String JavaDoc _sourceformat2JavaConflict = javaHelper.format2Idl2JavaConflict(_source.name());
105         boolean _source_navig = _source.is_navigable();
106         boolean _source_chang = _source.is_changeable();
107         org.omg.mof.Model.MultiplicityType _msource = _source.multiplicity();
108         boolean _source_ordered = _msource.isOrdered;
109         boolean _source_unique = _msource.isUnique;
110         int _source_upper = _msource.upper;
111         //int _source_lower = _msource.lower ;
112

113         //Association_end2
114
String JavaDoc _destformat1 = javaHelper.javaQualifiedName(_dest.type());
115         String JavaDoc _destformat2 = javaHelper.format2(_dest.name());
116         String JavaDoc _destformat2JavaConflict = javaHelper.format2Idl2JavaConflict(_dest.name());
117         boolean _dest_navig = _dest.is_navigable();
118         boolean _dest_chang = _dest.is_changeable();
119         org.omg.mof.Model.MultiplicityType _mdest = _dest.multiplicity();
120         boolean _dest_ordered = _mdest.isOrdered;
121         boolean _dest_unique = _mdest.isUnique;
122         int _dest_upper = _mdest.upper;
123         //int _dest_lower = _mdest.lower ;
124

125         outputln(
126             "private java.util.Vector _all_"
127                 + _aformat2
128                 + " = new java.util.Vector();");
129
130         outputln("// Operation all_is_of_type_links");
131         outputln(
132             "public "
133                 + _aformat1
134                 + "Link[] all_"
135                 + _aformat2
136                 + "_Links() throws org.omg.mof.Reflective.MofError {");
137         outputln(
138             _aformat1
139                 + "Link[] temp1 = new "
140                 + _aformat1
141                 + "Link [_all_"
142                 + _aformat2
143                 + ".size()];");
144         outputln(
145             "for (int i=0; i< _all_"
146                 + _aformat2
147                 + ".size();i++) temp1[i]= ("
148                 + _aformat1
149                 + "Link)_all_"
150                 + _aformat2
151                 + ".elementAt(i);");
152         outputln("return temp1;");
153         outputln("}");
154
155         outputln("//Operation exists");
156         outputln(
157             "public boolean exists("
158                 + _sourceformat1
159                 + " "
160                 + _sourceformat2JavaConflict
161                 + ", "
162                 + _destformat1
163                 + " "
164                 + _destformat2JavaConflict
165                 + ") throws org.omg.mof.Reflective.MofError {");
166         outputln("boolean exist = false;");
167         outputln("int i= 0;");
168         outputln("while((i< _all_" + _aformat2 + ".size())&& !exist){");
169         outputln(
170             _aformat1
171                 + "Link temp1 = ("
172                 + _aformat1
173                 + "Link)_all_"
174                 + _aformat2
175                 + ".elementAt(i);");
176         outputln(
177             "if (temp1."
178                 + _sourceformat2JavaConflict
179                 + "._is_equivalent("
180                 + _sourceformat2JavaConflict
181                 + ") && temp1."
182                 + _destformat2JavaConflict
183                 + "._is_equivalent("
184                 + _destformat2JavaConflict
185                 + ")) exist = true;");
186         outputln("i++;");
187         outputln("}");
188         outputln("return exist ;");
189         outputln("} ");
190
191         if (_source_navig) {
192             outputln("// Operation " + _sourceformat2);
193             output("public " + _sourceformat1);
194             if ((_source_upper > 1) || (_source_upper == -1)) {
195                 outputln(
196                     "[] "
197                         + _sourceformat2JavaConflict
198                         + " ("
199                         + _destformat1
200                         + " "
201                         + _destformat2JavaConflict
202                         + ") throws org.omg.mof.Reflective.MofError {");
203                 outputln(
204                     "java.util.Vector temp1 = new java.util.Vector();");
205                 outputln(
206                     "for (int i=0; i< _all_"
207                         + _aformat2
208                         + ".size(); i++){");
209                 outputln(
210                     _aformat1
211                         + "Link temp2 = ("
212                         + _aformat1
213                         + "Link)_all_"
214                         + _aformat2
215                         + ".elementAt(i);");
216                 outputln(
217                     "if (temp2."
218                         + _destformat2JavaConflict
219                         + "._is_equivalent("
220                         + _destformat2JavaConflict
221                         + ")) temp1.addElement(temp2."
222                         + _sourceformat2JavaConflict
223                         + ");");
224                 outputln("}");
225                 outputln(
226                     _sourceformat1
227                         + "[] temp3 = new "
228                         + _sourceformat1
229                         + "[temp1.size()];");
230                 outputln(
231                     "for (int i=0; i< temp1.size();i++) temp3[i]= ("
232                         + _sourceformat1
233                         + ")temp1.elementAt(i);");
234                 outputln("return temp3;");
235                 outputln("}");
236             } else {
237                 outputln(
238                     " "
239                         + _sourceformat2JavaConflict
240                         + "("
241                         + _destformat1
242                         + " "
243                         + _destformat2JavaConflict
244                         + ") throws org.omg.mof.Reflective.MofError {");
245                 outputln("boolean exist = false;");
246                 outputln("int i= 0;");
247                 outputln(_aformat1 + "Link temp1 = null;");
248                 outputln(
249                     "while((i< _all_" + _aformat2 + ".size())&& !exist){");
250                 outputln(
251                     "temp1= ("
252                         + _aformat1
253                         + "Link)_all_"
254                         + _aformat2
255                         + ".elementAt(i);");
256                 outputln(
257                     "if (temp1."
258                         + _destformat2JavaConflict
259                         + "._is_equivalent("
260                         + _destformat2JavaConflict
261                         + ")) exist = true;");
262                 outputln("i++;");
263                 outputln("}");
264                 outputln("return temp1." + _sourceformat2JavaConflict + ";");
265                 outputln("}");
266             }
267         }
268
269         if (_dest_navig) {
270             outputln("// Operation " + _destformat2);
271             output("public " + _destformat1);
272             if ((_dest_upper > 1) || (_dest_upper == -1)) {
273                 outputln(
274                     "[] "
275                         + _destformat2JavaConflict
276                         + "("
277                         + _sourceformat1
278                         + " "
279                         + _sourceformat2JavaConflict
280                         + ") throws org.omg.mof.Reflective.MofError {");
281                 outputln(
282                     "java.util.Vector temp1 = new java.util.Vector();");
283                 outputln(
284                     "for (int i=0; i< _all_"
285                         + _aformat2
286                         + ".size(); i++){");
287                 outputln(
288                     _aformat1
289                         + "Link temp2= ("
290                         + _aformat1
291                         + "Link)_all_"
292                         + _aformat2
293                         + ".elementAt(i);");
294                 outputln(
295                     "if (temp2."
296                         + _sourceformat2JavaConflict
297                         + "._is_equivalent("
298                         + _sourceformat2JavaConflict
299                         + ")) temp1.addElement(temp2."
300                         + _sourceformat2JavaConflict
301                         + ");");
302                 outputln("}");
303                 outputln(
304                     _destformat1
305                         + "[] temp3 = new "
306                         + _destformat1
307                         + "[temp1.size()];");
308                 outputln(
309                     "for (int i=0; i< temp1.size();i++) temp3[i]= ("
310                         + _destformat1
311                         + ")temp1.elementAt(i);");
312                 outputln("return temp3;");
313                 outputln("}");
314             } else {
315                 outputln(
316                     " "
317                         + _destformat2JavaConflict
318                         + "("
319                         + _sourceformat1
320                         + " "
321                         + _sourceformat2JavaConflict
322                         + ") throws org.omg.mof.Reflective.MofError {");
323                 outputln("boolean exist = false;");
324                 outputln("int i= 0;");
325                 outputln(_aformat1 + "Link temp1 = null;");
326                 outputln(
327                     "while((i< _all_" + _aformat2 + ".size())&& !exist){");
328                 outputln(
329                     "temp1= ("
330                         + _aformat1
331                         + "Link)_all_"
332                         + _aformat2
333                         + ".elementAt(i);");
334                 outputln(
335                     "if (temp1."
336                         + _sourceformat2JavaConflict
337                         + "._is_equivalent("
338                         + _sourceformat2JavaConflict
339                         + ")) exist = true;");
340                 outputln("i++;");
341                 outputln("}");
342                 outputln("return temp1." + _destformat2JavaConflict + ";");
343                 outputln("}");
344             }
345
346         }
347
348         if (_source_chang && _dest_chang) {
349             outputln("// Operation add");
350             outputln(
351                 "public void add("
352                     + _sourceformat1
353                     + " "
354                     + _sourceformat2JavaConflict
355                     + ", "
356                     + _destformat1
357                     + " "
358                     + _destformat2JavaConflict
359                     + ") throws org.omg.mof.Reflective.MofError {");
360             outputln(
361                 _aformat1
362                     + "Link temp = new "
363                     + _aformat1
364                     + "Link("
365                     + _sourceformat2JavaConflict
366                     + ", "
367                     + _destformat2JavaConflict
368                     + ");");
369             outputln("_all_" + _aformat2 + ".addElement(temp);");
370             outputln("}");
371         }
372
373         if (_source_chang
374             && _source_navig
375             && _dest_chang
376             && ((_source_upper > 1) || (_source_upper == -1))
377             && _source_ordered) {
378             outputln("// Operation add_before" + _sourceformat2);
379             outputln(
380                 "public void add_before_"
381                     + _sourceformat2
382                     + "("
383                     + _sourceformat1
384                     + " "
385                     + _sourceformat2JavaConflict
386                     + ", "
387                     + _destformat1
388                     + " "
389                     + _destformat2JavaConflict
390                     + ", "
391                     + _sourceformat1
392                     + " before) throws org.omg.mof.Reflective.NotFound, org.omg.mof.Reflective.MofError{ ");
393             outputln("boolean exist = false;");
394             outputln("int i= 0;");
395             outputln(
396                 "while((i< _all_" + _aformat2 + ".size())&& !exist){");
397             outputln(
398                 _aformat1
399                     + "Link temp1= ("
400                     + _aformat1
401                     + "Link)_all_"
402                     + _aformat2
403                     + ".elementAt(i);");
404             outputln(
405                 "if (temp1."
406                     + _sourceformat2JavaConflict
407                     + "._is_equivalent(before)) exist = true;");
408             outputln("i++;");
409             outputln("}");
410             outputln("if(exist) {");
411             outputln(
412                 _aformat1
413                     + "Link temp = new "
414                     + _aformat1
415                     + "Link("
416                     + _sourceformat2JavaConflict
417                     + ", "
418                     + _destformat2JavaConflict
419                     + ");");
420             outputln(
421                 "_all_" + _aformat2 + ".insertElementAt(temp, i-1);");
422             outputln("}");
423             outputln(
424                 "else throw new org.omg.mof.Reflective.NotFound() ;");
425             outputln("}");
426         }
427
428         if (_dest_chang
429             && _dest_navig
430             && _source_chang
431             && ((_dest_upper > 1) || (_dest_upper == -1))
432             && _dest_ordered) {
433             outputln("// Operation add_before" + _destformat2);
434             outputln(
435                 "public void add_before_"
436                     + _destformat2
437                     + "("
438                     + _sourceformat1
439                     + " "
440                     + _sourceformat2JavaConflict
441                     + ", "
442                     + _destformat1
443                     + " "
444                     + _destformat2JavaConflict
445                     + ", "
446                     + _destformat1
447                     + " before) throws org.omg.mof.Reflective.NotFound, org.omg.mof.Reflective.MofError{ ");
448             outputln("boolean exist = false;");
449             outputln("int i= 0;");
450             outputln(
451                 "while((i< _all_" + _aformat2 + ".size())&& !exist){");
452             outputln(
453                 _aformat1
454                     + "Link temp1= ("
455                     + _aformat1
456                     + "Link)_all_"
457                     + _aformat2
458                     + ".elementAt(i);");
459             outputln(
460                 "if (temp1."
461                     + _destformat2
462                     + "._is_equivalent(before)) exist = true;");
463             outputln("i++;");
464             outputln("}");
465             outputln("if(exist) {");
466             outputln(
467                 _aformat1
468                     + "Link temp = new "
469                     + _aformat1
470                     + "Link("
471                     + _sourceformat2JavaConflict
472                     + ", "
473                     + _destformat2JavaConflict
474                     + ");");
475             outputln(
476                 "_all_" + _aformat2 + ".insertElementAt(temp, i-1);");
477             outputln("}");
478             outputln(
479                 "else throw new org.omg.mof.Reflective.NotFound() ;");
480             outputln("}");
481         }
482
483         if (_source_navig && _source_chang) {
484             outputln("// Operation modify" + _sourceformat2);
485             outputln(
486                 "public void modify_"
487                     + _sourceformat2
488                     + "("
489                     + _sourceformat1
490                     + " "
491                     + _sourceformat2JavaConflict
492                     + ", "
493                     + _destformat1
494                     + " "
495                     + _destformat2JavaConflict
496                     + ", "
497                     + _sourceformat1
498                     + " new_"
499                     + _sourceformat2
500                     + ") throws org.omg.mof.Reflective.NotFound, org.omg.mof.Reflective.MofError{ ");
501             outputln("boolean exist = false;");
502             outputln("int i= 0;");
503             outputln(
504                 "while((i< _all_" + _aformat2 + ".size())&& !exist){");
505             outputln(
506                 _aformat1
507                     + "Link temp1= ("
508                     + _aformat1
509                     + "Link)_all_"
510                     + _aformat2
511                     + ".elementAt(i);");
512             outputln(
513                 "if (temp1."
514                     + _sourceformat2JavaConflict
515                     + "._is_equivalent("
516                     + _sourceformat2JavaConflict
517                     + ") && temp1."
518                     + _destformat2JavaConflict
519                     + "._is_equivalent("
520                     + _destformat2JavaConflict
521                     + ")) exist = true;");
522             outputln("i++;");
523             outputln("}");
524             outputln("if(exist) {");
525             outputln(
526                 _aformat1
527                     + "Link temp = new "
528                     + _aformat1
529                     + "Link(new_"
530                     + _sourceformat2
531                     + ", "
532                     + _destformat2JavaConflict
533                     + ");");
534             outputln("_all_" + _aformat2 + ".setElementAt(temp, i-1);");
535             outputln("}");
536             outputln(
537                 "else throw new org.omg.mof.Reflective.NotFound() ;");
538             outputln("}");
539         }
540
541         if (_dest_navig && _dest_chang) {
542             outputln("// Operation modify" + _sourceformat2);
543             outputln(
544                 "public void modify_"
545                     + _destformat2
546                     + " ("
547                     + _sourceformat1
548                     + " "
549                     + _sourceformat2JavaConflict
550                     + ", "
551                     + _destformat1
552                     + " "
553                     + _destformat2JavaConflict
554                     + ", "
555                     + _destformat1
556                     + " new_"
557                     + _destformat2
558                     + ") throws org.omg.mof.Reflective.NotFound, org.omg.mof.Reflective.MofError{ ");
559             outputln("boolean exist = false;");
560             outputln("int i= 0;");
561             outputln(
562                 "while((i< _all_" + _aformat2 + ".size())&& !exist){");
563             outputln(
564                 _aformat1
565                     + "Link temp1= ("
566                     + _aformat1
567                     + "Link)_all_"
568                     + _aformat2
569                     + ".elementAt(i);");
570             outputln(
571                 "if (temp1."
572                     + _sourceformat2JavaConflict
573                     + "._is_equivalent("
574                     + _sourceformat2JavaConflict
575                     + ") && temp1."
576                     + _destformat2JavaConflict
577                     + "._is_equivalent("
578                     + _destformat2JavaConflict
579                     + ")) exist = true;");
580             outputln("i++;");
581             outputln("}");
582             outputln("if(exist) {");
583             outputln(
584                 _aformat1
585                     + "Link temp = new "
586                     + _aformat1
587                     + "Link("
588                     + _sourceformat2JavaConflict
589                     + ", new_"
590                     + _destformat2
591                     + ");");
592             outputln("_all_" + _aformat2 + ".setElementAt(temp, i-1);");
593             outputln("}");
594             outputln(
595                 "else throw new org.omg.mof.Reflective.NotFound() ;");
596             outputln("}");
597         }
598
599         if (_source_chang && _dest_chang) {
600             outputln(
601                 "public void remove ("
602                     + _sourceformat1
603                     + " "
604                     + _sourceformat2JavaConflict
605                     + ", "
606                     + _destformat1
607                     + " "
608                     + _destformat2JavaConflict
609                     + ") throws org.omg.mof.Reflective.NotFound, org.omg.mof.Reflective.MofError{ ");
610             outputln("boolean exist = false;");
611             outputln("int i= 0;");
612             outputln(
613                 "while((i< _all_" + _aformat2 + ".size())&& !exist){");
614             outputln(
615                 _aformat1
616                     + "Link temp1= ("
617                     + _aformat1
618                     + "Link)_all_"
619                     + _aformat2
620                     + ".elementAt(i);");
621             outputln(
622                 "if (temp1."
623                     + _sourceformat2JavaConflict
624                     + "._is_equivalent("
625                     + _sourceformat2JavaConflict
626                     + ") && temp1."
627                     + _destformat2JavaConflict
628                     + "._is_equivalent("
629                     + _destformat2JavaConflict
630                     + ")) exist = true;");
631             outputln("i++;");
632             outputln("}");
633             outputln("if(exist) {");
634             outputln("_all_" + _aformat2 + ".removeElementAt(i-1);");
635             outputln("}");
636             outputln(
637                 "else throw new org.omg.mof.Reflective.NotFound() ;");
638             outputln("}");
639         }
640         outputln("}");
641     }
642     /**
643      * Returns the javaHelper.
644      * @return JavaCommon
645      */

646     public JavaCommon getJavaHelper() {
647         return javaHelper;
648     }
649
650     /**
651      * Returns the mofHelper.
652      * @return MOFCommon
653      */

654     public MOFCommon getMofHelper() {
655         return mofHelper;
656     }
657
658     /**
659      * Sets the javaHelper.
660      * @param javaHelper The javaHelper to set
661      */

662     public void setJavaHelper(JavaCommon javaHelper) {
663         this.javaHelper = javaHelper;
664     }
665
666     /**
667      * Sets the mofHelper.
668      * @param mofHelper The mofHelper to set
669      */

670     public void setMofHelper(MOFCommon mofHelper) {
671         this.mofHelper = mofHelper;
672     }
673
674 }
Popular Tags