KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > generation > compiler > Compiler


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
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  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S.Chassande-Barrioz.
25  *
26  */

27
28 package org.objectweb.speedo.generation.compiler;
29
30 import org.apache.tools.ant.BuildException;
31 import org.apache.tools.ant.Project;
32 import org.apache.tools.ant.taskdefs.Javac;
33 import org.apache.tools.ant.types.Path;
34 import org.objectweb.speedo.generation.lib.AbstractGeneratorComponent;
35 import org.objectweb.speedo.generation.lib.NamingRules;
36 import org.objectweb.speedo.metadata.SpeedoClass;
37 import org.objectweb.speedo.metadata.SpeedoPackage;
38 import org.objectweb.speedo.metadata.SpeedoXMLDescriptor;
39 import org.objectweb.speedo.metadata.SpeedoIdentity;
40 import org.objectweb.speedo.api.SpeedoException;
41 import org.objectweb.speedo.api.SpeedoProperties;
42 import org.objectweb.util.monolog.api.BasicLevel;
43 import org.objectweb.util.monolog.wrapper.ant.MonologBuildListener;
44
45 import java.io.File JavaDoc;
46 import java.util.Iterator JavaDoc;
47
48 /**
49  * Compiles all generated <code>.java</code> files.
50  * <p>This class is based on <b>Ant</b> that is a Java-based build tool (see <a HREF="http://jakarta.apache.org/ant">http://jakarta.apache.org/ant</a> for more information).</p>
51  * @author S.Chassande-Barrioz
52  */

53 public class Compiler extends AbstractGeneratorComponent {
54
55     public final static String JavaDoc LOGGER_NAME
56             = SpeedoProperties.LOGGER_NAME + ".generation.compiler";
57
58     /**
59      * System separator character (e.g '/' under Unix systems)
60      */

61     public final static char separator = File.separatorChar;
62
63     /**
64      * Ant Task intended to java compilation
65      */

66     protected Javac compiler;
67
68     /**
69      * Initializes this Compiler.
70      * <ul><li>Initializes the Ant Task Javac so that it could compiles all generated classes.</li>
71      * <li>Sets the classpath, the source files, the destination directory and the other parameters.</li></ul>
72      */

73     public boolean init() {
74         logger = scp.loggerFactory.getLogger(LOGGER_NAME);
75         if (scp.getXmldescriptor().isEmpty())
76             return false;
77
78         // Sets the owning Project
79
compiler = new Javac();
80         Project projectCompiler = new Project();
81         if (scp.javac != null) {
82             String JavaDoc c = scp.javac.getProject().getProperty("build.compiler");
83             if (c != null && c.length() >0) {
84                 projectCompiler.setProperty("build.compiler", c);
85                 if (c.indexOf("eclipse") != -1) {
86                     projectCompiler.addBuildListener(new MonologBuildListener(logger));
87                 }
88             }
89         }
90         projectCompiler.init();
91         compiler.setProject(projectCompiler);
92
93         // Sets the classpath
94
compiler.setClasspath(scp.classpath);
95
96         // Sets the destination directory
97
File JavaDoc destDirs = new File JavaDoc(scp.output);
98         destDirs.mkdirs();
99         compiler.setDebug(true);
100         //compiler.setDebugLevel("-g:lines,vars,source");
101
compiler.setDestdir(destDirs);
102         boolean javacdebug = scp.loggerFactory.getLogger(LOGGER_NAME + ".javac")
103             .isLoggable(BasicLevel.DEBUG);
104         compiler.setVerbose(javacdebug);
105         compiler.setNowarn(!javacdebug);
106         compiler.setDeprecation(javacdebug);
107         compiler.setClasspath(scp.speedoclasspath);
108
109         // Sets the source files
110
Path srcCompiler = new Path(compiler.getProject());
111         srcCompiler.setLocation(new File JavaDoc(scp.output));
112
113         logger.log(BasicLevel.DEBUG, "input: " + scp.input);
114         logger.log(BasicLevel.DEBUG, "output: " + scp.output);
115         for (Iterator JavaDoc itDesc = scp.getXmldescriptor().values().iterator(); itDesc.hasNext();) {
116             SpeedoXMLDescriptor desc = (SpeedoXMLDescriptor) itDesc.next();
117             for (Iterator JavaDoc itPack = desc.jdoPackage.values().iterator(); itPack.hasNext();) {
118                 SpeedoPackage sp = (SpeedoPackage) itPack.next();
119
120                 for (Iterator JavaDoc itclass = sp.jdoClass.values().iterator(); itclass.hasNext();) {
121                     SpeedoClass sc = (SpeedoClass) itclass.next();
122                     String JavaDoc className = sc.getFQName();
123
124                     String JavaDoc c = NamingRules.proxyName(className).replace('.', '/') + ".java";
125                     compiler.createInclude().setName(c);
126                     logger.log(BasicLevel.DEBUG, c);
127
128                     c = NamingRules.fieldsName(className).replace('.', '/') + ".java";
129                     compiler.createInclude().setName(c);
130                     logger.log(BasicLevel.DEBUG, c);
131
132                     c = NamingRules.homeName(className).replace('.', '/') + ".java";
133                     compiler.createInclude().setName(c);
134                     logger.log(BasicLevel.DEBUG, c);
135
136                     c = NamingRules.fpncName(className).replace('.', '/') + ".java";
137                     compiler.createInclude().setName(c);
138                     logger.log(BasicLevel.DEBUG, c);
139
140                     c = NamingRules.kfpncName(className).replace('.', '/') + ".java";
141                     compiler.createInclude().setName(c);
142                     logger.log(BasicLevel.DEBUG, c);
143
144                     c = NamingRules.fqMappingName(className).replace('.', '/') + ".java";
145                     compiler.createInclude().setName(c);
146                     logger.log(BasicLevel.DEBUG, c);
147
148                     if (sc.identityType == SpeedoIdentity.USER_ID
149                             && sc.generateObjectId()) {
150                         c = NamingRules.generatedObjectIdName(sc.getFQName())
151                             .replace('.', '/') + ".java";
152                         compiler.createInclude().setName(c);
153                         logger.log(BasicLevel.DEBUG, c);
154                     }
155                 }
156             }
157         }
158         compiler.setSrcdir(srcCompiler);
159         return true;
160     }
161
162     /**
163      * Tries to process the compilation internally.
164      *
165      * @exception SpeedoException if compilation fails
166      */

167     public void process() throws SpeedoException {
168         if (scp.getXmldescriptor().isEmpty())
169             return;
170         try {
171             compiler.execute();
172         } catch (BuildException e) {
173             throw new SpeedoException("Error during the compilation", e);
174         }
175     }
176 }
177
Popular Tags