KickJava   Java API By Example, From Geeks To Geeks.

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


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.importer;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import javax.jmi.model.MofClass;
26 import javax.jmi.model.MofPackage;
27
28 import org.objectweb.modfact.jmi.helper.JMIProvider;
29 import org.objectweb.modfact.jmi.helper.XMIIOHelper;
30 import org.objectweb.modfact.jmi.xmiio.common.AbstractXMIIOGenerator;
31
32 /**
33  * The XMI Import generator.
34  * This class allows the generation of the XMI importer (into Java).
35  */

36 public class GenerationXMIImportAttributes extends AbstractXMIIOGenerator {
37
38     /** The generated association attributes methods. */
39     private java.util.List JavaDoc _associationAttributes;
40
41     /** The classes of the package. */
42     private java.util.Vector JavaDoc classes = new java.util.Vector JavaDoc();
43
44     /** The associations of the package. */
45     private java.util.Vector JavaDoc associations = new java.util.Vector JavaDoc();
46
47     /**
48      * Default Constructor.
49      */

50     public GenerationXMIImportAttributes() {
51         super();
52         _associationAttributes = new java.util.ArrayList JavaDoc();
53     }
54
55     /**
56      * XMIImport generation.
57      * @param package_ The root package.
58      * @param class_name_ The class name of the generated class.
59      */

60     public void generateXMIImportAttributes(javax.jmi.model.MofPackage package_, String JavaDoc class_name_) throws java.io.FileNotFoundException JavaDoc {
61         packageModuleTemplate(package_, class_name_);
62         out.flush();
63     }
64
65     /**
66      * XMIImport generation for constructor.
67      * @param class_name The class name.
68      * @param package_name The package name.
69      */

70     public void constructor(String JavaDoc class_name, String JavaDoc package_name) {
71         outputln("/** The XMIImportClasses class. */");
72         outputln(package_name + "XMIImportClasses _xmi_import_classes;");
73         outputln();
74         outputln("/** Hashtables for types (Key / Model) */");
75         outputln("private java.util.Hashtable _types;");
76         outputln();
77         outputln("/**");
78         outputln(" * Default " + class_name + " Constructor");
79         outputln(" */");
80         outputln("public " + class_name + "(java.util.Hashtable types) {");
81         outputln("_types = types;");
82         outputln("}");
83         outputln();
84         outputln("public void setXMIImportClasses (" + package_name + "XMIImportClasses xmi_import_classes) {");
85         outputln("_xmi_import_classes = xmi_import_classes;");
86         outputln("}");
87         outputln();
88     }
89
90     /**
91      * Parse the content of the package.
92      * @param package_ The root package.
93      */

94     private void processContents(javax.jmi.model.MofPackage package_) {
95         java.util.List JavaDoc contentsList = package_.getContents();
96         Iterator JavaDoc it = contentsList.iterator();
97         while (it.hasNext()) {
98             javax.jmi.model.ModelElement contents = (javax.jmi.model.ModelElement) it.next();
99             if (contents instanceof javax.jmi.model.MofPackage)
100                 processContents((javax.jmi.model.MofPackage) contents);
101             else if (contents instanceof javax.jmi.model.Import)
102                 processContents((javax.jmi.model.MofPackage) ((javax.jmi.model.Import) contents).getImportedNamespace());
103             else if (contents instanceof javax.jmi.model.MofClass)
104                 classes.addElement((javax.jmi.model.MofClass) contents);
105             else if (contents instanceof javax.jmi.model.Association)
106                 associations.addElement((javax.jmi.model.Association) contents);
107         }
108     }
109
110     /**
111      * Package processing.
112      * @param package_ The package to process.
113      * @param class_name_ The class name of the generated class.
114      */

115     public void packageModuleTemplate(javax.jmi.model.MofPackage package_, String JavaDoc class_name_) {
116         if (package_.getVisibility() == javax.jmi.model.VisibilityKindEnum.forName("public_vis")) {
117             processContents(package_);
118
119             java.util.Vector JavaDoc classes_ordered = new java.util.Vector JavaDoc();
120             while (classes.size() != 0) {
121                 javax.jmi.model.MofClass courant = null;
122                 for (int i = 0; i < classes.size(); i++) {
123                     courant = (javax.jmi.model.MofClass) classes.elementAt(i);
124                     java.util.List JavaDoc supertypes = courant.getSupertypes();
125                     boolean is_top = true;
126                     Iterator JavaDoc itSup = supertypes.iterator();
127                     while (itSup.hasNext()) {
128                         if (classes.contains((javax.jmi.model.GeneralizableElement) itSup.next()))
129                             is_top = false;
130                     }
131                     if (is_top) {
132                         classes_ordered.addElement(courant);
133                         classes.removeElement(courant);
134                     }
135                 }
136             }
137
138             String JavaDoc packageName = JMIProvider.jmiPackageExtentName(package_);
139             String JavaDoc packageQualifiedName = JMIProvider.qualifierOf(package_);
140             outputln("package " + packageQualifiedName + ".xmi;");
141             outputln();
142             annotationTemplate(package_.getAnnotation());
143             outputln("public class " + class_name_ + " extends org.objectweb.modfact.jmi.xmiio.importer.XMIImport {");
144             constructor(class_name_, packageName);
145             classAttributes(packageName, package_);
146             associationAttributes(packageName, associations);
147             initiateMethod(packageName, package_);
148             readingAttributes(packageName, classes_ordered);
149             putAssociationsValues(packageName, associations);
150             outputln("} // end of class " + class_name_);
151             outputln();
152         }
153     }
154
155     /**
156      * XMIImport generation for the class attributes.
157      * @param package_name_ The package name.
158      * @param package_ The package.
159      */

160     public void classAttributes(String JavaDoc package_name_, MofPackage package_) {
161         outputln("// Factories Declaration");
162         String JavaDoc model_package_type;
163         model_package_type = JMIProvider.jmiPackageQualifiedName(package_) + "Package";
164         outputln("private " + model_package_type + " _" + JMIProvider.jmiFormat2(package_name_) + "_package = null;");
165         outputln();
166     }
167
168     /**
169      * XMIImport generation for the association attributes.
170      * @param package_name_ The package name.
171      * @param associations_ The associations of the package.
172      */

173     public void associationAttributes(String JavaDoc package_name_, Vector JavaDoc associations_) {
174         outputln("// Hashtables for associations (Model / Key Vector)");
175         for (int i = 0; i < associations_.size(); i++) {
176             javax.jmi.model.Association asso = (javax.jmi.model.Association) associations_.elementAt(i);
177             try {
178                 javax.jmi.model.Reference[] refs = XMIIOHelper.getReferences(asso);
179                 for (int j = 0; j < refs.length; j++) {
180                     outputln(
181                         "private java.util.Hashtable _"
182                             + JMIProvider.jmiFormat2(JMIProvider.jmiAssociationName(asso))
183                             + "_"
184                             + JMIProvider.jmiFormat2(refs[j].getName())
185                             + " = new java.util.Hashtable();");
186                 }
187             } catch (NullPointerException JavaDoc notSet) {
188                 // Nothing to do
189
}
190         }
191         outputln();
192     }
193
194     /**
195      * XMIImport generation for the initiate method.
196      * @param package_name_ The package name.
197      * @param package_ The package.
198      */

199     public void initiateMethod(String JavaDoc package_name_, MofPackage package_) {
200         String JavaDoc model_package_type;
201         model_package_type = JMIProvider.jmiPackageQualifiedName(package_) + "Package";
202         outputln("public void initiate(" + model_package_type + " _model_package) {");
203         outputln("// Factories creation");
204         outputln("this._" + JMIProvider.jmiFormat2(package_name_) + "_package = _model_package;");
205         outputln("}");
206         outputln();
207     }
208
209     /**
210      * XMIImport generation for the attributes reading methods.
211      * @param package_name_ The package name.
212      * @param classes_ The classes of the package.
213      */

214     public void readingAttributes(String JavaDoc package_name_, Vector JavaDoc classes_) {
215         outputln("// ==================================================================");
216         outputln("// readXXXYYY with XXX an entity and YYY its property");
217         outputln("// ==================================================================");
218         outputln();
219         // Classes
220
for (int i = 0; i < classes_.size(); i++) {
221             javax.jmi.model.MofClass cls = (javax.jmi.model.MofClass) classes_.elementAt(i);
222             String JavaDoc clsName = JMIProvider.jmiClassName(cls);
223             java.util.List JavaDoc contents = cls.getContents();
224             Iterator JavaDoc it = contents.iterator();
225             while (it.hasNext()) {
226                 javax.jmi.model.ModelElement content = (javax.jmi.model.ModelElement) it.next();
227                 if (content instanceof javax.jmi.model.Attribute) {
228                     javax.jmi.model.Attribute attr = (javax.jmi.model.Attribute) content;
229                     javax.jmi.model.Classifier type = attr.getType();
230                     String JavaDoc attrName = attr.getName();
231                     if (attr.isChangeable()) {
232                         if (type instanceof javax.jmi.model.DataType) {
233                             if (type instanceof javax.jmi.model.EnumerationType) {
234                                 java.util.List JavaDoc associatedTags = new java.util.ArrayList JavaDoc();
235                                 try {
236                                     java.util.List JavaDoc clsContents = type.getContainer().getContents();
237                                     Iterator JavaDoc itContents = clsContents.iterator();
238                                     while (itContents.hasNext()) {
239                                         javax.jmi.model.ModelElement clsContent = (javax.jmi.model.ModelElement) itContents.next();
240                                         if (clsContent instanceof javax.jmi.model.Tag) {
241                                             javax.jmi.model.Tag tag = (javax.jmi.model.Tag) clsContent;
242                                             java.util.Collection JavaDoc elements = tag.getElements();
243                                             Iterator JavaDoc itElements = elements.iterator();
244                                             while (itElements.hasNext()) {
245                                                 javax.jmi.model.ModelElement element = (javax.jmi.model.ModelElement) itElements.next();
246                                                 if (element.equals(type))
247                                                     associatedTags.add(tag);
248                                             }
249                                         }
250                                     }
251                                 } catch (NullPointerException JavaDoc notSet) {
252                                     // Nothing to do
253
}
254                                 readClassEnumerationTypeAttributeMethod(
255                                     package_name_,
256                                     cls,
257                                     attr,
258                                     (javax.jmi.model.EnumerationType) type,
259                                     associatedTags);
260                             } else if (type instanceof javax.jmi.model.StructureType) {
261                                 readClassStructureTypeAttributeMethod(package_name_, cls, attr);
262                             } else if (type instanceof javax.jmi.model.AliasType) {
263                                 //readClassAliasTypeMethod(package_name_, cls, attr, type_code.content_type());
264
} else {
265                                 readClassAttributeMethod(package_name_, cls, (javax.jmi.model.Attribute) content, type);
266                             }
267                         } else {
268                             readClassAttributeMethod(package_name_, cls, (javax.jmi.model.Attribute) content, type);
269                         }
270                     }
271                 } else if (content instanceof javax.jmi.model.Reference) {
272                     try {
273                         javax.jmi.model.Reference ref = (javax.jmi.model.Reference) content;
274                         javax.jmi.model.AssociationEnd assoEnd = ref.getReferencedEnd();
275                         javax.jmi.model.AssociationEnd otherEnd = XMIIOHelper.otherAssociationEnd(assoEnd);
276                         boolean none = (assoEnd.getAggregation() == javax.jmi.model.AggregationKindEnum.forName("none"));
277                         boolean composite = (assoEnd.getAggregation() == javax.jmi.model.AggregationKindEnum.forName("composite"));
278
279                         if (none) {
280                             if (composite) {
281                                 javax.jmi.model.MofClass[] subClasses = XMIIOHelper.subClasses(cls);
282                                 readCompositeAssociationAttributeMethod(package_name_, cls, subClasses, assoEnd.getName());
283                             }
284                             javax.jmi.model.Association asso = (javax.jmi.model.Association) assoEnd.getContainer();
285                             javax.jmi.model.Reference[] refs = XMIIOHelper.getReferences(asso);
286                             for (int r = 0; r < refs.length; r++) {
287                                 String JavaDoc hashtable =
288                                     "_"
289                                         + JMIProvider.jmiFormat2(JMIProvider.jmiAssociationName(asso))
290                                         + "_"
291                                         + JMIProvider.jmiFormat2(refs[r].getName());
292                                 try {
293                                     readAssociationAttributeMethod(package_name_, refs[r].getContainer(), refs[r], hashtable);
294                                 } catch (NullPointerException JavaDoc notSet) {
295                                     notSet.printStackTrace();
296                                 }
297                             }
298                         }
299                     } catch (NullPointerException JavaDoc notSet) {
300                         // Nothing to do
301
}
302                 }
303             }
304         }
305     }
306
307     /**
308      * XMIImport generation for the class attribute reading method.
309      * @param package_name_ The package name.
310      * @param classes_ The class.
311      * @param attribute_ The attribute.
312      * @param type_ The Type of the attribute.
313      */

314     public void readClassAttributeMethod(
315         String JavaDoc package_name_,
316         javax.jmi.model.MofClass class_,
317         javax.jmi.model.Attribute attribute_,
318         javax.jmi.model.Classifier type_) {
319         String JavaDoc type_name_ = type_.getName();
320         String JavaDoc type = JMIProvider.jmiFormat1(type_name_);
321         String JavaDoc attrName = JMIProvider.findSubstituteName(attribute_);
322         if (attrName == null)
323             attrName = attribute_.getName();
324         String JavaDoc attrName4 = XMIIOHelper.format1FirstMin(attrName);
325         String JavaDoc className = JMIProvider.jmiClassName(class_);
326         outputln("/**");
327         outputln(" * Read the '" + JMIProvider.jmiFormat1(attrName) + "' attribute of the " + JMIProvider.jmiFormat1(className) + ".");
328         outputln(" * @param parent The parent of the current element in the XMI file.");
329         outputln(" * @param model The " + JMIProvider.jmiFormat1(className) + " to set.");
330         outputln(" * @param element The current element in the XMI file.");
331         outputln(" */");
332         output(
333             "public void read"
334                 + JMIProvider.classNameFormat(className)
335                 + JMIProvider.jmiFormat1(attrName)
336                 + "(");
337         //output(
338
// "public void read"
339
// + JMIProvider.classNameFormat(className)
340
// + JMIProvider.jmiFormat1(JMIProvider.attributNameFormat(attrName))
341
// + "(");
342
outputln("org.w3c.dom.Element parent, " + JMIProvider.jmiClassQualifiedName(class_) + " model, org.w3c.dom.Element element) {");
343         if (attribute_.getMultiplicity().getUpper() == 1) {
344             if (type_ instanceof javax.jmi.model.PrimitiveType) {
345                 String JavaDoc method = primitiveTypeToReadMethod((javax.jmi.model.PrimitiveType) type_);
346                 outputln("if (parent.hasAttribute(\"" + attrName4 + "\"))");
347                 outputln(
348                     " model."
349                         + JMIProvider.jmiMutatorName(attribute_)
350                         + "("
351                         + method
352                         + "(parent.getAttribute(\""
353                         + attrName4
354                         + "\")));");
355                 outputln("else if (element != null && nodeNameCorrespondsTo(element, \"" + JMIProvider.jmiFormat1(attrName) + "\")) {");
356                 outputln("if (element.getFirstChild() != null)");
357                 outputln(
358                     " model."
359                         + JMIProvider.jmiMutatorName(attribute_)
360                         + "("
361                         + method
362                         + "(element.getFirstChild().getNodeValue().trim()));");
363                 outputln("else");
364                 outputln(
365                     " model."
366                         + JMIProvider.jmiMutatorName(attribute_)
367                         + "("
368                         + method
369                         + "(element.getAttribute(\"xmi.value\").trim()));");
370                 outputln("}");
371             } else {
372                 outputln(
373                     "if (element != null && element.getFirstChild() != null && nodeNameCorrespondsTo(element, \""
374                         + JMIProvider.jmiFormat1(attrName)
375                         + "\"))");
376                 String JavaDoc paramType = JMIProvider.jmiClassifierQualifiedName(attribute_.getType());
377                 String JavaDoc getMethod = ".get()";
378                 if (attribute_.getType().isAbstract())
379                     getMethod = ".getFirstObject()";
380                 outputln(
381                     " model."
382                         + JMIProvider.jmiMutatorName(attribute_)
383                         + "(("
384                         + paramType
385                         + ")_xmi_import_classes."
386                         + XMIIOHelper.format1FirstMin(JMIProvider.classNameFormat(type_name_))
387                         + "Template((org.w3c.dom.Element)element.getFirstChild())"
388                         + getMethod
389                         + ");");
390             }
391         } else {
392             if (type_ instanceof javax.jmi.model.PrimitiveType) {
393                 String JavaDoc method = primitiveTypeToReadMethod((javax.jmi.model.PrimitiveType) type_);
394                 outputln("if (parent.hasAttribute(\"" + attrName4 + "\"))");
395                 outputln(
396                     " model."
397                         + JMIProvider.jmiAccessorName(attribute_)
398                         + "().add("
399                         + method
400                         + "(parent.getAttribute(\""
401                         + attrName4
402                         + "\")));");
403                 outputln("else if (element != null && nodeNameCorrespondsTo(element, \"" + JMIProvider.jmiFormat1(attrName) + "\")) {");
404                 outputln("if (element.getFirstChild() != null)");
405                 outputln(
406                     " model."
407                         + JMIProvider.jmiAccessorName(attribute_)
408                         + "().add("
409                         + method
410                         + "(element.getFirstChild().getNodeValue().trim()));");
411                 outputln("else");
412                 outputln(
413                     " model."
414                         + JMIProvider.jmiAccessorName(attribute_)
415                         + "().add("
416                         + method
417                         + "(element.getAttribute(\"xmi.value\").trim()));");
418                 outputln("}");
419             } else {
420                 outputln(
421                     "if (element != null && element.getFirstChild() != null && nodeNameCorrespondsTo(element, \""
422                         + JMIProvider.jmiFormat1(attrName)
423                         + "\"))");
424                 String JavaDoc paramType = JMIProvider.jmiClassifierQualifiedName(attribute_.getType());
425                 if (attribute_.getType().isAbstract()) {
426                     outputln("org.objectweb.modfact.jmi.xmiio.mofobject.MOFObjectList mofList = _xmi_import_classes." + XMIIOHelper.format1FirstMin(type) + "Template((org.w3c.dom.Element)element.getFirstChild());");
427                     outputln("java.util.Iterator it = mofList.getObjects();");
428                     outputln("while (it.hasNext()) {");
429                     outputln(TABULATION + "model."+ JMIProvider.jmiAccessorName(attribute_) + "().add((" + paramType + ")it.next());");
430                     outputln("}");
431                 } else {
432                     outputln(
433                         " model."
434                             + JMIProvider.jmiAccessorName(attribute_)
435                             + "().add(("
436                             + paramType
437                             + ")_xmi_import_classes."
438                             + XMIIOHelper.format1FirstMin(type)
439                             + "Template((org.w3c.dom.Element)element.getFirstChild()).get());");
440                 }
441             }
442         }
443         outputln("}");
444         outputln();
445     }
446
447     /**
448      * XMIImport generation for the class attribute reading method (for a TypeCode attribute).
449      * @param package_name_ The package name.
450      * @param classes_ The class.
451      * @param attribute_name_ The attribute name.
452      */

453     public void readClassTypeCodeAttributeMethod(String JavaDoc package_name_, javax.jmi.model.MofClass class_, String JavaDoc attribute_name_) {
454         String JavaDoc class_name = JMIProvider.jmiClassName(class_);
455         outputln("/**");
456         outputln(
457             " * Read the '" + JMIProvider.jmiFormat1(attribute_name_) + "' attribute of the " + JMIProvider.jmiFormat1(class_name) + ".");
458         outputln(" * @param parent The parent of the current element in the XMI file.");
459         outputln(" * @param model The " + JMIProvider.jmiFormat1(class_name) + " to set.");
460         outputln(" * @param element The current element in the XMI file.");
461         outputln(" */");
462         output("public void read" + JMIProvider.jmiFormat1(class_name) + JMIProvider.jmiFormat1(attribute_name_) + "(");
463         outputln("org.w3c.dom.Element parent, " + JMIProvider.jmiClassQualifiedName(class_) + " model, org.w3c.dom.Element element) {");
464         outputln("String repositoryID = \"IDL:\" + JMIProvider.jmiFormat1(model.getName());");
465         outputln("if (element != null && nodeNameCorrespondsTo(element, \"" + JMIProvider.jmiFormat1(attribute_name_) + "\")) {");
466         outputln("org.w3c.dom.Element _corba_typecode = (org.w3c.dom.Element) element.getElementsByTagName(\"XMI.CorbaTypeCode\").item(0);");
467         outputln("org.w3c.dom.Element _element = (org.w3c.dom.Element) _corba_typecode.getChildNodes().item(0);");
468         outputln("model.set_" + JMIProvider.jmiFormat2(attribute_name_) + "(xml2TypeCode(_element, repositoryID));");
469         outputln("}");
470         outputln("}");
471         outputln();
472     }
473
474     /**
475      * XMIImport generation for the class attribute reading method (for an EnumarationType attribute).
476      * @param package_name_ The package name.
477      * @param classes_ The class.
478      * @param attribute_ The attribute.
479      * @param type_ The EnumerationType.
480      * @param associated_tags_ The tags associated to the Data Type.
481      */

482     public void readClassEnumerationTypeAttributeMethod(
483         String JavaDoc package_name_,
484         javax.jmi.model.MofClass class_,
485         javax.jmi.model.Attribute attribute_,
486         javax.jmi.model.EnumerationType type,
487         java.util.List JavaDoc associated_tags_) {
488         String JavaDoc attrName = attribute_.getName();
489         String JavaDoc attrName4 = XMIIOHelper.format1FirstMin(attrName);
490         String JavaDoc className = JMIProvider.jmiClassName(class_);
491         outputln("/**");
492         outputln(" * Read the '" + JMIProvider.jmiFormat1(attrName) + "' attribute of the " + JMIProvider.jmiFormat1(className) + ".");
493         outputln(" * @param parent The parent of the current element in the XMI file.");
494         outputln(" * @param model The " + JMIProvider.jmiFormat1(className) + " to set.");
495         outputln(" * @param element The current element in the XMI file.");
496         outputln(" */");
497         output(
498             "public void read"
499                 + JMIProvider.jmiFormat1(className)
500                 + JMIProvider.jmiFormat1(attrName)
501                 + "(");
502         //output(
503
// "public void read"
504
// + JMIProvider.jmiFormat1(className)
505
// + JMIProvider.jmiFormat1(JMIProvider.attributNameFormat(attrName))
506
// + "(");
507
outputln("org.w3c.dom.Element parent, " + JMIProvider.jmiClassQualifiedName(class_) + " model, org.w3c.dom.Element element) {");
508         outputln("java.lang.String " + attrName4 + " = new java.lang.String();");
509         outputln("if (parent.hasAttribute(\"" + attrName4 + "\"))");
510         outputln(TABULATION + attrName4 + " = parent.getAttribute(\"" + attrName4 + "\");");
511         outputln("else if (element != null && nodeNameCorrespondsTo(element, \"" + JMIProvider.jmiFormat1(attrName) + "\")) {");
512         outputln("if (element.getFirstChild() != null)");
513         outputln(TABULATION + attrName4 + " = element.getFirstChild().getNodeValue().trim();");
514         outputln("else");
515         outputln(TABULATION + attrName4 + " = element.getAttribute(\"xmi.value\").trim();");
516         outputln("}");
517         outputln("if (" + attrName4 + " != null && " + attrName4 + ".length() > 0) {");
518
519         // Get the prefix
520
String JavaDoc prefix = new String JavaDoc();
521         java.util.Iterator JavaDoc it = associated_tags_.iterator();
522         while (it.hasNext()) {
523             javax.jmi.model.Tag tag = (javax.jmi.model.Tag) it.next();
524             if (tag.getTagId().equals("org.omg.xmi.enumerationUnprefix")) {
525                 java.util.List JavaDoc values = tag.getValues();
526                 Iterator JavaDoc itValues = values.iterator();
527                 while (itValues.hasNext()) {
528                     Object JavaDoc v = itValues.next();
529                     if (v instanceof String JavaDoc)
530                         prefix = v.toString();
531                 }
532             }
533         }
534         Iterator JavaDoc itLabels = type.getLabels().iterator();
535         while (itLabels.hasNext()) {
536             String JavaDoc label = (String JavaDoc) itLabels.next();
537             String JavaDoc member_name = XMIIOHelper.replaceFirst(label, prefix, "");
538             outputln("if (" + attrName4 + ".endsWith(\"" + member_name + "\"))");
539             outputln(
540                 TABULATION
541                     + "model.set"
542                     + JMIProvider.jmiFormat1(attrName)
543                     + "("
544                     + JMIProvider.jmiClassifierQualifiedName(attribute_.getType())
545                     + "Enum.forName(\""
546                     + label
547                     + "\"));");
548         }
549         outputln("}");
550         outputln("}");
551         outputln();
552     }
553
554     /**
555      * XMIImport generation for the class attribute reading method (for a StructureType attribute).
556      * @param package_name_ The package name.
557      * @param classes_ The class.
558      * @param attribute_ The attribute.
559      */

560     public void readClassStructureTypeAttributeMethod(
561         String JavaDoc package_name_,
562         javax.jmi.model.MofClass class_,
563         javax.jmi.model.Attribute attribute_) {
564         String JavaDoc attrName = attribute_.getName();
565         String JavaDoc className = JMIProvider.jmiClassName(class_);
566         outputln("/**");
567         outputln(" * Read the '" + JMIProvider.jmiFormat1(attrName) + "' attribute of the " + JMIProvider.jmiFormat1(className) + ".");
568         outputln(" * @param parent The parent of the current element in the XMI file.");
569         outputln(" * @param model The " + JMIProvider.jmiFormat1(className) + " to set.");
570         outputln(" * @param element The current element in the XMI file.");
571         outputln(" */");
572         output("public void read" + JMIProvider.jmiFormat1(className) + JMIProvider.jmiFormat1(attrName) + "(");
573         outputln("org.w3c.dom.Element parent, " + JMIProvider.jmiClassQualifiedName(class_) + " model, org.w3c.dom.Element element) {");
574         outputln("if (element != null && nodeNameCorrespondsTo(element, \"" + JMIProvider.jmiFormat1(attrName) + "\")) {");
575
576         outputln("org.w3c.dom.NodeList _" + JMIProvider.jmiFormat2(attrName) + " = element.getChildNodes();");
577         outputln("for (int j = 0; j < _" + JMIProvider.jmiFormat2(attrName) + ".getLength(); j++) {");
578         outputln("org.w3c.dom.Element _item = (org.w3c.dom.Element) _" + JMIProvider.jmiFormat2(attrName) + ".item(j);");
579         outputln("if (_item.getFirstChild() != null) {");
580         outputln("java.lang.String value = _item.getFirstChild().getNodeValue().trim();");
581
582         outputln("}");
583         outputln("}");
584         output(
585             "model.set" + JMIProvider.jmiFormat1(attrName) + "(new " + JMIProvider.jmiClassifierQualifiedName(attribute_.getType()) + "(");
586         outputln("));");
587         outputln("}");
588         outputln("}");
589         outputln();
590     }
591
592     /**
593      * XMIImport generation for the class attribute reading method (for an AliasType attribute).
594      * @param package_name_ The package name.
595      * @param cls The Model class.
596      * @param attribute_ The AliasType attribute.
597      * @param content_type_ The Content Type of the AliasType.
598      */

599     public void readClassAliasTypeMethod(
600         String JavaDoc package_name_,
601         javax.jmi.model.MofClass cls,
602         javax.jmi.model.Attribute attribute_,
603         org.omg.CORBA.TypeCode JavaDoc content_type_) {
604         String JavaDoc attrName = attribute_.getName();
605         String JavaDoc attrName4 = XMIIOHelper.format1FirstMin(attrName);
606         String JavaDoc attrName1 = JMIProvider.jmiFormat1(attrName);
607         String JavaDoc attrName2 = JMIProvider.jmiFormat2(attrName);
608         String JavaDoc clsName = JMIProvider.jmiClassName(cls);
609         outputln("/**");
610         outputln(" * Read the '" + JMIProvider.jmiFormat1(attrName) + "' attribute of the " + JMIProvider.jmiFormat1(clsName) + ".");
611         outputln(" * @param parent The parent of the current element in the XMI file.");
612         outputln(" * @param model The " + JMIProvider.jmiFormat1(clsName) + " to set.");
613         outputln(" * @param element The current element in the XMI file.");
614         outputln(" */");
615         output("public void read" + JMIProvider.jmiFormat1(clsName) + JMIProvider.jmiFormat1(attrName) + "(");
616         outputln("org.w3c.dom.Element parent, " + JMIProvider.jmiClassQualifiedName(cls) + " model, org.w3c.dom.Element element) {");
617         String JavaDoc contentType = typeCode2ReadingMethod(content_type_);
618         outputln("if (parent.hasAttribute(\"" + attrName4 + "\"))");
619         if (attribute_.getMultiplicity().getUpper() == 1) {
620             outputln(" model.set" + attrName1 + "(" + contentType + "(parent.getAttribute(\"" + attrName4 + "\")));");
621             outputln(
622                 "else if (element != null && element.getFirstChild() != null && nodeNameCorrespondsTo(element, \""
623                     + JMIProvider.jmiFormat1(attrName)
624                     + "\"))");
625             outputln(" model.set" + attrName1 + "(" + contentType + "(element.getFirstChild().getNodeValue().trim()));");
626         } else {
627             outputln(" model.get" + attrName1 + "().add(" + contentType + "(parent.getAttribute(\"" + attrName4 + "\")));");
628             outputln(
629                 "else if (element != null && element.getFirstChild() != null && nodeNameCorrespondsTo(element, \""
630                     + JMIProvider.jmiFormat1(attrName)
631                     + "\"))");
632             outputln(" model.get" + attrName1 + "().add(" + contentType + "(element.getFirstChild().getNodeValue().trim()));");
633         }
634         outputln("}");
635         outputln();
636     }
637
638     /**
639      * Pass from the TypeCode to the name of the associated reading method.
640      * @param type_code The TypeCode.
641      * @return The name of the reading method.
642      */

643     public String JavaDoc typeCode2ReadingMethod(org.omg.CORBA.TypeCode JavaDoc type_code) {
644         switch (type_code.kind().value()) {
645             case org.omg.CORBA.TCKind._tk_boolean :
646                 return "readBooleanType";
647             case org.omg.CORBA.TCKind._tk_short :
648             case org.omg.CORBA.TCKind._tk_ushort :
649             case org.omg.CORBA.TCKind._tk_long :
650             case org.omg.CORBA.TCKind._tk_ulong :
651                 return "readIntegerType";
652             case org.omg.CORBA.TCKind._tk_longlong :
653             case org.omg.CORBA.TCKind._tk_ulonglong :
654                 return "readIntegerType";
655             case org.omg.CORBA.TCKind._tk_double :
656                 return "readDoubleType";
657             case org.omg.CORBA.TCKind._tk_float :
658                 return "readFloatType";
659             case org.omg.CORBA.TCKind._tk_string :
660             case org.omg.CORBA.TCKind._tk_wstring :
661                 return "readStringType";
662             default :
663                 return "readStringType";
664         }
665     }
666
667     /**
668      * Pass from the PrimitiveType to the name of the associated reading method.
669      * @param type The PrimitiveType.
670      * @return The name of the reading method.
671      */

672     public String JavaDoc primitiveTypeToReadMethod(javax.jmi.model.PrimitiveType type_) {
673         String JavaDoc typeName = type_.getName().toLowerCase();
674         if (typeName.equals("string")) {
675             return "java.lang.String.valueOf";
676         } else if (typeName.equals("boolean")) {
677             return "readBooleanType";
678         } else if (typeName.equals("integer") || typeName.equals("long")) {
679             return "java.lang.Integer.parseInt";
680         } else if (typeName.equals("float")) {
681             return "java.lang.Float.parseFloat";
682         } else if (typeName.equals("double")) {
683             return "java.lang.Double.parseDouble";
684         } else {
685             throw new RuntimeException JavaDoc("unknown primitive type: " + type_.getName());
686         }
687     }
688
689     /**
690      * XMIImport generation for the association attribute reading method.
691      * @param package_name_ The package name.
692      * @param class_ The class name.
693      * @param ref_ The reference attribute.
694      * @param hashtable_ The name of the hashtable.
695      */

696     public void readAssociationAttributeMethod(
697         String JavaDoc package_name_,
698         javax.jmi.model.Namespace class_,
699         javax.jmi.model.Reference ref_,
700         String JavaDoc hashtable_) {
701         String JavaDoc className = JMIProvider.findSubstituteName(class_);
702         if (className == null)
703             className = class_.getName();
704         className = JMIProvider.classNameFormat(className);
705         String JavaDoc refName = JMIProvider.findSubstituteName(ref_);
706         if (refName == null)
707             refName = ref_.getName();
708         refName = JMIProvider.classNameFormat(refName);
709         String JavaDoc typeName = class_.getName();
710         String JavaDoc signature = "public void read" + JMIProvider.jmiFormat1(className) + JMIProvider.jmiFormat1(refName) + "(";
711         if (!_associationAttributes.contains(signature)) {
712             outputln("/**");
713             outputln(" * Read the '" + JMIProvider.jmiFormat1(refName) + "' attribute of the " + JMIProvider.jmiFormat1(className) + ".");
714             outputln(" * @param parent The parent of the current element in the XMI file.");
715             outputln(" * @param model The " + JMIProvider.jmiFormat1(className) + " to set.");
716             outputln(" * @param element The current element in the XMI file.");
717             outputln(" */");
718             _associationAttributes.add(signature);
719             output(signature);
720             outputln(
721                 "org.w3c.dom.Element parent, "
722                     + JMIProvider.jmiClassQualifiedName((MofClass) class_)
723                     + " model, org.w3c.dom.Element element) {");
724
725             //// If generation problem with the '_' character : use '_helper.format#(refName)' instead of 'refName'.
726
outputln(
727                 "if (element != null && element.getNodeName().trim().endsWith(\""
728                     + JMIProvider.jmiFormat1(typeName)
729                     + "."
730                     + XMIIOHelper.format1FirstMin(ref_.getName())
731                     + "\")) {");
732             outputln("org.w3c.dom.NodeList _child = element.getChildNodes();");
733             if (ref_.getType().isAbstract()) {
734                 outputln(
735                     "org.objectweb.modfact.jmi.xmiio.mofobject.MOFObjectList list = _xmi_import_classes."
736                         + XMIIOHelper.format1FirstMin(JMIProvider.classNameFormat(ref_.getType().getName()))
737                         + "Template(element);");
738                 outputln("java.util.Iterator it = list.getObjects();");
739                 outputln("while (it.hasNext())");
740                 if (ref_.getMultiplicity().getUpper() == 1) {
741                     outputln(
742                         TABULATION
743                             + "model.set"
744                             + JMIProvider.jmiFormat1(ref_.getName())
745                             + "(("
746                             + JMIProvider.jmiClassifierQualifiedName(ref_.getType())
747                             + ")it.next());");
748                 } else {
749                     outputln(
750                         TABULATION
751                             + "model.get"
752                             + JMIProvider.jmiFormat1(ref_.getName())
753                             + "().add(("
754                             + JMIProvider.jmiClassifierQualifiedName(ref_.getType())
755                             + ")it.next());");
756                 }
757                 outputln("it = list.getXmiIdRef();");
758                 outputln("while (it.hasNext())");
759                 outputln(
760                     "org.objectweb.modfact.jmi.helper.XMIIOHelper.addHashtableValue("
761                         + hashtable_
762                         + ", model, (java.lang.String)it.next());");
763             } else {
764                 outputln("int i = 0;");
765                 outputln("while (i<_child.getLength() || (_child.getLength()==0 && i==0)) {");
766                 outputln("org.w3c.dom.Element _current = (org.w3c.dom.Element) _child.item(i);");
767                 outputln("if (_current != null) {");
768                 String JavaDoc variableType = JMIProvider.jmiClassifierQualifiedName(ref_.getType());
769                 String JavaDoc ref_type_name = JMIProvider.findSubstituteName(ref_.getType());
770                 if (ref_type_name == null)
771                     ref_type_name = JMIProvider.classNameFormat(ref_.getType().getName());
772                 outputln(
773                     "org.objectweb.modfact.jmi.xmiio.mofobject.MOFObject mof_obj = _xmi_import_classes."
774                         + XMIIOHelper.format1FirstMin(ref_type_name)
775                         + "Template(_current);");
776                 outputln("if (mof_obj.isAnObject())");
777                 if (ref_.getMultiplicity().getUpper() == 1) {
778                     outputln(TABULATION + "model.set" + JMIProvider.jmiFormat1(ref_.getName()) + "((" + variableType + ")mof_obj.get());");
779                 } else {
780                     outputln(
781                         TABULATION + "model.get" + JMIProvider.jmiFormat1(ref_.getName()) + "().add((" + variableType + ")mof_obj.get());");
782                 }
783                 outputln("else if (mof_obj.isValid())");
784                 outputln(
785                     TABULATION
786                         + "org.objectweb.modfact.jmi.helper.XMIIOHelper.addHashtableValue("
787                         + hashtable_
788                         + ", model, mof_obj.get());");
789                 outputln("}");
790                 outputln("i++;");
791                 outputln("}");
792             }
793             outputln("}");
794             outputln("}");
795             outputln();
796         }
797     }
798
799     /**
800      * XMIImport generation for the composite association attribute method.
801      * @param package_name_ The package name.
802      * @param class_ The class.
803      * @param subclass_ The subclass.
804      * @param attribute_name_ The attribute name.
805      */

806     public void readCompositeAssociationAttributeMethod(
807         String JavaDoc package_name_,
808         javax.jmi.model.MofClass class_,
809         javax.jmi.model.MofClass[] subclass_,
810         String JavaDoc attribute_name_) {
811         String JavaDoc className = JMIProvider.jmiClassName(class_);
812         outputln("/**");
813         outputln(
814             " * Read the '" + JMIProvider.jmiFormat1(attribute_name_) + "' attribute of the " + JMIProvider.jmiFormat1(className) + ".");
815         outputln(" * @param parent The parent of the current element in the XMI file.");
816         outputln(" * @param model The " + JMIProvider.jmiFormat1(attribute_name_) + " to set.");
817         outputln(" * @param element The current element in the XMI file.");
818         outputln(" */");
819         output(
820             "public void read"
821                 + JMIProvider.classNameFormat(className)
822                 + JMIProvider.jmiFormat1(JMIProvider.attributNameFormat(attribute_name_))
823                 + "(");
824         outputln("org.w3c.dom.Element parent, org.w3c.dom.Element element) {");
825         outputln(
826             "if (element != null && element.getNodeName().trim().endsWith(\""
827                 + JMIProvider.jmiClassQualifiedName(class_)
828                 + "."
829                 + JMIProvider.jmiFormat1(attribute_name_)
830                 + "\")) {");
831         outputln("org.w3c.dom.NodeList _child = element.getChildNodes();");
832         outputln("int i = 0;");
833         outputln("while (i<_child.getLength() || (_child.getLength()==0 && i==0)) {");
834         outputln("org.w3c.dom.Element _current = (org.w3c.dom.Element) _child.item(i);");
835         outputln("java.lang.String _current_name = _current.getNodeName().trim();");
836         for (int i = 0; i < subclass_.length; i++) {
837             if (i > 0)
838                 output("else ");
839             String JavaDoc subClassName = JMIProvider.jmiClassName(subclass_[i]);
840             outputln("if (_current_name.endsWith(\"" + JMIProvider.jmiFormat1(subClassName) + "\"))");
841             outputln(" _xmi_import_classes." + XMIIOHelper.format1FirstMin(subClassName) + "Template(_current);");
842         }
843         outputln("i++;");
844         outputln("}");
845         outputln("}");
846         outputln("}");
847         outputln();
848     }
849
850     /**
851      * XMIImport generation for the "put" methods.
852      * @param package_name_ The package name.
853      * @param associations_ The associations of the package.
854      */

855     public void putAssociationsValues(String JavaDoc package_name_, Vector JavaDoc associations_) {
856         for (int i = 0; i < associations_.size(); i++) {
857             javax.jmi.model.Association asso = (javax.jmi.model.Association) associations_.elementAt(i);
858             try {
859                 javax.jmi.model.Reference[] refs = XMIIOHelper.getReferences(asso);
860                 for (int j = 0; j < refs.length; j++) {
861                     javax.jmi.model.AssociationEnd assoEnd = refs[j].getReferencedEnd();
862                     if (assoEnd.getAggregation() == javax.jmi.model.AggregationKindEnum.forName("none") && assoEnd.isChangeable()) {
863                     //if (assoEnd.isChangeable()) {
864
String JavaDoc hashtable =
865                             "_"
866                                 + JMIProvider.jmiFormat2(JMIProvider.jmiAssociationName(asso))
867                                 + "_"
868                                 + JMIProvider.jmiFormat2(refs[j].getName());
869                         String JavaDoc containerName = JMIProvider.findSubstituteName(refs[j].getContainer());
870                         if (containerName == null)
871                             containerName = JMIProvider.jmiFormat1(refs[j].getContainer().getName());
872                         String JavaDoc type = JMIProvider.jmiClassQualifiedName((MofClass) refs[j].getContainer());
873                         String JavaDoc typeName = refs[j].getContainer().getName();
874                         String JavaDoc typeClass = type;
875                         String JavaDoc ref = refs[j].getName();
876                         String JavaDoc typeRef = JMIProvider.jmiClassifierQualifiedName(refs[j].getReferencedEnd().getType());
877                         String JavaDoc vector = JMIProvider.jmiFormat2(ref) + "_vector";
878                         String JavaDoc type2 = JMIProvider.jmiFormat2(type);
879                         String JavaDoc ref1 = JMIProvider.jmiFormat1(ref);
880                         String JavaDoc ref2 = JMIProvider.jmiFormat2(ref);
881                         annotationTemplate(asso.getAnnotation());
882                         outputln("public void put" + JMIProvider.jmiFormat1(typeName) + JMIProvider.jmiFormat1(ref) + "() {");
883                         outputln("java.util.Enumeration _keys = " + hashtable + ".keys();");
884                         outputln("while (_keys.hasMoreElements()) {");
885                         outputln(typeClass + " _" + type2 + " = (" + typeClass + ") _keys.nextElement();");
886                         outputln("java.util.Vector _" + vector + " = (java.util.Vector) " + hashtable + ".get(_" + type2 + ");");
887                         outputln("for (int i=0 ; i<_" + vector + ".size() ; i++) {");
888                         outputln("java.lang.String _key = ((java.lang.String) _" + vector + ".elementAt(i)).trim();");
889                         outputln(typeRef + " _value = (" + typeRef + ") _types.get(_key);");
890                         if (refs[j].getMultiplicity().getUpper() == 1) {
891                             if (refs[j].getMultiplicity().getLower() == 0) {
892                                 outputln("try {");
893                                 outputln("if (_value != null && !_value.equals(_" + type2 + ".get" + ref1 + "()))");
894                                 outputln(" _" + type2 + ".set" + ref1 + "(_value);");
895                                 outputln("} catch (NullPointerException notSet) {");
896                                 outputln("_" + type2 + ".set" + ref1 + "(_value);");
897                                 outputln("}");
898                             } else {
899                                 outputln(
900                                     "if (_"
901                                         + type2
902                                         + ".get"
903                                         + ref1
904                                         + "() == null || (_value != null && !_value.equals(_"
905                                         + type2
906                                         + ".get"
907                                         + ref1
908                                         + "()))) {");
909                                 outputln("_" + type2 + ".set" + ref1 + "(_value);");
910                                 outputln("}");
911                             }
912                         } else {
913                             outputln("if (_value != null && !(_" + type2 + ".get" + ref1 + "().contains(_value))) {");
914                             outputln("_" + type2 + ".get" + ref1 + "().add(_value);");
915                             outputln("}");
916                         }
917                         if (refs.length > 1
918                             && refs[(j + 1) % 2].getReferencedEnd().getAggregation() == javax.jmi.model.AggregationKindEnum.forName("none")) {
919                             // There is j=0 or j=1, so, to switch between the values, we can use: (j+1)%2
920
if (refs[(j + 1) % 2].getMultiplicity().getUpper() == 1)
921                                 outputln(
922                                     "_value.set"
923                                         + JMIProvider.jmiFormat1(refs[(j + 1) % 2].getName())
924                                         + "(_"
925                                         + JMIProvider.jmiFormat2(type)
926                                         + ");");
927                             else
928                                 outputln(
929                                     "_value.get"
930                                         + JMIProvider.jmiFormat1(refs[(j + 1) % 2].getName())
931                                         + "().add(_"
932                                         + JMIProvider.jmiFormat2(type)
933                                         + ");");
934                         }
935                         outputln("}");
936                         outputln("}");
937                         outputln("}");
938                         outputln();
939                     }
940                 }
941             } catch (NullPointerException JavaDoc notSet) {
942                 // Nothing to do
943
}
944         }
945     }
946
947 }
948
Popular Tags