KickJava   Java API By Example, From Geeks To Geeks.

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


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 GenerationXMIImportAssociations extends AbstractXMIIOGenerator {
33
34     /** The logger. */
35     protected ModFactLogger _logger = ModFactLogger.getLogger("fr.lip6.src.mdafactory.generation.xmiimport.GenerationXMIImport");
36
37     /**
38      * Default Constructor.
39      */

40     public GenerationXMIImportAssociations() {
41         super();
42         ModFactLogger logger = ModFactLogger.getLogger("fr.lip6.src.mdafactory.generation.xmiimport.GenerationXMIImportApplication");
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(org.omg.mof.Model.Package package_, String JavaDoc class_name_)
51         throws
52             org.omg.mof.Reflective.MofError,
53             org.omg.CORBA.TypeCodePackage.BadKind JavaDoc,
54             org.omg.CORBA.TypeCodePackage.Bounds JavaDoc,
55             java.io.FileNotFoundException JavaDoc {
56         _logger.log(Level.FINE, "XMIImport beginning of the generation");
57         _logger.log(Level.INFO, class_name_ + " generation ...");
58         packageModuleTemplate(package_, class_name_);
59         flushFile();
60     }
61
62     /**
63      * XMIImport generation for constructor.
64      * @param class_name The class name.
65      * @param package_name The package name.
66      */

67     public void constructor(String JavaDoc class_name, String JavaDoc package_name) {
68         _logger.log(Level.FINE, "XMIImport constructor(" + class_name + ")");
69         outputln("/** The logger. */");
70         outputln("org.objectweb.modfact.corba.logging.ModFactLogger _logger;");
71         outputln();
72         outputln("/** The XMIImportClasses class. */");
73         outputln(package_name + "XMIImportClasses _xmi_import_classes;");
74         outputln();
75         outputln("/** Hashtables for types (Key / Model) */");
76         outputln("private java.util.Hashtable _types;");
77         outputln();
78         outputln("/**");
79         outputln(" * Default " + class_name + " Constructor");
80         outputln(" * @param logger The logger.");
81         outputln(" * @param types The defined types.");
82         outputln(" */");
83         outputln("public " + class_name + "(org.objectweb.modfact.corba.logging.ModFactLogger logger, java.util.Hashtable types) {");
84         outputln("_logger = logger;");
85         outputln("_types = types;");
86         outputln("}");
87         outputln();
88         outputln("public void setXMIImportClasses (" + package_name + "XMIImportClasses xmi_import_classes) {");
89         outputln("_xmi_import_classes = xmi_import_classes;");
90         outputln("}");
91         outputln();
92     }
93
94     /**
95      * Package processing.
96      * @param package_ The package to process.
97      * @param class_name_ The class name of the generated class.
98      */

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

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

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

179     public void initiateMethodAssociations(String JavaDoc package_name_, org.omg.mof.Model.Association associations_[]) throws MofError {
180         for (int i = 0; i < associations_.length; i++) {
181             org.omg.mof.Model.Association asso = (org.omg.mof.Model.Association) associations_[i];
182             String JavaDoc javaName;
183             try {
184                 javaName = idlHelper.format2(javaHelper.javaQualifiedName(asso));
185             } catch (NotSet notSet) {
186                 javaName = asso.name();
187             }
188             String JavaDoc method = idlHelper.format2(asso.name()) + "_ref()";
189             try {
190                 org.omg.mof.Model.ModelElement assoTmp = asso;
191                 while (!assoTmp.container().name().equals(package_name_)) {
192                     method = idlHelper.format2(assoTmp.container().name()) + "_ref()." + method;
193                     assoTmp = assoTmp.container();
194                 }
195             } catch (org.omg.CORBA.MARSHAL JavaDoc marshal) { /* !!! The marshal exception contains the NotSet Exception */
196                 if (!marshal.getMessage().equals("IDL:mof.omg.org/Reflective/NotSet:1.0"))
197                     throw marshal;
198             } catch (org.omg.mof.Reflective.NotSet notSet) {
199                 // Nothing to do
200
}
201             outputln("_" + javaName + "_class = _model_package." + method + ";");
202         }
203     }
204
205     /**
206      * XMIImport generation for the association template.
207      * @param associations_ The list of association of the package.
208      */

209     public void associationTemplate(org.omg.mof.Model.Association associations_[]) throws org.omg.mof.Reflective.MofError {
210         for (int i = 0; i < associations_.length; i++) {
211             org.omg.mof.Model.Association asso = (org.omg.mof.Model.Association) associations_[i];
212             String JavaDoc methodName = idlHelper.format1FirstMin(asso.name()) + "Template";
213             String JavaDoc param = idlHelper.format2(asso.name()) + "_element";
214             org.omg.mof.Model.AssociationEnd[] ends = mofHelper.associationEndsOfAssociation(asso);
215             outputln("public void " + methodName + "(org.w3c.dom.Element " + param + ") throws org.omg.mof.Reflective.MofError {");
216             for (int j = 0; j < ends.length; j++) {
217                 try {
218                     outputln(
219                         javaHelper.javaQualifiedName(ends[j].type())
220                             + " "
221                             + javaHelper.testJavaConflict(idlHelper.format1FirstMin(ends[j].name()))
222                             + " = null;");
223                 } catch (NotSet notSet) {
224                     // Nothing to do.
225
}
226             }
227             outputln("org.w3c.dom.NodeList _child_nodes = " + param + ".getChildNodes();");
228             outputln("for (int i=0 ; i<_child_nodes.getLength() ; i++) {");
229             outputln("org.w3c.dom.Element _current = (org.w3c.dom.Element) _child_nodes.item(i);");
230             for (int j = 0; j < ends.length; j++) {
231                 String JavaDoc ends_variable = javaHelper.testJavaConflict(idlHelper.format1FirstMin(ends[j].name()));
232                 String JavaDoc type_variable;
233                 try {
234                     type_variable = javaHelper.javaQualifiedName(ends[j].type());
235                 } catch (NotSet notSet) {
236                     type_variable = ends[j].type().name();
237                 }
238                 String JavaDoc methodTemplate = "_xmi_import_classes." + idlHelper.format1FirstMin(ends[j].type().name()) + "Template(_current)";
239                 output("if (_current.getNodeName().endsWith(\"" + idlHelper.format1(ends[j].type().name()) + "\")");
240                 try {
241                     org.omg.mof.Model.Class[] subclasses = mofHelper.subClassesOfClass(ends[j].type());
242                     for (int k = 0; k < subclasses.length; k++) {
243                         output(" || _current.getNodeName().endsWith(\"" + idlHelper.format1(subclasses[k].name()) + "\")");
244                         if (k % 2 == 0 && k < subclasses.length - 1) {
245                             outputln();
246                             output(TABULATION);
247                         }
248                     }
249                 } catch (org.omg.mof.Reflective.NotSet notSet) {
250                     // Nothing to do.
251
}
252                 outputln(") {");
253
254                 if (ends[j].type().is_abstract()) {
255                     outputln(
256                         "org.objectweb.modfact.corba.xmiio.mofobject.MOFObject "
257                             + ends_variable
258                             + "Obj = "
259                             + methodTemplate
260                             + ".getFirstObject();");
261                 } else {
262                     outputln(
263                         "org.objectweb.modfact.corba.xmiio.mofobject.MOFObject " + ends_variable + "Obj = " + methodTemplate + ";");
264                 }
265                 outputln("if (" + ends_variable + "Obj.isAnObject())");
266                 outputln(TABULATION + ends_variable + " = (" + type_variable + ")" + ends_variable + "Obj.get();");
267                 outputln("else");
268                 outputln(TABULATION + ends_variable + " = (" + type_variable + ") _types.get(" + ends_variable + "Obj.get());");
269                 outputln("}");
270             }
271             outputln("}");
272             try {
273                 output("_" + idlHelper.format2(javaHelper.javaQualifiedName(asso)) + "_class.add(");
274             } catch (NotSet notSet) {
275                 // Nothing to do.
276
}
277             for (int j = 0; j < ends.length; j++) {
278                 output(javaHelper.testJavaConflict(idlHelper.format1FirstMin(ends[j].name())));
279                 if (j < ends.length - 1)
280                     output(", ");
281             }
282             outputln(");");
283             outputln("}");
284             outputln();
285         }
286         outputln();
287     }
288
289 }
290
Popular Tags