KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > compiler > lib > JormCompiler


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.compiler.lib;
25
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
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 /**
53  *
54  * @author Sebastien Chassande-Barrioz
55  */

56 public class JormCompiler {
57     /**
58      * The logger used by the compiler to log messages
59      */

60     protected Logger logger;
61
62     /**
63      * The logger factory used by the compiler to log messages
64      */

65     protected LoggerFactory loggerFactory = null;
66
67     /**
68      * The manager is in charge of the meta information management.
69      */

70     protected Manager manager;
71
72     /**
73      * The parser object is able to parse XML files and build an internal
74      * representation of its content.
75      */

76     protected Parser parser;
77
78     /**
79      * The verifier is in charge to check the semantical validity of the meta
80      * information.
81      */

82     protected Verifier verifier;
83
84     /**
85      * generator is a simple JormGenerator object, which is able to generate
86      * java file.
87      */

88     protected Generator generator;
89
90     /**
91      * domWriter is a DomWriter object that generates .pd files from org.w3c.dom.Document
92      * objects representing Class and CompositeName objects.
93      */

94     protected Writer writer;
95
96     /**
97      * The compiler parameters
98      */

99     protected JormCompilerParameter compilerParameter;
100
101     /**
102      * The compiler configurator
103      */

104     protected JormCompilerConfigurator compilerConfigurator;
105
106     /**
107      * It builds a JormCompiler with the default JormCompilerParameter and
108      * JormCompilerParameter.
109      */

110     public JormCompiler() {
111         compilerParameter = new JormCompilerParameterImpl();
112         compilerConfigurator = new JormCompilerConfiguratorImpl();
113     }
114
115     /**
116      * It builds a JormCompiler with the specified JormCompilerParameter
117      * instance.
118      */

119     public JormCompiler(JormCompilerParameter cp, JormCompilerConfigurator cc) {
120         this.compilerParameter = cp;
121         this.compilerConfigurator = cc;
122     }
123
124     /**
125      * It retrieves the CompilerParameter associated to the compiler
126      * @return the compiler parameter
127      */

128     public JormCompilerParameter getCompilerParameter() {
129         return compilerParameter;
130     }
131
132     /**
133      * It retrieves the CompilerParameter associated to the compiler
134      * @return the compiler parameter
135      */

136     public JormCompilerConfigurator getCompilerConfigurator() {
137         return compilerConfigurator;
138     }
139
140     /**
141      * It assigns a LoggerFactory and a Logger if it is not already done.
142      */

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 JavaDoc it = compilerConfigurator.knownMappers();
164         while (it.hasNext()) {
165             String JavaDoc mn = (String JavaDoc) 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 JavaDoc it = compilerConfigurator.knownMappers();
185         while (it.hasNext()) {
186             String JavaDoc mn = (String JavaDoc) 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 JavaDoc it = compilerConfigurator.knownMappers();
208         while (it.hasNext()) {
209             String JavaDoc mn = (String JavaDoc) 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 JavaDoc it = compilerConfigurator.knownMappers();
230         while (it.hasNext()) {
231             String JavaDoc mn = (String JavaDoc) it.next();
232             logger.log(BasicLevel.DEBUG, "Register specific generator for mapper [" + mn + "]");
233             generator.addMappingGenerator(mn, compilerConfigurator.getMOPFactory(mn));
234         }
235     }
236
237     /**
238      * Creates class instances for the XML parser, the Meta Information Manager
239      * and manages the JormGenerator system.
240      */

241     public void process() throws PException {
242         setupLogger();
243         // parser part of the Jorm Compiler System
244
logger.log(BasicLevel.DEBUG, "process with jormc; files:" + compilerParameter.getInputFiles());
245         Collection JavaDoc metaObjects = parseFiles(compilerParameter.getInputFiles());
246         // generates the .pd files
247
logger.log(BasicLevel.DEBUG, "generates " + metaObjects.size() + "jorm files");
248         if (compilerParameter.isGeneratedPDFiles()) {
249             generateJormFiles(metaObjects);
250         }
251         //verifyMetaInfo(metaObjects);
252
if (!compilerParameter.isParseOnly()) {
253             Collection JavaDoc javafiles = generateFiles(metaObjects);
254             if (compilerParameter.isJavac()) {
255                 compileFiles(javafiles);
256             }
257         }
258     }
259
260     /**
261      * Parses the input files and build the meta information.
262      */

263     public Collection JavaDoc parseFiles(Collection JavaDoc 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     /**
272      * Verifies if the metainformation is in a clean state.
273      */

274     public void verifyMetaInfo(Collection JavaDoc mos) throws PException {
275         setupLogger();
276         setupVerifier();
277         verifier.verify(mos);
278     }
279
280     /**
281      * Generates java files for all meta object specified in the Collection
282      * parameter. If the parameter is null, all classes described in the meta
283      * information manager are generated.
284      * @param c is the collection of meta objects
285      * @return a Collection of String. The elements are the file names of the
286      * generated files
287      * @exception PException
288      */

289     public Collection JavaDoc generateFiles(Collection JavaDoc 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 JavaDoc e) {
298             throw new PExceptionCompiler(e, "Impossible to create a JavaFileHolder");
299         }
300         Iterator JavaDoc 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 JavaDoc) {
309                 logger.log(BasicLevel.INFO, "Generation for the [" + ((Class JavaDoc) o).getName() + "] class");
310                 try {
311                     generator.generate((Class JavaDoc) o, targetHolder, compilerParameter, compilerConfigurator);
312                 } catch (Exception JavaDoc e) {
313                     logger.log(BasicLevel.ERROR,
314                                "Error during the generation of the class "
315                                + ((Class JavaDoc) 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 JavaDoc) 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 JavaDoc al = new ArrayList JavaDoc();
329         for (Iterator JavaDoc it = targetHolder.iterateFile(); it.hasNext();) {
330             try {
331                 al.add(((File JavaDoc) it.next()).getCanonicalPath());
332             } catch (IOException JavaDoc e) {
333                 throw new PException(e, "Cannot build the list of generated files.");
334             }
335         }
336         return al;
337     }
338
339     /**
340      * Compiles with javac the java files generated by the generator system.
341      * @param javafiles All java files to be compiled with javac.
342      */

343     public void compileFiles(Collection JavaDoc javafiles) throws PException {
344         setupLogger();
345         // TODO
346
throw new UnsupportedOperationException JavaDoc("NOT YET IMPLEMENTED!");
347     }
348
349     /**
350      * Retrieves the MIanager instance. It can be null.
351      */

352     public Manager getMIManager() {
353         return manager;
354     }
355
356     /**
357      * Assignes the Meta info manager
358      */

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.setPathExplorer(compilerParameter.classpath);
373
writer.init(compilerConfigurator);
374     }
375
376     /**
377      * Generates .files for all meta object specified in the Collection
378      * parameter. If the parameter is null, all classes described in the meta
379      * information manager are generated.
380      * @param c is the collection of meta objects
381      * @return a Collection of String. The elements are the file names of the
382      * generated files
383      * @exception PException
384      */

385     public Collection JavaDoc generateJormFiles(Collection JavaDoc 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 JavaDoc e) {
394             throw new PExceptionCompiler(e, "Impossible to create a JavaFileHolder");
395         }
396         Iterator JavaDoc 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 JavaDoc) {
407                 logger.log(BasicLevel.INFO, ".pd file generation for the [" + ((Class JavaDoc) o).getName() + "] class");
408                 try {
409                     writer.write(o, targetHolder);
410                 } catch (Exception JavaDoc e) {
411                     logger.log(BasicLevel.ERROR,
412                                "Error during the .pd file generation of the class "
413                                + ((Class JavaDoc) 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 JavaDoc) 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 JavaDoc al = new ArrayList JavaDoc();
427         for (Iterator JavaDoc it = targetHolder.iterateFile(); it.hasNext();) {
428             try {
429                 al.add(((File JavaDoc) it.next()).getCanonicalPath());
430             } catch (IOException JavaDoc e) {
431                 throw new PExceptionCompiler(e, "Impossible to retrieve File descriptors");
432             }
433         }
434         return al;
435     }
436 }
437
Popular Tags