KickJava   Java API By Example, From Geeks To Geeks.

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


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
26
27 /**
28  * The XMI Import generator.
29  * This class allows the generation of the XMI importer (into Java).
30  */

31 public class GenerationXMIImport extends AbstractXMIIOGenerator {
32
33     /** The logger. */
34     ModFactLogger _logger = ModFactLogger.getLogger("fr.lip6.src.mdafactory.generation.xmiimport.GenerationXMIImport");
35
36     /**
37      * Default Constructor.
38      */

39     public GenerationXMIImport() {
40         super();
41         ModFactLogger logger = ModFactLogger.getLogger("fr.lip6.src.mdafactory.generation.xmiimport.GenerationXMIImportApplication");
42     }
43
44     /**
45      * XMIImport generation.
46      * @param package_ The root package.
47      */

48     public void generateXMIImport(org.omg.mof.Model.Package package_) throws org.omg.mof.Reflective.MofError, org.omg.CORBA.TypeCodePackage.BadKind JavaDoc, org.omg.CORBA.TypeCodePackage.Bounds JavaDoc, java.io.IOException JavaDoc {
49         _logger.log(Level.FINE, "XMIImport beginning of the generation");
50         String JavaDoc className = getClassName(package_.name());
51         _logger.log(Level.INFO, className + " generation ...");
52         packageModuleTemplate(package_);
53         flushFile();
54     }
55
56     /**
57      * Get the class name.
58      * @param package_name_ The root package name.
59      * @return The class name.
60      */

61     protected String JavaDoc getClassName(String JavaDoc package_name_) {
62         return super.getClassName(package_name_, "XMIImport");
63     }
64
65     /**
66      * XMIImport generation for constructor.
67      * @param class_name The class name.
68      */

69     public void constructor(String JavaDoc class_name, String JavaDoc package_name) {
70         _logger.log(Level.FINE, "XMIImport constructor(" + class_name + ")");
71         outputln("/** The XMIImportAssociations class. */");
72         outputln(package_name + "XMIImportAssociations _xmi_import_associations;");
73         outputln();
74         outputln("/** The XMIImportAttributes class. */");
75         outputln(package_name + "XMIImportAttributes _xmi_import_attributes;");
76         outputln();
77         outputln("/** The XMIImportClasses class. */");
78         outputln(package_name + "XMIImportClasses _xmi_import_classes;");
79         outputln();
80         outputln("/** The logger. */");
81         outputln("org.objectweb.modfact.corba.logging.ModFactLogger _logger;");
82         outputln();
83         outputln("/**");
84         outputln(" * Default " + class_name + " Constructor");
85         outputln(" */");
86         outputln("public " + class_name + "() {");
87         outputln("this(org.objectweb.modfact.corba.logging.ModFactLogger.getLogger(\"" + package_name + "XMIImport\"));");
88         outputln("}");
89         outputln();
90         outputln("/**");
91         outputln(" * " + class_name + " Constructor with quiet flag");
92         outputln(" */");
93         outputln("public " + class_name + "(boolean quiet) {");
94         outputln("this(org.objectweb.modfact.corba.logging.ModFactLogger.getLogger(\"" + package_name + "XMIImport\"));");
95         outputln("if (quiet)");
96         outputln(TABULATION + "_logger = org.objectweb.modfact.corba.logging.ModFactLogger.getLogger(\"" + package_name + "XMIImport\");");
97         outputln("}");
98         outputln();
99         outputln("/**");
100         outputln(" * " + class_name + " Constructor");
101         outputln(" * @param logger The logger.");
102         outputln(" */");
103         outputln("public " + class_name + "(org.objectweb.modfact.corba.logging.ModFactLogger logger) {");
104         outputln("_logger = logger;");
105         outputln("_xmi_import_associations = new " + package_name + "XMIImportAssociations (logger, _types);");
106         outputln("_xmi_import_attributes = new " + package_name + "XMIImportAttributes (logger, _types);");
107         outputln("_xmi_import_classes = new " + package_name + "XMIImportClasses (logger, _types);");
108         outputln("_xmi_import_classes.setXMIImportAttributes(_xmi_import_attributes);");
109         outputln("_xmi_import_associations.setXMIImportClasses(_xmi_import_classes);");
110         outputln("_xmi_import_attributes.setXMIImportClasses(_xmi_import_classes);");
111         outputln("}");
112         outputln();
113     }
114
115     /**
116      * Package processing.
117      * @param package_ The package to process.
118      */

119     public void packageModuleTemplate(org.omg.mof.Model.Package package_) throws org.omg.mof.Reflective.MofError, org.omg.CORBA.TypeCodePackage.BadKind JavaDoc, org.omg.CORBA.TypeCodePackage.Bounds JavaDoc {
120         _logger.log(Level.FINE, "XMIImport packageModuleTemplate(" + package_.name() + ")");
121         if (package_.visibility().equals(org.omg.mof.Model.VisibilityKind.public_vis)) {
122             org.omg.mof.Model.Association associations[] = mofHelper.allAssociationsOfPackage(package_);
123             org.omg.mof.Model.Class classes_ordered[] = mofHelper.allClassesOfPackageOrderedByInheritance(package_);
124             _logger.log(Level.FINE, "XMIImport produce");
125
126             String JavaDoc className = getClassName(package_.name());
127             outputln("package " + javaHelper.javaPackage(package_) + ".xmi;");
128             outputln();
129             annotationTemplate(package_.annotation());
130             outputln("public class " + className + " extends org.objectweb.modfact.corba.xmiio.importer.XMIImportParser {");
131             classAttributes(package_);
132             constructor(className, package_.name());
133             associationAttributes(package_.name());
134             initiateMethod(package_, classes_ordered);
135             rootPackage(package_.name(), classes_ordered, associations);
136             completeImportMethod(associations);
137             outputln("} // end of class " + className);
138             outputln();
139         }
140     }
141
142     /**
143     * XMIImport generation for the class attributes.
144     * @param package_ The package.
145      * @param associations_ The associations of the package.
146     */

147    public void classAttributes(org.omg.mof.Model.Package package_) throws org.omg.mof.Reflective.MofError {
148        String JavaDoc package_name = package_.name();
149        _logger.log(Level.FINE, "XMIImport classAttributes(" + package_name + ")");
150        outputln("// Factories Declaration");
151        String JavaDoc model_package_type;
152        if (mappingValid) {
153            model_package_type = javaHelper.javaPackage(package_) + "._" + idlHelper.format1(package_name) + "Package";
154        } else {
155            model_package_type = javaHelper.javaPackage(package_) + "." + idlHelper.format1(package_name) + "Package";
156        }
157        outputln("private " + model_package_type + " _" + idlHelper.format2(package_name) + "_package = null;");
158        outputln();
159    }
160    
161     /**
162      * XMIImport generation for the association attributes.
163      * @param package_name_ The package name.
164      * @param associations_ The associations of the package.
165      */

166     public void associationAttributes(String JavaDoc package_name_) throws org.omg.mof.Reflective.MofError {
167         _logger.log(Level.FINE, "XMIImport associationAttributes(" + package_name_ + ")");
168         outputln("// Hashtables for associations (Model / Key Vector)");
169         outputln();
170         outputln("/** Hashtables for types (Key / Model) */");
171         outputln("private java.util.Hashtable _types = new java.util.Hashtable();");
172         outputln();
173     }
174
175     /**
176      * XMIImport generation for the initiate method.
177      * @param package_ The package.
178      * @param classes_ The classes of the package.
179      * @param associations_ The associations of the package.
180      */

181     public void initiateMethod(org.omg.mof.Model.Package package_, org.omg.mof.Model.Class classes_ []) throws org.omg.mof.Reflective.MofError {
182         String JavaDoc package_name = package_.name();
183         _logger.log(Level.FINE, "XMIImport initiateMethod(" + package_name + ", " + classes_.length + ")");
184         String JavaDoc model_package_type;
185         if (mappingValid) {
186             model_package_type = javaHelper.javaPackage(package_) + "._" + idlHelper.format1(package_name) + "Package";
187         } else {
188             model_package_type = javaHelper.javaPackage(package_) + "." + idlHelper.format1(package_name) + "Package";
189         }
190         outputln("public void initiate(" + model_package_type + " _model_package, org.omg.CORBA.ORB orb)");
191         outputln(" throws org.omg.mof.Reflective.MofError {");
192         outputln("super.setORB(orb);");
193         outputln("// Factories creation");
194         outputln("_logger.log(org.objectweb.modfact.corba.logging.Level.FINE, \"initiate(\" + _model_package + \")\");");
195         outputln("this._" + idlHelper.format2(package_name) + "_package = _model_package;");
196         outputln("_xmi_import_classes.initiate(_model_package);");
197         outputln("_xmi_import_associations.initiate(_model_package);");
198         outputln("_xmi_import_attributes.initiate(_model_package);");
199         outputln("}");
200         outputln();
201     }
202
203     /**
204      * XMIImport generation for the root package.
205      * @param package_name_ The package name.
206      * @param classes_ The classes of the package.
207      * @param associations_ The associations of the package.
208      */

209     public void rootPackage(String JavaDoc package_name_, org.omg.mof.Model.Class classes_ [], org.omg.mof.Model.Association associations_ []) throws org.omg.mof.Reflective.MofError {
210         _logger.log(Level.FINE, "XMIImport rootTemplate(" + package_name_ + ", " + classes_.length + ", " + associations_.length + ")");
211         annotationTemplate(" The root package processing");
212         outputln("public org.omg.CORBA.portable.IDLEntity rootPackageTemplate(org.w3c.dom.Element _root_package_element)");
213         outputln(" throws org.omg.mof.Reflective.MofError {");
214         outputln("_logger.log(org.objectweb.modfact.corba.logging.Level.FINE, \"rootPackageTemplate(\" + _root_package_element + \")\");");
215         outputln("java.lang.String prefix_element = new java.lang.String();");
216         outputln("java.lang.String namespace = _root_package_element.getNamespaceURI();");
217         outputln("if (namespace != null && namespace.length() > 0)");
218         outputln(" prefix_element = _root_package_element.getPrefix() + \":\";");
219         outputln("org.w3c.dom.Element _current = _root_package_element;");
220         outputln("String _current_name = _root_package_element.getNodeName().trim();");
221
222         for (int i = 0; i < classes_.length; i++) {
223             org.omg.mof.Model.Class cls = (org.omg.mof.Model.Class) classes_[i];
224             outputln("if (_current_name.endsWith(prefix_element + \"" + idlHelper.format1(cls.name()) + "\")) {");
225             outputln("_xmi_import_classes." + idlHelper.format1FirstMin(cls.name()) + "Template(_current);");
226             if (i == classes_.length - 1 && associations_.length == 0)
227                 outputln("}");
228             else
229                 output("} else ");
230         }
231         for (int i = 0; i < associations_.length; i++) {
232             org.omg.mof.Model.Association asso = (org.omg.mof.Model.Association) associations_[i];
233             outputln("if (_current_name.endsWith(prefix_element + \"" + asso.name() + "\")) {");
234             outputln("_xmi_import_associations." + idlHelper.format1FirstMin(asso.name()) + "Template(_current);");
235             if (i == associations_.length - 1)
236                 outputln("}");
237             else
238                 output("} else ");
239         }
240         outputln("return (org.omg.CORBA.portable.IDLEntity) _" + idlHelper.format2(package_name_) + "_package;");
241         outputln("}");
242         outputln();
243     }
244
245     /**
246      * XMIImport generation for the completeImport() method.
247      * @param associations_ The associations of the package.
248      */

249     public void completeImportMethod(org.omg.mof.Model.Association associations_ []) throws org.omg.mof.Reflective.MofError {
250         outputln("public void completeImport () throws org.omg.mof.Reflective.MofError {");
251         for (int i = 0; i < associations_.length; i++) {
252             org.omg.mof.Model.Association asso = (org.omg.mof.Model.Association) associations_[i];
253             try {
254                 org.omg.mof.Model.Reference[] refs = mofHelper.referencesOfAssociation(asso);
255                 for (int j = 0; j < refs.length; j++) {
256                     org.omg.mof.Model.AssociationEnd assoEnd = refs[j].referenced_end();
257                     if (assoEnd.aggregation().value() == org.omg.mof.Model.AggregationKind._none && assoEnd.is_changeable())
258                         outputln("_xmi_import_attributes.put" + idlHelper.format1(refs[j].container().name()) + idlHelper.format1(refs[j].name()) + "();");
259                 }
260             } catch (org.omg.mof.Reflective.NotSet notSet) {
261                 // Nothing to do
262
}
263         }
264         outputln("}");
265         outputln();
266     }
267
268 }
269
Popular Tags