KickJava   Java API By Example, From Geeks To Geeks.

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


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.corba.xmiio.importer;
21
22 import org.objectweb.modfact.corba.logging.Level;
23 import org.objectweb.modfact.corba.logging.ModFactLogger;
24 import org.objectweb.modfact.corba.xmiio.common.AbstractXMIIOGenerator;
25 import org.omg.mof.Reflective.MofError;
26 import org.omg.mof.Reflective.NotSet;
27
28 /**
29  * The XMI Import generator.
30  * This class allows the generation of the XMI importer (into Java).
31  */

32 public class GenerationXMIImportClasses extends AbstractXMIIOGenerator {
33
34     /** The logger. */
35     ModFactLogger _logger = ModFactLogger.getLogger("fr.lip6.src.mdafactory.generation.xmiimport.GenerationXMIImport");
36
37     /** The generated association attributes methods. */
38     private java.util.List JavaDoc _associationAttributes;
39
40     /**
41      * Default Constructor.
42      */

43     public GenerationXMIImportClasses() {
44         super();
45         ModFactLogger logger = ModFactLogger.getLogger("fr.lip6.src.mdafactory.generation.xmiimport.GenerationXMIImportApplication");
46         _associationAttributes = new java.util.ArrayList JavaDoc();
47     }
48
49     /**
50      * XMIImport generation.
51      * @param package_ The root package.
52      * @param class_name_ The class name of the generated class.
53      */

54     public void generateXMIImportClasses(org.omg.mof.Model.Package package_, String JavaDoc class_name_) throws org.omg.mof.Reflective.MofError, org.omg.CORBA.TypeCodePackage.BadKind JavaDoc, org.omg.CORBA.TypeCodePackage.Bounds JavaDoc, java.io.FileNotFoundException JavaDoc {
55         _logger.log(Level.FINE, "XMIImport beginning of the generation");
56         _logger.log(Level.INFO, class_name_ + " generation ...");
57         packageModuleTemplate(package_, class_name_);
58         flushFile();
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         _logger.log(Level.FINE, "XMIImport constructor(" + class_name + ")");
67         outputln("/** The logger. */");
68         outputln("org.objectweb.modfact.corba.logging.ModFactLogger _logger;");
69         outputln();
70         outputln("/** The XMIImportAttributes class. */");
71         outputln(package_name + "XMIImportAttributes _xmi_import_attributes;");
72         outputln();
73         outputln("/** Hashtables for types (Key / Model) */");
74         outputln("private java.util.Hashtable _types;");
75         outputln();
76         outputln("/**");
77         outputln(" * Default " + class_name + " Constructor");
78         outputln(" * @param logger The logger.");
79         outputln(" * @param types The defined types.");
80         outputln(" */");
81         outputln("public " + class_name + "(org.objectweb.modfact.corba.logging.ModFactLogger logger, java.util.Hashtable types) {");
82         outputln("_logger = logger;");
83         outputln("_types = types;");
84         outputln("}");
85         outputln();
86         outputln("public void setXMIImportAttributes(" + package_name + "XMIImportAttributes xmi_import_attributes) {");
87         outputln("_xmi_import_attributes = xmi_import_attributes;");
88         outputln("}");
89         outputln();
90     }
91
92     /**
93      * Package processing.
94      * @param package_ The package to process.
95      * @param class_name_ The class name of the generated class.
96      */

97     public void packageModuleTemplate(org.omg.mof.Model.Package package_, String JavaDoc class_name_) throws org.omg.mof.Reflective.MofError, org.omg.CORBA.TypeCodePackage.BadKind JavaDoc, org.omg.CORBA.TypeCodePackage.Bounds JavaDoc {
98         _logger.log(Level.FINE, "XMIImport packageModuleTemplate(" + package_.name() + ")");
99         if (package_.visibility().equals(org.omg.mof.Model.VisibilityKind.public_vis)) {
100             org.omg.mof.Model.Class classes_ordered [] = mofHelper.allClassesOfPackageOrderedByInheritance(package_);
101             _logger.log(Level.FINE, "XMIImport produce");
102
103             outputln("package " + javaHelper.javaPackage(package_) + ".xmi;");
104             outputln();
105             annotationTemplate(package_.annotation());
106             outputln("public class " + class_name_ + " extends org.objectweb.modfact.corba.xmiio.importer.XMIImport {");
107             classAttributes(package_, classes_ordered);
108             constructor(class_name_, package_.name());
109             initiateMethod(package_, classes_ordered);
110             classTemplate(package_.name(), classes_ordered);
111             outputln("} // end of class " + class_name_);
112             outputln();
113         }
114     }
115     
116     /**
117     * XMIImport generation for the class attributes.
118     * @param package_name_ The package name.
119     * @param classes_ The classes of the package.
120     */

121    public void classAttributes(org.omg.mof.Model.Package package_, org.omg.mof.Model.Class classes_ []) throws MofError {
122        String JavaDoc package_name = package_.name();
123        _logger.log(Level.FINE, "XMIImport classAttributes(" + package_ + ")");
124        outputln("// Factories Declaration");
125        String JavaDoc model_package_type;
126        if (mappingValid) {
127            model_package_type = javaHelper.javaPackage(package_) + "._" + idlHelper.format1(package_name) + "Package";
128        } else {
129            model_package_type = javaHelper.javaPackage(package_) + "." + idlHelper.format1(package_name) + "Package";
130        }
131        outputln("private " + model_package_type + " _" + idlHelper.format2(package_name) + "_package = null;");
132        // Classes
133
for (int i = 0; i < classes_.length; i++) {
134         org.omg.mof.Model.Class cls = (org.omg.mof.Model.Class) classes_[i];
135         String JavaDoc className;
136         try {
137             className = idlHelper.className(cls);
138         } catch (NotSet notSet) {
139             className = cls.name();
140         }
141         String JavaDoc javaName;
142         try {
143             javaName = javaHelper.javaPackage(org.omg.mof.Model.PackageHelper.narrow(cls.container())) + "." + className;
144             outputln("private " + javaName + "Class _" + idlHelper.format2(javaHelper.javaQualifiedName(cls)) + "_class = null;");
145         } catch (org.omg.mof.Reflective.NotSet notSet) {
146             // Nothing to do.
147
}
148     }
149     outputln();
150    }
151
152     /**
153      * XMIImport generation for the initiate method.
154      * @param package_ The package.
155      * @param classes_ The classes of the package.
156      */

157     public void initiateMethod(org.omg.mof.Model.Package package_, org.omg.mof.Model.Class classes_ []) throws MofError {
158         String JavaDoc package_name = package_.name();
159         _logger.log(Level.FINE, "XMIImport initiateMethod(" + package_name + ", " + classes_.length + ")");
160         String JavaDoc model_package_type;
161         if (mappingValid) {
162             model_package_type = javaHelper.javaPackage(package_) + "._" + idlHelper.format1(package_name) + "Package";
163         } else {
164             model_package_type = javaHelper.javaPackage(package_) + "." + idlHelper.format1(package_name) + "Package";
165         }
166         outputln("public void initiate(" + model_package_type + " _model_package)");
167         outputln(" throws org.omg.mof.Reflective.MofError {");
168         outputln("_logger.log(org.objectweb.modfact.corba.logging.Level.FINE, \"initiate(\" + _model_package + \")\");");
169         outputln("// Factories creation");
170         outputln("this._" + idlHelper.format2(package_name) + "_package = _model_package;");
171         // Classes
172
initiateMethodClasses(package_name, classes_);
173         outputln("}");
174         outputln();
175     }
176     
177     /**
178      * XMIImport generation for the "class attributes" of the initiate method.
179      * @param package_name_ The package name.
180      * @param classes_ The classes of the package.
181      */

182     public void initiateMethodClasses(String JavaDoc package_name_, org.omg.mof.Model.Class classes_ []) throws MofError {
183         for (int i = 0; i < classes_.length; i++) {
184             org.omg.mof.Model.Class cls = (org.omg.mof.Model.Class) classes_[i];
185             String JavaDoc className = cls.name();
186             String JavaDoc javaName;
187             try {
188                 className = javaHelper.className(cls);
189                 javaName = idlHelper.format2(javaHelper.javaQualifiedName(cls));
190             } catch (NotSet notSet) {
191                 javaName = idlHelper.format2(className);
192             }
193             String JavaDoc method = idlHelper.format2(className) + "_ref()";
194             try {
195                 org.omg.mof.Model.ModelElement clsTmp = cls;
196                 while (!clsTmp.container().name().equals(package_name_)) {
197                     String JavaDoc containerName = clsTmp.container().name();
198                     if (clsTmp.container()._is_a(org.omg.mof.Model.ClassifierHelper.id())) {
199                         org.omg.mof.Model.Classifier container = org.omg.mof.Model.ClassifierHelper.narrow(clsTmp.container());
200                         containerName = javaHelper.javaClassifierName(container);
201                     }
202                     method = idlHelper.format2(containerName) + "_ref()." + method;
203                     clsTmp = clsTmp.container();
204                 }
205             } catch (org.omg.CORBA.MARSHAL JavaDoc marshal) {
206                 /* The marshal exception contains the NotSet Exception */
207                 if (!marshal.getMessage().equals("IDL:mof.omg.org/Reflective/NotSet:1.0"))
208                     throw marshal;
209             } catch (org.omg.mof.Reflective.NotSet notSet) {
210                 // Nothing to do
211
}
212             outputln("_" + javaName + "_class = _model_package." + method + ";");
213         }
214     }
215
216     /**
217      * XMIImport generation for the class template.
218      * @param package_name_ The package name.
219      * @param classes_ The classes of the package.
220      */

221     public void classTemplate(String JavaDoc package_name_, org.omg.mof.Model.Class classes_ []) throws org.omg.mof.Reflective.MofError {
222         _logger.log(Level.FINE, "XMIImport classTemplate(" + package_name_ + ", " + classes_.length + ")");
223         outputln("// ==================================================================");
224         outputln("// XXXTemplate with XXX an entity");
225         outputln("// ==================================================================");
226         // Classes
227
for (int i = 0; i < classes_.length; i++) {
228             org.omg.mof.Model.Class cls = (org.omg.mof.Model.Class) classes_[i];
229             if (cls.is_abstract())
230                 classAbstractTemplate(cls);
231             else
232                 classSpecificTemplate(cls);
233             outputln();
234         }
235         outputln();
236     }
237
238     /**
239       * XMIImport generation for the abstract class template.
240       * @param cls The abstract class of the package.
241       */

242     public void classAbstractTemplate(org.omg.mof.Model.Class cls) throws org.omg.mof.Reflective.MofError {
243         String JavaDoc clsName = cls.name();
244         String JavaDoc clsName1 = idlHelper.format1(clsName);
245         String JavaDoc clsName2 = idlHelper.format2(clsName);
246         String JavaDoc javaName;
247         try {
248             javaName = javaHelper.javaQualifiedName(cls);
249         } catch (NotSet notSet) {
250             javaName = clsName;
251         }
252         String JavaDoc methodName = idlHelper.format1FirstMin(clsName);
253         String JavaDoc returnType = javaName;
254         String JavaDoc attributeName = "_" + clsName2 + "_element";
255         annotationTemplate(cls.annotation());
256         outputln("public org.objectweb.modfact.corba.xmiio.mofobject.MOFObjectList " + methodName + "Template(org.w3c.dom.Element " + attributeName + ")");
257         outputln(" throws org.omg.mof.Reflective.MofError {");
258         outputln("_logger.log(org.objectweb.modfact.corba.logging.Level.FINE, \"" + methodName + "Template(\" + " + attributeName + " + \")\");");
259         outputln("org.objectweb.modfact.corba.xmiio.mofobject.MOFObjectList list = new org.objectweb.modfact.corba.xmiio.mofobject.MOFObjectList();");
260         outputln("// Get the " + clsName1 + " values");
261         outputln("if (" + attributeName + " == null) return list;");
262         outputln("if (" + attributeName + ".getChildNodes().getLength() == 0 && " + attributeName + ".hasAttribute(\"xmi.idref\")) {");
263         outputln("// The " + clsName1 + " element is already read or it will be read (ref).");
264         outputln("list.addXmiIdRef(" + attributeName + ".getAttribute(\"xmi.idref\"));");
265         outputln("return list;");
266         outputln("}");
267         outputln("int i = 0;");
268         outputln("org.w3c.dom.NodeList _child = " + attributeName + ".getChildNodes();");
269         outputln("while (i<_child.getLength() || (_child.getLength()==0 && i==0)) {");
270         outputln("org.w3c.dom.Element _current = (org.w3c.dom.Element) _child.item(i);");
271         try {
272             org.omg.mof.Model.Class[] subtypes = mofHelper.subClassesOfClass(cls);
273             for (int j = 0; j < subtypes.length; j++) {
274                 outputln("if (_current != null && nodeNameCorrespondsTo(_current, \"" + idlHelper.format1(subtypes[j].name()) + "\"))");
275                 if (subtypes[j].is_abstract())
276                     outputln(TABULATION + "list.addMOFObjectList(" + idlHelper.format1FirstMin(subtypes[j].name()) + "Template(_current));");
277                 else
278                     outputln(TABULATION + idlHelper.format1FirstMin(subtypes[j].name()) + "Template(_current).addMOFObject(list);");
279                 output("else ");
280             }
281         } catch (org.omg.mof.Reflective.NotSet notSet) {
282             // Nothing to do
283
}
284         outputln("if (_current != null && nodeNameCorrespondsTo(_current, \"" + idlHelper.format1(clsName) + "\"))");
285         outputln(TABULATION + "list.addXmiIdRef(_current.getAttribute(\"xmi.idref\"));");
286         outputln("else if (_current != null)");
287         outputln(TABULATION + "System.err.println(\"" + idlHelper.format1(clsName) + " is an abstract type, it can't be instanciated (\" + _current.getNodeName() + \" encountered).\");");
288         outputln("i++;");
289         outputln("}");
290         outputln("return list;");
291         outputln("}");
292     }
293
294     /**
295      * XMIImport generation for the specific class template.
296      * @param cls The specific class of the package.
297      */

298     public void classSpecificTemplate(org.omg.mof.Model.Class cls) throws org.omg.mof.Reflective.MofError {
299         String JavaDoc className;
300         String JavaDoc javaName;
301         try {
302             className = idlHelper.className(cls);
303             javaName = javaHelper.javaPackage(org.omg.mof.Model.PackageHelper.narrow(cls.container())) + "." + className;
304         } catch (NotSet notSet) {
305             className = cls.name();
306             javaName = className;
307         }
308         String JavaDoc clsName = cls.name();
309         try {
310             clsName = idlHelper.className(cls);
311         } catch (NotSet notSet) {
312             // Nothing to do.
313
}
314         String JavaDoc clsName1 = idlHelper.format1(clsName);
315         String JavaDoc clsName2 = idlHelper.format2(clsName);
316         String JavaDoc methodName = idlHelper.format1FirstMin(cls.name());
317         String JavaDoc returnType = javaName;
318         String JavaDoc returnMethod = "org.objectweb.modfact.corba.xmiio.mofobject.MOFObject";
319         String JavaDoc attributeName = "_" + clsName2 + "_element";
320         annotationTemplate(cls.annotation());
321         outputln("public " + returnMethod + " " + methodName + "Template(org.w3c.dom.Element " + attributeName + ")");
322         outputln(" throws org.omg.mof.Reflective.MofError {");
323         outputln("_logger.log(org.objectweb.modfact.corba.logging.Level.FINE, \"" + methodName + "Template(\" + " + attributeName + " + \")\");");
324         outputln("// Get the " + clsName1 + " values");
325         outputln("if (" + attributeName + " == null)");
326         outputln(TABULATION + "return new org.objectweb.modfact.corba.xmiio.mofobject.NullObject();");
327         outputln("if (" + attributeName + ".getChildNodes().getLength() == 0 && " + attributeName + ".hasAttribute(\"xmi.idref\")) {");
328         outputln("// The " + clsName1 + " is already read or it will be read (ref).");
329         outputln("return new org.objectweb.modfact.corba.xmiio.mofobject.XMIIdRef(" + attributeName + ".getAttribute(\"xmi.idref\"));");
330         outputln("}");
331         outputln("// Default " + clsName1 + " creation");
332         outputln(returnType + " _" + clsName2 + " = ");
333         try {
334             output(" _" + idlHelper.format2(javaHelper.javaQualifiedName(cls)) + "_class.create_" + clsName2 + "(");
335         } catch (NotSet notSet) {
336             output(" null;");
337         }
338         org.omg.mof.Model.MofAttribute attributes [] = mofHelper.allAttributesOfClass(cls, org.omg.mof.Model.ScopeKind.instance_level, false);
339         for (int j = 0; j < attributes.length; j++) {
340             org.omg.mof.Model.MofAttribute attr = (org.omg.mof.Model.MofAttribute) attributes[j];
341             if (attr.is_changeable()) {
342                 if (j > 0)
343                     output(", ");
344                 if (attr.multiplicity().upper == -1) { // upper = n
345
try {
346                         if (attr.type()._is_a(org.omg.mof.Model.DataTypeHelper.id())) {
347                             org.omg.CORBA.TypeCode JavaDoc typeCode = org.omg.mof.Model.DataTypeHelper.narrow(attr.type()).type_code();
348                             if (typeCode.kind().value() == org.omg.CORBA.TCKind._tk_alias) {
349                                 if (typeCode.content_type().kind().value() == org.omg.CORBA.TCKind._tk_any)
350                                     output("new " + typeCode.content_type().name() + " [0]");
351                                 else
352                                     output("new " + xmiImpHelper.typeCode2Java(typeCode.content_type()) + " [0]");
353                             } else {
354                                 output("new " + xmiImpHelper.typeCode2Java(typeCode) + " [0]");
355                             }
356
357                         }
358                     } catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc badKind) {
359                         output(xmiImpHelper.defaultAttributeValue(attr.type()));
360                     }
361                 } else
362                     output(xmiImpHelper.defaultAttributeValue(attr.type()));
363             }
364         }
365         outputln(");");
366         outputln("_types.put(((java.lang.String)_" + clsName2 + "_element.getAttribute(\"xmi.id\")).trim(), " + "_" + clsName2 + ");");
367         for (int j = 0; j < attributes.length ; j++) {
368             org.omg.mof.Model.MofAttribute attr = (org.omg.mof.Model.MofAttribute) attributes[j];
369             if (attr.multiplicity().lower == 0 && attr.multiplicity().upper != -1) {
370                 outputln("_" + clsName2 + ".unset_" + idlHelper.format2(attr.name()) + "();");
371             }
372         }
373         outputln("int i = 0;");
374         outputln("org.w3c.dom.NodeList _child = " + attributeName + ".getChildNodes();");
375         outputln("while (i<_child.getLength() || (_child.getLength()==0 && i==0)) {");
376         outputln("org.w3c.dom.Element _current = (org.w3c.dom.Element) _child.item(i);");
377         String JavaDoc param = "_" + clsName2 + "_element, _" + clsName2 + ", _current";
378         attributes = mofHelper.allAttributesOfClass(cls, org.omg.mof.Model.ScopeKind.instance_level, false);
379         for (int j = 0; j < attributes.length ; j++) {
380             org.omg.mof.Model.MofAttribute attr = (org.omg.mof.Model.MofAttribute) attributes[j];
381             if (attr.is_changeable()) {
382                 try {
383                     outputln("_xmi_import_attributes.read" + idlHelper.format1(attr.container().name()) + idlHelper.format1(attr.name()) + "(" + param + ");");
384                 } catch (org.omg.mof.Reflective.NotSet notSet) {
385                     outputln("_xmi_import_attributes.read" + idlHelper.format1(attr.name()) + "(" + param + ");");
386                 }
387             }
388         }
389         org.omg.mof.Model.Reference references [] = mofHelper.allReferencesOfClass(cls);
390         for (int j = 0; j < references.length ; j++) {
391             org.omg.mof.Model.Reference ref = (org.omg.mof.Model.Reference) references[j];
392             org.omg.mof.Model.AssociationEnd assoEnd = ref.referenced_end();
393             if (assoEnd.aggregation().value() == org.omg.mof.Model.AggregationKind._none) {
394                 try {
395                     outputln("_xmi_import_attributes.read" + idlHelper.format1(ref.container().name()) + idlHelper.format1(ref.name()) + "(" + param + ");");
396                 } catch (org.omg.mof.Reflective.NotSet notSet) {
397                     outputln("_xmi_import_attributes.read" + idlHelper.format1(ref.name()) + "(" + param + ");");
398                 }
399             } else if (assoEnd.aggregation().value() == org.omg.mof.Model.AggregationKind._composite) {
400                 try {
401                     org.omg.mof.Model.AssociationEnd otherEnd = mofHelper.oppositeAssociationEnd(assoEnd);
402                     org.omg.mof.Model.Reference[] refs = mofHelper.referencesOfAssociation(org.omg.mof.Model.AssociationHelper.narrow(assoEnd.container()));
403                     for (int k = 0; k < refs.length; k++) {
404                         if (!refs[k]._is_equivalent(ref)) {
405                             outputln("_xmi_import_attributes.read" + idlHelper.format1(refs[k].type().name()) + idlHelper.format1(assoEnd.name()) + "(" + param + ");");
406                         }
407                     }
408                 } catch (org.omg.mof.Reflective.NotSet notSet) {
409                     // Nothing to do
410
}
411             }
412         }
413         outputln("i++;");
414         outputln("}");
415         outputln("return new org.objectweb.modfact.corba.xmiio.mofobject.BuiltObject(_" + clsName2 + ");");
416         outputln("}");
417     }
418
419 }
420
Popular Tags