KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > xmiio > exporter > GenerationXMIExport


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.exporter;
21
22 import java.util.List JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import javax.jmi.model.Attribute;
26 import javax.jmi.model.Classifier;
27 import javax.jmi.model.DataType;
28 import javax.jmi.model.EnumerationType;
29 import javax.jmi.model.Import;
30 import javax.jmi.model.ModelElement;
31 import javax.jmi.model.MofClass;
32 import javax.jmi.model.MofPackage;
33 import javax.jmi.model.Namespace;
34 import javax.jmi.model.PrimitiveType;
35 import javax.jmi.model.Reference;
36 import javax.jmi.model.ScopeKindEnum;
37 import javax.jmi.model.VisibilityKindEnum;
38
39 import org.objectweb.modfact.jmi.helper.JMIProvider;
40 import org.objectweb.modfact.jmi.helper.MofHelper;
41 import org.objectweb.modfact.jmi.helper.XMIIOHelper;
42 import org.objectweb.modfact.jmi.logging.Level;
43 import org.objectweb.modfact.jmi.logging.ModFactLogger;
44 import org.objectweb.modfact.jmi.xmiio.common.AbstractXMIIOGenerator;
45
46 /**
47  * The XMI Export generator.
48  * This class allows the generation of the XMI exporter (into XMI).
49  */

50 public class GenerationXMIExport extends AbstractXMIIOGenerator {
51
52     /** The logger. */
53     protected ModFactLogger _logger = ModFactLogger.getLogger("fr.lip6.src.mdafactory.generation.xmiio.exporter.GenerationXMIExport");
54
55     /**
56      * Default Constructor.
57      */

58     public GenerationXMIExport() {
59         super();
60         ModFactLogger logger = ModFactLogger.getLogger("fr.lip6.src.mdafactory.generation.xmiio.exporter.GenerationXMIExportApplication");
61     }
62
63     /**
64      * XMIExport generation.
65      * @param package_ The root package.
66      */

67     public void generateXMIExport(MofPackage package_, String JavaDoc className) throws java.io.IOException JavaDoc {
68         _logger.log(Level.FINE, "XMIExport beginning of the generation");
69         _logger.log(Level.FINE, className + " generation ...");
70         packageModuleTemplate(package_, className);
71         flushFile();
72     }
73
74     java.util.Vector JavaDoc classes = new java.util.Vector JavaDoc();
75     java.util.Vector JavaDoc dataTypes = new java.util.Vector JavaDoc();
76
77     private void processContents(MofPackage package_) {
78         java.util.List JavaDoc contentsList = package_.getContents();
79         java.util.Iterator JavaDoc it = contentsList.iterator();
80         while (it.hasNext()) {
81             javax.jmi.model.ModelElement contents = (javax.jmi.model.ModelElement) it.next();
82             if (contents instanceof javax.jmi.model.MofPackage)
83                 processContents((javax.jmi.model.MofPackage) contents);
84             else if (contents instanceof javax.jmi.model.Import)
85                 processContents((javax.jmi.model.MofPackage) ((javax.jmi.model.Import) contents).getImportedNamespace());
86             else if (contents instanceof javax.jmi.model.MofClass)
87                 classes.addElement((javax.jmi.model.MofClass) contents);
88             else if (contents instanceof javax.jmi.model.DataType)
89                 dataTypes.addElement((javax.jmi.model.DataType) contents);
90         }
91     }
92
93     /**
94      * Find all the Class Attributes of the package.
95      * @param package_ The package to parse.
96      * @return The found Class Attributes.
97      */

98     protected java.util.Vector JavaDoc findAllClassAttributes(MofPackage package_) {
99         MofClass[] contents = XMIIOHelper.allClassesOfPackageOrderedByInheritance(package_);
100         java.util.Vector JavaDoc attributes = new java.util.Vector JavaDoc();
101         for (int i = 0; i < contents.length; i++) {
102             if (contents[i] instanceof MofClass) {
103                 MofClass class_ = (MofClass) contents[i];
104                 Reference references[] = MofHelper.referencesOfClass(class_, true);
105                 for (int l = 0; l < references.length; l++) {
106                     Classifier classifier = references[l].getType();
107                     if (!attributes.contains(classifier))
108                         attributes.add(classifier);
109                 }
110                 Attribute attr[] = MofHelper.attributesOfClass(class_, ScopeKindEnum.forName("instance_level"), true);
111                 for (int l = 0; l < attr.length; l++) {
112                     Classifier classifier = attr[l].getType();
113                     if (classifier instanceof MofClass && !attributes.contains(classifier))
114                         attributes.add(classifier);
115                 }
116             } else if (contents[i] instanceof Import) {
117                 Import imp = (Import) contents[i];
118                 java.util.Vector JavaDoc call = findAllClassAttributes((MofPackage) imp.getImportedNamespace());
119                 java.util.Iterator JavaDoc iterator = call.iterator();
120                 while (iterator.hasNext()) {
121                     boolean found = false;
122                     Object JavaDoc next = iterator.next();
123                     for (int k = 0; k < attributes.size(); k++) {
124                         if (attributes.elementAt(k).equals(next))
125                             found = true;
126                     }
127                     if (!found)
128                         attributes.add(next);
129                 }
130             }
131         }
132         return attributes;
133     }
134
135     /**
136      * Package processing.
137      * @param package_ The package to process.
138      */

139     public void packageModuleTemplate(MofPackage package_, String JavaDoc className) {
140         _logger.log(Level.FINE, "XMIExport packageModuleTemplate");
141         if (package_.getVisibility() == VisibilityKindEnum.forName("public_vis")) {
142             MofClass classes_ordered[] = XMIIOHelper.allClassesOfPackageOrderedByInheritance(package_);
143
144             outputln("package " + JMIProvider.qualifierOf(package_) + ".xmi;");
145             outputln();
146             annotationTemplate(package_.getAnnotation());
147             outputln("public class " + className + " extends org.objectweb.modfact.jmi.xmiio.exporter.XMIExport {");
148
149             processContents(package_);
150             constructor(package_.getName(), className);
151             classAttributes(findAllClassAttributes(package_));
152             classDataTypeAttributes(dataTypes);
153             classTemplate(package_, classes_ordered);
154             rootPackage(package_, classes_ordered);
155             outputln("} // end of class " + className);
156             outputln();
157         }
158     }
159
160     /**
161      * XMIExport generation for constructor.
162      * @param package_name The package name.
163      * @param class_name The class name.
164      */

165     public void constructor(String JavaDoc package_name, String JavaDoc class_name) {
166         _logger.log(Level.FINE, "XMIExport constructor(" + package_name + ", " + class_name + ")");
167         outputln("/**");
168         outputln(" * Default " + class_name + " Constructor");
169         outputln(" * @param dtd The DTD File.");
170         outputln(" */");
171         outputln("public " + class_name + "(java.lang.String dtd) {");
172         outputln("this(dtd, org.objectweb.modfact.jmi.logging.ModFactLogger.getLogger(\"" + package_name + "XMIExport\"));");
173         outputln("}");
174         outputln();
175         outputln("/**");
176         outputln(" * " + class_name + " Constructor");
177         outputln(" * @param dtd The DTD File.");
178         outputln(" * @param logger The logger.");
179         outputln(" */");
180         outputln("public " + class_name + "(java.lang.String dtd, org.objectweb.modfact.jmi.logging.ModFactLogger logger) {");
181         outputln("super(dtd, \"" + JMIProvider.jmiFormat1(package_name) + "\", \"Model\");");
182         outputln("_logger = logger;");
183         outputln("}");
184         outputln();
185     }
186
187     protected String JavaDoc refMethod(MofClass cls, String JavaDoc package_name) {
188         String JavaDoc method = "get" + JMIProvider.jmiFormat1(JMIProvider.jmiClassName(cls)) + "()";
189         ModelElement clsTmp = cls;
190         while (!clsTmp.getContainer().getName().equals(package_name)) {
191             Namespace ns = clsTmp.getContainer();
192             if (ns instanceof Classifier) {
193                 Classifier container = (Classifier) ns;
194                 method = "get" + JMIProvider.jmiPackageExtentName((MofPackage) container) + "()." + method;
195             } else {
196                 method = "get" + JMIProvider.jmiPackageExtentName((MofPackage) clsTmp.getContainer()) + "()." + method;
197             }
198             clsTmp = clsTmp.getContainer();
199         }
200         return method;
201     }
202
203     /**
204      * Generate the methods for the writing of complex type.
205      * @param attributes List of DataType attributes.
206      */

207     public void classAttributes(Vector JavaDoc attributes) {
208         _logger.log(Level.FINE, "XMIExport classAttributeTemplate()");
209         java.util.Iterator JavaDoc iterator = attributes.iterator();
210         outputln("// ==================================================================");
211         outputln("// Method to write some Classes ");
212         outputln("// ==================================================================");
213         while (iterator.hasNext()) {
214             Classifier classifier2 = (Classifier) iterator.next();
215             MofClass classifier = (MofClass) classifier2;
216             annotationTemplate(classifier.getAnnotation());
217             String JavaDoc classifierQualifiedName;
218             classifierQualifiedName = JMIProvider.jmiClassifierQualifiedName(classifier);
219             outputln("public void write" + JMIProvider.jmiFormat1(classifier.getName()) + "(" + classifierQualifiedName + " _toWrite) {");
220             outputln("if (_toWrite == null)");
221             outputln(" return;");
222             outputln("outputln(\"<" + JMIProvider.jmiFormat1(classifier.getName()) + " xmi.id=\\\"\" + getXMIId(_toWrite) + \"\\\">\");");
223             Attribute attr[] = MofHelper.attributesOfClass(classifier, ScopeKindEnum.forName("instance_level"), true);
224             for (int j = 0; j < attr.length; j++) {
225                 if (!attr[j].isDerived())
226                     classAttributeTemplate(attr[j], "_toWrite");
227             }
228             Reference ref[] = MofHelper.referencesOfClass(classifier, true);
229             for (int j = 0; j < ref.length; j++) {
230                 classReferenceTemplate(ref[j], "_toWrite");
231             }
232             outputln("outputln(\"</" + JMIProvider.jmiFormat1(classifier.getName()) + ">\");");
233             outputln("}");
234             outputln();
235         }
236     }
237
238     /**
239      * Generate the methods for the writing of complex type.
240      * @param attributes List of DataType attributes.
241      */

242     public void classDataTypeAttributes(Vector JavaDoc attributes) {
243         _logger.log(Level.FINE, "XMIExport classAttributeTemplate()");
244         java.util.Iterator JavaDoc iterator = attributes.iterator();
245         outputln("// ==================================================================");
246         outputln("// Method to write the DataTypes ");
247         outputln("// ==================================================================");
248         while (iterator.hasNext()) {
249             DataType dataType = (DataType) iterator.next();
250             if (dataType instanceof EnumerationType) {
251                 EnumerationType enumType = (EnumerationType) dataType;
252                 outputln(
253                     "public java.lang.String enumeration"
254                         + JMIProvider.jmiFormat1(enumType.getName())
255                         + "ToString("
256                         + JMIProvider.jmiClassifierQualifiedName(enumType)
257                         + " value) {");
258
259                 List JavaDoc labels = enumType.getLabels();
260                 for (int i = 0; i < labels.size(); i++) {
261                     outputln(
262                         "if (value.equals(" + JMIProvider.jmiClassifierQualifiedName(enumType) + "Enum." + labels.get(i).toString().toUpperCase() + "))");
263                     outputln(" return \"" + JMIProvider.jmiFormat2(labels.get(i).toString()) + "\";");
264                 }
265                 outputln("return new java.lang.String();");
266                 outputln("}");
267                 outputln();
268             }
269         }
270     }
271
272     /**
273      * XMIExport generation for the class template.
274      * @param package_ The package.
275      * @param classes The classes of the package.
276      */

277     public void classTemplate(MofPackage package_, MofClass classes[]) {
278         _logger.log(Level.FINE, "XMIExport classTemplate()");
279         String JavaDoc package_name = package_.getName();
280         outputln("// ==================================================================");
281         outputln("// XXXTemplate with XXX an entity ");
282         outputln("// ==================================================================");
283         // Classes
284
for (int i = 0; i < classes.length; i++) {
285             MofClass cls = classes[i];
286             annotationTemplate(cls.getAnnotation());
287             String JavaDoc model_package_type;
288             model_package_type = JMIProvider.jmiPackageQualifiedName(package_) + "Package";
289             outputln("public void " + XMIIOHelper.format1FirstMin(cls.getName()) + "Template(" + model_package_type + " _model_package) {");
290             String JavaDoc clsClass = XMIIOHelper.format1FirstMin(cls.getName()) + "Class";
291             String JavaDoc clsTab = XMIIOHelper.testJavaConflict(XMIIOHelper.format1FirstMin(cls.getName()) + "s");
292             String JavaDoc clsName;
293             String JavaDoc xmiQualifiedName = JMIProvider.jmiFormat1(cls.getName());
294             try {
295                 clsName = JMIProvider.jmiClassName(cls);
296             } catch (NullPointerException JavaDoc notSet) {
297                 clsName = cls.getName();
298             }
299             String JavaDoc javaQualifiedName = JMIProvider.jmiClassQualifiedName(cls);
300             outputln(
301                 "_logger.log(org.objectweb.modfact.jmi.logging.Level.FINE, \""
302                     + XMIIOHelper.format1FirstMin(cls.getName())
303                     + "Template(\" + _model_package + \")\");");
304             outputln(javaQualifiedName + "Class " + clsClass + " = _model_package." + refMethod(cls, package_name) + ";");
305             try {
306                 outputln("java.util.Collection " + clsTab + " = " + clsClass + ".refAllOfType" + "();");
307             } catch (NullPointerException JavaDoc notSet) {
308                 outputln("java.util.Collection " + clsTab + " = " + clsClass + ".refAllOfType" + "();");
309             }
310             //outputln("for (int i=0 ; i<" + clsTab + ".length ; i++) {");
311
outputln("java.util.Iterator iterator = " + clsTab + ".iterator();");
312             outputln("while (iterator.hasNext()) {");
313             outputln(javaQualifiedName + " current = (" + javaQualifiedName + ")iterator.next();");
314             outputln("outputln(\"<" + xmiQualifiedName + " xmi.id=\\\"\" + getXMIId(current) + \"\\\">\");");
315             Attribute attributes[] = MofHelper.attributesOfClass(cls, ScopeKindEnum.forName("instance_level"), true);
316             for (int j = 0; j < attributes.length; j++) {
317                 if (!attributes[j].isDerived())
318                     classAttributeTemplate(attributes[j], "current");
319             }
320             Reference references[] = MofHelper.referencesOfClass(cls, true);
321             for (int j = 0; j < references.length; j++) {
322                 Reference ref = references[j];
323                 String JavaDoc variable = JMIProvider.jmiAccessorName(ref);
324
325                 if (ref.getMultiplicity().getUpper() == -1) {
326                     outputln("for (java.util.Iterator it = current." + variable + "().iterator() ; it.hasNext() ;) {");
327                     outputln(
328                         "write"
329                             + JMIProvider.jmiFormat1(ref.getType().getName())
330                             + "(("
331                             + JMIProvider.jmiClassifierQualifiedName(ref.getType())
332                             + ")it.next());");
333                     outputln("}");
334                 } else {
335                     String JavaDoc content = "write" + JMIProvider.jmiFormat1(ref.getType().getName()) + "(current." + variable + "());";
336                     if (ref.getMultiplicity().getLower() == 0) {
337                         outputln("try {");
338                         outputln(content);
339                         outputln("} catch (java.lang.NullPointerException notSet) { /* Nothing to do */ }");
340                     } else {
341                         outputln(content);
342                     }
343                 }
344
345             }
346             outputln("outputln(\"</" + xmiQualifiedName + ">\");");
347             outputln("}");
348             outputln("}");
349             outputln();
350         }
351     }
352
353     /**
354      * XMIExport generation for the class attribute template.
355      * @param attribute The class attribute.
356      * @param variable_name The variable name.
357      */

358     public void classAttributeTemplate(Attribute attribute, String JavaDoc variable_name) {
359         _logger.log(Level.FINE, "XMIExport classAttributeTemplate(" + attribute.getName() + ")");
360         try {
361             String JavaDoc attrName = JMIProvider.jmiFormat1(attribute.getContainer().getName()) + "." + XMIIOHelper.format1FirstMin(attribute.getName());
362             String JavaDoc tag1 = "<" + attrName + ">";
363             String JavaDoc tag2 = "</" + attrName + ">";
364             String JavaDoc readAttribute = variable_name + "." + JMIProvider.jmiAccessorName(attribute) + "()";
365             boolean voidType = false;
366             String JavaDoc content = "outputln(\"" + tag1 + "\" + " + readAttribute + " + \"" + tag2 + "\");";
367             Classifier type = attribute.getType();
368             if (type instanceof EnumerationType) {
369                 EnumerationType enumType = (EnumerationType) type;
370                 String JavaDoc condition = "if (" + readAttribute + "!= null)";
371                 readAttribute = "enumeration" + JMIProvider.jmiFormat1(enumType.getName()) + "ToString(" + readAttribute + ")";
372                 voidType = true;
373                 content = condition + "\n" + TABULATION + "outputln(\"" + tag1 + "\" + " + readAttribute + " + \"" + tag2 + "\");";
374             } else if (type instanceof MofClass) {
375                     MofClass class_ = (MofClass) type;
376                     content = "if (" + readAttribute + " != null) {\n";
377                     readAttribute = "write" + JMIProvider.jmiFormat1(class_.getName()) + "(" + readAttribute + ")";
378                     voidType = true;
379                     content = content + "outputln(\"" + tag1 + "\");\n" + readAttribute + ";\n" + "outputln(\"" + tag2 + "\");" + "\n}";
380             }
381
382             if (attribute.getMultiplicity().getUpper() == -1) {
383                 outputln("for (java.util.Iterator it = " + readAttribute + ".iterator() ; it.hasNext() ;) {");
384                 outputln("output(\"" + tag1 + "\" + it.next());");
385                 outputln("if (it.hasNext()) output(\" \");");
386                 outputln("outputln(\"" + tag2 + "\");");
387                 outputln("}");
388             } else {
389                 if (!voidType && !(attribute.getType() instanceof PrimitiveType))
390                     outputln("if (" + readAttribute + " != null)");
391                 else if (attribute.getType() instanceof PrimitiveType)
392                     outputln("if (" + variable_name + ".refGetValue(\"" + XMIIOHelper.format1FirstMin(attribute.getName()) + "\") != null)");
393                 outputln(content);
394             }
395         } catch (NullPointerException JavaDoc notSet) {
396             // Nothing to do
397
}
398     }
399
400     /**
401      * XMIExport generation for the class reference template.
402      * @param reference The class reference.
403      * @param variable_name The variable name.
404      */

405     public void classReferenceTemplate(Reference reference, String JavaDoc variable_name) {
406         _logger.log(Level.FINE, "XMIExport classReferenceTemplate(" + reference.getName() + ")");
407         try {
408             String JavaDoc refName = JMIProvider.jmiFormat1(reference.getContainer().getName()) + "." + JMIProvider.jmiFormat1(reference.getName());
409             String JavaDoc refList = XMIIOHelper.format2Idl2JavaConflict(reference.getName());
410             String JavaDoc refType = JMIProvider.jmiClassifierQualifiedName(reference.getType());
411             String JavaDoc refTypeXmi = JMIProvider.jmiFormat1(reference.getType().getName());
412             if (reference.getMultiplicity().getUpper() == -1) {
413                 outputln("java.util.Collection " + refList + " = " + variable_name + "." + JMIProvider.jmiAccessorName(reference) + "();");
414                 outputln("if (" + refList + ".size() > 0) {");
415                 outputln("outputln(\"<" + refName + ">\");");
416                 outputln("java.util.Iterator it = " + refList + ".iterator();");
417                 outputln("while (it.hasNext())");
418                 outputln(" outputln(\"<" + refTypeXmi + " xmi.idref=\\\"\" + getXMIId(it.next()) + \"\\\" />\");");
419                 outputln("outputln(\"</" + refName + ">\");");
420                 outputln("}");
421             } else {
422                 if (reference.getMultiplicity().getLower() == 0)
423                     outputln("try {");
424                 outputln(refType + " " + refList + " = " + variable_name + "." + JMIProvider.jmiAccessorName(reference) + "();");
425                 outputln("if (" + refList + " != null) {");
426                 outputln("outputln(\"<" + refName + ">\");");
427                 outputln("outputln(\"<" + refTypeXmi + " xmi.idref=\\\"\" + getXMIId(" + refList + ") + \"\\\" />\");");
428                 outputln("outputln(\"</" + refName + ">\");");
429                 outputln("}");
430                 if (reference.getMultiplicity().getLower() == 0)
431                     outputln("} catch (java.lang.NullPointerException notSet) { /* Nothing to do */ }");
432             }
433         } catch (NullPointerException JavaDoc notSet) {
434             // Nothing to do
435
}
436     }
437
438     /**
439      * XMIExport generation for the root package.
440      * @param package_ The package.
441      * @param classes The classes of the package.
442      */

443     public void rootPackage(MofPackage package_, MofClass classes[]) {
444         _logger.log(Level.FINE, "XMIExport rootPackage()");
445         outputln("public void rootPackageTemplate(Object _root_package) {");
446         outputln("_logger.log(org.objectweb.modfact.jmi.logging.Level.FINE, \"rootPackageTemplate(\" + _root_package + \")\");");
447         String JavaDoc packageName = JMIProvider.jmiFormat1(package_.getName());
448         String JavaDoc model_package_type = JMIProvider.jmiPackageQualifiedName(package_) + "Package";
449         outputln(model_package_type + " _model_package = (" + model_package_type + ")_root_package;");
450         for (int i = 0; i < classes.length; i++) {
451             MofClass cls = (MofClass) classes[i];
452             if (!cls.isAbstract())
453                 outputln(XMIIOHelper.format1FirstMin(cls.getName()) + "Template(_model_package);");
454         }
455         outputln("}");
456         outputln();
457     }
458
459 }
460
Popular Tags