1 20 package org.objectweb.modfact.jmi.xmiio.exporter; 21 22 import java.util.List ; 23 import java.util.Vector ; 24 25 import javax.jmi.model.Attribute; 26 import javax.jmi.model.Classifier; 27 import javax.jmi.model.DataType; 28 import javax.jmi.model.EnumerationType; 29 import javax.jmi.model.Import; 30 import javax.jmi.model.ModelElement; 31 import javax.jmi.model.MofClass; 32 import javax.jmi.model.MofPackage; 33 import javax.jmi.model.Namespace; 34 import javax.jmi.model.PrimitiveType; 35 import javax.jmi.model.Reference; 36 import javax.jmi.model.ScopeKindEnum; 37 import javax.jmi.model.VisibilityKindEnum; 38 39 import org.objectweb.modfact.jmi.helper.JMIProvider; 40 import org.objectweb.modfact.jmi.helper.MofHelper; 41 import org.objectweb.modfact.jmi.helper.XMIIOHelper; 42 import org.objectweb.modfact.jmi.logging.Level; 43 import org.objectweb.modfact.jmi.logging.ModFactLogger; 44 import org.objectweb.modfact.jmi.xmiio.common.AbstractXMIIOGenerator; 45 46 50 public class GenerationXMIExport extends AbstractXMIIOGenerator { 51 52 53 protected ModFactLogger _logger = ModFactLogger.getLogger("fr.lip6.src.mdafactory.generation.xmiio.exporter.GenerationXMIExport"); 54 55 58 public GenerationXMIExport() { 59 super(); 60 ModFactLogger logger = ModFactLogger.getLogger("fr.lip6.src.mdafactory.generation.xmiio.exporter.GenerationXMIExportApplication"); 61 } 62 63 67 public void generateXMIExport(MofPackage package_, String className) throws java.io.IOException { 68 _logger.log(Level.FINE, "XMIExport beginning of the generation"); 69 _logger.log(Level.FINE, className + " generation ..."); 70 packageModuleTemplate(package_, className); 71 flushFile(); 72 } 73 74 java.util.Vector classes = new java.util.Vector (); 75 java.util.Vector dataTypes = new java.util.Vector (); 76 77 private void processContents(MofPackage package_) { 78 java.util.List contentsList = package_.getContents(); 79 java.util.Iterator it = contentsList.iterator(); 80 while (it.hasNext()) { 81 javax.jmi.model.ModelElement contents = (javax.jmi.model.ModelElement) it.next(); 82 if (contents instanceof javax.jmi.model.MofPackage) 83 processContents((javax.jmi.model.MofPackage) contents); 84 else if (contents instanceof javax.jmi.model.Import) 85 processContents((javax.jmi.model.MofPackage) ((javax.jmi.model.Import) contents).getImportedNamespace()); 86 else if (contents instanceof javax.jmi.model.MofClass) 87 classes.addElement((javax.jmi.model.MofClass) contents); 88 else if (contents instanceof javax.jmi.model.DataType) 89 dataTypes.addElement((javax.jmi.model.DataType) contents); 90 } 91 } 92 93 98 protected java.util.Vector findAllClassAttributes(MofPackage package_) { 99 MofClass[] contents = XMIIOHelper.allClassesOfPackageOrderedByInheritance(package_); 100 java.util.Vector attributes = new java.util.Vector (); 101 for (int i = 0; i < contents.length; i++) { 102 if (contents[i] instanceof MofClass) { 103 MofClass class_ = (MofClass) contents[i]; 104 Reference references[] = MofHelper.referencesOfClass(class_, true); 105 for (int l = 0; l < references.length; l++) { 106 Classifier classifier = references[l].getType(); 107 if (!attributes.contains(classifier)) 108 attributes.add(classifier); 109 } 110 Attribute attr[] = MofHelper.attributesOfClass(class_, ScopeKindEnum.forName("instance_level"), true); 111 for (int l = 0; l < attr.length; l++) { 112 Classifier classifier = attr[l].getType(); 113 if (classifier instanceof MofClass && !attributes.contains(classifier)) 114 attributes.add(classifier); 115 } 116 } else if (contents[i] instanceof Import) { 117 Import imp = (Import) contents[i]; 118 java.util.Vector call = findAllClassAttributes((MofPackage) imp.getImportedNamespace()); 119 java.util.Iterator iterator = call.iterator(); 120 while (iterator.hasNext()) { 121 boolean found = false; 122 Object next = iterator.next(); 123 for (int k = 0; k < attributes.size(); k++) { 124 if (attributes.elementAt(k).equals(next)) 125 found = true; 126 } 127 if (!found) 128 attributes.add(next); 129 } 130 } 131 } 132 return attributes; 133 } 134 135 139 public void packageModuleTemplate(MofPackage package_, String className) { 140 _logger.log(Level.FINE, "XMIExport packageModuleTemplate"); 141 if (package_.getVisibility() == VisibilityKindEnum.forName("public_vis")) { 142 MofClass classes_ordered[] = XMIIOHelper.allClassesOfPackageOrderedByInheritance(package_); 143 144 outputln("package " + JMIProvider.qualifierOf(package_) + ".xmi;"); 145 outputln(); 146 annotationTemplate(package_.getAnnotation()); 147 outputln("public class " + className + " extends org.objectweb.modfact.jmi.xmiio.exporter.XMIExport {"); 148 149 processContents(package_); 150 constructor(package_.getName(), className); 151 classAttributes(findAllClassAttributes(package_)); 152 classDataTypeAttributes(dataTypes); 153 classTemplate(package_, classes_ordered); 154 rootPackage(package_, classes_ordered); 155 outputln("} // end of class " + className); 156 outputln(); 157 } 158 } 159 160 165 public void constructor(String package_name, String class_name) { 166 _logger.log(Level.FINE, "XMIExport constructor(" + package_name + ", " + class_name + ")"); 167 outputln("/**"); 168 outputln(" * Default " + class_name + " Constructor"); 169 outputln(" * @param dtd The DTD File."); 170 outputln(" */"); 171 outputln("public " + class_name + "(java.lang.String dtd) {"); 172 outputln("this(dtd, org.objectweb.modfact.jmi.logging.ModFactLogger.getLogger(\"" + package_name + "XMIExport\"));"); 173 outputln("}"); 174 outputln(); 175 outputln("/**"); 176 outputln(" * " + class_name + " Constructor"); 177 outputln(" * @param dtd The DTD File."); 178 outputln(" * @param logger The logger."); 179 outputln(" */"); 180 outputln("public " + class_name + "(java.lang.String dtd, org.objectweb.modfact.jmi.logging.ModFactLogger logger) {"); 181 outputln("super(dtd, \"" + JMIProvider.jmiFormat1(package_name) + "\", \"Model\");"); 182 outputln("_logger = logger;"); 183 outputln("}"); 184 outputln(); 185 } 186 187 protected String refMethod(MofClass cls, String package_name) { 188 String method = "get" + JMIProvider.jmiFormat1(JMIProvider.jmiClassName(cls)) + "()"; 189 ModelElement clsTmp = cls; 190 while (!clsTmp.getContainer().getName().equals(package_name)) { 191 Namespace ns = clsTmp.getContainer(); 192 if (ns instanceof Classifier) { 193 Classifier container = (Classifier) ns; 194 method = "get" + JMIProvider.jmiPackageExtentName((MofPackage) container) + "()." + method; 195 } else { 196 method = "get" + JMIProvider.jmiPackageExtentName((MofPackage) clsTmp.getContainer()) + "()." + method; 197 } 198 clsTmp = clsTmp.getContainer(); 199 } 200 return method; 201 } 202 203 207 public void classAttributes(Vector attributes) { 208 _logger.log(Level.FINE, "XMIExport classAttributeTemplate()"); 209 java.util.Iterator iterator = attributes.iterator(); 210 outputln("// =================================================================="); 211 outputln("// Method to write some Classes "); 212 outputln("// =================================================================="); 213 while (iterator.hasNext()) { 214 Classifier classifier2 = (Classifier) iterator.next(); 215 MofClass classifier = (MofClass) classifier2; 216 annotationTemplate(classifier.getAnnotation()); 217 String classifierQualifiedName; 218 classifierQualifiedName = JMIProvider.jmiClassifierQualifiedName(classifier); 219 outputln("public void write" + JMIProvider.jmiFormat1(classifier.getName()) + "(" + classifierQualifiedName + " _toWrite) {"); 220 outputln("if (_toWrite == null)"); 221 outputln(" return;"); 222 outputln("outputln(\"<" + JMIProvider.jmiFormat1(classifier.getName()) + " xmi.id=\\\"\" + getXMIId(_toWrite) + \"\\\">\");"); 223 Attribute attr[] = MofHelper.attributesOfClass(classifier, ScopeKindEnum.forName("instance_level"), true); 224 for (int j = 0; j < attr.length; j++) { 225 if (!attr[j].isDerived()) 226 classAttributeTemplate(attr[j], "_toWrite"); 227 } 228 Reference ref[] = MofHelper.referencesOfClass(classifier, true); 229 for (int j = 0; j < ref.length; j++) { 230 classReferenceTemplate(ref[j], "_toWrite"); 231 } 232 outputln("outputln(\"</" + JMIProvider.jmiFormat1(classifier.getName()) + ">\");"); 233 outputln("}"); 234 outputln(); 235 } 236 } 237 238 242 public void classDataTypeAttributes(Vector attributes) { 243 _logger.log(Level.FINE, "XMIExport classAttributeTemplate()"); 244 java.util.Iterator iterator = attributes.iterator(); 245 outputln("// =================================================================="); 246 outputln("// Method to write the DataTypes "); 247 outputln("// =================================================================="); 248 while (iterator.hasNext()) { 249 DataType dataType = (DataType) iterator.next(); 250 if (dataType instanceof EnumerationType) { 251 EnumerationType enumType = (EnumerationType) dataType; 252 outputln( 253 "public java.lang.String enumeration" 254 + JMIProvider.jmiFormat1(enumType.getName()) 255 + "ToString(" 256 + JMIProvider.jmiClassifierQualifiedName(enumType) 257 + " value) {"); 258 259 List labels = enumType.getLabels(); 260 for (int i = 0; i < labels.size(); i++) { 261 outputln( 262 "if (value.equals(" + JMIProvider.jmiClassifierQualifiedName(enumType) + "Enum." + labels.get(i).toString().toUpperCase() + "))"); 263 outputln(" return \"" + JMIProvider.jmiFormat2(labels.get(i).toString()) + "\";"); 264 } 265 outputln("return new java.lang.String();"); 266 outputln("}"); 267 outputln(); 268 } 269 } 270 } 271 272 277 public void classTemplate(MofPackage package_, MofClass classes[]) { 278 _logger.log(Level.FINE, "XMIExport classTemplate()"); 279 String package_name = package_.getName(); 280 outputln("// =================================================================="); 281 outputln("// XXXTemplate with XXX an entity "); 282 outputln("// =================================================================="); 283 for (int i = 0; i < classes.length; i++) { 285 MofClass cls = classes[i]; 286 annotationTemplate(cls.getAnnotation()); 287 String model_package_type; 288 model_package_type = JMIProvider.jmiPackageQualifiedName(package_) + "Package"; 289 outputln("public void " + XMIIOHelper.format1FirstMin(cls.getName()) + "Template(" + model_package_type + " _model_package) {"); 290 String clsClass = XMIIOHelper.format1FirstMin(cls.getName()) + "Class"; 291 String clsTab = XMIIOHelper.testJavaConflict(XMIIOHelper.format1FirstMin(cls.getName()) + "s"); 292 String clsName; 293 String xmiQualifiedName = JMIProvider.jmiFormat1(cls.getName()); 294 try { 295 clsName = JMIProvider.jmiClassName(cls); 296 } catch (NullPointerException notSet) { 297 clsName = cls.getName(); 298 } 299 String javaQualifiedName = JMIProvider.jmiClassQualifiedName(cls); 300 outputln( 301 "_logger.log(org.objectweb.modfact.jmi.logging.Level.FINE, \"" 302 + XMIIOHelper.format1FirstMin(cls.getName()) 303 + "Template(\" + _model_package + \")\");"); 304 outputln(javaQualifiedName + "Class " + clsClass + " = _model_package." + refMethod(cls, package_name) + ";"); 305 try { 306 outputln("java.util.Collection " + clsTab + " = " + clsClass + ".refAllOfType" + "();"); 307 } catch (NullPointerException notSet) { 308 outputln("java.util.Collection " + clsTab + " = " + clsClass + ".refAllOfType" + "();"); 309 } 310 outputln("java.util.Iterator iterator = " + clsTab + ".iterator();"); 312 outputln("while (iterator.hasNext()) {"); 313 outputln(javaQualifiedName + " current = (" + javaQualifiedName + ")iterator.next();"); 314 outputln("outputln(\"<" + xmiQualifiedName + " xmi.id=\\\"\" + getXMIId(current) + \"\\\">\");"); 315 Attribute attributes[] = MofHelper.attributesOfClass(cls, ScopeKindEnum.forName("instance_level"), true); 316 for (int j = 0; j < attributes.length; j++) { 317 if (!attributes[j].isDerived()) 318 classAttributeTemplate(attributes[j], "current"); 319 } 320 Reference references[] = MofHelper.referencesOfClass(cls, true); 321 for (int j = 0; j < references.length; j++) { 322 Reference ref = references[j]; 323 String variable = JMIProvider.jmiAccessorName(ref); 324 325 if (ref.getMultiplicity().getUpper() == -1) { 326 outputln("for (java.util.Iterator it = current." + variable + "().iterator() ; it.hasNext() ;) {"); 327 outputln( 328 "write" 329 + JMIProvider.jmiFormat1(ref.getType().getName()) 330 + "((" 331 + JMIProvider.jmiClassifierQualifiedName(ref.getType()) 332 + ")it.next());"); 333 outputln("}"); 334 } else { 335 String content = "write" + JMIProvider.jmiFormat1(ref.getType().getName()) + "(current." + variable + "());"; 336 if (ref.getMultiplicity().getLower() == 0) { 337 outputln("try {"); 338 outputln(content); 339 outputln("} catch (java.lang.NullPointerException notSet) { /* Nothing to do */ }"); 340 } else { 341 outputln(content); 342 } 343 } 344 345 } 346 outputln("outputln(\"</" + xmiQualifiedName + ">\");"); 347 outputln("}"); 348 outputln("}"); 349 outputln(); 350 } 351 } 352 353 358 public void classAttributeTemplate(Attribute attribute, String variable_name) { 359 _logger.log(Level.FINE, "XMIExport classAttributeTemplate(" + attribute.getName() + ")"); 360 try { 361 String attrName = JMIProvider.jmiFormat1(attribute.getContainer().getName()) + "." + XMIIOHelper.format1FirstMin(attribute.getName()); 362 String tag1 = "<" + attrName + ">"; 363 String tag2 = "</" + attrName + ">"; 364 String readAttribute = variable_name + "." + JMIProvider.jmiAccessorName(attribute) + "()"; 365 boolean voidType = false; 366 String content = "outputln(\"" + tag1 + "\" + " + readAttribute + " + \"" + tag2 + "\");"; 367 Classifier type = attribute.getType(); 368 if (type instanceof EnumerationType) { 369 EnumerationType enumType = (EnumerationType) type; 370 String condition = "if (" + readAttribute + "!= null)"; 371 readAttribute = "enumeration" + JMIProvider.jmiFormat1(enumType.getName()) + "ToString(" + readAttribute + ")"; 372 voidType = true; 373 content = condition + "\n" + TABULATION + "outputln(\"" + tag1 + "\" + " + readAttribute + " + \"" + tag2 + "\");"; 374 } else if (type instanceof MofClass) { 375 MofClass class_ = (MofClass) type; 376 content = "if (" + readAttribute + " != null) {\n"; 377 readAttribute = "write" + JMIProvider.jmiFormat1(class_.getName()) + "(" + readAttribute + ")"; 378 voidType = true; 379 content = content + "outputln(\"" + tag1 + "\");\n" + readAttribute + ";\n" + "outputln(\"" + tag2 + "\");" + "\n}"; 380 } 381 382 if (attribute.getMultiplicity().getUpper() == -1) { 383 outputln("for (java.util.Iterator it = " + readAttribute + ".iterator() ; it.hasNext() ;) {"); 384 outputln("output(\"" + tag1 + "\" + it.next());"); 385 outputln("if (it.hasNext()) output(\" \");"); 386 outputln("outputln(\"" + tag2 + "\");"); 387 outputln("}"); 388 } else { 389 if (!voidType && !(attribute.getType() instanceof PrimitiveType)) 390 outputln("if (" + readAttribute + " != null)"); 391 else if (attribute.getType() instanceof PrimitiveType) 392 outputln("if (" + variable_name + ".refGetValue(\"" + XMIIOHelper.format1FirstMin(attribute.getName()) + "\") != null)"); 393 outputln(content); 394 } 395 } catch (NullPointerException notSet) { 396 } 398 } 399 400 405 public void classReferenceTemplate(Reference reference, String variable_name) { 406 _logger.log(Level.FINE, "XMIExport classReferenceTemplate(" + reference.getName() + ")"); 407 try { 408 String refName = JMIProvider.jmiFormat1(reference.getContainer().getName()) + "." + JMIProvider.jmiFormat1(reference.getName()); 409 String refList = XMIIOHelper.format2Idl2JavaConflict(reference.getName()); 410 String refType = JMIProvider.jmiClassifierQualifiedName(reference.getType()); 411 String refTypeXmi = JMIProvider.jmiFormat1(reference.getType().getName()); 412 if (reference.getMultiplicity().getUpper() == -1) { 413 outputln("java.util.Collection " + refList + " = " + variable_name + "." + JMIProvider.jmiAccessorName(reference) + "();"); 414 outputln("if (" + refList + ".size() > 0) {"); 415 outputln("outputln(\"<" + refName + ">\");"); 416 outputln("java.util.Iterator it = " + refList + ".iterator();"); 417 outputln("while (it.hasNext())"); 418 outputln(" outputln(\"<" + refTypeXmi + " xmi.idref=\\\"\" + getXMIId(it.next()) + \"\\\" />\");"); 419 outputln("outputln(\"</" + refName + ">\");"); 420 outputln("}"); 421 } else { 422 if (reference.getMultiplicity().getLower() == 0) 423 outputln("try {"); 424 outputln(refType + " " + refList + " = " + variable_name + "." + JMIProvider.jmiAccessorName(reference) + "();"); 425 outputln("if (" + refList + " != null) {"); 426 outputln("outputln(\"<" + refName + ">\");"); 427 outputln("outputln(\"<" + refTypeXmi + " xmi.idref=\\\"\" + getXMIId(" + refList + ") + \"\\\" />\");"); 428 outputln("outputln(\"</" + refName + ">\");"); 429 outputln("}"); 430 if (reference.getMultiplicity().getLower() == 0) 431 outputln("} catch (java.lang.NullPointerException notSet) { /* Nothing to do */ }"); 432 } 433 } catch (NullPointerException notSet) { 434 } 436 } 437 438 443 public void rootPackage(MofPackage package_, MofClass classes[]) { 444 _logger.log(Level.FINE, "XMIExport rootPackage()"); 445 outputln("public void rootPackageTemplate(Object _root_package) {"); 446 outputln("_logger.log(org.objectweb.modfact.jmi.logging.Level.FINE, \"rootPackageTemplate(\" + _root_package + \")\");"); 447 String packageName = JMIProvider.jmiFormat1(package_.getName()); 448 String model_package_type = JMIProvider.jmiPackageQualifiedName(package_) + "Package"; 449 outputln(model_package_type + " _model_package = (" + model_package_type + ")_root_package;"); 450 for (int i = 0; i < classes.length; i++) { 451 MofClass cls = (MofClass) classes[i]; 452 if (!cls.isAbstract()) 453 outputln(XMIIOHelper.format1FirstMin(cls.getName()) + "Template(_model_package);"); 454 } 455 outputln("}"); 456 outputln(); 457 } 458 459 } 460 | Popular Tags |