1 20 package org.objectweb.modfact.jmi.api; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.io.PrintWriter ; 25 import java.util.Iterator ; 26 27 import javax.jmi.model.Association; 28 import javax.jmi.model.DataType; 29 import javax.jmi.model.EnumerationType; 30 import javax.jmi.model.ModelElement; 31 import javax.jmi.model.MofClass; 32 import javax.jmi.model.MofException; 33 import javax.jmi.model.MofPackage; 34 import javax.jmi.model.StructureType; 35 import javax.jmi.model.VisibilityKindEnum; 36 37 import org.objectweb.modfact.jmi.generator.ZipGenerator; 38 import org.objectweb.modfact.jmi.helper.JMIProvider; 39 import org.objectweb.modfact.jmi.helper.MofHelper; 40 import org.objectweb.modfact.jmi.logging.Level; 41 import org.objectweb.modfact.jmi.logging.ModFactLogger; 42 43 47 public class JMI1_0Generator extends ZipGenerator { 48 49 MofPackage[] input; 50 51 ModFactLogger logger; 52 53 56 public void setInput(ModelElement[] elt) { 57 input = new MofPackage[elt.length]; 58 for (int i = 0; i < input.length; i++) { 59 input[i] = (MofPackage) elt[i]; 60 } 61 } 62 63 66 public void setLogger(ModFactLogger log) { 67 logger = log; 68 } 69 70 73 public void generate() 74 throws IOException { 75 76 77 logger.log(Level.FINE, "Begin generation of JMI Interfaces"); 78 79 for (int p = 0; p < input.length; p++) { 80 if (input[p] 81 .getVisibility() 82 .equals(VisibilityKindEnum.forName("public_vis"))) { 83 84 PackageInterfaceGenerator packageGenerator = new PackageInterfaceGenerator(); 86 MofPackage[] param = new MofPackage[1]; 87 param[0] = input[p]; 88 packageGenerator.setInput(param); 89 ByteArrayOutputStream entryOutput1 = 90 new ByteArrayOutputStream (); 91 packageGenerator.setOutput(entryOutput1); 92 packageGenerator.generate(); 93 94 String path = JMIProvider.qualifierOf(input[p]); 95 path = path.replace('.', '/') +"/"; 96 97 String entryName = path 98 +JMIProvider.jmiPackageExtentName(input[p]) + "Package" + ".java"; 99 100 writeEntry(entryName, entryOutput1); 101 entryOutput1.close(); 102 103 logger.log(Level.FINE, entryName+" is generated"); 104 105 MofClass[] classes = MofHelper.classesOfPackage(input[p]); 107 108 for (int i = 0; i < classes.length; i++) { 109 if (classes[i].getVisibility() 110 == VisibilityKindEnum.forName("public_vis")) { 111 112 ClassProxyInterfaceGenerator classProxyGenerator = new ClassProxyInterfaceGenerator(); 114 MofClass[] paramCP = new MofClass[1]; 115 paramCP[0] = classes[i]; 116 classProxyGenerator.setInput(paramCP); 117 ByteArrayOutputStream entryOutputClassProxy = 118 new ByteArrayOutputStream (); 119 classProxyGenerator.setOutput(entryOutputClassProxy); 120 classProxyGenerator.generate(); 121 122 String entryNameClassProxy = path 123 +JMIProvider.jmiClassName(classes[i]) 124 + "Class" 125 + ".java"; 126 writeEntry(entryNameClassProxy, entryOutputClassProxy); 127 entryOutputClassProxy.close(); 128 129 logger.log(Level.FINE, entryNameClassProxy+" is generated"); 130 131 InstanceInterfaceGenerator classImpl = new InstanceInterfaceGenerator(); 133 MofClass[] paramCI = new MofClass[1]; 134 paramCI[0] = classes[i]; 135 classImpl.setInput(paramCI); 136 ByteArrayOutputStream entryOutputInstance = new ByteArrayOutputStream (); 137 classImpl.setOutput(entryOutputInstance); 138 classImpl.generate(); 139 140 String entryNameInstance = path 141 +JMIProvider.jmiClassName(classes[i]) + ".java"; 142 writeEntry(entryNameInstance, entryOutputInstance); 143 entryOutputInstance.close(); 144 145 logger.log(Level.FINE, entryNameInstance+" is generated"); 146 147 DataType[] datatypes = 149 MofHelper.datatypesOf(classes[i], false); 150 for (int j = 0; j < datatypes.length; j++) { 151 152 if (datatypes[j] instanceof EnumerationType) { 154 EnumerationTypeInterfaceGenerator enumInterfaceGenerator = 155 new EnumerationTypeInterfaceGenerator(); 156 DataType[] in = new DataType[1]; 157 in[0] = datatypes[j]; 158 enumInterfaceGenerator.setInput(in); 159 enumInterfaceGenerator.setLogger(logger); 160 ByteArrayOutputStream enumEntryOutputInt = 161 new ByteArrayOutputStream (); 162 enumInterfaceGenerator.setOutput( 163 enumEntryOutputInt); 164 enumInterfaceGenerator.generate(); 165 166 String entryNameEnumInt = path 167 +datatypes[j].getName() + ".java"; 168 writeEntry( 169 entryNameEnumInt, 170 enumEntryOutputInt); 171 enumEntryOutputInt.close(); 172 173 logger.log(Level.FINE, entryNameEnumInt+" is generated"); 174 175 EnumerationTypeImplementationGenerator enumGenerator = 176 new EnumerationTypeImplementationGenerator(); 177 DataType[] inImpl = new DataType[1]; 178 inImpl[0] = datatypes[j]; 179 enumGenerator.setInput(inImpl); 180 enumGenerator.setLogger(logger); 181 182 ByteArrayOutputStream enumEntryOutputImpl = 183 new ByteArrayOutputStream (); 184 enumGenerator.setOutput(enumEntryOutputImpl); 185 enumGenerator.generate(); 186 187 String entryNameEnumImpl = path+ 188 datatypes[j].getName() + "Enum.java"; 189 writeEntry( 190 entryNameEnumImpl, 191 enumEntryOutputImpl); 192 enumEntryOutputImpl.close(); 193 194 logger.log(Level.FINE, entryNameEnumImpl+" is generated"); 195 196 197 } else if (datatypes[j] instanceof StructureType) { 199 StructureTypeInterfaceGenerator structGenerator = 200 new StructureTypeInterfaceGenerator(); 201 DataType[] inImpl = new DataType[1]; 202 inImpl[0] = datatypes[j]; 203 structGenerator.setInput(inImpl); 204 structGenerator.setLogger(logger); 205 206 ByteArrayOutputStream structEntryOutputImpl = 207 new ByteArrayOutputStream (); 208 structGenerator.setOutput( 209 structEntryOutputImpl); 210 structGenerator.generate(); 211 212 String entryNameStruct = path 213 +datatypes[j].getName() + ".java"; 214 writeEntry( 215 entryNameStruct, 216 structEntryOutputImpl); 217 structEntryOutputImpl.close(); 218 219 logger.log(Level.FINE, entryNameStruct+" is generated"); 220 } 221 } 222 223 } 224 } 225 226 Association[] associations = 228 MofHelper.associationsOfPackage(input[p]); 229 for (int i = 0; i < associations.length; i++) { 230 if (associations[i] 231 .getVisibility() 232 .equals(VisibilityKindEnum.forName("public_vis"))) { 233 AssociationGenerator associationGenerator = 234 new AssociationGenerator(); 235 Association[] paramA = new Association[1]; 236 paramA[0] = associations[i]; 237 associationGenerator.setInput(paramA); 238 ByteArrayOutputStream entryOutputAssociation = 239 new ByteArrayOutputStream (); 240 associationGenerator.setOutput(entryOutputAssociation); 241 242 associationGenerator.generate(); 243 244 String entryNameAssociation = path+ 245 JMIProvider.jmiAssociationName(associations[i]) 246 + ".java"; 247 writeEntry( 248 entryNameAssociation, 249 entryOutputAssociation); 250 entryOutputAssociation.close(); 251 252 logger.log(Level.FINE, entryNameAssociation+" is generated"); 253 } 254 } 255 256 DataType[] datatypes = MofHelper.datatypesOf(input[p], false); 258 for (int i = 0; i < datatypes.length; i++) { 259 260 261 if (datatypes[i] instanceof StructureType) { 263 StructureTypeInterfaceGenerator structureTypeInterfaceGenerator = 264 new StructureTypeInterfaceGenerator(); 265 DataType[] paramT = new DataType[1]; 266 paramT[0] = datatypes[i]; 267 structureTypeInterfaceGenerator.setInput(paramT); 268 ByteArrayOutputStream entryOutputDataType = 269 new ByteArrayOutputStream (); 270 structureTypeInterfaceGenerator.setOutput( 271 entryOutputDataType); 272 273 structureTypeInterfaceGenerator.generate(); 274 275 String entryNameStructure = path+ 276 datatypes[i].getName() + ".java"; 277 writeEntry(entryNameStructure, entryOutputDataType); 278 entryOutputDataType.close(); 279 280 logger.log(Level.FINE, entryNameStructure+" is generated"); 281 282 283 } else if (datatypes[i] instanceof EnumerationType) { 285 EnumerationTypeImplementationGenerator enumTypeTemplateImpl = 286 new EnumerationTypeImplementationGenerator(); 287 DataType[] paramT = new DataType[1]; 288 paramT[0] = datatypes[i]; 289 enumTypeTemplateImpl.setInput(paramT); 290 ByteArrayOutputStream entryOutputDataType = 291 new ByteArrayOutputStream (); 292 enumTypeTemplateImpl.setOutput(entryOutputDataType); 293 294 enumTypeTemplateImpl.generate(); 295 296 String entryNameEnumImpl = path+ 297 datatypes[i].getName() + "Enum.java"; 298 writeEntry(entryNameEnumImpl, entryOutputDataType); 299 entryOutputDataType.close(); 300 301 logger.log(Level.FINE, entryNameEnumImpl+" is generated"); 302 303 EnumerationTypeInterfaceGenerator enumTypeTemplate = 304 new EnumerationTypeInterfaceGenerator(); 305 enumTypeTemplate.setInput(paramT); 306 ByteArrayOutputStream entryOutputDataTypeInt = 307 new ByteArrayOutputStream (); 308 enumTypeTemplate.setOutput(entryOutputDataTypeInt); 309 enumTypeTemplate.generate(); 310 311 String entryNameEnumInt = path + datatypes[i].getName() + ".java"; 312 writeEntry(entryNameEnumInt, entryOutputDataTypeInt); 313 entryOutputDataTypeInt.close(); 314 315 logger.log(Level.FINE, entryNameEnumInt+" is generated"); 316 } 317 } 318 319 exceptionTemplate(input[p], path); 320 321 } } 323 out.finish(); 324 logger.log(Level.FINE, " Generation is completed without error"); 325 } 326 327 328 void exceptionTemplate(MofPackage p, String path) throws IOException { 329 Iterator it = MofHelper.filterContentsByClass(p, MofException.class, false).iterator(); 330 while(it.hasNext()) { 331 MofException e = (MofException) it.next(); 332 String entryName = path +e.getName() + "Exception.java"; 333 ByteArrayOutputStream expOut = new ByteArrayOutputStream (); 334 PrintWriter w = new PrintWriter (expOut); 335 w.println("public class " +entryName.replace('/','.') +" extends javax.jmi.reflect.RefException {"); 336 w.println("}"); 337 writeEntry(entryName, expOut); 338 w.close(); 339 } 340 } 341 342 } 343 | Popular Tags |