KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > util > lib > AntJormParser


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 package org.objectweb.jorm.util.lib;
24
25 import org.apache.tools.ant.taskdefs.MatchingTask;
26 import org.apache.tools.ant.types.DTDLocation;
27 import org.apache.tools.ant.types.Path;
28 import org.apache.tools.ant.types.Reference;
29 import org.apache.tools.ant.BuildException;
30 import org.objectweb.jorm.util.io.api.PathExplorer;
31 import org.objectweb.jorm.util.io.lib.DirJavaExplorer;
32 import org.objectweb.jorm.compiler.api.JormCompilerParameter;
33 import org.objectweb.jorm.compiler.lib.JormCompiler;
34 import org.objectweb.jorm.compiler.lib.JormCompilerParameterImpl;
35 import org.objectweb.jorm.compiler.lib.JormCompilerConfiguratorImpl;
36 import org.objectweb.jorm.compiler.api.JormCompilerConfigurator;
37 import org.objectweb.jorm.api.PException;
38
39 import java.io.File JavaDoc;
40 import java.util.*;
41
42 public class AntJormParser extends MatchingTask {
43     private String JavaDoc jormConfFile = "jorm.properties";
44     private File JavaDoc pdDir = null;
45
46     /**
47      * System separator character (e.g '/' under Unix systems)
48      */

49     private final static char separator = File.separatorChar;
50
51     private boolean antDtdLocationsSet = false;
52     private ArrayList antDtdLocations = new ArrayList();
53     private boolean classpathSet = false;
54     private PathExplorer classpath = new DirJavaExplorer();
55     private boolean antLogConfFileSet = false;
56     private String JavaDoc antLogConfFile;
57     private boolean antProjectNameSet = false;
58     private String JavaDoc antProjectName;
59     private boolean antVerboseSet = false;
60     private boolean antVerbose;
61
62     private JormCompilerParameter jcp = null;
63     private JormCompilerConfigurator jcc = null;
64     private JormCompiler jormcompiler = null;
65
66     public AntJormParser() {
67
68         jcp = new JormCompilerParameterImpl();
69         jcc = new JormCompilerConfiguratorImpl();
70         jormcompiler = new JormCompiler(jcp, jcc);
71         //TODO
72
//jormcompiler = new JormCompiler();
73
//JormCompilerParameter jcp = jormcompiler.getCompilerParameter();
74
//JormConfiguratorImpl jcc = jormcompiler.getCompilerConfigurator();
75
}
76
77     /**
78      * It assigns the directory name where the include and exclude clause has
79      * been based. (REQUIRED)
80      */

81     public void setPdDir(File JavaDoc d) {
82         pdDir = d;
83         classpath.addPath(d.getAbsolutePath());
84                         System.out.println("pdDir " + pdDir.getAbsolutePath());
85     }
86
87     /**
88      * It assigns the classpath where jorm can load .pd files which are
89      * needed (the dependecies).
90      * (OPTIONAL: default value is the PdDir)
91      */

92     public void setOtherPdFiles(Path cp) {
93         String JavaDoc[] dl = cp.list();
94         for (int i = 0; i < dl.length; i++) {
95             classpath.addPath(dl[i]);
96             classpathSet = true;
97         }
98     }
99
100     public void setOtherPdFilesRef(Reference r) {
101         Path p = new Path(project);
102         p.setRefid(r);
103         setOtherPdFiles(p);
104     }
105
106     /**
107      * Create a DTD location record. This stores the location of a DTD. The
108      * DTD is identified by its public Id. The location may either be a file
109      * location or a resource location.
110      * @return the DTD location object to be configured by Ant
111      */

112     public DTDLocation createDTD() {
113         DTDLocation dtdLocation = new DTDLocation();
114         antDtdLocations.add(dtdLocation);
115         antDtdLocationsSet = true;
116         return dtdLocation;
117     }
118
119     /**
120      * It assigns the jorm configuration file. (REQUIRED)
121      * @param s the string representation of the configuration file.
122      */

123     public void setJormConf(String JavaDoc s) {
124         jormConfFile = s;
125     }
126
127     /**
128      * It assigns the jorm configuration file. (REQUIRED)
129      * @param s the string representation of the configuration file.
130      */

131     public void setLogConf(String JavaDoc s) {
132         antLogConfFile = s;
133         antLogConfFileSet = true;
134     }
135
136     /**
137      * It assigns the project name which must be used.
138      * This parameter can be override for a class if you use a local jorm
139      * configuration file which defines also the property.
140      * (OPTIONAL default value = "" empty string)
141      */

142     public void setProjectName(String JavaDoc s) {
143         antProjectName = s;
144         antProjectNameSet = true;
145     }
146
147     /**
148      * It assigns the verbose mode to the jorm generator.
149      * This parameter can be override for a class if you use a local jorm
150      * configuration file which defines also the property.
151      * (OPTIONAL the default value is false)
152      */

153     public void setVerbose(boolean verbose) {
154         antVerbose = verbose;
155         antVerboseSet = true;
156     }
157
158     public void initJormCompiler() throws PException {
159         try {
160             if (jormConfFile == null) {
161                 jcc.configure();
162             } // set jcc.mappers from data stored in the jorm.properties file
163
else {
164               jcc.configure(jormConfFile);
165             } // set jcc.mappers from data stored in jormConfFile
166
log("jcc.configure() ----------- ok");
167             Collection c = jcc.getSubMappers("rdb");
168             log("rdb sub mappers: " + c);
169             if (!antLogConfFileSet) {
170                 jcc.configureLog(jormConfFile);
171             } else {
172                 jcc.configureLog(antLogConfFile);
173             }
174             log("jcc.configureLog() ----------- ok");
175             // TODO : jcc.knownMappers() seems to be no more used in the loadConfFile() method.
176
log("jormcOpts file= " + jcc.getJormcOptsFile());
177             jcp.loadConfFile(jcc.getGlobalJormcOptsFile(), jcc.knownMappers());
178             log("jcp.loadConfFile ----------- ok");
179             log("pdDir= " + pdDir);
180             // The following properties can be set in the jormcOpts.properties file or overloaded in the code
181
if (antDtdLocationsSet) {
182                 jcp.setDtdLocations(antDtdLocations);
183             }
184             if (antProjectName != null) {
185                 jcp.setProjectName(antProjectName);
186             }
187             log("jcp.projectName= " + jcp.getProjectName());
188             Collection temp = new MyArrayList(super.getDirectoryScanner(pdDir).getIncludedFiles());
189             String JavaDoc inputFile = null;
190             for (Iterator it = temp.iterator(); it.hasNext();) {
191                 inputFile = pdDir.getAbsolutePath() + separator + (String JavaDoc) it.next();
192                 jcp.getInputFiles().add(inputFile);
193                 log("input file= " + inputFile);
194             }
195             log("jcp.inputFiles ----------- ok");
196             if (classpathSet) {
197                 jcp.setClasspath(classpath);
198             } else {
199                 DirJavaExplorer pe = new DirJavaExplorer();
200                 pe.setLogger(jcc.getLoggerFactory().getLogger("org.objectweb.jorm.io.pathexplorer"));
201                 jcp.setClasspath(pe);
202             }
203             log("jcp.classpath ----------- ok");
204             jcp.setGeneratedWithMapperPackage(false);
205             // TODO: jcp.logConFile ?
206
if (antLogConfFileSet) {
207                 jcp.setLogConfFile(antLogConfFile);
208             }
209             if (antVerboseSet) {
210                 jcp.setVerbose(antVerbose);
211             }
212         } catch (PException e) {;
213             throw new PException(e, "Impossible to configure the Jorm parser");
214         }
215     }
216
217
218     /**
219      * execute method of the Ant task
220      */

221     public void execute() throws BuildException {
222         try {
223             initJormCompiler();
224             jormcompiler.parseFiles(jcp.getInputFiles());
225             log("parseFiles...... done");
226          } catch (PException e) {
227             Exception JavaDoc current = e;
228             while (current instanceof PException
229                     && ((PException) current).getNestedException() != null) {
230                 current = ((PException) current).getNestedException();
231             }
232             throw new BuildException(current);
233         }
234     }
235 }
236
237 class MyArrayList extends ArrayList {
238     public MyArrayList(Object JavaDoc[] os) {
239         super(os.length);
240         for (int i = 0; i < os.length; i++)
241             add(os[i]);
242     }
243 }
244
Popular Tags