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.GeneralizableElement; 26 import javax.jmi.model.MofPackage; 27 import javax.jmi.model.VisibilityKindEnum; 28 29 import org.objectweb.modfact.jmi.helper.JMIProvider; 30 import org.objectweb.modfact.jmi.helper.MofHelper; 31 import org.objectweb.modfact.jmi.helper.XMIIOHelper; 32 import org.objectweb.modfact.jmi.xmiio.common.AbstractXMIIOGenerator; 33 34 38 public class GenerationXMIImportClasses extends AbstractXMIIOGenerator { 39 40 41 private java.util.List _associationAttributes; 42 43 44 private java.util.Vector classes = new java.util.Vector (); 45 46 49 public GenerationXMIImportClasses() { 50 super(); 51 _associationAttributes = new java.util.ArrayList (); 52 } 53 54 59 public void generateXMIImportClasses(MofPackage package_, String class_name_) 60 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 XMIImportAttributes class. */"); 72 outputln(package_name + "XMIImportAttributes _xmi_import_attributes;"); 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(" * @param types The defined types."); 80 outputln(" */"); 81 outputln("public " + class_name + "(java.util.Hashtable types) {"); 82 outputln("_types = types;"); 83 outputln("}"); 84 outputln(); 85 outputln("public void setXMIImportAttributes(" + package_name + "XMIImportAttributes xmi_import_attributes) {"); 86 outputln("_xmi_import_attributes = xmi_import_attributes;"); 87 outputln("}"); 88 outputln(); 89 } 90 91 95 private void processContents(MofPackage package_) { 96 java.util.List contentsList = package_.getContents(); 97 Iterator it = contentsList.iterator(); 98 while (it.hasNext()) { 99 javax.jmi.model.ModelElement contents = (javax.jmi.model.ModelElement) it.next(); 100 if (contents instanceof javax.jmi.model.MofPackage) 101 processContents((javax.jmi.model.MofPackage) contents); 102 else if (contents instanceof javax.jmi.model.Import) 103 processContents((javax.jmi.model.MofPackage)((javax.jmi.model.Import) contents).getImportedNamespace()); 104 else if (contents instanceof javax.jmi.model.MofClass) 105 classes.addElement((javax.jmi.model.MofClass) contents); 106 } 107 } 108 109 114 public void packageModuleTemplate(MofPackage package_, String class_name_) { 115 if (package_.getVisibility() == VisibilityKindEnum.forName("public_vis")) { 116 processContents(package_); 117 java.util.Vector classes_ordered = new java.util.Vector (); 118 while (classes.size() != 0) { 119 javax.jmi.model.MofClass courant = null; 120 for (int i = 0; i < classes.size(); i++) { 121 courant = (javax.jmi.model.MofClass) classes.elementAt(i); 122 java.util.List supertypes = courant.getSupertypes(); 123 boolean is_top = true; 124 Iterator stypesIt = supertypes.iterator(); 125 while (stypesIt.hasNext()) { 126 GeneralizableElement supertype = (GeneralizableElement) stypesIt.next(); 127 if (classes.contains(supertype)) 128 is_top = false; 129 } 130 if (is_top) { 131 classes_ordered.addElement(courant); 132 classes.removeElement(courant); 133 } 134 } 135 } 136 137 String packageName = JMIProvider.jmiPackageExtentName(package_); 138 String packageQualifiedName = JMIProvider.qualifierOf(package_); 139 outputln("package " + packageQualifiedName + ".xmi;"); 140 outputln(); 141 annotationTemplate(package_.getAnnotation()); 142 outputln("public class " + class_name_ + " extends org.objectweb.modfact.jmi.xmiio.importer.XMIImport {"); 143 classAttributes(packageName, package_, classes_ordered); 144 constructor(class_name_, packageName); 145 initiateMethod(packageName, package_, classes_ordered); 146 classTemplate(packageName, classes_ordered); 147 outputln("} // end of class " + class_name_); 148 outputln(); 149 } 150 } 151 152 158 public void classAttributes(String package_name_, MofPackage package_, Vector classes_) { 159 outputln("// Factories Declaration"); 160 String model_package_type; 161 model_package_type = JMIProvider.jmiPackageQualifiedName(package_) + "Package"; 162 outputln("private " + model_package_type + " _" + JMIProvider.jmiFormat2(package_name_) + "_package = null;"); 163 164 for (int i = 0; i < classes_.size(); i++) { 166 javax.jmi.model.MofClass cls = (javax.jmi.model.MofClass) classes_.elementAt(i); 167 String className = JMIProvider.jmiClassName(cls); 168 String javaName; 169 try { 170 javaName = JMIProvider.qualifierOf((MofPackage) cls.getContainer()) + "." + className; 171 } catch (NullPointerException notSet) { 172 javaName = className; 173 } 174 outputln( 175 "private " 176 + javaName 177 + "Class _" 178 + JMIProvider.jmiFormat2(JMIProvider.jmiClassQualifiedName(cls)) 179 + "_class = null;"); 180 } 181 outputln(); 182 } 183 184 190 public void initiateMethod(String package_name_, MofPackage package_, Vector classes_) { 191 String model_package_type; 192 model_package_type = JMIProvider.jmiPackageQualifiedName(package_) + "Package"; 193 outputln("public void initiate(" + model_package_type + " _model_package) {"); 194 outputln("// Factories creation"); 195 outputln("this._" + JMIProvider.jmiFormat2(package_name_) + "_package = _model_package;"); 196 initiateMethodClasses(package_, classes_); 198 outputln("}"); 199 outputln(); 200 } 201 202 207 public void initiateMethodClasses(MofPackage package_, Vector classes_) { 208 for (int i = 0; i < classes_.size(); i++) { 209 javax.jmi.model.MofClass cls = (javax.jmi.model.MofClass) classes_.elementAt(i); 210 String javaName = JMIProvider.jmiFormat2(JMIProvider.jmiClassQualifiedName(cls)); 211 String method = "get" + (JMIProvider.jmiClassName(cls)) + "()"; 212 213 try { 214 javax.jmi.model.ModelElement clsTmp = cls; 215 while (!clsTmp.getContainer().equals(package_)) { 216 method = "get" + JMIProvider.jmiPackageExtentName((MofPackage)clsTmp.getContainer()) + "()." + method; 217 clsTmp = clsTmp.getContainer(); 218 } 219 } catch (NullPointerException notSet) { 220 } 222 outputln("_" + javaName + "_class = _model_package." + method + ";"); 223 } 224 } 225 226 231 public void classTemplate(String package_name_, Vector classes_) { 232 outputln("// =================================================================="); 233 outputln("// XXXTemplate with XXX an entity"); 234 outputln("// =================================================================="); 235 for (int i = 0; i < classes_.size(); i++) { 237 javax.jmi.model.MofClass cls = (javax.jmi.model.MofClass) classes_.elementAt(i); 238 if (cls.isAbstract()) 239 classAbstractTemplate(cls); 240 else 241 classSpecificTemplate(cls); 242 outputln(); 243 } 244 outputln(); 245 } 246 247 251 public void classAbstractTemplate(javax.jmi.model.MofClass cls) { 252 String clsName = JMIProvider.jmiClassName(cls); 253 String clsName1 = JMIProvider.jmiFormat1(clsName); 254 String clsName2 = JMIProvider.jmiFormat2(clsName); 255 String javaName = JMIProvider.jmiClassQualifiedName(cls); 256 String methodName = clsName1.substring(0, 1).toLowerCase() + clsName1.substring(1); 257 String returnType = javaName; 258 try { 259 returnType = JMIProvider.qualifierOf((MofPackage) cls.getContainer()) + "." + clsName; 260 } catch (NullPointerException notSet) { 261 returnType = clsName; 262 } 263 String attributeName = "_" + clsName2 + "_element"; 264 annotationTemplate(cls.getAnnotation()); 265 outputln( 266 "public org.objectweb.modfact.jmi.xmiio.mofobject.MOFObjectList " 267 + methodName 268 + "Template(org.w3c.dom.Element " 269 + attributeName 270 + ") {"); 271 outputln("org.objectweb.modfact.jmi.xmiio.mofobject.MOFObjectList list = new org.objectweb.modfact.jmi.xmiio.mofobject.MOFObjectList();"); 272 outputln("// Get the " + clsName1 + " values"); 273 outputln("if (" + attributeName + " == null) return list;"); 274 outputln( 275 "if (" 276 + attributeName 277 + ".getChildNodes().getLength() == 0 && " 278 + attributeName 279 + ".hasAttribute(\"xmi.idref\")) {"); 280 outputln("// The " + clsName1 + " element is already read or it will be read (ref)."); 281 outputln("list.addXmiIdRef(" + attributeName + ".getAttribute(\"xmi.idref\"));"); 282 outputln("return list;"); 283 outputln("}"); 284 outputln("int i = 0;"); 285 outputln("org.w3c.dom.NodeList _child = " + attributeName + ".getChildNodes();"); 286 outputln("while (i<_child.getLength() || (_child.getLength()==0 && i==0)) {"); 287 outputln("org.w3c.dom.Element _current = (org.w3c.dom.Element) _child.item(i);"); 288 try { 289 javax.jmi.model.MofClass[] subtypes = XMIIOHelper.subClasses(cls); 290 for (int j = 0; j < subtypes.length; j++) { 291 if (containerContains(cls.getContainer(), subtypes[j].getContainer())) { 292 String subTypeXMLName = JMIProvider.jmiFormat1(subtypes[j].getName()); 293 String subTypeName = JMIProvider.jmiFormat1(JMIProvider.jmiClassName(subtypes[j])); 294 String subTypeTemplateMethod = subTypeName.substring(0, 1).toLowerCase() + subTypeName.substring(1); 295 outputln("if (_current != null && nodeNameCorrespondsTo(_current, \"" + subTypeXMLName + "\"))"); 296 if (subtypes[j].isAbstract()) 297 outputln(TABULATION + "list.addMOFObjectList(" + subTypeTemplateMethod + "Template(_current));"); 298 else 299 outputln(TABULATION + subTypeTemplateMethod + "Template(_current).addMOFObject(list);"); 300 output("else "); 301 } 302 } 303 } catch (NullPointerException notSet) { 304 } 306 outputln( 307 "if (_current != null && nodeNameCorrespondsTo(_current, \"" + JMIProvider.jmiFormat1(clsName) + "\"))"); 308 outputln(TABULATION + "list.addXmiIdRef(_current.getAttribute(\"xmi.idref\"));"); 309 outputln("else if (_current != null)"); 310 outputln( 311 TABULATION 312 + "System.err.println(\"" 313 + JMIProvider.jmiFormat1(clsName) 314 + " is an abstract type, it can't be instanciated (\" + _current.getNodeName() + \" encountered).\");"); 315 outputln("i++;"); 316 outputln("}"); 317 outputln("return list;"); 318 outputln("}"); 319 } 320 321 327 private boolean containerContains(javax.jmi.model.Namespace parent, javax.jmi.model.Namespace toSearch) { 328 if (parent == null || toSearch == null) 329 return false; 330 if (toSearch.refMofId().equals(parent.refMofId()) || containerContains(parent, toSearch.getContainer()) 331 || containerContains(parent.getContainer(), toSearch)) 332 return true; 333 334 java.util.List contents = parent.getContents(); 335 Iterator it = contents.iterator(); 336 boolean result = false; 337 while (it.hasNext()) { 338 Object element = it.next(); 339 javax.jmi.model.Import i; 340 if (element instanceof javax.jmi.model.Import) 341 result = containerContains(((javax.jmi.model.Import)element).getImportedNamespace() , toSearch); 342 } 343 return result; 344 } 345 346 350 public void classSpecificTemplate(javax.jmi.model.MofClass cls) { 351 String className = JMIProvider.jmiClassName(cls); 352 String javaName; 353 try { 354 javaName = JMIProvider.jmiPackageQualifiedName((MofPackage) cls.getContainer()) + "." + className; 355 } catch (NullPointerException notSet) { 356 javaName = className; 357 } 358 String clsName = JMIProvider.jmiClassName(cls); 359 String clsName1 = JMIProvider.jmiFormat1(clsName); 360 String clsName2 = JMIProvider.jmiFormat2(clsName); 361 String methodName = XMIIOHelper.format1FirstMin(clsName1); 362 String returnType = javaName; 363 try { 364 returnType = JMIProvider.qualifierOf((MofPackage) cls.getContainer()) + "." + clsName; 365 } catch (NullPointerException notSet) { 366 returnType = clsName; 367 } 368 String returnMethod = "org.objectweb.modfact.jmi.xmiio.mofobject.MOFObject"; 369 String attributeName = "_" + clsName2 + "_element"; 370 annotationTemplate(cls.getAnnotation()); 371 outputln("public " + returnMethod + " " + methodName + "Template(org.w3c.dom.Element " + attributeName + ") {"); 372 outputln("// Get the " + clsName1 + " values"); 373 outputln("if (" + attributeName + " == null)"); 374 outputln(TABULATION + "return new org.objectweb.modfact.jmi.xmiio.mofobject.NullObject();"); 375 outputln( 376 "if (" 377 + attributeName 378 + ".getChildNodes().getLength() == 0 && " 379 + attributeName 380 + ".hasAttribute(\"xmi.idref\")) {"); 381 outputln("// The " + clsName1 + " is already read or it will be read (ref)."); 382 outputln( 383 "return new org.objectweb.modfact.jmi.xmiio.mofobject.XMIIdRef(" 384 + attributeName 385 + ".getAttribute(\"xmi.idref\"));"); 386 outputln("}"); 387 outputln("// Default " + clsName1 + " creation"); 388 outputln(returnType + " _" + clsName2 + " = "); 389 output( 390 " _" 391 + JMIProvider.jmiFormat2(JMIProvider.jmiClassQualifiedName(cls)) 392 + "_class.create" 393 + clsName1 394 + "("); 395 396 javax.jmi.model.Attribute[] attributes = 397 MofHelper.attributesOfClass(cls, javax.jmi.model.ScopeKindEnum.forName("instance_level"), true); 398 outputln(");"); 399 outputln( 400 "_types.put(((java.lang.String)_" 401 + clsName2 402 + "_element.getAttribute(\"xmi.id\")).trim(), " 403 + "_" 404 + clsName2 405 + ");"); 406 outputln("int i = 0;"); 407 outputln("org.w3c.dom.NodeList _child = " + attributeName + ".getChildNodes();"); 408 outputln("while (i<_child.getLength() || (_child.getLength()==0 && i==0)) {"); 409 outputln("org.w3c.dom.Element _current = (org.w3c.dom.Element) _child.item(i);"); 410 String param = "_" + clsName2 + "_element, _" + clsName2 + ", _current"; 411 attributes = MofHelper.attributesOfClass(cls, javax.jmi.model.ScopeKindEnum.forName("instance_level"), true); 412 for (int j = 0; j < attributes.length; j++) { 413 javax.jmi.model.Attribute attr = (javax.jmi.model.Attribute) attributes[j]; 414 if (attr.isChangeable()) { 415 String attr_name = JMIProvider.findSubstituteName(attr); 416 if (attr_name == null) 417 attr_name = attr.getName(); 418 try { 419 String attr_container_name = JMIProvider.findSubstituteName(attr.getContainer()); 420 if (attr_container_name == null) 421 attr_container_name = attr.getContainer().getName(); 422 outputln( 423 "_xmi_import_attributes.read" 424 + JMIProvider.classNameFormat(attr_container_name) 425 + JMIProvider.classNameFormat(attr_name) 426 + "(" 427 + param 428 + ");"); 429 } catch (NullPointerException notSet) { 430 outputln("_xmi_import_attributes.read" + JMIProvider.classNameFormat(attr_name) + "(" + param + ");"); 431 } 432 } 433 } 434 javax.jmi.model.Reference[] references = MofHelper.referencesOfClass(cls, true); 435 for (int j = 0; j < references.length; j++) { 436 javax.jmi.model.Reference ref = (javax.jmi.model.Reference) references[j]; 437 javax.jmi.model.AssociationEnd assoEnd = ref.getReferencedEnd(); 438 if (assoEnd.getAggregation() == javax.jmi.model.AggregationKindEnum.forName("none")) { 439 String ref_name = JMIProvider.findSubstituteName(ref); 440 if (ref_name == null) 441 ref_name = ref.getName(); 442 try { 443 String ref_container_name = JMIProvider.findSubstituteName(ref.getContainer()); 444 if (ref_container_name == null) 445 ref_container_name = ref.getContainer().getName(); 446 outputln( 447 "_xmi_import_attributes.read" 448 + JMIProvider.classNameFormat(ref_container_name) 449 + JMIProvider.classNameFormat(ref_name) 450 + "(" 451 + param 452 + ");"); 453 } catch (NullPointerException notSet) { 454 outputln("_xmi_import_attributes.read" + JMIProvider.classNameFormat(ref_name) + "(" + param + ");"); 455 } 456 } else if (assoEnd.getAggregation() == javax.jmi.model.AggregationKindEnum.forName("composite")) { 457 try { 458 javax.jmi.model.AssociationEnd otherEnd = XMIIOHelper.otherAssociationEnd(assoEnd); 459 javax.jmi.model.Reference[] refs = 460 XMIIOHelper.getReferences((javax.jmi.model.Association) assoEnd.getContainer()); 461 for (int k = 0; k < refs.length; k++) { 462 if (refs[k] != ref) { 463 String ref_type_name = JMIProvider.findSubstituteName(refs[k].getType()); 464 if (ref_type_name == null) 465 ref_type_name = refs[k].getType().getName(); 466 String asso_end_name = JMIProvider.findSubstituteName(assoEnd); 467 if (asso_end_name == null) 468 asso_end_name = assoEnd.getName(); 469 outputln( 470 "_xmi_import_attributes.read" 471 + JMIProvider.classNameFormat(ref_type_name) 472 + JMIProvider.classNameFormat(asso_end_name) 473 + "(" 474 + param 475 + ");"); 476 } 477 } 478 } catch (NullPointerException notSet) { 479 } 481 } 482 } 483 outputln("i++;"); 484 outputln("}"); 485 outputln("return new org.objectweb.modfact.jmi.xmiio.mofobject.BuiltObject(_" + clsName2 + ");"); 486 outputln("}"); 487 } 488 489 } 490 | Popular Tags |