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 GenerationXMIImport extends AbstractXMIIOGenerator { 39 40 44 public void generateXMIImport(MofPackage package_) throws java.io.IOException { 45 String className = getClassName(JMIProvider.jmiPackageExtentName(package_)); 46 packageModuleTemplate(package_); 47 out.flush(); 48 } 49 50 55 protected String getClassName(String package_name_) { 56 String packageName1 = JMIProvider.jmiFormat1(package_name_); 57 String className = packageName1 + "XMIImport"; 58 return className; 59 } 60 61 65 public void constructor(String class_name, String package_name) { 66 outputln("/** The XMIImportAssociations class. */"); 67 outputln(package_name + "XMIImportAssociations _xmi_import_associations;"); 68 outputln(); 69 outputln("/** The XMIImportAttributes class. */"); 70 outputln(package_name + "XMIImportAttributes _xmi_import_attributes;"); 71 outputln(); 72 outputln("/** The XMIImportClasses class. */"); 73 outputln(package_name + "XMIImportClasses _xmi_import_classes;"); 74 outputln(); 75 outputln("/**"); 76 outputln(" * " + class_name + " Constructor"); 77 outputln(" */"); 78 outputln("public " + class_name + "() {"); 79 outputln("_xmi_import_associations = new " + package_name + "XMIImportAssociations (_types);"); 80 outputln("_xmi_import_attributes = new " + package_name + "XMIImportAttributes (_types);"); 81 outputln("_xmi_import_classes = new " + package_name + "XMIImportClasses (_types);"); 82 outputln("_xmi_import_classes.setXMIImportAttributes(_xmi_import_attributes);"); 83 outputln("_xmi_import_associations.setXMIImportClasses(_xmi_import_classes);"); 84 outputln("_xmi_import_attributes.setXMIImportClasses(_xmi_import_classes);"); 85 outputln("}"); 86 outputln(); 87 } 88 89 java.util.Vector classes = new java.util.Vector (); 90 java.util.Vector associations = new java.util.Vector (); 91 92 private void processContents(MofPackage package_) { 93 java.util.List contentsList = package_.getContents(); 94 Iterator it = contentsList.iterator(); 95 while (it.hasNext()) { 96 javax.jmi.model.ModelElement contents = (javax.jmi.model.ModelElement) it.next(); 97 if (contents instanceof javax.jmi.model.MofPackage) 98 processContents((javax.jmi.model.MofPackage) contents); 99 else if (contents instanceof javax.jmi.model.Import) 100 processContents((javax.jmi.model.MofPackage) ((javax.jmi.model.Import) contents).getImportedNamespace()); 101 else if (contents instanceof javax.jmi.model.MofClass) 102 classes.addElement((javax.jmi.model.MofClass) contents); 103 else if (contents instanceof javax.jmi.model.Association) 104 associations.addElement((javax.jmi.model.Association) contents); 105 } 106 } 107 108 112 public void packageModuleTemplate(MofPackage package_) { 113 if (package_.getVisibility() == VisibilityKindEnum.forName("public_vis")) { 114 processContents(package_); 115 java.util.Vector classes_ordered = new java.util.Vector (); 116 while (classes.size() != 0) { 117 javax.jmi.model.MofClass courant = null; 118 for (int i = 0; i < classes.size(); i++) { 119 courant = (javax.jmi.model.MofClass) classes.elementAt(i); 120 java.util.List supertypes = courant.getSupertypes(); 121 boolean is_top = true; 122 Iterator stypesIt = supertypes.iterator(); 123 while (stypesIt.hasNext()) { 124 GeneralizableElement supertype = (GeneralizableElement) stypesIt.next(); 125 if (classes.contains(supertype)) 126 is_top = false; 127 } 128 if (is_top) { 129 classes_ordered.addElement(courant); 130 classes.removeElement(courant); 131 } 132 } 133 } 134 135 String packageName = JMIProvider.jmiPackageExtentName(package_); 136 String className = getClassName(packageName); 137 String packageQualifiedName = JMIProvider.qualifierOf(package_); 138 outputln("package " + packageQualifiedName + ".xmi;"); 139 outputln(); 140 annotationTemplate(package_.getAnnotation()); 141 outputln("public class " + className + " extends org.objectweb.modfact.jmi.xmiio.importer.XMIImportParser {"); 142 classAttributes(packageName, package_); 143 constructor(className, packageName); 144 associationAttributes(packageName, associations); 145 initiateMethod(packageName, package_, classes_ordered, associations); 146 rootPackage(packageName, classes_ordered, associations); 147 completeImportMethod(associations); 148 outputln("} // end of class " + className); 149 outputln(); 150 } 151 } 152 153 158 public void classAttributes(String package_name_, MofPackage package_) { 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 outputln(); 164 } 165 166 171 public void associationAttributes(String package_name_, Vector associations_) { 172 outputln("// Hashtables for associations (Model / Key Vector)"); 173 outputln(); 174 outputln("/** Hashtables for types (Key / Model) */"); 175 outputln("private java.util.Hashtable _types = new java.util.Hashtable();"); 176 outputln(); 177 } 178 179 185 public void initiateMethod(String package_name_, MofPackage package_, Vector classes_, Vector associations_) { 186 String model_package_type; 187 model_package_type = JMIProvider.jmiPackageQualifiedName(package_) + "Package"; 188 outputln("public void initiate(" + model_package_type + " _model_package) {"); 189 outputln("// Factories creation"); 190 outputln("this._" + JMIProvider.jmiFormat2(package_name_) + "_package = _model_package;"); 192 outputln("_xmi_import_classes.initiate(_model_package);"); 193 outputln("_xmi_import_associations.initiate(_model_package);"); 194 outputln("_xmi_import_attributes.initiate(_model_package);"); 195 outputln("}"); 196 outputln(); 197 } 198 199 205 public void rootPackage(String package_name_, Vector classes_, Vector associations_) { 206 annotationTemplate(" The root package processing"); 207 outputln("public java.lang.Object rootPackageTemplate(org.w3c.dom.Element _root_package_element) {"); 208 outputln("java.lang.String prefix_element = new java.lang.String();"); 209 outputln("java.lang.String namespace = _root_package_element.getNamespaceURI();"); 210 outputln("if (namespace != null && namespace.length() > 0)"); 211 outputln(" prefix_element = _root_package_element.getPrefix() + \":\";"); 212 outputln("org.w3c.dom.Element _current = _root_package_element;"); 213 outputln("String _current_name = _root_package_element.getNodeName().trim();"); 214 215 for (int i = 0; i < classes_.size(); i++) { 216 javax.jmi.model.MofClass cls = (javax.jmi.model.MofClass) classes_.elementAt(i); 217 String xmlClassName = JMIProvider.jmiFormat1(cls.getName()); 218 String className = JMIProvider.jmiFormat1(JMIProvider.jmiClassName(cls)); 219 String classNameMin = className.substring(0, 1).toLowerCase() + className.substring(1); 220 outputln("if (_current_name.endsWith(prefix_element + \"" + className + "\")) {"); 221 outputln("_xmi_import_classes." + classNameMin + "Template(_current);"); 222 if (i == classes_.size() - 1 && associations_.size() == 0) 223 outputln("}"); 224 else 225 output("} else "); 226 } 227 for (int i = 0; i < associations_.size(); i++) { 228 javax.jmi.model.Association asso = (javax.jmi.model.Association) associations_.elementAt(i); 229 String xmlAssoName = asso.getName(); 230 String assoName = JMIProvider.jmiFormat1(JMIProvider.jmiAssociationName(asso)); 231 String assoNameMin = assoName.substring(0, 1).toLowerCase() + assoName.substring(1); 232 javax.jmi.model.AssociationEnd[] ends = MofHelper.associationEndsOfAssociation(asso); 233 boolean endsChangeable = true; 234 for (int j = 0; j < ends.length; j++) { 235 endsChangeable = (endsChangeable && ends[j].isChangeable()); 236 } 237 if (endsChangeable) { 238 outputln("if (_current_name.endsWith(prefix_element + \"" + xmlAssoName + "\")) {"); 239 outputln("_xmi_import_associations." + assoNameMin + "Template(_current);"); 240 if (i == associations_.size() - 1) 241 outputln("}"); 242 else 243 output("} else "); 244 } 245 } 246 outputln("return _" + JMIProvider.jmiFormat2(package_name_) + "_package;"); 247 outputln("}"); 248 outputln(); 249 } 250 251 255 public void completeImportMethod(Vector associations_) { 256 outputln("public void completeImport () {"); 257 for (int i = 0; i < associations_.size(); i++) { 258 javax.jmi.model.Association asso = (javax.jmi.model.Association) associations_.elementAt(i); 259 try { 260 javax.jmi.model.Reference[] refs = XMIIOHelper.getReferences(asso); 261 for (int j = 0; j < refs.length; j++) { 262 javax.jmi.model.AssociationEnd assoEnd = refs[j].getReferencedEnd(); 263 if (assoEnd.getAggregation() == javax.jmi.model.AggregationKindEnum.forName("none") && assoEnd.isChangeable()) 264 outputln( 265 "_xmi_import_attributes.put" 266 + JMIProvider.jmiFormat1(refs[j].getContainer().getName()) 267 + JMIProvider.jmiFormat1(refs[j].getName()) 268 + "();"); 269 } 270 } catch (NullPointerException notSet) { 271 } 273 } 274 outputln("}"); 275 outputln(); 276 } 277 278 } 279 | Popular Tags |