KickJava   Java API By Example, From Geeks To Geeks.

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


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 GenerationXMIImportClasses extends AbstractXMIIOGenerator {
39
40     /** The generated association attributes methods. */
41     private java.util.List JavaDoc _associationAttributes;
42
43     /** The classes of the package. */
44     private java.util.Vector JavaDoc classes = new java.util.Vector JavaDoc();
45
46     /**
47      * Default Constructor.
48      */

49     public GenerationXMIImportClasses() {
50         super();
51         _associationAttributes = new java.util.ArrayList JavaDoc();
52     }
53
54     /**
55      * XMIImport generation.
56      * @param package_ The root package.
57      * @param class_name_ The class name of the generated class.
58      */

59     public void generateXMIImportClasses(MofPackage package_, String JavaDoc class_name_)
60         throws java.io.FileNotFoundException JavaDoc {
61         packageModuleTemplate(package_, class_name_);
62         out.flush();
63     }
64
65     /**
66      * XMIImport generation for constructor.
67      * @param class_name The class name.
68      * @param package_name The package name.
69      */

70     public void constructor(String JavaDoc class_name, String JavaDoc package_name) {
71         outputln("/** The XMIImportAttributes class. */");
72         outputln(package_name + "XMIImportAttributes _xmi_import_attributes;");
73         outputln();
74         outputln("/** Hashtables for types (Key / Model) */");
75         outputln("private java.util.Hashtable _types;");
76         outputln();
77         outputln("/**");
78         outputln(" * Default " + class_name + " Constructor");
79         outputln(" * @param types The defined types.");
80         outputln(" */");
81         outputln("public " + class_name + "(java.util.Hashtable types) {");
82         outputln("_types = types;");
83         outputln("}");
84         outputln();
85         outputln("public void setXMIImportAttributes(" + package_name + "XMIImportAttributes xmi_import_attributes) {");
86         outputln("_xmi_import_attributes = xmi_import_attributes;");
87         outputln("}");
88         outputln();
89     }
90
91     /**
92      * Parse the content of the package.
93      * @param package_ The root package.
94      */

95     private void processContents(MofPackage package_) {
96         java.util.List JavaDoc contentsList = package_.getContents();
97         Iterator JavaDoc it = contentsList.iterator();
98         while (it.hasNext()) {
99             javax.jmi.model.ModelElement contents = (javax.jmi.model.ModelElement) it.next();
100             if (contents instanceof javax.jmi.model.MofPackage)
101                 processContents((javax.jmi.model.MofPackage) contents);
102             else if (contents instanceof javax.jmi.model.Import)
103                 processContents((javax.jmi.model.MofPackage)((javax.jmi.model.Import) contents).getImportedNamespace());
104             else if (contents instanceof javax.jmi.model.MofClass)
105                 classes.addElement((javax.jmi.model.MofClass) contents);
106         }
107     }
108
109     /**
110      * Package processing.
111      * @param package_ The package to process.
112      * @param class_name_ The class name of the generated class.
113      */

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

158     public void classAttributes(String JavaDoc package_name_, MofPackage package_, Vector JavaDoc classes_) {
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
164         // Classes
165
for (int i = 0; i < classes_.size(); i++) {
166             javax.jmi.model.MofClass cls = (javax.jmi.model.MofClass) classes_.elementAt(i);
167             String JavaDoc className = JMIProvider.jmiClassName(cls);
168             String JavaDoc javaName;
169             try {
170                 javaName = JMIProvider.qualifierOf((MofPackage) cls.getContainer()) + "." + className;
171             } catch (NullPointerException JavaDoc notSet) {
172                 javaName = className;
173             }
174             outputln(
175                 "private "
176                     + javaName
177                     + "Class _"
178                     + JMIProvider.jmiFormat2(JMIProvider.jmiClassQualifiedName(cls))
179                     + "_class = null;");
180         }
181         outputln();
182     }
183
184     /**
185      * XMIImport generation for the initiate method.
186      * @param package_name_ The package name.
187      * @param package_ The package.
188      * @param classes_ The classes of the package.
189      */

190     public void initiateMethod(String JavaDoc package_name_, MofPackage package_, Vector JavaDoc classes_) {
191         String JavaDoc model_package_type;
192         model_package_type = JMIProvider.jmiPackageQualifiedName(package_) + "Package";
193         outputln("public void initiate(" + model_package_type + " _model_package) {");
194         outputln("// Factories creation");
195         outputln("this._" + JMIProvider.jmiFormat2(package_name_) + "_package = _model_package;");
196         // Classes
197
initiateMethodClasses(package_, classes_);
198         outputln("}");
199         outputln();
200     }
201
202     /**
203      * XMIImport generation for the "class attributes" of the initiate method.
204      * @param package_ The package.
205      * @param classes_ The classes of the package.
206      */

207     public void initiateMethodClasses(MofPackage package_, Vector JavaDoc classes_) {
208         for (int i = 0; i < classes_.size(); i++) {
209             javax.jmi.model.MofClass cls = (javax.jmi.model.MofClass) classes_.elementAt(i);
210             String JavaDoc javaName = JMIProvider.jmiFormat2(JMIProvider.jmiClassQualifiedName(cls));
211             String JavaDoc method = "get" + (JMIProvider.jmiClassName(cls)) + "()";
212             
213             try {
214                 javax.jmi.model.ModelElement clsTmp = cls;
215                 while (!clsTmp.getContainer().equals(package_)) {
216                     method = "get" + JMIProvider.jmiPackageExtentName((MofPackage)clsTmp.getContainer()) + "()." + method;
217                     clsTmp = clsTmp.getContainer();
218                 }
219             } catch (NullPointerException JavaDoc notSet) {
220                 // Nothing to do
221
}
222             outputln("_" + javaName + "_class = _model_package." + method + ";");
223         }
224     }
225
226     /**
227      * XMIImport generation for the class template.
228      * @param package_name_ The package name.
229      * @param classes_ The classes of the package.
230      */

231     public void classTemplate(String JavaDoc package_name_, Vector JavaDoc classes_) {
232         outputln("// ==================================================================");
233         outputln("// XXXTemplate with XXX an entity");
234         outputln("// ==================================================================");
235         // Classes
236
for (int i = 0; i < classes_.size(); i++) {
237             javax.jmi.model.MofClass cls = (javax.jmi.model.MofClass) classes_.elementAt(i);
238             if (cls.isAbstract())
239                 classAbstractTemplate(cls);
240             else
241                 classSpecificTemplate(cls);
242             outputln();
243         }
244         outputln();
245     }
246
247     /**
248      * XMIImport generation for the abstract class template.
249      * @param cls The abstract class of the package.
250      */

251     public void classAbstractTemplate(javax.jmi.model.MofClass cls) {
252         String JavaDoc clsName = JMIProvider.jmiClassName(cls);
253         String JavaDoc clsName1 = JMIProvider.jmiFormat1(clsName);
254         String JavaDoc clsName2 = JMIProvider.jmiFormat2(clsName);
255         String JavaDoc javaName = JMIProvider.jmiClassQualifiedName(cls);
256         String JavaDoc methodName = clsName1.substring(0, 1).toLowerCase() + clsName1.substring(1);
257         String JavaDoc returnType = javaName;
258         try {
259             returnType = JMIProvider.qualifierOf((MofPackage) cls.getContainer()) + "." + clsName;
260         } catch (NullPointerException JavaDoc notSet) {
261             returnType = clsName;
262         }
263         String JavaDoc attributeName = "_" + clsName2 + "_element";
264         annotationTemplate(cls.getAnnotation());
265         outputln(
266             "public org.objectweb.modfact.jmi.xmiio.mofobject.MOFObjectList "
267                 + methodName
268                 + "Template(org.w3c.dom.Element "
269                 + attributeName
270                 + ") {");
271         outputln("org.objectweb.modfact.jmi.xmiio.mofobject.MOFObjectList list = new org.objectweb.modfact.jmi.xmiio.mofobject.MOFObjectList();");
272         outputln("// Get the " + clsName1 + " values");
273         outputln("if (" + attributeName + " == null) return list;");
274         outputln(
275             "if ("
276                 + attributeName
277                 + ".getChildNodes().getLength() == 0 && "
278                 + attributeName
279                 + ".hasAttribute(\"xmi.idref\")) {");
280         outputln("// The " + clsName1 + " element is already read or it will be read (ref).");
281         outputln("list.addXmiIdRef(" + attributeName + ".getAttribute(\"xmi.idref\"));");
282         outputln("return list;");
283         outputln("}");
284         outputln("int i = 0;");
285         outputln("org.w3c.dom.NodeList _child = " + attributeName + ".getChildNodes();");
286         outputln("while (i<_child.getLength() || (_child.getLength()==0 && i==0)) {");
287         outputln("org.w3c.dom.Element _current = (org.w3c.dom.Element) _child.item(i);");
288         try {
289             javax.jmi.model.MofClass[] subtypes = XMIIOHelper.subClasses(cls);
290             for (int j = 0; j < subtypes.length; j++) {
291                 if (containerContains(cls.getContainer(), subtypes[j].getContainer())) {
292                     String JavaDoc subTypeXMLName = JMIProvider.jmiFormat1(subtypes[j].getName());
293                     String JavaDoc subTypeName = JMIProvider.jmiFormat1(JMIProvider.jmiClassName(subtypes[j]));
294                     String JavaDoc subTypeTemplateMethod = subTypeName.substring(0, 1).toLowerCase() + subTypeName.substring(1);
295                     outputln("if (_current != null && nodeNameCorrespondsTo(_current, \"" + subTypeXMLName + "\"))");
296                     if (subtypes[j].isAbstract())
297                        outputln(TABULATION + "list.addMOFObjectList(" + subTypeTemplateMethod + "Template(_current));");
298                     else
299                        outputln(TABULATION + subTypeTemplateMethod + "Template(_current).addMOFObject(list);");
300                     output("else ");
301                 }
302             }
303         } catch (NullPointerException JavaDoc notSet) {
304             // Nothing to do
305
}
306         outputln(
307             "if (_current != null && nodeNameCorrespondsTo(_current, \"" + JMIProvider.jmiFormat1(clsName) + "\"))");
308         outputln(TABULATION + "list.addXmiIdRef(_current.getAttribute(\"xmi.idref\"));");
309         outputln("else if (_current != null)");
310         outputln(
311             TABULATION
312                 + "System.err.println(\""
313                 + JMIProvider.jmiFormat1(clsName)
314                 + " is an abstract type, it can't be instanciated (\" + _current.getNodeName() + \" encountered).\");");
315         outputln("i++;");
316         outputln("}");
317         outputln("return list;");
318         outputln("}");
319     }
320     
321     /**
322      * Tests if a namespace contains an other namespace (directly or by imports).
323      * @param parent The namespace to parse.
324      * @param toSearch The namespace to search.
325      * @return TRUE if the search namespace is contained in the namespace.
326      */

327     private boolean containerContains(javax.jmi.model.Namespace parent, javax.jmi.model.Namespace toSearch) {
328         if (parent == null || toSearch == null)
329             return false;
330         if (toSearch.refMofId().equals(parent.refMofId()) || containerContains(parent, toSearch.getContainer())
331             || containerContains(parent.getContainer(), toSearch))
332             return true;
333             
334         java.util.List JavaDoc contents = parent.getContents();
335         Iterator JavaDoc it = contents.iterator();
336         boolean result = false;
337         while (it.hasNext()) {
338             Object JavaDoc element = it.next();
339             javax.jmi.model.Import i;
340             if (element instanceof javax.jmi.model.Import)
341                 result = containerContains(((javax.jmi.model.Import)element).getImportedNamespace() , toSearch);
342         }
343         return result;
344     }
345
346     /**
347      * XMIImport generation for the specific class template.
348      * @param cls The specific class of the package.
349      */

350     public void classSpecificTemplate(javax.jmi.model.MofClass cls) {
351         String JavaDoc className = JMIProvider.jmiClassName(cls);
352         String JavaDoc javaName;
353         try {
354             javaName = JMIProvider.jmiPackageQualifiedName((MofPackage) cls.getContainer()) + "." + className;
355         } catch (NullPointerException JavaDoc notSet) {
356             javaName = className;
357         }
358         String JavaDoc clsName = JMIProvider.jmiClassName(cls);
359         String JavaDoc clsName1 = JMIProvider.jmiFormat1(clsName);
360         String JavaDoc clsName2 = JMIProvider.jmiFormat2(clsName);
361         String JavaDoc methodName = XMIIOHelper.format1FirstMin(clsName1);
362         String JavaDoc returnType = javaName;
363         try {
364             returnType = JMIProvider.qualifierOf((MofPackage) cls.getContainer()) + "." + clsName;
365         } catch (NullPointerException JavaDoc notSet) {
366             returnType = clsName;
367         }
368         String JavaDoc returnMethod = "org.objectweb.modfact.jmi.xmiio.mofobject.MOFObject";
369         String JavaDoc attributeName = "_" + clsName2 + "_element";
370         annotationTemplate(cls.getAnnotation());
371         outputln("public " + returnMethod + " " + methodName + "Template(org.w3c.dom.Element " + attributeName + ") {");
372         outputln("// Get the " + clsName1 + " values");
373         outputln("if (" + attributeName + " == null)");
374         outputln(TABULATION + "return new org.objectweb.modfact.jmi.xmiio.mofobject.NullObject();");
375         outputln(
376             "if ("
377                 + attributeName
378                 + ".getChildNodes().getLength() == 0 && "
379                 + attributeName
380                 + ".hasAttribute(\"xmi.idref\")) {");
381         outputln("// The " + clsName1 + " is already read or it will be read (ref).");
382         outputln(
383             "return new org.objectweb.modfact.jmi.xmiio.mofobject.XMIIdRef("
384                 + attributeName
385                 + ".getAttribute(\"xmi.idref\"));");
386         outputln("}");
387         outputln("// Default " + clsName1 + " creation");
388         outputln(returnType + " _" + clsName2 + " = ");
389         output(
390             " _"
391                 + JMIProvider.jmiFormat2(JMIProvider.jmiClassQualifiedName(cls))
392                 + "_class.create"
393                 + clsName1
394                 + "(");
395
396         javax.jmi.model.Attribute[] attributes =
397             MofHelper.attributesOfClass(cls, javax.jmi.model.ScopeKindEnum.forName("instance_level"), true);
398         outputln(");");
399         outputln(
400             "_types.put(((java.lang.String)_"
401                 + clsName2
402                 + "_element.getAttribute(\"xmi.id\")).trim(), "
403                 + "_"
404                 + clsName2
405                 + ");");
406         outputln("int i = 0;");
407         outputln("org.w3c.dom.NodeList _child = " + attributeName + ".getChildNodes();");
408         outputln("while (i<_child.getLength() || (_child.getLength()==0 && i==0)) {");
409         outputln("org.w3c.dom.Element _current = (org.w3c.dom.Element) _child.item(i);");
410         String JavaDoc param = "_" + clsName2 + "_element, _" + clsName2 + ", _current";
411         attributes = MofHelper.attributesOfClass(cls, javax.jmi.model.ScopeKindEnum.forName("instance_level"), true);
412         for (int j = 0; j < attributes.length; j++) {
413             javax.jmi.model.Attribute attr = (javax.jmi.model.Attribute) attributes[j];
414             if (attr.isChangeable()) {
415                 String JavaDoc attr_name = JMIProvider.findSubstituteName(attr);
416                 if (attr_name == null)
417                     attr_name = attr.getName();
418                 try {
419                     String JavaDoc attr_container_name = JMIProvider.findSubstituteName(attr.getContainer());
420                     if (attr_container_name == null)
421                         attr_container_name = attr.getContainer().getName();
422                     outputln(
423                         "_xmi_import_attributes.read"
424                             + JMIProvider.classNameFormat(attr_container_name)
425                             + JMIProvider.classNameFormat(attr_name)
426                             + "("
427                             + param
428                             + ");");
429                 } catch (NullPointerException JavaDoc notSet) {
430                     outputln("_xmi_import_attributes.read" + JMIProvider.classNameFormat(attr_name) + "(" + param + ");");
431                 }
432             }
433         }
434         javax.jmi.model.Reference[] references = MofHelper.referencesOfClass(cls, true);
435         for (int j = 0; j < references.length; j++) {
436             javax.jmi.model.Reference ref = (javax.jmi.model.Reference) references[j];
437             javax.jmi.model.AssociationEnd assoEnd = ref.getReferencedEnd();
438             if (assoEnd.getAggregation() == javax.jmi.model.AggregationKindEnum.forName("none")) {
439                 String JavaDoc ref_name = JMIProvider.findSubstituteName(ref);
440                 if (ref_name == null)
441                     ref_name = ref.getName();
442                 try {
443                     String JavaDoc ref_container_name = JMIProvider.findSubstituteName(ref.getContainer());
444                     if (ref_container_name == null)
445                         ref_container_name = ref.getContainer().getName();
446                     outputln(
447                         "_xmi_import_attributes.read"
448                             + JMIProvider.classNameFormat(ref_container_name)
449                             + JMIProvider.classNameFormat(ref_name)
450                             + "("
451                             + param
452                             + ");");
453                 } catch (NullPointerException JavaDoc notSet) {
454                     outputln("_xmi_import_attributes.read" + JMIProvider.classNameFormat(ref_name) + "(" + param + ");");
455                 }
456             } else if (assoEnd.getAggregation() == javax.jmi.model.AggregationKindEnum.forName("composite")) {
457                 try {
458                     javax.jmi.model.AssociationEnd otherEnd = XMIIOHelper.otherAssociationEnd(assoEnd);
459                     javax.jmi.model.Reference[] refs =
460                         XMIIOHelper.getReferences((javax.jmi.model.Association) assoEnd.getContainer());
461                     for (int k = 0; k < refs.length; k++) {
462                         if (refs[k] != ref) {
463                             String JavaDoc ref_type_name = JMIProvider.findSubstituteName(refs[k].getType());
464                             if (ref_type_name == null)
465                                 ref_type_name = refs[k].getType().getName();
466                             String JavaDoc asso_end_name = JMIProvider.findSubstituteName(assoEnd);
467                             if (asso_end_name == null)
468                                 asso_end_name = assoEnd.getName();
469                             outputln(
470                                 "_xmi_import_attributes.read"
471                                     + JMIProvider.classNameFormat(ref_type_name)
472                                     + JMIProvider.classNameFormat(asso_end_name)
473                                     + "("
474                                     + param
475                                     + ");");
476                         }
477                     }
478                 } catch (NullPointerException JavaDoc notSet) {
479                     // Nothing to do
480
}
481             }
482         }
483         outputln("i++;");
484         outputln("}");
485         outputln("return new org.objectweb.modfact.jmi.xmiio.mofobject.BuiltObject(_" + clsName2 + ");");
486         outputln("}");
487     }
488
489 }
490
Popular Tags