1 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 ; 27 import org.omg.CORBA.TypeCodePackage.Bounds ; 28 import org.omg.mof.Reflective.MofError; 29 import org.omg.mof.Reflective.NotSet; 30 31 35 public class GenerationXMIImportAttributes extends AbstractXMIIOGenerator { 36 37 38 protected ModFactLogger _logger = ModFactLogger.getLogger("fr.lip6.src.mdafactory.generation.xmiimport.GenerationXMIImport"); 39 40 41 private java.util.List _associationAttributes; 42 43 46 public GenerationXMIImportAttributes() { 47 super(); 48 ModFactLogger logger = ModFactLogger.getLogger("fr.lip6.src.mdafactory.generation.xmiimport.GenerationXMIImportApplication"); 49 _associationAttributes = new java.util.ArrayList (); 50 } 51 52 57 public void generateXMIImportAttributes(org.omg.mof.Model.Package package_, String class_name_) 58 throws 59 org.omg.mof.Reflective.MofError, 60 org.omg.CORBA.TypeCodePackage.BadKind , 61 org.omg.CORBA.TypeCodePackage.Bounds , 62 java.io.FileNotFoundException { 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 74 public void constructor(String class_name, String 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 108 public void packageModuleTemplate(org.omg.mof.Model.Package package_, String class_name_) 109 throws org.omg.mof.Reflective.MofError, org.omg.CORBA.TypeCodePackage.BadKind , org.omg.CORBA.TypeCodePackage.Bounds { 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 135 public void classAttributes(org.omg.mof.Model.Package package_) throws org.omg.mof.Reflective.MofError { 136 String 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 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 155 public void associationAttributes(String 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 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 } 177 } 178 outputln(); 179 } 180 181 185 public void initiateMethod(org.omg.mof.Model.Package package_) throws org.omg.mof.Reflective.MofError { 186 String package_name = package_.name(); 187 _logger.log(Level.FINE, "XMIImport initiateMethod(" + package_name + ")"); 188 String 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 208 public void readingAttributes(String package_name_, org.omg.mof.Model.Class classes_[]) 209 throws org.omg.mof.Reflective.MofError, org.omg.CORBA.TypeCodePackage.BadKind , org.omg.CORBA.TypeCodePackage.Bounds { 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 for (int i = 0; i < classes_.length; i++) { 217 org.omg.mof.Model.Class cls = (org.omg.mof.Model.Class) classes_[i]; 218 String 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 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 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 associatedTags = new java.util.ArrayList (); 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 } 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 } 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 } 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 (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 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 } 308 } 309 } 310 } 311 } 312 313 319 public void readClassAttributeMethod( 320 String package_name_, 321 org.omg.mof.Model.Class class_, 322 org.omg.mof.Model.MofAttribute attribute_, 323 String type_name_, 324 org.omg.CORBA.TypeCode type_) 325 throws BadKind , Bounds , MofError, NotSet { 326 _logger.log( 327 Level.FINE, 328 "XMIImport readClassAttributeMethod(" + package_name_ + ", " + class_.name() + ", " + attribute_.name() + ", " + type_ + ")"); 329 String 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 operation = "add_"; 335 if (attribute_.multiplicity().upper == 1) 336 operation = "set_"; 337 String attrName = attribute_.name(); 338 String 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 (type_ != null && idlHelper.isBaseType(type_)) { 350 String 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 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 382 public void readClassTypeCodeAttributeMethod(String package_name_, org.omg.mof.Model.Class class_, String attribute_name_) 383 throws MofError, NotSet { 384 String 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 415 public void readClassEnumerationTypeAttributeMethod( 416 String package_name_, 417 org.omg.mof.Model.Class class_, 418 org.omg.mof.Model.MofAttribute attribute_, 419 org.omg.CORBA.TypeCode type_code_, 420 java.util.List associated_tags_) 421 throws org.omg.mof.Reflective.MofError, org.omg.CORBA.TypeCodePackage.BadKind , org.omg.CORBA.TypeCodePackage.Bounds { 422 String attrName = attribute_.name(); 423 String 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 operation = "add_"; 459 if (attribute_.multiplicity().upper == 1) 460 operation = "set_"; 461 462 String prefix = new String (); 464 java.util.Iterator 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 [] 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 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 504 public void readClassStructureTypeAttributeMethod( 505 String package_name_, 506 org.omg.mof.Model.Class class_, 507 org.omg.mof.Model.MofAttribute attribute_, 508 org.omg.CORBA.TypeCode type_code_) 509 throws org.omg.mof.Reflective.MofError, org.omg.CORBA.TypeCodePackage.BadKind , org.omg.CORBA.TypeCodePackage.Bounds { 510 try { 511 String attrName = attribute_.name(); 512 String classQualifiedName = javaHelper.javaQualifiedName(class_); 513 String 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 } 585 } 586 587 594 public void readClassAliasTypeMethod( 595 String package_name_, 596 org.omg.mof.Model.Class cls, 597 org.omg.mof.Model.MofAttribute attribute_, 598 org.omg.CORBA.TypeCode content_type_) 599 throws MofError, NotSet { 600 _logger.log(Level.FINE, "XMIImport readClassAliasType(" + package_name_ + ", " + attribute_.name() + ")"); 601 String attrName = attribute_.name(); 602 String attrName4 = idlHelper.format1FirstMin(attrName); 603 String 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 operation = "add_"; 614 if (attribute_.multiplicity().upper == 1) 615 operation = "set_"; 616 outputln("if (parent.hasAttribute(\"" + attrName4 + "\"))"); 617 618 String 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 634 public String typeCode2ReadingMethod(org.omg.CORBA.TypeCode 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 665 public void readAssociationAttributeMethod( 666 String package_name_, 667 org.omg.mof.Model.Namespace class_, 668 org.omg.mof.Model.Reference ref_, 669 String hashtable_) 670 throws MofError, NotSet { 671 String className = class_.name(); 672 String refName = ref_.name(); 673 String typeName = class_.name(); 674 _logger.log( 675 Level.FINE, 676 "XMIImport readAssociationAttributeMethod(" + package_name_ + ", " + className + ", " + refName + ", " + hashtable_ + ")"); 677 String 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 outputln( 693 "if (element != null && element.getNodeName().trim().endsWith(\"" + idlHelper.format1(typeName) + "." + refName + "\")) {"); 694 outputln("org.w3c.dom.NodeList _child = element.getChildNodes();"); 695 String 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 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 747 public void readCompositeAssociationAttributeMethod( 748 String package_name_, 749 org.omg.mof.Model.Class class_, 750 org.omg.mof.Model.Class[] subclass_, 751 String 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 802 public void putAssociationsValues(String 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 hashtable = "_" + idlHelper.format2(asso.name()) + "_" + idlHelper.format2(refs[j].name()); 813 String type = javaHelper.javaQualifiedName(refs[j].container()); 814 String typeName = refs[j].container().name(); 815 String typeClass = type; 816 String ref = refs[j].name(); 817 String typeRef = javaHelper.javaQualifiedName(refs[j].referenced_end().type()); 818 String vector = idlHelper.format2(ref) + "_vector"; 819 String type2 = idlHelper.format2(type); 820 String 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 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 } 883 } 884 } 885 886 } 887 | Popular Tags |