KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.objectweb.jorm.api.PException;
27 import org.objectweb.jorm.compiler.api.PExceptionCompiler;
28 import org.objectweb.jorm.compiler.api.JormCompilerParameter;
29 import org.objectweb.jorm.util.io.api.PathExplorer;
30 import org.objectweb.jorm.util.io.lib.DirJavaExplorer;
31 import org.objectweb.util.monolog.api.BasicLevel;
32 import org.objectweb.util.monolog.api.Logger;
33 import org.objectweb.util.monolog.api.LoggerFactory;
34
35 import java.io.FileInputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Collection JavaDoc;
40 import java.util.Properties JavaDoc;
41 import java.util.StringTokenizer JavaDoc;
42 import java.util.Iterator JavaDoc;
43
44 /**
45  * A JormCompilerParameter object is an object which manages informations
46  * read by the command line parser. These informations are needed to
47  * the compiler system to parse, generate, and compile.
48  * @author X. Spengler
49  */

50 public class JormCompilerParameterImpl implements JormCompilerParameter {
51     private String JavaDoc logConfFile = null;
52     /**
53      * classpath is an object which stores a set of paths (or jar files).
54      */

55     private PathExplorer classpath = new DirJavaExplorer();
56
57     /**
58      * path where files will be generated
59      */

60     private String JavaDoc output = null;
61
62     /**
63      * verbose flag, set by the user
64      */

65     private boolean verbose = false;
66
67     /**
68      * a vector of input file names (.pd files)
69      */

70     private Collection JavaDoc inputFiles = new ArrayList JavaDoc();
71
72     /**
73      * keep the source files or not.
74      */

75     private boolean keepSrc = true;
76
77     /**
78      * javaCompiler is the name of the javac compiler used to compile
79      * the java sources.
80      */

81     private String JavaDoc javacName = "javac";
82
83     /**
84      * projectName is the name of the project for which we generate
85      */

86     private String JavaDoc projectName = "";
87
88     /**
89      * if the compile flag is set to true, the java sources will be compiled,
90      * if false, there will be no compilation.
91      */

92     private boolean javac = false;
93
94     /**
95      * if the build flag is set to true, files are generated, else false.
96      */

97     private boolean parseOnly = false;
98
99     /**
100      *
101      */

102     private String JavaDoc bindingInheritance = null;
103
104     /**
105      *
106      */

107     private String JavaDoc classMappingInheritance = null;
108
109     /**
110      * if the bindingAbstract is set to true, the binding object will be
111      * abstract, else false
112      */

113     private boolean bindingAbstract = false;
114
115     /**
116      * a simple logger to log
117      */

118     private Logger logger = null;
119
120     /**
121      * the list of DTD locations
122      */

123     private ArrayList JavaDoc dtdLocations = new ArrayList JavaDoc();
124
125     /**
126      * indicates if the jorm .pd files must be generated
127      */

128     private boolean generatedPDFiles = false;
129
130     private boolean generatedWithMapperPackage = true;
131
132     /**
133      * Prints informations managed by the current object.
134      */

135     public void print() {
136         if (logger.isLoggable(BasicLevel.DEBUG)) {
137             logger.log(BasicLevel.DEBUG, "output: " + output);
138             logger.log(BasicLevel.DEBUG, "verbose: " + verbose);
139             logger.log(BasicLevel.DEBUG, "inputFiles: ");
140             Iterator JavaDoc it = inputFiles.iterator();
141             while (it.hasNext()) {
142                 logger.log(BasicLevel.DEBUG, ((String JavaDoc) it.next()));
143             }
144         }
145     }
146
147     /**
148      * Computes the actual class name which the generated PClassMapping class
149      * must extend.
150      * @param cn The class name of the persistent class for which the
151      * code is generated.
152      * @return The computed class name.
153      */

154     public String JavaDoc computePClassMappingInheritance(String JavaDoc cn) {
155         return computeInheritanceString(cn, classMappingInheritance);
156     }
157
158     /**
159      * Computes the actual class name which the generated PBinding class
160      * must extend.
161      * @param cn The class name of the persistent class for which the
162      * code is generated.
163      * @return The computed class name.
164      */

165     public String JavaDoc computePBindingInheritance(String JavaDoc cn) {
166         return computeInheritanceString(cn, bindingInheritance);
167     }
168
169     /**
170      * Load a jorm configuration file.
171      * @param file the name of the jorm configuration file
172      */

173     public void loadConfFile(String JavaDoc file, Iterator JavaDoc knownmappers) throws PException {
174         try {
175             loadConfFile(new FileInputStream JavaDoc(file), knownmappers);
176         } catch (IOException JavaDoc e) {
177             throw new PExceptionCompiler(e, "Cannot open the jorm configuration file: " + file);
178         }
179     }
180
181     /**
182      * Load a jorm configuration file.
183      * @param in the input stream of the jorm configuration file
184      */

185     public void loadConfFile(InputStream JavaDoc in, Iterator JavaDoc knownmappers) throws PException {
186         if (in == null) {
187             throw new PExceptionCompiler("Cannot load jorm configuration file: InputStream is null.");
188         }
189         Properties JavaDoc p = new Properties JavaDoc();
190         try {
191             p.load(in);
192         } catch (IOException JavaDoc e) {
193             throw new PExceptionCompiler(e, "An error occured during the loading of the jorm configuration file.");
194         }
195         String JavaDoc s = null;
196         if ((s = p.getProperty("log.config.file")) != null)
197             logConfFile = s;
198         if ((s = p.getProperty("keepsrc")) != null)
199             keepSrc = new Boolean JavaDoc(s).booleanValue();
200         if ((s = p.getProperty("verbose")) != null)
201             verbose = new Boolean JavaDoc(s).booleanValue();
202         if ((s = p.getProperty("javacompiler")) != null)
203             javacName = s;
204         if ((s = p.getProperty("projectname")) != null)
205             projectName = s;
206         if ((s = p.getProperty("bindinginheritance")) != null)
207             bindingInheritance = s;
208         if ((s = p.getProperty("classmappinginheritance")) != null)
209             classMappingInheritance = s;
210         if ((s = p.getProperty("inputfiles")) != null) {
211             StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(s, ",");
212             while (tok.hasMoreTokens()) {
213                 inputFiles.add(tok.nextToken().trim());
214             }
215         }
216     }
217
218     public PathExplorer getClasspath() {
219         return classpath;
220     }
221
222     public String JavaDoc getOutput() {
223         return output;
224     }
225
226     public boolean isVerbose() {
227         return verbose;
228     }
229
230     public Collection JavaDoc getInputFiles() {
231         return inputFiles;
232     }
233
234     public boolean isKeepSrc() {
235         return keepSrc;
236     }
237
238     public String JavaDoc getJavacName() {
239         return javacName;
240     }
241
242     public String JavaDoc getProjectName() {
243         return projectName;
244     }
245
246     public boolean isJavac() {
247         return javac;
248     }
249
250     public boolean isParseOnly() {
251         return parseOnly;
252     }
253
254     public String JavaDoc getBindingInheritance() {
255         return bindingInheritance;
256     }
257
258     public String JavaDoc getClassMappingInheritance() {
259         return classMappingInheritance;
260     }
261
262     public boolean isBindingAbstract() {
263         return bindingAbstract;
264     }
265
266     public ArrayList JavaDoc getDtdLocations() {
267         return dtdLocations;
268     }
269
270     public boolean isGeneratedPDFiles() {
271         return generatedPDFiles;
272     }
273
274     public boolean isGeneratedWithMapperPackage() {
275         return generatedWithMapperPackage;
276     }
277
278     // IMPLEMENTATION OF METHODS FROM THE Loggable INTERFACE
279

280     public void setLogger(Logger logger) {
281         this.logger = logger;
282     }
283
284     public void setLoggerFactory(LoggerFactory lf) {
285     }
286
287     public Logger getLogger() {
288         return logger;
289     }
290
291     public LoggerFactory getLoggerFactory() {
292         return null;
293     }
294
295     // IMPLEMENTATION OF METHODS FROM THE Cloneable INTERFACE
296

297     public Object JavaDoc clone() {
298         JormCompilerParameterImpl res = new JormCompilerParameterImpl();
299         res.classpath = classpath;
300         res.output = output;
301         res.verbose = verbose;
302         res.inputFiles = inputFiles;
303         res.keepSrc = keepSrc;
304         res.javacName = javacName;
305         res.projectName = projectName;
306         res.javac = javac;
307         res.parseOnly = parseOnly;
308         res.bindingInheritance = bindingInheritance;
309         res.classMappingInheritance = classMappingInheritance;
310         res.bindingAbstract = bindingAbstract;
311         res.logger = logger;
312         res.dtdLocations = dtdLocations;
313         return res;
314     }
315
316     // PRIVATE METHODS
317

318     private static String JavaDoc computeInheritanceString(String JavaDoc cn, String JavaDoc pattern) {
319         if (cn == null || cn.length() == 0) {
320             return null;
321         }
322         int pIdx = pattern.indexOf(INHERITANCE_PACKAGE_PATTERN);
323         int cIdx = pattern.indexOf(INHERITANCE_CLASSNAME_PATTERN);
324
325         if (pIdx == -1 && cIdx == -1) {
326             return pattern;
327         } else {
328             String JavaDoc[] icn = isolatePackageName(cn);
329             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(pattern, "%cp", true);
330             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
331             boolean hasPercent = false;
332             while (st.hasMoreTokens()) {
333                 String JavaDoc token = st.nextToken();
334                 if (token.equals("%") && !hasPercent) {
335                     hasPercent = true;
336                 } else if (hasPercent) {
337                     if (token.equals("p")) {
338                         if (icn[0] != null) {
339                             sb.append(icn[0]);
340                         }
341                     } else if (token.equals("c")) {
342                         sb.append(icn[1]);
343                     } else {
344                         //ignore the unknown tokens
345
}
346                     hasPercent = false;
347                 } else {
348                     sb.append(token);
349                 }
350             }
351             return sb.toString();
352         }
353     }
354
355     private static String JavaDoc[] isolatePackageName(String JavaDoc fqcn) {
356         int idx = fqcn.lastIndexOf(".");
357         if (idx == -1)
358             return new String JavaDoc[]{null, fqcn};
359         else
360             return new String JavaDoc[]{
361                 fqcn.substring(0, idx + 1),
362                 fqcn.substring(idx + 1, fqcn.length())
363             };
364     }
365
366     public void setClasspath(PathExplorer classpath) {
367         this.classpath = classpath;
368     }
369
370     public void setOutput(String JavaDoc output) {
371         this.output = output;
372     }
373
374     public void setVerbose(boolean verbose) {
375         this.verbose = verbose;
376     }
377
378     public void setKeepSrc(boolean keepSrc) {
379         this.keepSrc = keepSrc;
380     }
381
382     public void setProjectName(String JavaDoc projectName) {
383         this.projectName = projectName;
384     }
385
386     public void setJavac(boolean javac) {
387         this.javac = javac;
388     }
389
390     public void setBindingInheritance(String JavaDoc bindingInheritance) {
391         this.bindingInheritance = bindingInheritance;
392     }
393
394     public void setClassMappingInheritance(String JavaDoc classMappingInheritance) {
395         this.classMappingInheritance = classMappingInheritance;
396     }
397
398     public void setBindingAbstract(boolean bindingAbstract) {
399         this.bindingAbstract = bindingAbstract;
400     }
401
402     public void setDtdLocations(ArrayList JavaDoc dtdLocations) {
403         this.dtdLocations = dtdLocations;
404     }
405
406     public void setGeneratedPDFiles(boolean generatedPDFiles) {
407         this.generatedPDFiles = generatedPDFiles;
408     }
409
410     public void setInputFiles(Collection JavaDoc inputFiles) {
411         this.inputFiles = inputFiles;
412     }
413
414     public void setGeneratedWithMapperPackage(boolean generatedWithMapperPackage) {
415         this.generatedWithMapperPackage = generatedWithMapperPackage;
416     }
417
418     public void setLogConfFile(String JavaDoc logConfFile) {
419         this.logConfFile = logConfFile;
420     }
421 }
422
Popular Tags