KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > impl > PackageImplementationGenerator


1 package org.objectweb.modfact.jmi.impl;
2
3 import java.util.Iterator JavaDoc;
4
5 import javax.jmi.model.Association;
6 import javax.jmi.model.Import;
7 import javax.jmi.model.ModelElement;
8 import javax.jmi.model.MofClass;
9 import javax.jmi.model.MofPackage;
10 import javax.jmi.model.VisibilityKindEnum;
11
12 import org.objectweb.modfact.jmi.helper.ImplHelper;
13 import org.objectweb.modfact.jmi.helper.JMIProvider;
14 import org.objectweb.modfact.jmi.helper.MofHelper;
15 import org.objectweb.modfact.jmi.logging.ModFactLogger;
16
17
18 /**
19  * @author Xavier
20  *
21  */

22 public class PackageImplementationGenerator
23     extends CommonImplementationGenerator {
24
25     MofPackage[] input;
26
27     ModFactLogger logger;
28
29     /**
30      * Set Input
31      */

32     public void setInput(ModelElement[] elt) {
33         input = new MofPackage[elt.length];
34         for (int i = 0; i < input.length; i++) {
35             input[i] = (MofPackage) elt[i];
36         }
37     }
38
39     /**
40      * Set Trace
41      */

42     public void setLogger(ModFactLogger log) {
43         logger = log;
44     }
45
46     /**
47      * @see org.objectweb.modfact.generation.Generator#generate()
48      */

49     public void generate() {
50         //Tag Import todo
51
MofPackage _package = input[0];
52         //javadocsTemplate(_package);
53

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