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.MofPackage; 26 27 import org.objectweb.modfact.jmi.helper.JMIProvider; 28 import org.objectweb.modfact.jmi.helper.MofHelper; 29 import org.objectweb.modfact.jmi.helper.XMIIOHelper; 30 import org.objectweb.modfact.jmi.xmiio.common.AbstractXMIIOGenerator; 31 32 36 public class GenerationXMIImportAssociations extends AbstractXMIIOGenerator { 37 38 41 public GenerationXMIImportAssociations() { 42 super(); 43 } 44 45 50 public void generateXMIImportAssociations(javax.jmi.model.MofPackage package_, String class_name_) 51 throws java.io.FileNotFoundException { 52 packageModuleTemplate(package_, class_name_); 53 out.flush(); 54 } 55 56 61 public void constructor(String class_name, String package_name) { 62 outputln("/** The XMIImportClasses class. */"); 63 outputln(package_name + "XMIImportClasses _xmi_import_classes;"); 64 outputln(); 65 outputln("/** Hashtables for types (Key / Model) */"); 66 outputln("private java.util.Hashtable _types;"); 67 outputln(); 68 outputln("/**"); 69 outputln(" * Default " + class_name + " Constructor"); 70 outputln(" * @param types The defined types."); 71 outputln(" */"); 72 outputln("public " + class_name + "(java.util.Hashtable types) {"); 73 outputln("_types = types;"); 74 outputln("}"); 75 outputln(); 76 outputln("public void setXMIImportClasses (" + package_name + "XMIImportClasses xmi_import_classes) {"); 77 outputln("_xmi_import_classes = xmi_import_classes;"); 78 outputln("}"); 79 outputln(); 80 } 81 82 java.util.Vector classes = new java.util.Vector (); 83 java.util.Vector associations = new java.util.Vector (); 84 85 private void processContents(MofPackage package_) { 86 java.util.List contentsList = package_.getContents(); 87 Iterator it = contentsList.iterator(); 88 while (it.hasNext()) { 89 javax.jmi.model.ModelElement contents = (javax.jmi.model.ModelElement) it.next(); 90 if (contents instanceof javax.jmi.model.MofPackage) 91 processContents((javax.jmi.model.MofPackage) contents); 92 else if (contents instanceof javax.jmi.model.Import) 93 processContents((javax.jmi.model.MofPackage) ((javax.jmi.model.Import) contents).getImportedNamespace()); 94 else if (contents instanceof javax.jmi.model.MofClass) 95 classes.addElement((javax.jmi.model.MofClass) contents); 96 else if (contents instanceof javax.jmi.model.Association) 97 associations.addElement((javax.jmi.model.Association) contents); 98 } 99 } 100 101 106 public void packageModuleTemplate(javax.jmi.model.MofPackage package_, String class_name_) { 107 if (package_.getVisibility() == javax.jmi.model.VisibilityKindEnum.forName("public_vis")) { 108 processContents(package_); 109 110 java.util.Vector classes_ordered = new java.util.Vector (); 111 while (classes.size() != 0) { 112 javax.jmi.model.MofClass courant = null; 113 for (int i = 0; i < classes.size(); i++) { 114 courant = (javax.jmi.model.MofClass) classes.elementAt(i); 115 java.util.List supertypes = courant.getSupertypes(); 116 boolean is_top = true; 117 Iterator itSup = supertypes.iterator(); 118 while (itSup.hasNext()) { 119 if (classes.contains((javax.jmi.model.GeneralizableElement) itSup.next())) 120 is_top = false; 121 } 122 if (is_top) { 123 classes_ordered.addElement(courant); 124 classes.removeElement(courant); 125 } 126 } 127 } 128 129 133 String packageName = JMIProvider.jmiPackageExtentName(package_); 134 String packageQualifiedName = JMIProvider.qualifierOf(package_); 135 outputln("package " + packageQualifiedName + ".xmi;"); 136 outputln(); 137 annotationTemplate(package_.getAnnotation()); 138 outputln("public class " + class_name_ + " extends org.objectweb.modfact.jmi.xmiio.importer.XMIImport {"); 139 classAttributes(packageName, package_, associations); 140 constructor(class_name_, packageName); 141 initiateMethod(packageName, package_, associations); 142 associationTemplate(associations); 143 outputln("} // end of class " + class_name_); 144 outputln(); 145 } 146 } 147 148 153 public void classAttributes(String package_name_, MofPackage package_, Vector associations_) { 154 outputln("// Factories Declaration"); 155 String model_package_type; 156 model_package_type = JMIProvider.jmiPackageQualifiedName(package_) + "Package"; 157 outputln("private " + model_package_type + " _" + JMIProvider.jmiFormat2(package_name_) + "_package = null;"); 158 outputln(); 159 for (int i = 0; i < associations_.size(); i++) { 161 javax.jmi.model.Association asso = (javax.jmi.model.Association) associations_.elementAt(i); 162 String javaName = JMIProvider.jmiAssociationQualifiedName(asso); 163 outputln("private " + javaName + " _" + JMIProvider.jmiFormat2(javaName) + "_class = null;"); 164 } 165 } 166 167 172 public void initiateMethod(String package_name_, MofPackage package_, Vector associations_) { 173 String model_package_type; 174 model_package_type = JMIProvider.jmiPackageQualifiedName(package_) + "Package"; 175 outputln("public void initiate(" + model_package_type + " _model_package) {"); 176 outputln("// Factories creation"); 177 outputln("this._" + JMIProvider.jmiFormat2(package_name_) + "_package = _model_package;"); 178 initiateMethodAssociations(package_name_, package_, associations_); 180 outputln("}"); 181 outputln(); 182 } 183 184 189 public void initiateMethodAssociations(String package_name_, MofPackage package_, Vector associations_) { 190 for (int i = 0; i < associations_.size(); i++) { 191 javax.jmi.model.Association asso = (javax.jmi.model.Association) associations_.elementAt(i); 192 String javaName = JMIProvider.jmiFormat2(JMIProvider.jmiAssociationQualifiedName(asso)); 193 String method = "get" + JMIProvider.jmiAssociationName(asso) + "()"; 194 try { 195 javax.jmi.model.ModelElement assoTmp = asso; 196 while (!assoTmp.getContainer().equals(package_)) { 197 method = "get" + JMIProvider.jmiPackageExtentName((MofPackage) assoTmp.getContainer()) + "()." + method; 198 assoTmp = assoTmp.getContainer(); 199 } 200 201 } catch (NullPointerException notSet) { 202 } 204 outputln("_" + javaName + "_class = _model_package." + method + ";"); 205 } 206 } 207 208 212 public void associationTemplate(Vector associations_) { 213 for (int i = 0; i < associations_.size(); i++) { 214 javax.jmi.model.Association asso = (javax.jmi.model.Association) associations_.elementAt(i); 215 String assoName = JMIProvider.jmiAssociationName(asso); 216 String methodName = XMIIOHelper.format1FirstMin(assoName) + "Template"; 217 String param = JMIProvider.jmiFormat2(assoName) + "_element"; 218 javax.jmi.model.AssociationEnd[] ends = MofHelper.associationEndsOfAssociation(asso); 219 boolean endsChangeable = true; 220 for (int j = 0; j < ends.length; j++) { 221 endsChangeable = (endsChangeable && ends[j].isChangeable()); 222 } 223 if (endsChangeable) { 224 outputln("public void " + methodName + "(org.w3c.dom.Element " + param + ") {"); 225 for (int j = 0; j < ends.length; j++) { 226 outputln( 227 JMIProvider.jmiClassifierQualifiedName(ends[j].getType()) 228 + " " 229 + XMIIOHelper.testJavaConflict(XMIIOHelper.format1FirstMin(ends[j].getName())) 230 + " = null;"); 231 } 232 outputln("org.w3c.dom.NodeList _child_nodes = " + param + ".getChildNodes();"); 233 outputln("for (int i=0 ; i<_child_nodes.getLength() ; i++) {"); 234 outputln("org.w3c.dom.Element _current = (org.w3c.dom.Element) _child_nodes.item(i);"); 235 for (int j = 0; j < ends.length; j++) { 236 String ends_variable = XMIIOHelper.testJavaConflict(XMIIOHelper.format1FirstMin(ends[j].getName())); 237 String type_variable = JMIProvider.jmiClassifierQualifiedName(ends[j].getType()); 238 String end_type_name = JMIProvider.findSubstituteName(ends[j].getType()); 239 if (end_type_name == null) 240 end_type_name = JMIProvider.classNameFormat(ends[j].getType().getName()); 241 String methodTemplate = "_xmi_import_classes." + XMIIOHelper.format1FirstMin(end_type_name) + "Template(_current)"; 242 output("if (_current.getNodeName().endsWith(\"" + JMIProvider.jmiFormat1(ends[j].getType().getName()) + "\")"); 243 try { 244 javax.jmi.model.MofClass[] subclasses = XMIIOHelper.subClasses(ends[j].getType()); 245 for (int k = 0; k < subclasses.length; k++) { 246 output(" || _current.getNodeName().endsWith(\"" + JMIProvider.jmiFormat1(subclasses[k].getName()) + "\")"); 247 if (k % 2 == 0 && k < subclasses.length - 1) { 248 outputln(); 249 output(TABULATION); 250 } 251 } 252 } catch (NullPointerException notSet) { 253 } 255 outputln(") {"); 256 257 if (ends[j].getType().isAbstract()) { 258 outputln( 259 "org.objectweb.modfact.jmi.xmiio.mofobject.MOFObject " 260 + ends_variable 261 + "Obj = " 262 + methodTemplate 263 + ".getFirstObject();"); 264 } else { 265 outputln("org.objectweb.modfact.jmi.xmiio.mofobject.MOFObject " + ends_variable + "Obj = " + methodTemplate + ";"); 266 } 267 outputln("if (" + ends_variable + "Obj.isAnObject())"); 268 outputln(TABULATION + ends_variable + " = (" + type_variable + ")" + ends_variable + "Obj.get();"); 269 outputln("else"); 270 outputln(TABULATION + ends_variable + " = (" + type_variable + ") _types.get(" + ends_variable + "Obj.get());"); 271 outputln("}"); 272 } 273 outputln("if (i%2 == 1)"); 275 output(TABULATION + "_" + JMIProvider.jmiFormat2(JMIProvider.jmiAssociationQualifiedName(asso)) + "_class.add("); 276 for (int j = 0; j < ends.length; j++) { 277 output(XMIIOHelper.testJavaConflict(XMIIOHelper.format1FirstMin(ends[j].getName()))); 278 if (j < ends.length - 1) 279 output(", "); 280 } 281 outputln(");"); 282 outputln("}"); 283 outputln("}"); 284 outputln(); 285 } 286 } 287 outputln(); 288 } 289 290 } 291 | Popular Tags |