1 20 package org.objectweb.modfact.jmi.xmiio.importer; 21 22 import java.util.Iterator ; 23 import java.util.Vector ; 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 36 public class GenerationXMIImportAttributes extends AbstractXMIIOGenerator { 37 38 39 private java.util.List _associationAttributes; 40 41 42 private java.util.Vector classes = new java.util.Vector (); 43 44 45 private java.util.Vector associations = new java.util.Vector (); 46 47 50 public GenerationXMIImportAttributes() { 51 super(); 52 _associationAttributes = new java.util.ArrayList (); 53 } 54 55 60 public void generateXMIImportAttributes(javax.jmi.model.MofPackage package_, String class_name_) throws java.io.FileNotFoundException { 61 packageModuleTemplate(package_, class_name_); 62 out.flush(); 63 } 64 65 70 public void constructor(String class_name, String 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 94 private void processContents(javax.jmi.model.MofPackage package_) { 95 java.util.List contentsList = package_.getContents(); 96 Iterator 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 115 public void packageModuleTemplate(javax.jmi.model.MofPackage package_, String class_name_) { 116 if (package_.getVisibility() == javax.jmi.model.VisibilityKindEnum.forName("public_vis")) { 117 processContents(package_); 118 119 java.util.Vector classes_ordered = new java.util.Vector (); 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 supertypes = courant.getSupertypes(); 125 boolean is_top = true; 126 Iterator 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 packageName = JMIProvider.jmiPackageExtentName(package_); 139 String 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 160 public void classAttributes(String package_name_, MofPackage package_) { 161 outputln("// Factories Declaration"); 162 String 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 173 public void associationAttributes(String package_name_, Vector 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 notSet) { 188 } 190 } 191 outputln(); 192 } 193 194 199 public void initiateMethod(String package_name_, MofPackage package_) { 200 String 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 214 public void readingAttributes(String package_name_, Vector classes_) { 215 outputln("// =================================================================="); 216 outputln("// readXXXYYY with XXX an entity and YYY its property"); 217 outputln("// =================================================================="); 218 outputln(); 219 for (int i = 0; i < classes_.size(); i++) { 221 javax.jmi.model.MofClass cls = (javax.jmi.model.MofClass) classes_.elementAt(i); 222 String clsName = JMIProvider.jmiClassName(cls); 223 java.util.List contents = cls.getContents(); 224 Iterator 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 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 associatedTags = new java.util.ArrayList (); 235 try { 236 java.util.List clsContents = type.getContainer().getContents(); 237 Iterator 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 elements = tag.getElements(); 243 Iterator 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 notSet) { 252 } 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 } 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 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 notSet) { 295 notSet.printStackTrace(); 296 } 297 } 298 } 299 } catch (NullPointerException notSet) { 300 } 302 } 303 } 304 } 305 } 306 307 314 public void readClassAttributeMethod( 315 String package_name_, 316 javax.jmi.model.MofClass class_, 317 javax.jmi.model.Attribute attribute_, 318 javax.jmi.model.Classifier type_) { 319 String type_name_ = type_.getName(); 320 String type = JMIProvider.jmiFormat1(type_name_); 321 String attrName = JMIProvider.findSubstituteName(attribute_); 322 if (attrName == null) 323 attrName = attribute_.getName(); 324 String attrName4 = XMIIOHelper.format1FirstMin(attrName); 325 String 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 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 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 paramType = JMIProvider.jmiClassifierQualifiedName(attribute_.getType()); 377 String 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 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 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 453 public void readClassTypeCodeAttributeMethod(String package_name_, javax.jmi.model.MofClass class_, String attribute_name_) { 454 String 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 482 public void readClassEnumerationTypeAttributeMethod( 483 String package_name_, 484 javax.jmi.model.MofClass class_, 485 javax.jmi.model.Attribute attribute_, 486 javax.jmi.model.EnumerationType type, 487 java.util.List associated_tags_) { 488 String attrName = attribute_.getName(); 489 String attrName4 = XMIIOHelper.format1FirstMin(attrName); 490 String 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 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 String prefix = new String (); 521 java.util.Iterator 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 values = tag.getValues(); 526 Iterator itValues = values.iterator(); 527 while (itValues.hasNext()) { 528 Object v = itValues.next(); 529 if (v instanceof String ) 530 prefix = v.toString(); 531 } 532 } 533 } 534 Iterator itLabels = type.getLabels().iterator(); 535 while (itLabels.hasNext()) { 536 String label = (String ) itLabels.next(); 537 String 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 560 public void readClassStructureTypeAttributeMethod( 561 String package_name_, 562 javax.jmi.model.MofClass class_, 563 javax.jmi.model.Attribute attribute_) { 564 String attrName = attribute_.getName(); 565 String 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 599 public void readClassAliasTypeMethod( 600 String package_name_, 601 javax.jmi.model.MofClass cls, 602 javax.jmi.model.Attribute attribute_, 603 org.omg.CORBA.TypeCode content_type_) { 604 String attrName = attribute_.getName(); 605 String attrName4 = XMIIOHelper.format1FirstMin(attrName); 606 String attrName1 = JMIProvider.jmiFormat1(attrName); 607 String attrName2 = JMIProvider.jmiFormat2(attrName); 608 String 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 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 643 public String typeCode2ReadingMethod(org.omg.CORBA.TypeCode 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 672 public String primitiveTypeToReadMethod(javax.jmi.model.PrimitiveType type_) { 673 String 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 ("unknown primitive type: " + type_.getName()); 686 } 687 } 688 689 696 public void readAssociationAttributeMethod( 697 String package_name_, 698 javax.jmi.model.Namespace class_, 699 javax.jmi.model.Reference ref_, 700 String hashtable_) { 701 String className = JMIProvider.findSubstituteName(class_); 702 if (className == null) 703 className = class_.getName(); 704 className = JMIProvider.classNameFormat(className); 705 String refName = JMIProvider.findSubstituteName(ref_); 706 if (refName == null) 707 refName = ref_.getName(); 708 refName = JMIProvider.classNameFormat(refName); 709 String typeName = class_.getName(); 710 String 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 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 variableType = JMIProvider.jmiClassifierQualifiedName(ref_.getType()); 769 String 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 806 public void readCompositeAssociationAttributeMethod( 807 String package_name_, 808 javax.jmi.model.MofClass class_, 809 javax.jmi.model.MofClass[] subclass_, 810 String attribute_name_) { 811 String 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 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 855 public void putAssociationsValues(String package_name_, Vector 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 String hashtable = 865 "_" 866 + JMIProvider.jmiFormat2(JMIProvider.jmiAssociationName(asso)) 867 + "_" 868 + JMIProvider.jmiFormat2(refs[j].getName()); 869 String containerName = JMIProvider.findSubstituteName(refs[j].getContainer()); 870 if (containerName == null) 871 containerName = JMIProvider.jmiFormat1(refs[j].getContainer().getName()); 872 String type = JMIProvider.jmiClassQualifiedName((MofClass) refs[j].getContainer()); 873 String typeName = refs[j].getContainer().getName(); 874 String typeClass = type; 875 String ref = refs[j].getName(); 876 String typeRef = JMIProvider.jmiClassifierQualifiedName(refs[j].getReferencedEnd().getType()); 877 String vector = JMIProvider.jmiFormat2(ref) + "_vector"; 878 String type2 = JMIProvider.jmiFormat2(type); 879 String ref1 = JMIProvider.jmiFormat1(ref); 880 String 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 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 notSet) { 942 } 944 } 945 } 946 947 } 948 | Popular Tags |