KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > impl > tayloredBased > 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.jmi.impl.tayloredBased;
21
22 import org.objectweb.modfact.jmi.helper.*;
23 import org.objectweb.modfact.jmi.logging.ModFactLogger;
24
25 import javax.jmi.model.*;
26 import java.util.*;
27
28 /**
29  * @author Xavier
30  *
31  */

32 public class PackageImplementationGenerator
33     extends CommonImplementationGenerator {
34
35     MofPackage[] input;
36
37     ModFactLogger logger;
38
39     /**
40      * Set Input
41      */

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

52     public void setLogger(ModFactLogger log) {
53         logger = log;
54     }
55
56     /**
57      * @see org.objectweb.modfact.generation.Generator#generate()
58      */

59     public void generate() {
60         //Tag Import todo
61
MofPackage _package = input[0];
62         //javadocsTemplate(_package);
63

64         out.println("package " +ImplHelper.implPrefix + JMIProvider.shortQualifierOf(_package) + ";");
65         out.println("import " + JMIProvider.qualifierOf(_package) + ".*;");
66         
67         out.println("import org.objectweb.modfact.jmi.reflect.*;");
68
69         String JavaDoc pQName = JMIProvider.jmiPackageQualifiedName(_package);
70
71         out.println(
72             "public class "
73                 + JMIProvider.jmiPackageExtentName(_package)
74                 + "PackageImpl extends RefPackageImpl"
75                 + " implements "
76                 + pQName
77                 + "Package { ");
78
79         factoryTemplate(_package);
80
81         //for each StructType
82
dataTypeTemplates(_package);
83
84         /////////// Package import //////////////////
85
Import[] imports = MofHelper.importsOfPackage(_package);
86         for (int i = 0; i < imports.length; i++) {
87             if (VisibilityKindEnum
88                 .forName("public_vis")
89                 .equals(imports[i].getVisibility())
90                 && imports[i].isClustered()
91                 && imports[i].getImportedNamespace() instanceof MofPackage) {
92                 MofPackage importedPackage =
93                     (MofPackage) imports[i].getImportedNamespace();
94                 out.println(
95                     "\tpublic "
96                         + JMIProvider.jmiPackageQualifiedName(importedPackage)
97                         + "Package get"
98                         + JMIProvider.jmiImportName(imports[i])
99                         + "() {");
100                 out.println(
101                     "\t\treturn "
102                         + "("
103                         + JMIProvider.jmiPackageQualifiedName(importedPackage)
104                         + "Package)"
105                         + "refPackage(\""
106                         + importedPackage.getName()
107                         + "\")"
108                         + ";");
109
110                 out.println("\t}");
111             }
112         }
113
114         //for each contained package where visibility = public_vis
115
MofPackage[] containedPackages = MofHelper.packagesOfPackage(_package);
116         for (int i = 0; i < containedPackages.length; i++) {
117             if (containedPackages[i]
118                 .getVisibility()
119                 .equals(VisibilityKindEnum.forName("public_vis"))) {
120
121                 out.println(
122                     "\tpublic "
123                         + JMIProvider.jmiPackageQualifiedName(
124                             containedPackages[i])
125                         + "Package get"
126                         + JMIProvider.jmiPackageExtentName(containedPackages[i])
127                         + "() {");
128                 out.println(
129                     "\t\treturn "
130                         + "("
131                         + JMIProvider.jmiPackageQualifiedName(
132                             containedPackages[i])
133                         + "Package)"
134                         + "refPackage(\""
135                         + containedPackages[i].getName()
136                         + "\")"
137                         + ";");
138                 out.println("\t}");
139             }
140         }
141
142         //for each contained class where visibility = public_vis
143
MofClass[] containedClasses = MofHelper.classesOfPackage(_package);
144         for (int i = 0; i < containedClasses.length; i++) {
145             if (containedClasses[i]
146                 .getVisibility()
147                 .equals(VisibilityKindEnum.forName("public_vis"))) {
148                 out.println(
149                     "\tpublic "
150                         + JMIProvider.jmiClassQualifiedName(containedClasses[i])
151                         + "Class get"
152                         + JMIProvider.jmiClassName(containedClasses[i])
153                         + "() {");
154                 out.println(
155                     "\t\treturn "
156                         + "("
157                         + JMIProvider.jmiClassQualifiedName(containedClasses[i])
158                         + "Class)"
159                         + "refClass(\""
160                         + containedClasses[i].getName()
161                         + "\")"
162                         + ";");
163                 out.println("\t}");
164             }
165         }
166
167         //for each contained association where visibility = public_vis
168
Association[] containedAssociations =
169             MofHelper.associationsOfPackage(_package);
170         for (int i = 0; i < containedAssociations.length; i++) {
171             if (containedAssociations[i]
172                 .getVisibility()
173                 .equals(VisibilityKindEnum.forName("public_vis"))) {
174                 out.println(
175                     "\tpublic "
176                         + JMIProvider.jmiAssociationQualifiedName(
177                             containedAssociations[i])
178                         + " get"
179                         + JMIProvider.jmiAssociationName(
180                             containedAssociations[i])
181                         + "() {");
182                 out.println(
183                     "\t\treturn "
184                         + "("
185                         + JMIProvider.jmiAssociationQualifiedName(
186                             containedAssociations[i])
187                         + ")"
188                         + "refAssociation(\""
189                         + containedAssociations[i].getName()
190                         + "\")"
191                         + ";");
192                 out.println("\t}");
193             }
194         }
195
196         newPackageTemplate(_package);
197         newClassTemplate(_package);
198         newAssociationTemplate(_package);
199         newEnumTemplate(_package);
200         newStructTemplate(_package);
201
202         out.println("}");
203         out.flush();
204     }
205
206     void newPackageTemplate(MofPackage p) {
207         out.println("\tpublic RefPackageImpl newPackage(String n) {");
208         Iterator it =
209             MofHelper
210                 .filterContentsByClass(p, MofPackage.class, true)
211                 .iterator();
212         while (it.hasNext()) {
213             MofPackage content = (MofPackage) it.next();
214             out.println(
215                 "\t\t if(n.equals(\""
216                     + content.getName()
217                     + "\")) "
218                     + "return new " +ImplHelper.implPrefix
219                     + JMIProvider.shortQualifierOf(content)
220                     + "."
221                     + JMIProvider.jmiPackageExtentName(content)
222                     + "Package"
223                     + "Impl"
224                     + "();");
225         }
226         out.println("throw new RuntimeException(\"invalide type\");");
227         out.println("\t}");
228     }
229
230     void newClassTemplate(MofPackage p) {
231         out.println("\tpublic RefClassImpl newClass(String n) {");
232         Iterator it =
233             MofHelper.filterContentsByClass(p, MofClass.class, true).iterator();
234         while (it.hasNext()) {
235             MofClass content = (MofClass) it.next();
236             out.println(
237                 "\t\t if(n.equals(\""
238                     + content.getName()
239                     + "\")) "
240                     + "return new " +ImplHelper.implPrefix
241                     + JMIProvider.shortQualifierOf(content)
242                     + "."
243                     + JMIProvider.jmiClassName(content)
244                     + "Class"
245                     + "Impl"
246                     + "();");
247         }
248         out.println("\t\t throw new RuntimeException(\"invalide Class: '\" + n + \"'\");");
249         out.println("\t}");
250     }
251
252     void newAssociationTemplate(MofPackage p) {
253         out.println("\tpublic RefAssociationImpl newAssociation(String n) {");
254         Iterator it =
255             MofHelper
256                 .filterContentsByClass(p, Association.class, true)
257                 .iterator();
258         while (it.hasNext()) {
259             Association content = (Association) it.next();
260             out.println(
261                 "\t\t if(n.equals(\""
262                     + content.getName()
263                     + "\")) "
264                     + "return new " +ImplHelper.implPrefix
265                     + JMIProvider.shortQualifierOf(content)
266                     + "."
267                     + JMIProvider.jmiAssociationName(content)
268                     + "Impl"
269                     + "();");
270         }
271         out.println("\t\t throw new RuntimeException(\"invalide Association: '\" + n + \"'\");");
272         out.println("\t}");
273     }
274
275     // factory for outer mostpackage
276
void factoryTemplate(MofPackage _package) {
277
278         if (_package.getContainer() == null) {
279
280             String JavaDoc pQName = JMIProvider.jmiPackageQualifiedName(_package);
281             String JavaDoc pShortQName = JMIProvider.shortQualifierOf(_package) +"."
282                                 +JMIProvider.jmiPackageExtentName(_package);
283
284             out.println(
285                 "\t public static "
286                     + pQName
287                     + "Package"
288                     + " create(javax.jmi.model.MofPackage meta) {");
289             out.println(
290                 "\t\t "
291                     + ImplHelper.implPrefix + pShortQName
292                     + "PackageImpl p = new "
293                     + ImplHelper.implPrefix + pShortQName
294                     + "PackageImpl();");
295             out.println("\t\t initRepository(p, meta);");
296             out.println("\t\t return p;");
297             out.println("\t } \n");
298
299             out.println(
300                 "\t public static "
301                     + pQName
302                     + "Package"
303                     + " create(String metaModelURL) throws Exception {");
304             out.println(
305                 "\t\t javax.jmi.model.MofPackage meta=(javax.jmi.model.MofPackage)"
306                     + "createMetaObject(\""
307                     + _package.getName()
308                     + "\", metaModelURL);");
309             out.println("\t\t return create(meta);");
310             out.println("\t }");
311         }
312     }
313
314 }
315
Popular Tags