KickJava   Java API By Example, From Geeks To Geeks.

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


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.GeneralizableElement;
26 import javax.jmi.model.MofPackage;
27 import javax.jmi.model.VisibilityKindEnum;
28
29 import org.objectweb.modfact.jmi.helper.JMIProvider;
30 import org.objectweb.modfact.jmi.helper.MofHelper;
31 import org.objectweb.modfact.jmi.helper.XMIIOHelper;
32 import org.objectweb.modfact.jmi.xmiio.common.AbstractXMIIOGenerator;
33
34 /**
35  * The XMI Import generator.
36  * This class allows the generation of the XMI importer (into Java).
37  */

38 public class GenerationXMIImport extends AbstractXMIIOGenerator {
39
40     /**
41      * XMIImport generation.
42      * @param package_ The root package.
43      */

44     public void generateXMIImport(MofPackage package_) throws java.io.IOException JavaDoc {
45         String JavaDoc className = getClassName(JMIProvider.jmiPackageExtentName(package_));
46         packageModuleTemplate(package_);
47         out.flush();
48     }
49
50     /**
51      * Get the class name.
52      * @param package_name_ The root package name.
53      * @return The class name.
54      */

55     protected String JavaDoc getClassName(String JavaDoc package_name_) {
56         String JavaDoc packageName1 = JMIProvider.jmiFormat1(package_name_);
57         String JavaDoc className = packageName1 + "XMIImport";
58         return className;
59     }
60
61     /**
62      * XMIImport generation for constructor.
63      * @param class_name The class name.
64      */

65     public void constructor(String JavaDoc class_name, String JavaDoc package_name) {
66         outputln("/** The XMIImportAssociations class. */");
67         outputln(package_name + "XMIImportAssociations _xmi_import_associations;");
68         outputln();
69         outputln("/** The XMIImportAttributes class. */");
70         outputln(package_name + "XMIImportAttributes _xmi_import_attributes;");
71         outputln();
72         outputln("/** The XMIImportClasses class. */");
73         outputln(package_name + "XMIImportClasses _xmi_import_classes;");
74         outputln();
75         outputln("/**");
76         outputln(" * " + class_name + " Constructor");
77         outputln(" */");
78         outputln("public " + class_name + "() {");
79         outputln("_xmi_import_associations = new " + package_name + "XMIImportAssociations (_types);");
80         outputln("_xmi_import_attributes = new " + package_name + "XMIImportAttributes (_types);");
81         outputln("_xmi_import_classes = new " + package_name + "XMIImportClasses (_types);");
82         outputln("_xmi_import_classes.setXMIImportAttributes(_xmi_import_attributes);");
83         outputln("_xmi_import_associations.setXMIImportClasses(_xmi_import_classes);");
84         outputln("_xmi_import_attributes.setXMIImportClasses(_xmi_import_classes);");
85         outputln("}");
86         outputln();
87     }
88
89     java.util.Vector JavaDoc classes = new java.util.Vector JavaDoc();
90     java.util.Vector JavaDoc associations = new java.util.Vector JavaDoc();
91
92     private void processContents(MofPackage package_) {
93         java.util.List JavaDoc contentsList = package_.getContents();
94         Iterator JavaDoc it = contentsList.iterator();
95         while (it.hasNext()) {
96             javax.jmi.model.ModelElement contents = (javax.jmi.model.ModelElement) it.next();
97             if (contents instanceof javax.jmi.model.MofPackage)
98                 processContents((javax.jmi.model.MofPackage) contents);
99             else if (contents instanceof javax.jmi.model.Import)
100                 processContents((javax.jmi.model.MofPackage) ((javax.jmi.model.Import) contents).getImportedNamespace());
101             else if (contents instanceof javax.jmi.model.MofClass)
102                 classes.addElement((javax.jmi.model.MofClass) contents);
103             else if (contents instanceof javax.jmi.model.Association)
104                 associations.addElement((javax.jmi.model.Association) contents);
105         }
106     }
107
108     /**
109      * Package processing.
110      * @param package_ The package to process.
111      */

112     public void packageModuleTemplate(MofPackage package_) {
113         if (package_.getVisibility() == VisibilityKindEnum.forName("public_vis")) {
114             processContents(package_);
115             java.util.Vector JavaDoc classes_ordered = new java.util.Vector JavaDoc();
116             while (classes.size() != 0) {
117                 javax.jmi.model.MofClass courant = null;
118                 for (int i = 0; i < classes.size(); i++) {
119                     courant = (javax.jmi.model.MofClass) classes.elementAt(i);
120                     java.util.List JavaDoc supertypes = courant.getSupertypes();
121                     boolean is_top = true;
122                     Iterator JavaDoc stypesIt = supertypes.iterator();
123                     while (stypesIt.hasNext()) {
124                         GeneralizableElement supertype = (GeneralizableElement) stypesIt.next();
125                         if (classes.contains(supertype))
126                             is_top = false;
127                     }
128                     if (is_top) {
129                         classes_ordered.addElement(courant);
130                         classes.removeElement(courant);
131                     }
132                 }
133             }
134             
135             String JavaDoc packageName = JMIProvider.jmiPackageExtentName(package_);
136             String JavaDoc className = getClassName(packageName);
137             String JavaDoc packageQualifiedName = JMIProvider.qualifierOf(package_);
138             outputln("package " + packageQualifiedName + ".xmi;");
139             outputln();
140             annotationTemplate(package_.getAnnotation());
141             outputln("public class " + className + " extends org.objectweb.modfact.jmi.xmiio.importer.XMIImportParser {");
142             classAttributes(packageName, package_);
143             constructor(className, packageName);
144             associationAttributes(packageName, associations);
145             initiateMethod(packageName, package_, classes_ordered, associations);
146             rootPackage(packageName, classes_ordered, associations);
147             completeImportMethod(associations);
148             outputln("} // end of class " + className);
149             outputln();
150         }
151     }
152
153     /**
154     * XMIImport generation for the class attributes.
155     * @param package_name_ The package name.
156      * @param associations_ The associations of the package.
157     */

158     public void classAttributes(String JavaDoc package_name_, MofPackage package_) {
159         outputln("// Factories Declaration");
160         String JavaDoc model_package_type;
161         model_package_type = JMIProvider.jmiPackageQualifiedName(package_) + "Package";
162         outputln("private " + model_package_type + " _" + JMIProvider.jmiFormat2(package_name_) + "_package = null;");
163         outputln();
164     }
165
166     /**
167      * XMIImport generation for the association attributes.
168      * @param package_name_ The package name.
169      * @param associations_ The associations of the package.
170      */

171     public void associationAttributes(String JavaDoc package_name_, Vector JavaDoc associations_) {
172         outputln("// Hashtables for associations (Model / Key Vector)");
173         outputln();
174         outputln("/** Hashtables for types (Key / Model) */");
175         outputln("private java.util.Hashtable _types = new java.util.Hashtable();");
176         outputln();
177     }
178
179     /**
180      * XMIImport generation for the initiate method.
181      * @param package_name_ The package name.
182      * @param classes_ The classes of the package.
183      * @param associations_ The associations of the package.
184      */

185     public void initiateMethod(String JavaDoc package_name_, MofPackage package_, Vector JavaDoc classes_, Vector JavaDoc associations_) {
186         String JavaDoc model_package_type;
187         model_package_type = JMIProvider.jmiPackageQualifiedName(package_) + "Package";
188         outputln("public void initiate(" + model_package_type + " _model_package) {");
189         outputln("// Factories creation");
190         //outputln("org.omg." + model_package_type + " _model_package = (org.omg." + model_package_type + ") _model_ref_package;");
191
outputln("this._" + JMIProvider.jmiFormat2(package_name_) + "_package = _model_package;");
192         outputln("_xmi_import_classes.initiate(_model_package);");
193         outputln("_xmi_import_associations.initiate(_model_package);");
194         outputln("_xmi_import_attributes.initiate(_model_package);");
195         outputln("}");
196         outputln();
197     }
198
199     /**
200      * XMIImport generation for the root package.
201      * @param package_name_ The package name.
202      * @param classes_ The classes of the package.
203      * @param associations_ The associations of the package.
204      */

205     public void rootPackage(String JavaDoc package_name_, Vector JavaDoc classes_, Vector JavaDoc associations_) {
206         annotationTemplate(" The root package processing");
207         outputln("public java.lang.Object rootPackageTemplate(org.w3c.dom.Element _root_package_element) {");
208         outputln("java.lang.String prefix_element = new java.lang.String();");
209         outputln("java.lang.String namespace = _root_package_element.getNamespaceURI();");
210         outputln("if (namespace != null && namespace.length() > 0)");
211         outputln(" prefix_element = _root_package_element.getPrefix() + \":\";");
212         outputln("org.w3c.dom.Element _current = _root_package_element;");
213         outputln("String _current_name = _root_package_element.getNodeName().trim();");
214
215         for (int i = 0; i < classes_.size(); i++) {
216             javax.jmi.model.MofClass cls = (javax.jmi.model.MofClass) classes_.elementAt(i);
217             String JavaDoc xmlClassName = JMIProvider.jmiFormat1(cls.getName());
218             String JavaDoc className = JMIProvider.jmiFormat1(JMIProvider.jmiClassName(cls));
219             String JavaDoc classNameMin = className.substring(0, 1).toLowerCase() + className.substring(1);
220             outputln("if (_current_name.endsWith(prefix_element + \"" + className + "\")) {");
221             outputln("_xmi_import_classes." + classNameMin + "Template(_current);");
222             if (i == classes_.size() - 1 && associations_.size() == 0)
223                 outputln("}");
224             else
225                 output("} else ");
226         }
227         for (int i = 0; i < associations_.size(); i++) {
228             javax.jmi.model.Association asso = (javax.jmi.model.Association) associations_.elementAt(i);
229             String JavaDoc xmlAssoName = asso.getName();
230             String JavaDoc assoName = JMIProvider.jmiFormat1(JMIProvider.jmiAssociationName(asso));
231             String JavaDoc assoNameMin = assoName.substring(0, 1).toLowerCase() + assoName.substring(1);
232             javax.jmi.model.AssociationEnd[] ends = MofHelper.associationEndsOfAssociation(asso);
233             boolean endsChangeable = true;
234             for (int j = 0; j < ends.length; j++) {
235                 endsChangeable = (endsChangeable && ends[j].isChangeable());
236             }
237             if (endsChangeable) {
238                 outputln("if (_current_name.endsWith(prefix_element + \"" + xmlAssoName + "\")) {");
239                 outputln("_xmi_import_associations." + assoNameMin + "Template(_current);");
240                 if (i == associations_.size() - 1)
241                     outputln("}");
242                 else
243                     output("} else ");
244             }
245         }
246         outputln("return _" + JMIProvider.jmiFormat2(package_name_) + "_package;");
247         outputln("}");
248         outputln();
249     }
250
251     /**
252      * XMIImport generation for the completeImport() method.
253      * @param associations_ The associations of the package.
254      */

255     public void completeImportMethod(Vector JavaDoc associations_) {
256         outputln("public void completeImport () {");
257         for (int i = 0; i < associations_.size(); i++) {
258             javax.jmi.model.Association asso = (javax.jmi.model.Association) associations_.elementAt(i);
259             try {
260                 javax.jmi.model.Reference[] refs = XMIIOHelper.getReferences(asso);
261                 for (int j = 0; j < refs.length; j++) {
262                     javax.jmi.model.AssociationEnd assoEnd = refs[j].getReferencedEnd();
263                     if (assoEnd.getAggregation() == javax.jmi.model.AggregationKindEnum.forName("none") && assoEnd.isChangeable())
264                         outputln(
265                             "_xmi_import_attributes.put"
266                                 + JMIProvider.jmiFormat1(refs[j].getContainer().getName())
267                                 + JMIProvider.jmiFormat1(refs[j].getName())
268                                 + "();");
269                 }
270             } catch (NullPointerException JavaDoc notSet) {
271                 // Nothing to do
272
}
273         }
274         outputln("}");
275         outputln();
276     }
277
278 }
279
Popular Tags