KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

57     public void generateXMIImportAttributes(org.omg.mof.Model.Package package_, String JavaDoc class_name_)
58         throws
59             org.omg.mof.Reflective.MofError,
60             org.omg.CORBA.TypeCodePackage.BadKind JavaDoc,
61             org.omg.CORBA.TypeCodePackage.Bounds JavaDoc,
62             java.io.FileNotFoundException JavaDoc {
63         _logger.log(Level.FINE, "XMIImport beginning of the generation");
64         _logger.log(Level.INFO, class_name_ + " generation ...");
65         packageModuleTemplate(package_, class_name_);
66         flushFile();
67     }
68
69     /**
70      * XMIImport generation for constructor.
71      * @param class_name The class name.
72      * @param package_name The package name.
73      */

74     public void constructor(String JavaDoc class_name, String JavaDoc package_name) {
75         _logger.log(Level.FINE, "XMIImport constructor(" + class_name + ")");
76         outputln("/** The logger. */");
77         outputln("org.objectweb.modfact.corba.logging.ModFactLogger _logger;");
78         outputln("/** The XMIImportClasses class. */");
79         outputln(package_name + "XMIImportClasses _xmi_import_classes;");
80         outputln();
81         outputln("/** Hashtables for types (Key / Model) */");
82         outputln("private java.util.Hashtable _types;");
83         outputln();
84         outputln("/** The XMI Import helper. */");
85         outputln("org.objectweb.modfact.corba.helper.XMIImportCommon _helper;");
86         outputln();
87         outputln("/**");
88         outputln(" * Default " + class_name + " Constructor");
89         outputln(" * @param logger The logger.");
90         outputln(" */");
91         outputln("public " + class_name + "(org.objectweb.modfact.corba.logging.ModFactLogger logger, java.util.Hashtable types) {");
92         outputln("_logger = logger;");
93         outputln("_types = types;");
94         outputln("_helper = new org.objectweb.modfact.corba.provider.XMIImportProvider();");
95         outputln("}");
96         outputln();
97         outputln("public void setXMIImportClasses (" + package_name + "XMIImportClasses xmi_import_classes) {");
98         outputln("_xmi_import_classes = xmi_import_classes;");
99         outputln("}");
100         outputln();
101     }
102
103     /**
104      * Package processing.
105      * @param package_ The package to process.
106      * @param class_name_ The class name of the generated class.
107      */

108     public void packageModuleTemplate(org.omg.mof.Model.Package package_, String JavaDoc class_name_)
109         throws org.omg.mof.Reflective.MofError, org.omg.CORBA.TypeCodePackage.BadKind JavaDoc, org.omg.CORBA.TypeCodePackage.Bounds JavaDoc {
110         _logger.log(Level.FINE, "XMIImport packageModuleTemplate(" + package_.name() + ")");
111         if (package_.visibility().equals(org.omg.mof.Model.VisibilityKind.public_vis)) {
112             org.omg.mof.Model.Association associations[] = mofHelper.allAssociationsOfPackage(package_);
113             org.omg.mof.Model.Class classes_ordered[] = mofHelper.allClassesOfPackageOrderedByInheritance(package_);
114             _logger.log(Level.FINE, "XMIImport produce");
115
116             outputln("package " + javaHelper.javaPackage(package_) + ".xmi;");
117             outputln();
118             annotationTemplate(package_.annotation());
119             outputln("public class " + class_name_ + " extends org.objectweb.modfact.corba.xmiio.importer.XMIImport {");
120             constructor(class_name_, package_.name());
121             classAttributes(package_);
122             associationAttributes(package_.name(), associations);
123             initiateMethod(package_);
124             readingAttributes(package_.name(), classes_ordered);
125             putAssociationsValues(package_.name(), associations);
126             outputln("} // end of class " + class_name_);
127             outputln();
128         }
129     }
130
131     /**
132      * XMIImport generation for the class attributes.
133      * @param package_ The package.
134      */

135     public void classAttributes(org.omg.mof.Model.Package package_) throws org.omg.mof.Reflective.MofError {
136         String JavaDoc package_name = package_.name();
137         _logger.log(Level.FINE, "XMIImport classAttributes(" + package_name + ")");
138         outputln("org.objectweb.modfact.corba.helper.IDLCommon idlHelper = new org.objectweb.modfact.corba.provider.IDLProvider();");
139         outputln("// Factories Declaration");
140         String JavaDoc model_package_type;
141         if (mappingValid) {
142             model_package_type = javaHelper.javaPackage(package_) + "._" + idlHelper.format1(package_name) + "Package";
143         } else {
144             model_package_type = javaHelper.javaPackage(package_) + "." + idlHelper.format1(package_name) + "Package";
145         }
146         outputln("private " + model_package_type + " _" + idlHelper.format2(package_name) + "_package = null;");
147         outputln();
148     }
149
150     /**
151      * XMIImport generation for the association attributes.
152      * @param package_name_ The package name.
153      * @param associations_ The associations of the package.
154      */

155     public void associationAttributes(String JavaDoc package_name_, org.omg.mof.Model.Association associations_[])
156         throws org.omg.mof.Reflective.MofError {
157         _logger.log(Level.FINE, "XMIImport associationAttributes(" + package_name_ + ", " + associations_.length + ")");
158         outputln("// Hashtables for associations (Model / Key Vector)");
159         for (int i = 0; i < associations_.length; i++) {
160             org.omg.mof.Model.Association asso = (org.omg.mof.Model.Association) associations_[i];
161             try {
162                 org.omg.mof.Model.Reference[] refs = xmiImpHelper.getReferences(asso);
163                 for (int j = 0; j < refs.length; j++) {
164                     outputln(
165                         "private java.util.Hashtable _"
166                             + idlHelper.format2(asso.name())
167                             + "_"
168                             + idlHelper.format2(refs[j].name())
169                             + " = new java.util.Hashtable();");
170                 }
171             } catch (org.omg.CORBA.MARSHAL JavaDoc marshal) {
172                 if (!marshal.getMessage().equals("IDL:mof.omg.org/Reflective/NotSet:1.0"))
173                     throw marshal;
174             } catch (org.omg.mof.Reflective.NotSet notSet) {
175                 // Nothing to do
176
}
177         }
178         outputln();
179     }
180
181     /**
182      * XMIImport generation for the initiate method.
183      * @param package_ The package.
184      */

185     public void initiateMethod(org.omg.mof.Model.Package package_) throws org.omg.mof.Reflective.MofError {
186         String JavaDoc package_name = package_.name();
187         _logger.log(Level.FINE, "XMIImport initiateMethod(" + package_name + ")");
188         String JavaDoc model_package_type;
189         if (mappingValid) {
190             model_package_type = javaHelper.javaPackage(package_) + "._" + idlHelper.format1(package_name) + "Package";
191         } else {
192             model_package_type = javaHelper.javaPackage(package_) + "." + idlHelper.format1(package_name) + "Package";
193         }
194         outputln("public void initiate(" + model_package_type + " _model_package)");
195         outputln(" throws org.omg.mof.Reflective.MofError {");
196         outputln("_logger.log(org.objectweb.modfact.corba.logging.Level.FINE, \"initiate(\" + _model_package + \")\");");
197         outputln("// Factories creation");
198         outputln("this._" + idlHelper.format2(package_name) + "_package = _model_package;");
199         outputln("}");
200         outputln();
201     }
202
203     /**
204      * XMIImport generation for the attributes reading methods.
205      * @param package_name_ The package name.
206      * @param classes_ The classes of the package.
207      */

208     public void readingAttributes(String JavaDoc package_name_, org.omg.mof.Model.Class classes_[])
209         throws org.omg.mof.Reflective.MofError, org.omg.CORBA.TypeCodePackage.BadKind JavaDoc, org.omg.CORBA.TypeCodePackage.Bounds JavaDoc {
210         _logger.log(Level.FINE, "XMIImport readingAttributes(" + package_name_ + ", " + classes_.length + ")");
211         outputln("// ==================================================================");
212         outputln("// readXXXYYY with XXX an entity and YYY its property");
213         outputln("// ==================================================================");
214         outputln();
215         // Classes
216
for (int i = 0; i < classes_.length; i++) {
217             org.omg.mof.Model.Class cls = (org.omg.mof.Model.Class) classes_[i];
218             String JavaDoc clsName = cls.name();
219             org.omg.mof.Model.ModelElement[] contents = cls.contents();
220             for (int j = 0; j < contents.length; j++) {
221                 if (contents[j]._is_a(org.omg.mof.Model.MofAttributeHelper.id())) {
222                     org.omg.mof.Model.MofAttribute attr = org.omg.mof.Model.MofAttributeHelper.narrow(contents[j]);
223                     org.omg.mof.Model.Classifier type = attr.type();
224                     String JavaDoc attrName = attr.name();
225                     if (attr.is_changeable()) {
226                         if (type._is_a(org.omg.mof.Model.DataTypeHelper.id())) {
227                             try {
228                                 org.omg.mof.Model.DataType data_type = org.omg.mof.Model.DataTypeHelper.narrow(type);
229                                 org.omg.CORBA.TypeCode JavaDoc type_code = data_type.type_code();
230                                 int type_code_value = type_code.kind().value();
231                                 if (type_code_value == org.omg.CORBA.TCKind._tk_TypeCode) {
232                                     readClassTypeCodeAttributeMethod(package_name_, cls, attrName);
233                                 } else if (type_code_value == org.omg.CORBA.TCKind._tk_enum) {
234                                     java.util.List JavaDoc associatedTags = new java.util.ArrayList JavaDoc();
235                                     org.omg.mof.Model.ModelElement clsContents[] = type.container().contents();
236                                     for (int c = 0; c < clsContents.length; c++) {
237                                         if (clsContents[c]._is_a(org.omg.mof.Model.TagHelper.id())) {
238                                             org.omg.mof.Model.Tag tag = org.omg.mof.Model.TagHelper.narrow(clsContents[c]);
239                                             org.omg.mof.Model.ModelElement[] elements = tag.elements();
240                                             for (int e = 0; e < elements.length; e++) {
241                                                 if (elements[e]._is_equivalent(type))
242                                                     associatedTags.add(tag);
243                                             }
244                                         }
245                                     }
246                                     readClassEnumerationTypeAttributeMethod(package_name_, cls, attr, type_code, associatedTags);
247                                 } else if (type_code_value == org.omg.CORBA.TCKind._tk_struct) {
248                                     readClassStructureTypeAttributeMethod(package_name_, cls, attr, type_code);
249                                 } else if (type_code_value == org.omg.CORBA.TCKind._tk_alias) {
250                                     readClassAliasTypeMethod(package_name_, cls, attr, type_code.content_type());
251                                 } else if (type_code_value == org.omg.CORBA.TCKind._tk_sequence) {
252                                     _logger.log(
253                                         Level.INFO,
254                                         "SequenceType " + idlHelper.format1(clsName) + "." + idlHelper.format1(attrName) + " ignored");
255                                     //throw new org.omg.CORBA.NO_IMPLEMENT("CollectionType reading method not implemented", 1,
256
// org.omg.CORBA.CompletionStatus.COMPLETED_NO);
257
} else
258                                     readClassAttributeMethod(
259                                         package_name_,
260                                         cls,
261                                         org.omg.mof.Model.MofAttributeHelper.narrow(contents[j]),
262                                         type.name(),
263                                         type_code);
264                             } catch (org.omg.mof.Reflective.NotSet notSet) {
265                                 // Nothing to do
266
}
267                         } else {
268                             try {
269                                 readClassAttributeMethod(
270                                     package_name_,
271                                     cls,
272                                     org.omg.mof.Model.MofAttributeHelper.narrow(contents[j]),
273                                     type.name(),
274                                     null);
275                             } catch (NotSet notSet) {
276                                 // Nothing to do
277
}
278                         }
279                     }
280                 } else if (contents[j]._is_a(org.omg.mof.Model.ReferenceHelper.id())) {
281                     try {
282                         org.omg.mof.Model.Reference ref = org.omg.mof.Model.ReferenceHelper.narrow(contents[j]);
283                         org.omg.mof.Model.AssociationEnd assoEnd = ref.referenced_end();
284                         org.omg.mof.Model.AssociationEnd otherEnd = mofHelper.oppositeAssociationEnd(assoEnd);
285                         boolean none = (assoEnd.aggregation().value() == org.omg.mof.Model.AggregationKind._none);
286                         boolean composite = (assoEnd.aggregation().value() == org.omg.mof.Model.AggregationKind._composite);
287
288                         //if (composite) {
289
// org.omg.mof.Model.Class[] subClasses = mofHelper.subClassesOfClass(cls);
290
// System.out.println("composite -> " + cls.name() + "-" + assoEnd.name());
291
// readCompositeAssociationAttributeMethod(package_name_, cls, subClasses, assoEnd.name());
292
//} else
293
if (none) {
294                             org.omg.mof.Model.Association asso = org.omg.mof.Model.AssociationHelper.narrow(assoEnd.container());
295                             org.omg.mof.Model.Reference[] refs = xmiImpHelper.getReferences(asso);
296                             for (int r = 0; r < refs.length; r++) {
297                                 String JavaDoc hashtable = "_" + idlHelper.format2(asso.name()) + "_" + idlHelper.format2(refs[r].name());
298                                 try {
299                                     readAssociationAttributeMethod(package_name_, refs[r].container(), refs[r], hashtable);
300                                 } catch (org.omg.mof.Reflective.NotSet notSet) {
301                                     notSet.printStackTrace();
302                                 }
303                             }
304                         }
305                     } catch (org.omg.mof.Reflective.NotSet notSet) {
306                         // Nothing to do
307
}
308                 }
309             }
310         }
311     }
312
313     /**
314      * XMIImport generation for the class attribute reading method.
315      * @param package_name_ The package name.
316      * @param classes_name_ The class name.
317      * @param attribute_name_ The attribute name.
318      */

319     public void readClassAttributeMethod(
320         String JavaDoc package_name_,
321         org.omg.mof.Model.Class class_,
322         org.omg.mof.Model.MofAttribute attribute_,
323         String JavaDoc type_name_,
324         org.omg.CORBA.TypeCode JavaDoc type_)
325         throws BadKind JavaDoc, Bounds JavaDoc, MofError, NotSet {
326         _logger.log(
327             Level.FINE,
328             "XMIImport readClassAttributeMethod(" + package_name_ + ", " + class_.name() + ", " + attribute_.name() + ", " + type_ + ")");
329         String JavaDoc type_name = type_name_;
330         if (type_name_.startsWith("*"))
331             type_name = idlHelper.format1(type_name_.substring(1));
332         else
333             type_name = idlHelper.format1(type_name_);
334         String JavaDoc operation = "add_";
335         if (attribute_.multiplicity().upper == 1)
336             operation = "set_";
337         String JavaDoc attrName = attribute_.name();
338         String JavaDoc attrName4 = idlHelper.format1FirstMin(attrName);
339         outputln("/**");
340         outputln(" * Read the '" + idlHelper.format1(attrName) + "' attribute of the " + idlHelper.format1(class_.name()) + ".");
341         outputln(" * @param parent The parent of the current element in the XMI file.");
342         outputln(" * @param model The " + idlHelper.format1(class_.name()) + " to set.");
343         outputln(" * @param element The current element in the XMI file.");
344         outputln(" */");
345         output("public void read" + idlHelper.format1(class_.name()) + idlHelper.format1(attrName) + "(");
346         outputln("org.w3c.dom.Element parent, " + javaHelper.javaQualifiedName(class_) + " model, org.w3c.dom.Element element)");
347         outputln("throws org.omg.mof.Reflective.MofError {");
348         //if (_helper.isPrimitiveType(type_name_)) {
349
if (type_ != null && idlHelper.isBaseType(type_)) {
350             String JavaDoc method = typeCode2ReadingMethod(type_);
351             outputln("if (parent.hasAttribute(\"" + attrName4 + "\"))");
352             outputln(" model." + operation + idlHelper.format2(attrName)
353                     + "(" + method + "(parent.getAttribute(\""
354                     + attrName4 + "\")));");
355             outputln("else if (element != null && nodeNameCorrespondsTo(element, \"" + idlHelper.format1(attrName) + "\")) {");
356             outputln("if (element.getFirstChild() != null)");
357             outputln(" model." + operation + idlHelper.format2(attrName)
358                     + "(" + method + "(element.getFirstChild().getNodeValue().trim()));");
359             outputln("else");
360             outputln(" model." + operation + idlHelper.format2(attrName)
361                     + "(" + method + "(element.getAttribute(\"xmi.value\").trim()));");
362             outputln("}");
363         } else {
364             outputln(
365                 "if (element != null && element.getFirstChild() != null && nodeNameCorrespondsTo(element, \""
366                     + idlHelper.format1(attrName) + "\"))");
367             String JavaDoc paramType = javaHelper.javaQualifiedName(attribute_.type());
368             outputln(" model." + operation + idlHelper.format2(attrName)
369                     + "((" + paramType + ")_xmi_import_classes." + idlHelper.format1FirstMin(type_name)
370                     + "Template((org.w3c.dom.Element)element.getFirstChild()).get());");
371         }
372         outputln("}");
373         outputln();
374     }
375
376     /**
377      * XMIImport generation for the class attribute reading method (for a TypeCode attribute).
378      * @param package_name_ The package name.
379      * @param classes_name_ The class name.
380      * @param attribute_name_ The attribute name.
381      */

382     public void readClassTypeCodeAttributeMethod(String JavaDoc package_name_, org.omg.mof.Model.Class class_, String JavaDoc attribute_name_)
383         throws MofError, NotSet {
384         String JavaDoc class_name = class_.name();
385         _logger.log(
386             Level.FINE,
387             "XMIImport readClassTypeCodeAttributeMethod(" + package_name_ + ", " + class_name + ", " + attribute_name_ + ")");
388         outputln("/**");
389         outputln(" * Read the '" + idlHelper.format1(attribute_name_) + "' attribute of the " + idlHelper.format1(class_name) + ".");
390         outputln(" * @param parent The parent of the current element in the XMI file.");
391         outputln(" * @param model The " + idlHelper.format1(class_name) + " to set.");
392         outputln(" * @param element The current element in the XMI file.");
393         outputln(" */");
394         output("public void read" + idlHelper.format1(class_name) + idlHelper.format1(attribute_name_) + "(");
395         outputln("org.w3c.dom.Element parent, " + javaHelper.javaQualifiedName(class_) + " model, org.w3c.dom.Element element)");
396         outputln("throws org.omg.mof.Reflective.MofError {");
397         outputln("String repositoryID = \"IDL:\" + idlHelper.format1(model.name());");
398         outputln("if (element != null && nodeNameCorrespondsTo(element, \"" + idlHelper.format1(attribute_name_) + "\")) {");
399         outputln("org.w3c.dom.Element _corba_typecode = (org.w3c.dom.Element) element.getElementsByTagName(\"XMI.CorbaTypeCode\").item(0);");
400         outputln("org.w3c.dom.Element _element = (org.w3c.dom.Element) _corba_typecode.getChildNodes().item(0);");
401         outputln("model.set_" + idlHelper.format2(attribute_name_) + "(xml2TypeCode(_element, repositoryID));");
402         outputln("}");
403         outputln("}");
404         outputln();
405     }
406
407     /**
408      * XMIImport generation for the class attribute reading method (for an EnumarationType attribute).
409      * @param package_name_ The package name.
410      * @param classes_ The class.
411      * @param attribute_ The attribute.
412      * @param type_code_ The attribute type code.
413      * @param associated_tags_ The tags associated to the Data Type.
414      */

415     public void readClassEnumerationTypeAttributeMethod(
416         String JavaDoc package_name_,
417         org.omg.mof.Model.Class class_,
418         org.omg.mof.Model.MofAttribute attribute_,
419         org.omg.CORBA.TypeCode JavaDoc type_code_,
420         java.util.List JavaDoc associated_tags_)
421         throws org.omg.mof.Reflective.MofError, org.omg.CORBA.TypeCodePackage.BadKind JavaDoc, org.omg.CORBA.TypeCodePackage.Bounds JavaDoc {
422         String JavaDoc attrName = attribute_.name();
423         String JavaDoc attrName4 = idlHelper.format1FirstMin(attrName);
424         _logger.log(
425             Level.FINE,
426             "XMIImport readClassEnumerationTypeAttributeMethod("
427                 + package_name_
428                 + ", "
429                 + class_.name()
430                 + ", "
431                 + attrName
432                 + ", "
433                 + type_code_.kind().value()
434                 + ")");
435         outputln("/**");
436         outputln(" * Read the '" + idlHelper.format1(attrName) + "' attribute of the " + idlHelper.format1(class_.name()) + ".");
437         outputln(" * @param parent The parent of the current element in the XMI file.");
438         outputln(" * @param model The " + idlHelper.format1(class_.name()) + " to set.");
439         outputln(" * @param element The current element in the XMI file.");
440         outputln(" */");
441         output("public void read" + idlHelper.format1(class_.name()) + idlHelper.format1(attrName) + "(");
442         try {
443             outputln("org.w3c.dom.Element parent, " + javaHelper.javaQualifiedName(class_) + " model, org.w3c.dom.Element element)");
444         } catch (NotSet notSet) {
445             outputln("org.w3c.dom.Element parent, " + idlHelper.format1(class_.name()) + " model, org.w3c.dom.Element element)");
446         }
447         outputln("throws org.omg.mof.Reflective.MofError {");
448         outputln("java.lang.String " + attrName4 + " = new java.lang.String();");
449         outputln("if (parent.hasAttribute(\"" + attrName4 + "\"))");
450         outputln(TABULATION + attrName4 + " = parent.getAttribute(\"" + attrName4 + "\");");
451         outputln("else if (element != null && nodeNameCorrespondsTo(element, \"" + idlHelper.format1(attrName) + "\")) {");
452         outputln("if (element.getFirstChild() != null)");
453         outputln(TABULATION + attrName4 + " = element.getFirstChild().getNodeValue().trim();");
454         outputln("else");
455         outputln(TABULATION + attrName4 + " = element.getAttribute(\"xmi.value\").trim();");
456         outputln("}");
457         outputln("if (" + attrName4 + " != null && " + attrName4 + ".length() > 0) {");
458         String JavaDoc operation = "add_";
459         if (attribute_.multiplicity().upper == 1)
460             operation = "set_";
461
462         // Get the prefix
463
String JavaDoc prefix = new String JavaDoc();
464         java.util.Iterator JavaDoc it = associated_tags_.iterator();
465         while (it.hasNext()) {
466             org.omg.mof.Model.Tag tag = (org.omg.mof.Model.Tag) it.next();
467             if (tag.tag_id().equals("org.omg.xmi.enumerationUnprefix")) {
468                 org.omg.CORBA.Any JavaDoc[] values = tag.values();
469                 for (int v = 0; v < values.length; v++) {
470                     if (values[v].type().kind().value() == org.omg.CORBA.TCKind._tk_string)
471                         prefix = values[v].extract_string();
472                 }
473             }
474         }
475         for (int i = 0; i < type_code_.member_count(); i++) {
476             String JavaDoc member_name = StringHelper.replaceFirst(type_code_.member_name(i), prefix, "");
477             outputln("if (" + attrName4 + ".endsWith(\"" + member_name + "\"))");
478             try {
479                 outputln(
480                     TABULATION
481                         + "model."
482                         + operation
483                         + idlHelper.format2(attrName)
484                         + "("
485                         + javaHelper.javaQualifiedName(attribute_.type())
486                         + "."
487                         + type_code_.member_name(i)
488                         + ");");
489             } catch (NotSet notSet) {
490                 outputln(TABULATION + "break;");
491             }
492         }
493         outputln("}");
494         outputln("}");
495         outputln();
496     }
497
498     /**
499      * XMIImport generation for the class attribute reading method (for a StructureType attribute).
500      * @param package_name_ The package name.
501      * @param classes_name_ The class name.
502      * @param attribute_name_ The attribute name.
503      */

504     public void readClassStructureTypeAttributeMethod(
505         String JavaDoc package_name_,
506         org.omg.mof.Model.Class class_,
507         org.omg.mof.Model.MofAttribute attribute_,
508         org.omg.CORBA.TypeCode JavaDoc type_code_)
509         throws org.omg.mof.Reflective.MofError, org.omg.CORBA.TypeCodePackage.BadKind JavaDoc, org.omg.CORBA.TypeCodePackage.Bounds JavaDoc {
510         try {
511             String JavaDoc attrName = attribute_.name();
512             String JavaDoc classQualifiedName = javaHelper.javaQualifiedName(class_);
513             String JavaDoc attributeQualifiedName = javaHelper.javaQualifiedName(attribute_.type());
514             _logger.log(
515                 Level.FINE,
516                 "XMIImport readClassStructureTypeAttributeMethod("
517                     + package_name_
518                     + ", "
519                     + class_.name()
520                     + ", "
521                     + attrName
522                     + ", "
523                     + type_code_.kind().value()
524                     + ")");
525             outputln("/**");
526             outputln(" * Read the '" + idlHelper.format1(attrName) + "' attribute of the " + idlHelper.format1(class_.name()) + ".");
527             outputln(" * @param parent The parent of the current element in the XMI file.");
528             outputln(" * @param model The " + idlHelper.format1(class_.name()) + " to set.");
529             outputln(" * @param element The current element in the XMI file.");
530             outputln(" */");
531             output("public void read" + idlHelper.format1(class_.name()) + idlHelper.format1(attrName) + "(");
532             outputln("org.w3c.dom.Element parent, " + classQualifiedName + " model, org.w3c.dom.Element element)");
533             outputln("throws org.omg.mof.Reflective.MofError {");
534             outputln("if (element != null && nodeNameCorrespondsTo(element, \"" + idlHelper.format1(attrName) + "\")) {");
535             for (int i = 0; i < type_code_.member_count(); i++) {
536                 outputln(
537                     xmiImpHelper.typeCode2Java(type_code_.member_type(i))
538                         + " _"
539                         + idlHelper.format2(type_code_.member_name(i))
540                         + " = "
541                         + xmiImpHelper.defaultAttributeValue(type_code_.member_type(i))
542                         + ";");
543             }
544             outputln("org.w3c.dom.NodeList _" + idlHelper.format2(attrName) + " = element.getChildNodes();");
545             outputln("for (int j = 0; j < _" + idlHelper.format2(attrName) + ".getLength(); j++) {");
546             outputln("org.w3c.dom.Element _item = (org.w3c.dom.Element) _" + idlHelper.format2(attrName) + ".item(j);");
547             outputln("if (_item.getFirstChild() != null) {");
548             outputln("java.lang.String value = _item.getFirstChild().getNodeValue().trim();");
549             for (int i = 0; i < type_code_.member_count(); i++) {
550                 outputln("if (j==" + i + ")");
551                 outputln(
552                     " _"
553                         + idlHelper.format2(type_code_.member_name(i))
554                         + " = read"
555                         + idlHelper.format1(xmiImpHelper.typeCode2Java(type_code_.member_type(i)))
556                         + "Type(value);");
557             }
558             outputln("}");
559             outputln("}");
560
561             for (int i = 0; i < type_code_.member_count(); i++) {
562                 outputln("if (element.hasAttribute(\"" + idlHelper.format1FirstMin(type_code_.member_name(i)) + "\"))");
563                 outputln(
564                     " _"
565                         + idlHelper.format2(type_code_.member_name(i))
566                         + " = read"
567                         + idlHelper.format1(xmiImpHelper.typeCode2Java(type_code_.member_type(i)))
568                         + "Type(element.getAttribute(\""
569                         + idlHelper.format1FirstMin(type_code_.member_name(i))
570                         + "\"));");
571             }
572             output("model.set_" + idlHelper.format2(attrName) + "(new " + attributeQualifiedName + "(");
573             for (int i = 0; i < type_code_.member_count(); i++) {
574                 if (i > 0)
575                     output(", ");
576                 output("_" + idlHelper.format2(type_code_.member_name(i)));
577             }
578             outputln("));");
579             outputln("}");
580             outputln("}");
581             outputln();
582         } catch (NotSet notSet) {
583             // Nothing to do.
584
}
585     }
586
587     /**
588      * XMIImport generation for the class attribute reading method (for an AliasType attribute).
589      * @param package_name_ The package name.
590      * @param cls The Model class.
591      * @param attribute_ The AliasType attribute.
592      * @param content_type_ The Content Type of the AliasType.
593      */

594     public void readClassAliasTypeMethod(
595         String JavaDoc package_name_,
596         org.omg.mof.Model.Class cls,
597         org.omg.mof.Model.MofAttribute attribute_,
598         org.omg.CORBA.TypeCode JavaDoc content_type_)
599         throws MofError, NotSet {
600         _logger.log(Level.FINE, "XMIImport readClassAliasType(" + package_name_ + ", " + attribute_.name() + ")");
601         String JavaDoc attrName = attribute_.name();
602         String JavaDoc attrName4 = idlHelper.format1FirstMin(attrName);
603         String JavaDoc attrName2 = idlHelper.format2(attrName);
604         outputln("/**");
605         outputln(" * Read the '" + idlHelper.format1(attrName) + "' attribute of the " + idlHelper.format1(cls.name()) + ".");
606         outputln(" * @param parent The parent of the current element in the XMI file.");
607         outputln(" * @param model The " + idlHelper.format1(cls.name()) + " to set.");
608         outputln(" * @param element The current element in the XMI file.");
609         outputln(" */");
610         output("public void read" + idlHelper.format1(cls.name()) + idlHelper.format1(attrName) + "(");
611         outputln("org.w3c.dom.Element parent, " + javaHelper.javaQualifiedName(cls) + " model, org.w3c.dom.Element element)");
612         outputln("throws org.omg.mof.Reflective.MofError {");
613         String JavaDoc operation = "add_";
614         if (attribute_.multiplicity().upper == 1)
615             operation = "set_";
616         outputln("if (parent.hasAttribute(\"" + attrName4 + "\"))");
617
618         String JavaDoc contentType = typeCode2ReadingMethod(content_type_);
619         outputln(" model." + operation + attrName2 + "(" + contentType + "(parent.getAttribute(\"" + attrName4 + "\")));");
620         outputln(
621             "else if (element != null && element.getFirstChild() != null && nodeNameCorrespondsTo(element, \""
622                 + idlHelper.format1(attrName)
623                 + "\"))");
624         outputln(" model." + operation + attrName2 + "(" + contentType + "(element.getFirstChild().getNodeValue().trim()));");
625         outputln("}");
626         outputln();
627     }
628
629     /**
630      * Pass from the TypeCode to the name of the associated reading method.
631      * @param type_code The TypeCode.
632      * @return The name of the reading method.
633      */

634     public String JavaDoc typeCode2ReadingMethod(org.omg.CORBA.TypeCode JavaDoc type_code) {
635         switch (type_code.kind().value()) {
636             case org.omg.CORBA.TCKind._tk_boolean :
637                 return "readBooleanType";
638             case org.omg.CORBA.TCKind._tk_short :
639             case org.omg.CORBA.TCKind._tk_ushort :
640             case org.omg.CORBA.TCKind._tk_long :
641             case org.omg.CORBA.TCKind._tk_ulong :
642                 return "java.lang.Integer.parseInt";
643             case org.omg.CORBA.TCKind._tk_longlong :
644             case org.omg.CORBA.TCKind._tk_ulonglong :
645                 return "java.lang.Long.parseLong";
646             case org.omg.CORBA.TCKind._tk_double :
647                 return "java.lang.Double.parseDouble";
648             case org.omg.CORBA.TCKind._tk_float :
649                 return "java.lang.Float.parseFloat";
650             case org.omg.CORBA.TCKind._tk_string :
651             case org.omg.CORBA.TCKind._tk_wstring :
652                 return "java.lang.String.valueOf";
653             default :
654                 return "java.lang.String.valueOf";
655         }
656     }
657
658     /**
659      * XMIImport generation for the association attribute reading method.
660      * @param package_name_ The package name.
661      * @param class_ The class name.
662      * @param ref_ The reference attribute.
663      * @param hashtable_ The name of the hashtable.
664      */

665     public void readAssociationAttributeMethod(
666         String JavaDoc package_name_,
667         org.omg.mof.Model.Namespace class_,
668         org.omg.mof.Model.Reference ref_,
669         String JavaDoc hashtable_)
670         throws MofError, NotSet {
671         String JavaDoc className = class_.name();
672         String JavaDoc refName = ref_.name();
673         String JavaDoc typeName = class_.name();
674         _logger.log(
675             Level.FINE,
676             "XMIImport readAssociationAttributeMethod(" + package_name_ + ", " + className + ", " + refName + ", " + hashtable_ + ")");
677         String JavaDoc signature = "public void read" + idlHelper.format1(className) + idlHelper.format1(refName) + "(";
678         if (!_associationAttributes.contains(signature)) {
679             outputln("/**");
680             outputln(" * Read the '" + idlHelper.format1(refName) + "' attribute of the " + idlHelper.format1(className) + ".");
681             outputln(" * @param parent The parent of the current element in the XMI file.");
682             outputln(" * @param model The " + idlHelper.format1(class_.name()) + " to set.");
683             outputln(" * @param element The current element in the XMI file.");
684             outputln(" */");
685             _associationAttributes.add(signature);
686             output(signature);
687
688             outputln("org.w3c.dom.Element parent, " + javaHelper.javaQualifiedName(class_) + " model, org.w3c.dom.Element element)");
689             outputln("throws org.omg.mof.Reflective.MofError {");
690
691             //// If generation problem with the '_' character : use 'idlhelper.format#(refName)' instead of 'refName'.
692
outputln(
693                 "if (element != null && element.getNodeName().trim().endsWith(\"" + idlHelper.format1(typeName) + "." + refName + "\")) {");
694             outputln("org.w3c.dom.NodeList _child = element.getChildNodes();");
695             String JavaDoc operation = "add_";
696             if (ref_.multiplicity().upper == 1)
697                 operation = "set_";
698             if (ref_.type().is_abstract()) {
699                 outputln(
700                     "org.objectweb.modfact.corba.xmiio.mofobject.MOFObjectList list = _xmi_import_classes."
701                         + idlHelper.format1FirstMin(ref_.type().name())
702                         + "Template(element);");
703                 outputln("java.util.Iterator it = list.getObjects();");
704                 outputln("while (it.hasNext())");
705                 outputln(
706                     TABULATION
707                         + "model."
708                         + operation
709                         + idlHelper.format2(refName)
710                         + "(("
711                         + javaHelper.javaQualifiedName(ref_.type())
712                         + ")it.next());");
713                 outputln("it = list.getXmiIdRef();");
714                 outputln("while (it.hasNext())");
715                 outputln(TABULATION + "_helper.addHashtableValue(" + hashtable_ + ", model, (java.lang.String)it.next());");
716             } else {
717                 outputln("int i = 0;");
718                 outputln("while (i<_child.getLength() || (_child.getLength()==0 && i==0)) {");
719                 outputln("org.w3c.dom.Element _current = (org.w3c.dom.Element) _child.item(i);");
720                 outputln("if (_current != null) {");
721                 String JavaDoc variableType = javaHelper.javaQualifiedName(ref_.type());
722                 outputln(
723                     "org.objectweb.modfact.corba.xmiio.mofobject.MOFObject mof_obj = _xmi_import_classes."
724                         + idlHelper.format1FirstMin(ref_.type().name())
725                         + "Template(_current);");
726                 outputln("if (mof_obj.isAnObject())");
727                 outputln(TABULATION + "model." + operation + idlHelper.format2(ref_.name()) + "((" + variableType + ")mof_obj.get());");
728                 outputln("else if (mof_obj.isValid())");
729                 outputln(TABULATION + "_helper.addHashtableValue(" + hashtable_ + ", model, mof_obj.get());");
730                 outputln("}");
731                 outputln("i++;");
732                 outputln("}");
733             }
734             outputln("}");
735             outputln("}");
736             outputln();
737         }
738     }
739
740     /**
741      * XMIImport generation for the composite association attribute method.
742      * @param package_name_ The package name.
743      * @param classes_ The class.
744      * @param subclass_ The subclass.
745      * @param attribute_name_ The attribute name.
746      */

747     public void readCompositeAssociationAttributeMethod(
748         String JavaDoc package_name_,
749         org.omg.mof.Model.Class class_,
750         org.omg.mof.Model.Class[] subclass_,
751         String JavaDoc attribute_name_)
752         throws MofError, NotSet {
753         _logger.log(
754             Level.FINE,
755             "XMIImport readCompositeAssociationAttributeMethod("
756                 + package_name_
757                 + ", "
758                 + class_.name()
759                 + ", "
760                 + subclass_.length
761                 + ", "
762                 + attribute_name_
763                 + ")");
764         outputln("/**");
765         outputln(" * Read the '" + idlHelper.format1(attribute_name_) + "' attribute of the " + idlHelper.format1(class_.name()) + ".");
766         outputln(" * @param parent The parent of the current element in the XMI file.");
767         outputln(" * @param model The " + idlHelper.format1(attribute_name_) + " to set.");
768         outputln(" * @param element The current element in the XMI file.");
769         outputln(" */");
770         output("public void read" + idlHelper.format1(class_.name()) + idlHelper.format1(attribute_name_) + "(");
771         outputln("org.w3c.dom.Element parent, " + javaHelper.javaQualifiedName(class_) + " model, org.w3c.dom.Element element)");
772         outputln("throws org.omg.mof.Reflective.MofError {");
773         outputln(
774             "if (element != null && element.getNodeName().trim().endsWith(\""
775                 + javaHelper.javaQualifiedName(class_)
776                 + "."
777                 + idlHelper.format1(attribute_name_)
778                 + "\")) {");
779         outputln("org.w3c.dom.NodeList _child = element.getChildNodes();");
780         outputln("int i = 0;");
781         outputln("while (i<_child.getLength() || (_child.getLength()==0 && i==0)) {");
782         outputln("org.w3c.dom.Element _current = (org.w3c.dom.Element) _child.item(i);");
783         outputln("java.lang.String _current_name = _current.getNodeName().trim();");
784         for (int i = 0; i < subclass_.length; i++) {
785             if (i > 0)
786                 output("else ");
787             outputln("if (_current_name.endsWith(\"" + idlHelper.format1(subclass_[i].name()) + "\"))");
788             outputln(" _xmi_import_classes." + idlHelper.format1FirstMin(subclass_[i].name()) + "Template(_current);");
789         }
790         outputln("i++;");
791         outputln("}");
792         outputln("}");
793         outputln("}");
794         outputln();
795     }
796
797     /**
798      * XMIImport generation for the "put" methods.
799      * @param package_name_ The package name.
800      * @param associations_ The associations of the package.
801      */

802     public void putAssociationsValues(String JavaDoc package_name_, org.omg.mof.Model.Association associations_[])
803         throws org.omg.mof.Reflective.MofError {
804         _logger.log(Level.FINE, "XMIImport putAssociationsValue(" + package_name_ + ", " + associations_.length + ")");
805         for (int i = 0; i < associations_.length; i++) {
806             org.omg.mof.Model.Association asso = (org.omg.mof.Model.Association) associations_[i];
807             try {
808                 org.omg.mof.Model.Reference[] refs = mofHelper.referencesOfAssociation(asso);
809                 for (int j = 0; j < refs.length; j++) {
810                     org.omg.mof.Model.AssociationEnd assoEnd = refs[j].referenced_end();
811                     if (assoEnd.aggregation().value() == org.omg.mof.Model.AggregationKind._none && assoEnd.is_changeable()) {
812                         String JavaDoc hashtable = "_" + idlHelper.format2(asso.name()) + "_" + idlHelper.format2(refs[j].name());
813                         String JavaDoc type = javaHelper.javaQualifiedName(refs[j].container());
814                         String JavaDoc typeName = refs[j].container().name();
815                         String JavaDoc typeClass = type;
816                         String JavaDoc ref = refs[j].name();
817                         String JavaDoc typeRef = javaHelper.javaQualifiedName(refs[j].referenced_end().type());
818                         String JavaDoc vector = idlHelper.format2(ref) + "_vector";
819                         String JavaDoc type2 = idlHelper.format2(type);
820                         String JavaDoc ref2 = idlHelper.format2(ref);
821                         annotationTemplate(asso.annotation());
822                         outputln("public void put" + idlHelper.format1(typeName) + idlHelper.format1(ref) + "()");
823                         outputln(" throws org.omg.mof.Reflective.MofError {");
824                         outputln(
825                             "_logger.log(org.objectweb.modfact.corba.logging.Level.FINE, \"put"
826                                 + idlHelper.format1(typeName)
827                                 + idlHelper.format1(ref)
828                                 + "()\");");
829                         outputln("java.util.Enumeration _keys = " + hashtable + ".keys();");
830                         outputln("while (_keys.hasMoreElements()) {");
831                         outputln(typeClass + " _" + type2 + " = (" + typeClass + ") _keys.nextElement();");
832                         outputln("java.util.Vector _" + vector + " = (java.util.Vector) " + hashtable + ".get(_" + type2 + ");");
833                         outputln("for (int i=0 ; i<_" + vector + ".size() ; i++) {");
834                         outputln("java.lang.String _key = ((java.lang.String) _" + vector + ".elementAt(i)).trim();");
835                         outputln(typeRef + " _value = (" + typeRef + ") _types.get(_key);");
836                         if (refs[j].multiplicity().upper == 1) {
837                             if (refs[j].multiplicity().lower == 0) {
838                                 outputln("try {");
839                                 outputln("if (_value != null && !_value._is_equivalent(_" + type2 + "." + ref2 + "()))");
840                                 outputln(" _" + type2 + ".set_" + ref2 + "(_value);");
841                                 outputln("} catch (org.omg.mof.Reflective.NotSet notSet) {");
842                                 outputln("_" + type2 + ".set_" + ref2 + "(_value);");
843                                 outputln("}");
844                             } else {
845                                 outputln(
846                                     "if (_"
847                                         + type2
848                                         + "."
849                                         + ref2
850                                         + "() == null || (_value != null && !_value._is_equivalent(_"
851                                         + type2
852                                         + "."
853                                         + ref2
854                                         + "()))) {");
855                                 outputln("_" + type2 + ".set_" + ref2 + "(_value);");
856                                 outputln("}");
857                             }
858                         } else {
859                             outputln("if (_value != null && !_helper.arrayContains(_" + type2 + "." + ref2 + "(), _value)) {");
860                             outputln("_" + type2 + ".add_" + ref2 + "(_value);");
861                             outputln("}");
862                         }
863
864                         if (refs.length > 1
865                             && refs[(j + 1) % 2].referenced_end().aggregation().value() != org.omg.mof.Model.AggregationKind._none) {
866                             // There is j=0 or j=1, so, to switch between the values, we can use: (j+1)%2
867
if (refs[(j + 1) % 2].multiplicity().upper == 1)
868                                 outputln(
869                                     "_value.set_" + idlHelper.format2(refs[(j + 1) % 2].name()) + "(_" + idlHelper.format2(type) + ");");
870                             else
871                                 outputln(
872                                     "_value.add_" + idlHelper.format2(refs[(j + 1) % 2].name()) + "(_" + idlHelper.format2(type) + ");");
873                         }
874                         outputln("}");
875                         outputln("}");
876                         outputln("}");
877                         outputln();
878                     }
879                 }
880             } catch (org.omg.mof.Reflective.NotSet notSet) {
881                 // Nothing to do
882
}
883         }
884     }
885
886 }
887
Popular Tags