KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > xmiio > importer > GenerationXMIImportAssociations


1 /**
2  * copyright 2002 2004 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.xmiio.importer;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import javax.jmi.model.MofPackage;
26
27 import org.objectweb.modfact.jmi.helper.JMIProvider;
28 import org.objectweb.modfact.jmi.helper.MofHelper;
29 import org.objectweb.modfact.jmi.helper.XMIIOHelper;
30 import org.objectweb.modfact.jmi.xmiio.common.AbstractXMIIOGenerator;
31
32 /**
33  * The XMI Import generator.
34  * This class allows the generation of the XMI importer (into Java).
35  */

36 public class GenerationXMIImportAssociations extends AbstractXMIIOGenerator {
37
38     /**
39      * Default Constructor.
40      */

41     public GenerationXMIImportAssociations() {
42         super();
43     }
44
45     /**
46      * XMIImport generation.
47      * @param package_ The root package.
48      * @param class_name_ The class name of the generated class.
49      */

50     public void generateXMIImportAssociations(javax.jmi.model.MofPackage package_, String JavaDoc class_name_)
51         throws java.io.FileNotFoundException JavaDoc {
52         packageModuleTemplate(package_, class_name_);
53         out.flush();
54     }
55
56     /**
57      * XMIImport generation for constructor.
58      * @param class_name The class name.
59      * @param package_name The package name.
60      */

61     public void constructor(String JavaDoc class_name, String JavaDoc package_name) {
62         outputln("/** The XMIImportClasses class. */");
63         outputln(package_name + "XMIImportClasses _xmi_import_classes;");
64         outputln();
65         outputln("/** Hashtables for types (Key / Model) */");
66         outputln("private java.util.Hashtable _types;");
67         outputln();
68         outputln("/**");
69         outputln(" * Default " + class_name + " Constructor");
70         outputln(" * @param types The defined types.");
71         outputln(" */");
72         outputln("public " + class_name + "(java.util.Hashtable types) {");
73         outputln("_types = types;");
74         outputln("}");
75         outputln();
76         outputln("public void setXMIImportClasses (" + package_name + "XMIImportClasses xmi_import_classes) {");
77         outputln("_xmi_import_classes = xmi_import_classes;");
78         outputln("}");
79         outputln();
80     }
81
82     java.util.Vector JavaDoc classes = new java.util.Vector JavaDoc();
83     java.util.Vector JavaDoc associations = new java.util.Vector JavaDoc();
84
85     private void processContents(MofPackage package_) {
86         java.util.List JavaDoc contentsList = package_.getContents();
87         Iterator JavaDoc it = contentsList.iterator();
88         while (it.hasNext()) {
89             javax.jmi.model.ModelElement contents = (javax.jmi.model.ModelElement) it.next();
90             if (contents instanceof javax.jmi.model.MofPackage)
91                 processContents((javax.jmi.model.MofPackage) contents);
92             else if (contents instanceof javax.jmi.model.Import)
93                 processContents((javax.jmi.model.MofPackage) ((javax.jmi.model.Import) contents).getImportedNamespace());
94             else if (contents instanceof javax.jmi.model.MofClass)
95                 classes.addElement((javax.jmi.model.MofClass) contents);
96             else if (contents instanceof javax.jmi.model.Association)
97                 associations.addElement((javax.jmi.model.Association) contents);
98         }
99     }
100
101     /**
102      * Package processing.
103      * @param package_ The package to process.
104      * @param class_name_ The class name of the generated class.
105      */

106     public void packageModuleTemplate(javax.jmi.model.MofPackage package_, String JavaDoc class_name_) {
107         if (package_.getVisibility() == javax.jmi.model.VisibilityKindEnum.forName("public_vis")) {
108             processContents(package_);
109
110             java.util.Vector JavaDoc classes_ordered = new java.util.Vector JavaDoc();
111             while (classes.size() != 0) {
112                 javax.jmi.model.MofClass courant = null;
113                 for (int i = 0; i < classes.size(); i++) {
114                     courant = (javax.jmi.model.MofClass) classes.elementAt(i);
115                     java.util.List JavaDoc supertypes = courant.getSupertypes();
116                     boolean is_top = true;
117                     Iterator JavaDoc itSup = supertypes.iterator();
118                     while (itSup.hasNext()) {
119                         if (classes.contains((javax.jmi.model.GeneralizableElement) itSup.next()))
120                             is_top = false;
121                     }
122                     if (is_top) {
123                         classes_ordered.addElement(courant);
124                         classes.removeElement(courant);
125                     }
126                 }
127             }
128
129             // // Get class contents to reduce CORBA calls during classes contents processing
130
// for (int i = 0; i < classes_ordered.size(); i++)
131
// _helper.allContentsOfClasses((org.omg.mof.Model.Class) classes_ordered.elementAt(i));
132

133             String JavaDoc packageName = JMIProvider.jmiPackageExtentName(package_);
134             String JavaDoc packageQualifiedName = JMIProvider.qualifierOf(package_);
135             outputln("package " + packageQualifiedName + ".xmi;");
136             outputln();
137             annotationTemplate(package_.getAnnotation());
138             outputln("public class " + class_name_ + " extends org.objectweb.modfact.jmi.xmiio.importer.XMIImport {");
139             classAttributes(packageName, package_, associations);
140             constructor(class_name_, packageName);
141             initiateMethod(packageName, package_, associations);
142             associationTemplate(associations);
143             outputln("} // end of class " + class_name_);
144             outputln();
145         }
146     }
147
148     /**
149     * XMIImport generation for the class attributes.
150     * @param package_name_ The package name.
151     * @param associations_ The associations of the package.
152     */

153     public void classAttributes(String JavaDoc package_name_, MofPackage package_, Vector JavaDoc associations_) {
154         outputln("// Factories Declaration");
155         String JavaDoc model_package_type;
156         model_package_type = JMIProvider.jmiPackageQualifiedName(package_) + "Package";
157         outputln("private " + model_package_type + " _" + JMIProvider.jmiFormat2(package_name_) + "_package = null;");
158         outputln();
159         // Associations
160
for (int i = 0; i < associations_.size(); i++) {
161             javax.jmi.model.Association asso = (javax.jmi.model.Association) associations_.elementAt(i);
162             String JavaDoc javaName = JMIProvider.jmiAssociationQualifiedName(asso);
163             outputln("private " + javaName + " _" + JMIProvider.jmiFormat2(javaName) + "_class = null;");
164         }
165     }
166
167     /**
168      * XMIImport generation for the initiate method.
169      * @param package_name_ The package name.
170      * @param associations_ The associations of the package.
171      */

172     public void initiateMethod(String JavaDoc package_name_, MofPackage package_, Vector JavaDoc associations_) {
173         String JavaDoc model_package_type;
174         model_package_type = JMIProvider.jmiPackageQualifiedName(package_) + "Package";
175         outputln("public void initiate(" + model_package_type + " _model_package) {");
176         outputln("// Factories creation");
177         outputln("this._" + JMIProvider.jmiFormat2(package_name_) + "_package = _model_package;");
178         // Associations
179
initiateMethodAssociations(package_name_, package_, associations_);
180         outputln("}");
181         outputln();
182     }
183
184     /**
185      * XMIImport generation for the "association attributes" of the initiate method.
186      * @param package_name_ The package name.
187      * @param classes_ The classes of the package.
188      */

189     public void initiateMethodAssociations(String JavaDoc package_name_, MofPackage package_, Vector JavaDoc associations_) {
190         for (int i = 0; i < associations_.size(); i++) {
191             javax.jmi.model.Association asso = (javax.jmi.model.Association) associations_.elementAt(i);
192             String JavaDoc javaName = JMIProvider.jmiFormat2(JMIProvider.jmiAssociationQualifiedName(asso));
193             String JavaDoc method = "get" + JMIProvider.jmiAssociationName(asso) + "()";
194             try {
195                 javax.jmi.model.ModelElement assoTmp = asso;
196                 while (!assoTmp.getContainer().equals(package_)) {
197                     method = "get" + JMIProvider.jmiPackageExtentName((MofPackage) assoTmp.getContainer()) + "()." + method;
198                     assoTmp = assoTmp.getContainer();
199                 }
200
201             } catch (NullPointerException JavaDoc notSet) {
202                 // Nothing to do
203
}
204             outputln("_" + javaName + "_class = _model_package." + method + ";");
205         }
206     }
207
208     /**
209      * XMIImport generation for the association template.
210      * @param associations_ The list of association of the package.
211      */

212     public void associationTemplate(Vector JavaDoc associations_) {
213         for (int i = 0; i < associations_.size(); i++) {
214             javax.jmi.model.Association asso = (javax.jmi.model.Association) associations_.elementAt(i);
215             String JavaDoc assoName = JMIProvider.jmiAssociationName(asso);
216             String JavaDoc methodName = XMIIOHelper.format1FirstMin(assoName) + "Template";
217             String JavaDoc param = JMIProvider.jmiFormat2(assoName) + "_element";
218             javax.jmi.model.AssociationEnd[] ends = MofHelper.associationEndsOfAssociation(asso);
219             boolean endsChangeable = true;
220             for (int j = 0; j < ends.length; j++) {
221                 endsChangeable = (endsChangeable && ends[j].isChangeable());
222             }
223             if (endsChangeable) {
224                 outputln("public void " + methodName + "(org.w3c.dom.Element " + param + ") {");
225                 for (int j = 0; j < ends.length; j++) {
226                     outputln(
227                         JMIProvider.jmiClassifierQualifiedName(ends[j].getType())
228                             + " "
229                             + XMIIOHelper.testJavaConflict(XMIIOHelper.format1FirstMin(ends[j].getName()))
230                             + " = null;");
231                 }
232                 outputln("org.w3c.dom.NodeList _child_nodes = " + param + ".getChildNodes();");
233                 outputln("for (int i=0 ; i<_child_nodes.getLength() ; i++) {");
234                 outputln("org.w3c.dom.Element _current = (org.w3c.dom.Element) _child_nodes.item(i);");
235                 for (int j = 0; j < ends.length; j++) {
236                     String JavaDoc ends_variable = XMIIOHelper.testJavaConflict(XMIIOHelper.format1FirstMin(ends[j].getName()));
237                     String JavaDoc type_variable = JMIProvider.jmiClassifierQualifiedName(ends[j].getType());
238                     String JavaDoc end_type_name = JMIProvider.findSubstituteName(ends[j].getType());
239                     if (end_type_name == null)
240                         end_type_name = JMIProvider.classNameFormat(ends[j].getType().getName());
241                     String JavaDoc methodTemplate = "_xmi_import_classes." + XMIIOHelper.format1FirstMin(end_type_name) + "Template(_current)";
242                     output("if (_current.getNodeName().endsWith(\"" + JMIProvider.jmiFormat1(ends[j].getType().getName()) + "\")");
243                     try {
244                         javax.jmi.model.MofClass[] subclasses = XMIIOHelper.subClasses(ends[j].getType());
245                         for (int k = 0; k < subclasses.length; k++) {
246                             output(" || _current.getNodeName().endsWith(\"" + JMIProvider.jmiFormat1(subclasses[k].getName()) + "\")");
247                             if (k % 2 == 0 && k < subclasses.length - 1) {
248                                 outputln();
249                                 output(TABULATION);
250                             }
251                         }
252                     } catch (NullPointerException JavaDoc notSet) {
253                         // Nothing to do.
254
}
255                     outputln(") {");
256
257                     if (ends[j].getType().isAbstract()) {
258                         outputln(
259                             "org.objectweb.modfact.jmi.xmiio.mofobject.MOFObject "
260                                 + ends_variable
261                                 + "Obj = "
262                                 + methodTemplate
263                                 + ".getFirstObject();");
264                     } else {
265                         outputln("org.objectweb.modfact.jmi.xmiio.mofobject.MOFObject " + ends_variable + "Obj = " + methodTemplate + ";");
266                     }
267                     outputln("if (" + ends_variable + "Obj.isAnObject())");
268                     outputln(TABULATION + ends_variable + " = (" + type_variable + ")" + ends_variable + "Obj.get();");
269                     outputln("else");
270                     outputln(TABULATION + ends_variable + " = (" + type_variable + ") _types.get(" + ends_variable + "Obj.get());");
271                     outputln("}");
272                 }
273                 // every other time (1/2)
274
outputln("if (i%2 == 1)");
275                 output(TABULATION + "_" + JMIProvider.jmiFormat2(JMIProvider.jmiAssociationQualifiedName(asso)) + "_class.add(");
276                 for (int j = 0; j < ends.length; j++) {
277                     output(XMIIOHelper.testJavaConflict(XMIIOHelper.format1FirstMin(ends[j].getName())));
278                     if (j < ends.length - 1)
279                         output(", ");
280                 }
281                 outputln(");");
282                 outputln("}");
283                 outputln("}");
284                 outputln();
285             }
286         }
287         outputln();
288     }
289
290 }
291
Popular Tags