1 23 24 package org.objectweb.jorm.compiler.lib; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 32 import org.objectweb.jorm.api.PException; 33 import org.objectweb.jorm.compiler.api.JormCompilerConfigurator; 34 import org.objectweb.jorm.compiler.api.JormCompilerParameter; 35 import org.objectweb.jorm.compiler.api.PExceptionCompiler; 36 import org.objectweb.jorm.generator.api.Generator; 37 import org.objectweb.jorm.metainfo.api.Class; 38 import org.objectweb.jorm.metainfo.api.CompositeName; 39 import org.objectweb.jorm.metainfo.api.Manager; 40 import org.objectweb.jorm.metainfo.api.MetaObject; 41 import org.objectweb.jorm.mi2xml.api.Writer; 42 import org.objectweb.jorm.util.api.Loggable; 43 import org.objectweb.jorm.util.io.api.TargetHolder; 44 import org.objectweb.jorm.util.io.lib.JavaFileHolder; 45 import org.objectweb.jorm.verifier.api.Verifier; 46 import org.objectweb.jorm.xml2mi.api.MappingParser; 47 import org.objectweb.jorm.xml2mi.api.Parser; 48 import org.objectweb.util.monolog.api.BasicLevel; 49 import org.objectweb.util.monolog.api.Logger; 50 import org.objectweb.util.monolog.api.LoggerFactory; 51 52 56 public class JormCompiler { 57 60 protected Logger logger; 61 62 65 protected LoggerFactory loggerFactory = null; 66 67 70 protected Manager manager; 71 72 76 protected Parser parser; 77 78 82 protected Verifier verifier; 83 84 88 protected Generator generator; 89 90 94 protected Writer writer; 95 96 99 protected JormCompilerParameter compilerParameter; 100 101 104 protected JormCompilerConfigurator compilerConfigurator; 105 106 110 public JormCompiler() { 111 compilerParameter = new JormCompilerParameterImpl(); 112 compilerConfigurator = new JormCompilerConfiguratorImpl(); 113 } 114 115 119 public JormCompiler(JormCompilerParameter cp, JormCompilerConfigurator cc) { 120 this.compilerParameter = cp; 121 this.compilerConfigurator = cc; 122 } 123 124 128 public JormCompilerParameter getCompilerParameter() { 129 return compilerParameter; 130 } 131 132 136 public JormCompilerConfigurator getCompilerConfigurator() { 137 return compilerConfigurator; 138 } 139 140 143 public void setupLogger() throws PException { 144 if (loggerFactory != null) { 145 logger.log(BasicLevel.DEBUG, "The log system is already setup"); 146 return; 147 } 148 loggerFactory = compilerConfigurator.getLoggerFactory(); 149 logger = loggerFactory.getLogger("org.objectweb.jorm.compiler"); 150 logger.log(BasicLevel.DEBUG, "Logger factory assigned"); 151 } 152 153 public void setupMIManager() throws PException { 154 if (manager != null) { 155 logger.log(BasicLevel.DEBUG, "The meta information manager is already defined"); 156 return; 157 } 158 logger.log(BasicLevel.DEBUG, "Instanciate the meta-information manager"); 159 manager = compilerConfigurator.getMIManager(); 160 ((Loggable) manager).setLoggerFactory(loggerFactory); 161 ((Loggable) manager).setLogger(loggerFactory.getLogger("org.objectweb.jorm.metainfo")); 162 manager.init(); 163 Iterator it = compilerConfigurator.knownMappers(); 164 while (it.hasNext()) { 165 String mn = (String ) it.next(); 166 logger.log(BasicLevel.DEBUG, "Register specific meta-information management for mapper [" + mn + "]"); 167 manager.addMappingFactory(mn, compilerConfigurator.getMIFactory(mn)); 168 } 169 } 170 171 public void setupParser() throws PException { 172 if (parser != null) { 173 logger.log(BasicLevel.DEBUG, "The parser is already defined"); 174 return; 175 } 176 logger.log(BasicLevel.DEBUG, "Instanciate the parser"); 177 parser = compilerConfigurator.getParser(); 178 ((Loggable) parser).setLoggerFactory(loggerFactory); 179 ((Loggable) parser).setLogger(loggerFactory.getLogger("org.objectweb.jorm.xml2mi")); 180 parser.setMetaInfoManager(manager); 181 logger.log(BasicLevel.DEBUG, "Assign the path explorer to parser"); 182 parser.setPathExplorer(compilerParameter.getClasspath()); 183 parser.init(true, compilerParameter.getDtdLocations()); 184 Iterator it = compilerConfigurator.knownMappers(); 185 while (it.hasNext()) { 186 String mn = (String ) it.next(); 187 logger.log(BasicLevel.DEBUG, "Register specific parser for mapper [" + mn + "]"); 188 MappingParser mp = compilerConfigurator.getMappingParser(mn); 189 ((Loggable) mp).setLoggerFactory(loggerFactory); 190 ((Loggable) mp).setLogger(loggerFactory.getLogger("org.objectweb.jorm.xml2mi." + mn)); 191 parser.addMappingParser(mn, mp); 192 } 193 logger.log(BasicLevel.DEBUG, "Parser setup terminated."); 194 } 195 196 public void setupVerifier() throws PException { 197 if (verifier != null) { 198 logger.log(BasicLevel.DEBUG, "The verifier is already defined"); 199 return; 200 } 201 logger.log(BasicLevel.DEBUG, "Instanciate the verifier"); 202 verifier = compilerConfigurator.getVerifier(); 203 ((Loggable) verifier).setLoggerFactory(loggerFactory); 204 ((Loggable) verifier).setLogger(loggerFactory.getLogger("org.objectweb.jorm.verifier")); 205 verifier.setMetaInfoManager(manager); 206 verifier.setProjectName(compilerParameter.getProjectName()); 207 Iterator it = compilerConfigurator.knownMappers(); 208 while (it.hasNext()) { 209 String mn = (String ) it.next(); 210 logger.log(BasicLevel.DEBUG, "Register specific verifier for mapper [" + mn + "]"); 211 verifier.addMappingVerifier(mn, compilerConfigurator.getMappingVerifier(mn)); 212 } 213 } 214 215 public void setupGenerator() throws PException { 216 if (generator != null) { 217 logger.log(BasicLevel.DEBUG, "The generator is already defined"); 218 return; 219 } 220 logger.log(BasicLevel.DEBUG, "Instanciate the generator"); 221 generator = compilerConfigurator.getGenerator(); 222 ((Loggable) generator).setLogger(loggerFactory.getLogger("org.objectweb.jorm.generator")); 223 ((Loggable) generator).setLoggerFactory(loggerFactory); 224 generator.setPathExplorer(compilerParameter.getClasspath()); 225 generator.setMetaInfoManager(manager); 226 generator.setCompilerConfigurator(compilerConfigurator); 227 generator.setCompilerParameter(compilerParameter); 228 generator.init(); 229 Iterator it = compilerConfigurator.knownMappers(); 230 while (it.hasNext()) { 231 String mn = (String ) it.next(); 232 logger.log(BasicLevel.DEBUG, "Register specific generator for mapper [" + mn + "]"); 233 generator.addMappingGenerator(mn, compilerConfigurator.getMOPFactory(mn)); 234 } 235 } 236 237 241 public void process() throws PException { 242 setupLogger(); 243 logger.log(BasicLevel.DEBUG, "process with jormc; files:" + compilerParameter.getInputFiles()); 245 Collection metaObjects = parseFiles(compilerParameter.getInputFiles()); 246 logger.log(BasicLevel.DEBUG, "generates " + metaObjects.size() + "jorm files"); 248 if (compilerParameter.isGeneratedPDFiles()) { 249 generateJormFiles(metaObjects); 250 } 251 if (!compilerParameter.isParseOnly()) { 253 Collection javafiles = generateFiles(metaObjects); 254 if (compilerParameter.isJavac()) { 255 compileFiles(javafiles); 256 } 257 } 258 } 259 260 263 public Collection parseFiles(Collection fns) throws PException { 264 setupLogger(); 265 setupMIManager(); 266 setupParser(); 267 logger.log(BasicLevel.DEBUG, "parsing of files"); 268 return parser.parse(fns.iterator()); 269 } 270 271 274 public void verifyMetaInfo(Collection mos) throws PException { 275 setupLogger(); 276 setupVerifier(); 277 verifier.verify(mos); 278 } 279 280 289 public Collection generateFiles(Collection c) throws PException { 290 setupLogger(); 291 setupMIManager(); 292 setupGenerator(); 293 TargetHolder targetHolder; 294 logger.log(BasicLevel.DEBUG, "Output directory=" + compilerParameter.getOutput()); 295 try { 296 targetHolder = new JavaFileHolder(compilerParameter.getOutput()); 297 } catch (Exception e) { 298 throw new PExceptionCompiler(e, "Impossible to create a JavaFileHolder"); 299 } 300 Iterator mos = (c != null 301 ? c.iterator() 302 : manager.getJormObjects().iterator()); 303 while (mos.hasNext()) { 304 MetaObject o = (MetaObject) mos.next(); 305 if (o instanceof CompositeName) { 306 logger.log(BasicLevel.INFO, "Generation for the [" + ((CompositeName) o).getName() + "] composite name"); 307 generator.generate(((CompositeName) o), targetHolder, compilerParameter); 308 } else if (o instanceof Class ) { 309 logger.log(BasicLevel.INFO, "Generation for the [" + ((Class ) o).getName() + "] class"); 310 try { 311 generator.generate((Class ) o, targetHolder, compilerParameter, compilerConfigurator); 312 } catch (Exception e) { 313 logger.log(BasicLevel.ERROR, 314 "Error during the generation of the class " 315 + ((Class ) o).getFQName(), e); 316 if (e instanceof PException) { 317 throw (PException) e; 318 } else { 319 throw new PExceptionCompiler(e, 320 "Error during the generation of the class " 321 + ((Class ) o).getFQName()); 322 } 323 } 324 } else { 325 throw new PExceptionCompiler("Meta Object is not a class or a composite name : " + o); 326 } 327 } 328 ArrayList al = new ArrayList (); 329 for (Iterator it = targetHolder.iterateFile(); it.hasNext();) { 330 try { 331 al.add(((File ) it.next()).getCanonicalPath()); 332 } catch (IOException e) { 333 throw new PException(e, "Cannot build the list of generated files."); 334 } 335 } 336 return al; 337 } 338 339 343 public void compileFiles(Collection javafiles) throws PException { 344 setupLogger(); 345 throw new UnsupportedOperationException ("NOT YET IMPLEMENTED!"); 347 } 348 349 352 public Manager getMIManager() { 353 return manager; 354 } 355 356 359 public void setMIManager(Manager manager) { 360 this.manager = manager; 361 } 362 363 public void setupWriter() throws PException { 364 if (writer != null) { 365 logger.log(BasicLevel.DEBUG, "The writer is already defined"); 366 return; 367 } 368 logger.log(BasicLevel.DEBUG, "Instanciate the jorm writer"); 369 writer = compilerConfigurator.getWriter(); 370 ((Loggable) writer).setLogger(loggerFactory.getLogger("org.objectweb.jorm.mi2xml")); 371 ((Loggable) writer).setLoggerFactory(loggerFactory); 372 writer.init(compilerConfigurator); 374 } 375 376 385 public Collection generateJormFiles(Collection c) throws PException { 386 setupLogger(); 387 setupMIManager(); 388 setupWriter(); 389 TargetHolder targetHolder; 390 logger.log(BasicLevel.DEBUG, "Output directory=" + compilerParameter.getOutput()); 391 try { 392 targetHolder = new JavaFileHolder(compilerParameter.getOutput()); 393 } catch (Exception e) { 394 throw new PExceptionCompiler(e, "Impossible to create a JavaFileHolder"); 395 } 396 Iterator mos = (c != null 397 ? c.iterator() 398 : manager.getJormObjects().iterator()); 399 while (mos.hasNext()) { 400 MetaObject o = (MetaObject) mos.next(); 401 if (o instanceof CompositeName) { 402 logger.log(BasicLevel.INFO, 403 ".pd file generation for the [" + ((CompositeName) o).getName() + "] composite name"); 404 writer.write(o, targetHolder); 405 406 } else if (o instanceof Class ) { 407 logger.log(BasicLevel.INFO, ".pd file generation for the [" + ((Class ) o).getName() + "] class"); 408 try { 409 writer.write(o, targetHolder); 410 } catch (Exception e) { 411 logger.log(BasicLevel.ERROR, 412 "Error during the .pd file generation of the class " 413 + ((Class ) o).getFQName(), e); 414 if (e instanceof PException) { 415 throw (PException) e; 416 } else { 417 throw new PExceptionCompiler(e, 418 "Error during the .pd file generation of the class " 419 + ((Class ) o).getFQName()); 420 } 421 } 422 } else { 423 throw new PExceptionCompiler("Meta Object is not a class or a composite name : " + o); 424 } 425 } 426 ArrayList al = new ArrayList (); 427 for (Iterator it = targetHolder.iterateFile(); it.hasNext();) { 428 try { 429 al.add(((File ) it.next()).getCanonicalPath()); 430 } catch (IOException e) { 431 throw new PExceptionCompiler(e, "Impossible to retrieve File descriptors"); 432 } 433 } 434 return al; 435 } 436 } 437 | Popular Tags |