KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

52     public void setLogger(ModFactLogger log) {
53         logger = log;
54     }
55
56     //Helper
57
JavaCommon javaHelper;
58     MOFCommon mofHelper;
59     
60     
61
62     public void generate()
63         throws
64             org.omg.mof.Reflective.NotSet,
65             org.omg.mof.Reflective.MofError,
66             org.omg.CORBA.TypeCodePackage.BadKind JavaDoc,
67             org.omg.CORBA.TypeCodePackage.Bounds JavaDoc {
68         outputln(
69             "// le Code généré du PackageFactory: "
70                 + input[0].name()
71                 + "PackageFactory");
72         packageFactoryImpl(input[0]);
73         flushFile();
74     }
75
76     public void packageFactoryImpl(org.omg.mof.Model.Package _package)
77         throws org.omg.mof.Reflective.NotSet, org.omg.mof.Reflective.MofError , BadKind JavaDoc , Bounds JavaDoc{
78         //Décalration du Package
79
output("package ");
80
81         // les deux formats du nom du package
82
String JavaDoc _nformat1 = javaHelper.format1Idl2JavaConflict(_package.name());
83         String JavaDoc className = javaHelper.format1Idl2JavaConflict(_package.name()+"PackageFactory")+"Impl";
84         String JavaDoc superClass = javaHelper.format1Idl2JavaConflict(_package.name()+"PackageFactory")+"POA";
85         String JavaDoc itemType = javaHelper.format1Idl2JavaConflict(_package.name()+"Package");
86         String JavaDoc itemClass = javaHelper.format1Idl2JavaConflict(_package.name()+"Package")+"Impl";
87         String JavaDoc _pformat2 = javaHelper.format2(_package.name());
88
89         // le nom du package
90
outputln(javaHelper.javaPackage(_package) + " ;");
91
92         outputln(
93             "public class "
94                 + className
95                 + " extends "
96                 + superClass
97                 + " {");
98
99         Vector JavaDoc _attributes = new Vector JavaDoc();
100         org.omg.mof.Model.Class[] classes = mofHelper.classesOfPackage(_package);
101         for (int i=0 ; i < classes.length ; i++) {
102             org.omg.mof.Model.MofAttribute[] attributes = mofHelper.attributesOfClass(classes[i] , ScopeKind.classifier_level , true);
103             for (int k=0 ; k<attributes.length ; k++) _attributes.addElement(attributes[k]);
104             
105         }
106
107         //
108
java.util.Vector JavaDoc _attributes_name =
109             new java.util.Vector JavaDoc(_attributes.size());
110         for (int i = 0; i < _attributes.size(); i++) {
111             org.omg.mof.Model.MofAttribute _current =
112                 (org.omg.mof.Model.MofAttribute) _attributes.elementAt(i);
113             String JavaDoc _attributeqname = " ";
114             for (int j = 0; j < _current.qualified_name().length; j++) {
115                 if (j > 0)
116                     _attributeqname =
117                         javaHelper.format2(_current.qualified_name()[j])
118                             + "_"
119                             + _attributeqname;
120                 else
121                     _attributeqname =
122                         javaHelper.format2(_current.qualified_name()[j]);
123             }
124             _attributes_name.addElement(_attributeqname);
125         }
126
127         //
128
output(
129             "public "
130                 + itemType
131                 + " create_"
132                 + _pformat2
133                 + "_package(");
134         for (int i = 0; i < _attributes.size(); i++) {
135             org.omg.mof.Model.MofAttribute _current =
136                 (org.omg.mof.Model.MofAttribute) _attributes.elementAt(i);
137             String JavaDoc _current_type = javaHelper.javaType(_current.type());
138             String JavaDoc _attributeqname = (String JavaDoc) _attributes_name.elementAt(i);
139
140             if (!((i != 0) || (i != (_attributes.size() - 1))))
141                 output(", ");
142
143             if ((_current.multiplicity().upper > 1)
144                 || (_current.multiplicity().upper == -1))
145                 output(_current_type + "[] " + _attributeqname);
146             else
147                 output(_current_type + " " + _attributeqname);
148         }
149         outputln(") throws org.omg.mof.Reflective.MofError {");
150         output(
151             itemClass
152                 + " _"
153                 + _pformat2
154                 + "_package = new "
155                 + itemClass
156                 + "(");
157         for (int i = 0; i < _attributes_name.size(); i++) {
158             String JavaDoc _attributeqname = (String JavaDoc) _attributes_name.elementAt(i);
159             if (!((i != 0) || (i != (_attributes.size() - 1))))
160                 output(", ");
161             output(_attributeqname);
162         }
163         outputln(");");
164
165         outputln("try {");
166         outputln("_poa().activate_object(_" + _pformat2 + "_package);");
167         outputln(
168             "org.omg.CORBA.Object obj = _poa().servant_to_reference(_"
169                 + _pformat2
170                 + "_package);");
171         outputln("return " + itemType + "Helper.narrow(obj);");
172         outputln("}");
173         outputln("catch (java.lang.Exception ex) {");
174         outputln("ex.printStackTrace();");
175         outputln("System.exit(0);");
176         outputln("}");
177         outputln("return null;");
178         outputln("}");
179         outputln("}");
180     }
181     /**
182      * Returns the javaHelper.
183      * @return JavaCommon
184      */

185     public JavaCommon getJavaHelper() {
186         return javaHelper;
187     }
188
189     /**
190      * Returns the mofHelper.
191      * @return MOFCommon
192      */

193     public MOFCommon getMofHelper() {
194         return mofHelper;
195     }
196
197     /**
198      * Sets the javaHelper.
199      * @param javaHelper The javaHelper to set
200      */

201     public void setJavaHelper(JavaCommon javaHelper) {
202         this.javaHelper = javaHelper;
203     }
204
205     /**
206      * Sets the mofHelper.
207      * @param mofHelper The mofHelper to set
208      */

209     public void setMofHelper(MOFCommon mofHelper) {
210         this.mofHelper = mofHelper;
211     }
212
213 }
Popular Tags