1 20 package org.objectweb.modfact.corba.xmiio.importer; 21 22 import org.objectweb.modfact.corba.logging.Level; 23 import org.objectweb.modfact.corba.logging.ModFactLogger; 24 import org.objectweb.modfact.corba.xmiio.common.AbstractXMIIOGenerator; 25 import org.omg.mof.Reflective.MofError; 26 import org.omg.mof.Reflective.NotSet; 27 28 32 public class GenerationXMIImportAssociations extends AbstractXMIIOGenerator { 33 34 35 protected ModFactLogger _logger = ModFactLogger.getLogger("fr.lip6.src.mdafactory.generation.xmiimport.GenerationXMIImport"); 36 37 40 public GenerationXMIImportAssociations() { 41 super(); 42 ModFactLogger logger = ModFactLogger.getLogger("fr.lip6.src.mdafactory.generation.xmiimport.GenerationXMIImportApplication"); 43 } 44 45 50 public void generateXMIImportAssociations(org.omg.mof.Model.Package package_, String class_name_) 51 throws 52 org.omg.mof.Reflective.MofError, 53 org.omg.CORBA.TypeCodePackage.BadKind , 54 org.omg.CORBA.TypeCodePackage.Bounds , 55 java.io.FileNotFoundException { 56 _logger.log(Level.FINE, "XMIImport beginning of the generation"); 57 _logger.log(Level.INFO, class_name_ + " generation ..."); 58 packageModuleTemplate(package_, class_name_); 59 flushFile(); 60 } 61 62 67 public void constructor(String class_name, String package_name) { 68 _logger.log(Level.FINE, "XMIImport constructor(" + class_name + ")"); 69 outputln("/** The logger. */"); 70 outputln("org.objectweb.modfact.corba.logging.ModFactLogger _logger;"); 71 outputln(); 72 outputln("/** The XMIImportClasses class. */"); 73 outputln(package_name + "XMIImportClasses _xmi_import_classes;"); 74 outputln(); 75 outputln("/** Hashtables for types (Key / Model) */"); 76 outputln("private java.util.Hashtable _types;"); 77 outputln(); 78 outputln("/**"); 79 outputln(" * Default " + class_name + " Constructor"); 80 outputln(" * @param logger The logger."); 81 outputln(" * @param types The defined types."); 82 outputln(" */"); 83 outputln("public " + class_name + "(org.objectweb.modfact.corba.logging.ModFactLogger logger, java.util.Hashtable types) {"); 84 outputln("_logger = logger;"); 85 outputln("_types = types;"); 86 outputln("}"); 87 outputln(); 88 outputln("public void setXMIImportClasses (" + package_name + "XMIImportClasses xmi_import_classes) {"); 89 outputln("_xmi_import_classes = xmi_import_classes;"); 90 outputln("}"); 91 outputln(); 92 } 93 94 99 public void packageModuleTemplate(org.omg.mof.Model.Package package_, String class_name_) 100 throws org.omg.mof.Reflective.MofError, org.omg.CORBA.TypeCodePackage.BadKind , org.omg.CORBA.TypeCodePackage.Bounds { 101 _logger.log(Level.FINE, "XMIImport packageModuleTemplate(" + package_.name() + ")"); 102 if (package_.visibility().equals(org.omg.mof.Model.VisibilityKind.public_vis)) { 103 org.omg.mof.Model.Association associations[] = mofHelper.allAssociationsOfPackage(package_); 104 _logger.log(Level.FINE, "XMIImport produce"); 105 106 outputln("package " + javaHelper.javaPackage(package_) + ".xmi;"); 107 outputln(); 108 annotationTemplate(package_.annotation()); 109 outputln("public class " + class_name_ + " extends org.objectweb.modfact.corba.xmiio.importer.XMIImport {"); 110 classAttributes(package_, associations); 111 constructor(class_name_, package_.name()); 112 initiateMethod(package_, associations); 113 associationTemplate(associations); 114 outputln("} // end of class " + class_name_); 115 outputln(); 116 } 117 } 118 119 124 public void classAttributes(org.omg.mof.Model.Package package_, org.omg.mof.Model.Association associations_[]) 125 throws org.omg.mof.Reflective.MofError { 126 String package_name = package_.name(); 127 _logger.log(Level.FINE, "XMIImport classAttributes(" + package_name + ")"); 128 outputln("// Factories Declaration"); 129 String model_package_type; 130 if (mappingValid) { 131 model_package_type = javaHelper.javaPackage(package_) + "._" + idlHelper.format1(package_name) + "Package"; 132 } else { 133 model_package_type = javaHelper.javaPackage(package_) + "." + idlHelper.format1(package_name) + "Package"; 134 } 135 outputln("private " + model_package_type + " _" + idlHelper.format2(package_name) + "_package = null;"); 136 outputln(); 137 for (int i = 0; i < associations_.length; i++) { 139 org.omg.mof.Model.Association asso = (org.omg.mof.Model.Association) associations_[i]; 140 try { 141 String javaName = javaHelper.javaQualifiedName(asso); 142 outputln("private " + javaName + " _" + idlHelper.format2(javaName) + "_class = null;"); 143 } catch (NotSet notSet) { 144 } 146 } 147 } 148 149 154 public void initiateMethod(org.omg.mof.Model.Package package_, org.omg.mof.Model.Association associations_[]) throws MofError { 155 String package_name = package_.name(); 156 _logger.log(Level.FINE, "XMIImport initiateMethod(" + package_name + ", " + associations_.length + ")"); 157 String model_package_type; 158 if (mappingValid) { 159 model_package_type = javaHelper.javaPackage(package_) + "._" + idlHelper.format1(package_name) + "Package"; 160 } else { 161 model_package_type = javaHelper.javaPackage(package_) + "." + idlHelper.format1(package_name) + "Package"; 162 } 163 outputln("public void initiate(" + model_package_type + " _model_package)"); 164 outputln(" throws org.omg.mof.Reflective.MofError {"); 165 outputln("_logger.log(org.objectweb.modfact.corba.logging.Level.FINE, \"initiate(\" + _model_package + \")\");"); 166 outputln("// Factories creation"); 167 outputln("this._" + idlHelper.format2(package_name) + "_package = _model_package;"); 168 initiateMethodAssociations(package_name, associations_); 170 outputln("}"); 171 outputln(); 172 } 173 174 179 public void initiateMethodAssociations(String package_name_, org.omg.mof.Model.Association associations_[]) throws MofError { 180 for (int i = 0; i < associations_.length; i++) { 181 org.omg.mof.Model.Association asso = (org.omg.mof.Model.Association) associations_[i]; 182 String javaName; 183 try { 184 javaName = idlHelper.format2(javaHelper.javaQualifiedName(asso)); 185 } catch (NotSet notSet) { 186 javaName = asso.name(); 187 } 188 String method = idlHelper.format2(asso.name()) + "_ref()"; 189 try { 190 org.omg.mof.Model.ModelElement assoTmp = asso; 191 while (!assoTmp.container().name().equals(package_name_)) { 192 method = idlHelper.format2(assoTmp.container().name()) + "_ref()." + method; 193 assoTmp = assoTmp.container(); 194 } 195 } catch (org.omg.CORBA.MARSHAL marshal) { 196 if (!marshal.getMessage().equals("IDL:mof.omg.org/Reflective/NotSet:1.0")) 197 throw marshal; 198 } catch (org.omg.mof.Reflective.NotSet notSet) { 199 } 201 outputln("_" + javaName + "_class = _model_package." + method + ";"); 202 } 203 } 204 205 209 public void associationTemplate(org.omg.mof.Model.Association associations_[]) throws org.omg.mof.Reflective.MofError { 210 for (int i = 0; i < associations_.length; i++) { 211 org.omg.mof.Model.Association asso = (org.omg.mof.Model.Association) associations_[i]; 212 String methodName = idlHelper.format1FirstMin(asso.name()) + "Template"; 213 String param = idlHelper.format2(asso.name()) + "_element"; 214 org.omg.mof.Model.AssociationEnd[] ends = mofHelper.associationEndsOfAssociation(asso); 215 outputln("public void " + methodName + "(org.w3c.dom.Element " + param + ") throws org.omg.mof.Reflective.MofError {"); 216 for (int j = 0; j < ends.length; j++) { 217 try { 218 outputln( 219 javaHelper.javaQualifiedName(ends[j].type()) 220 + " " 221 + javaHelper.testJavaConflict(idlHelper.format1FirstMin(ends[j].name())) 222 + " = null;"); 223 } catch (NotSet notSet) { 224 } 226 } 227 outputln("org.w3c.dom.NodeList _child_nodes = " + param + ".getChildNodes();"); 228 outputln("for (int i=0 ; i<_child_nodes.getLength() ; i++) {"); 229 outputln("org.w3c.dom.Element _current = (org.w3c.dom.Element) _child_nodes.item(i);"); 230 for (int j = 0; j < ends.length; j++) { 231 String ends_variable = javaHelper.testJavaConflict(idlHelper.format1FirstMin(ends[j].name())); 232 String type_variable; 233 try { 234 type_variable = javaHelper.javaQualifiedName(ends[j].type()); 235 } catch (NotSet notSet) { 236 type_variable = ends[j].type().name(); 237 } 238 String methodTemplate = "_xmi_import_classes." + idlHelper.format1FirstMin(ends[j].type().name()) + "Template(_current)"; 239 output("if (_current.getNodeName().endsWith(\"" + idlHelper.format1(ends[j].type().name()) + "\")"); 240 try { 241 org.omg.mof.Model.Class[] subclasses = mofHelper.subClassesOfClass(ends[j].type()); 242 for (int k = 0; k < subclasses.length; k++) { 243 output(" || _current.getNodeName().endsWith(\"" + idlHelper.format1(subclasses[k].name()) + "\")"); 244 if (k % 2 == 0 && k < subclasses.length - 1) { 245 outputln(); 246 output(TABULATION); 247 } 248 } 249 } catch (org.omg.mof.Reflective.NotSet notSet) { 250 } 252 outputln(") {"); 253 254 if (ends[j].type().is_abstract()) { 255 outputln( 256 "org.objectweb.modfact.corba.xmiio.mofobject.MOFObject " 257 + ends_variable 258 + "Obj = " 259 + methodTemplate 260 + ".getFirstObject();"); 261 } else { 262 outputln( 263 "org.objectweb.modfact.corba.xmiio.mofobject.MOFObject " + ends_variable + "Obj = " + methodTemplate + ";"); 264 } 265 outputln("if (" + ends_variable + "Obj.isAnObject())"); 266 outputln(TABULATION + ends_variable + " = (" + type_variable + ")" + ends_variable + "Obj.get();"); 267 outputln("else"); 268 outputln(TABULATION + ends_variable + " = (" + type_variable + ") _types.get(" + ends_variable + "Obj.get());"); 269 outputln("}"); 270 } 271 outputln("}"); 272 try { 273 output("_" + idlHelper.format2(javaHelper.javaQualifiedName(asso)) + "_class.add("); 274 } catch (NotSet notSet) { 275 } 277 for (int j = 0; j < ends.length; j++) { 278 output(javaHelper.testJavaConflict(idlHelper.format1FirstMin(ends[j].name()))); 279 if (j < ends.length - 1) 280 output(", "); 281 } 282 outputln(");"); 283 outputln("}"); 284 outputln(); 285 } 286 outputln(); 287 } 288 289 } 290 | Popular Tags |