KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > tools > CayenneGenerator


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.tools;
21
22 import java.io.File JavaDoc;
23
24 import org.apache.cayenne.gen.AntClassGenerator;
25 import org.apache.cayenne.gen.ClassGenerator;
26 import org.apache.cayenne.gen.DefaultClassGenerator;
27 import org.apache.tools.ant.BuildException;
28 import org.apache.tools.ant.Task;
29 import org.apache.tools.ant.types.Path;
30
31 import foundrylogic.vpp.VPPConfig;
32
33 /**
34  * Ant task to perform class generation from data map. This class is an Ant adapter to
35  * DefaultClassGenerator class.
36  *
37  * @author Andrus Adamchik, Kevin Menard
38  */

39 public class CayenneGenerator extends Task {
40
41     protected String JavaDoc includeEntitiesPattern;
42     protected String JavaDoc excludeEntitiesPattern;
43     protected VPPConfig vppConfig;
44
45     protected File JavaDoc map;
46     protected File JavaDoc additionalMaps[];
47     protected DefaultClassGenerator generator;
48
49     protected CayenneGeneratorUtil generatorUtil;
50     
51     public CayenneGenerator() {
52         generator = createGenerator();
53         generatorUtil = new CayenneGeneratorUtil();
54     }
55
56     /**
57      * Factory method to create internal class generator. Called from constructor.
58      */

59     protected DefaultClassGenerator createGenerator() {
60         AntClassGenerator gen = new AntClassGenerator();
61         gen.setParentTask(this);
62         return gen;
63     }
64
65     /**
66      * Executes the task. It will be called by ant framework.
67      */

68     public void execute() throws BuildException {
69         validateAttributes();
70         
71         // Take care of setting up VPP for the generator.
72
if (false == ClassGenerator.VERSION_1_1.equals(generator.getVersionString())) {
73             initializeVppConfig();
74             generator.setVppConfig(vppConfig);
75         }
76         
77         // Initialize the util generator state.
78
generatorUtil.setAdditionalMaps(additionalMaps);
79         generatorUtil.setExcludeEntitiesPattern(excludeEntitiesPattern);
80         generatorUtil.setGenerator(generator);
81         generatorUtil.setIncludeEntitiesPattern(includeEntitiesPattern);
82         generatorUtil.setLogger(new AntTaskLogger(this));
83         generatorUtil.setMap(map);
84
85         try {
86             generatorUtil.execute();
87         }
88         catch (Exception JavaDoc e) {
89             throw new BuildException(e);
90         }
91     }
92
93     /**
94      * Validates atttributes that are not related to internal DefaultClassGenerator.
95      * Throws BuildException if attributes are invalid.
96      */

97     protected void validateAttributes() throws BuildException {
98         if (map == null && this.getProject() == null) {
99             throw new BuildException("either 'map' or 'project' is required.");
100         }
101     }
102
103     /**
104      * Sets the map.
105      *
106      * @param map The map to set
107      */

108     public void setMap(File JavaDoc map) {
109         this.map = map;
110     }
111
112     /**
113      * Sets the additional DataMaps.
114      *
115      * @param additionalMapsPath The additional DataMaps to set
116      */

117     public void setAdditionalMaps(Path additionalMapsPath) {
118         String JavaDoc additionalMapFilenames[] = additionalMapsPath.list();
119         this.additionalMaps = new File JavaDoc[additionalMapFilenames.length];
120
121         for (int i = 0; i < additionalMapFilenames.length; i++) {
122             additionalMaps[i] = new File JavaDoc(additionalMapFilenames[i]);
123         }
124     }
125
126     /**
127      * Sets the destDir.
128      */

129     public void setDestDir(File JavaDoc destDir) {
130         generator.setDestDir(destDir);
131     }
132
133     /**
134      * Sets <code>overwrite</code> property.
135      */

136     public void setOverwrite(boolean overwrite) {
137         generator.setOverwrite(overwrite);
138     }
139
140     /**
141      * Sets <code>makepairs</code> property.
142      */

143     public void setMakepairs(boolean makepairs) {
144         generator.setMakePairs(makepairs);
145     }
146
147     /**
148      * Sets <code>template</code> property.
149      */

150     public void setTemplate(String JavaDoc template) {
151         generator.setTemplate(template);
152     }
153
154     /**
155      * Sets <code>supertemplate</code> property.
156      */

157     public void setSupertemplate(String JavaDoc supertemplate) {
158         generator.setSuperTemplate(supertemplate);
159     }
160
161     /**
162      * Sets <code>usepkgpath</code> property.
163      */

164     public void setUsepkgpath(boolean usepkgpath) {
165         generator.setUsePkgPath(usepkgpath);
166     }
167
168     /**
169      * Sets <code>superpkg</code> property.
170      */

171     public void setSuperpkg(String JavaDoc superpkg) {
172         generator.setSuperPkg(superpkg);
173     }
174
175     /**
176      * Sets <code>client</code> property.
177      *
178      * @since 1.2
179      */

180     public void setClient(boolean client) {
181         generator.setClient(client);
182     }
183
184     /**
185      * Sets <code>version</code> property.
186      *
187      * @since 1.2
188      */

189     public void setVersion(String JavaDoc versionString) {
190         try {
191             generator.setVersionString(versionString);
192         }
193         catch (IllegalStateException JavaDoc e) {
194             throw new BuildException(e.getMessage(), e);
195         }
196     }
197
198     /**
199      * Sets <code>encoding</code> property that allows to generate files using
200      * non-default encoding.
201      *
202      * @since 1.2
203      */

204     public void setEncoding(String JavaDoc encoding) {
205         generator.setEncoding(encoding);
206     }
207
208     /**
209      * Sets <code>excludeEntitiesPattern</code> property.
210      *
211      * @since 1.2
212      */

213     public void setExcludeEntities(String JavaDoc excludeEntitiesPattern) {
214         this.excludeEntitiesPattern = excludeEntitiesPattern;
215     }
216
217     /**
218      * Sets <code>includeEntitiesPattern</code> property.
219      *
220      * @since 1.2
221      */

222     public void setIncludeEntities(String JavaDoc includeEntitiesPattern) {
223         this.includeEntitiesPattern = includeEntitiesPattern;
224     }
225
226     /**
227      * Sets <code>outputPattern</code> property.
228      *
229      * @since 1.2
230      */

231     public void setOutputPattern(String JavaDoc outputPattern) {
232         generator.setOutputPattern(outputPattern);
233     }
234
235     /**
236      * Sets <code>mode</code> property.
237      *
238      * @since 1.2
239      */

240     public void setMode(String JavaDoc mode) {
241         generator.setMode(mode);
242     }
243
244     /**
245      * Provides a <code>VPPConfig</code> object to configure. (Written with
246      * createConfig() instead of addConfig() to avoid run-time dependency on VPP).
247      *
248      * @since 1.2
249      */

250     public Object JavaDoc createConfig() {
251         this.vppConfig = new VPPConfig();
252         return this.vppConfig;
253     }
254     
255     /**
256      * If no VppConfig element specified, use the default one.
257      *
258      * @since 1.2
259      */

260     private void initializeVppConfig() {
261         if (vppConfig == null) {
262             vppConfig = VPPConfig.getDefaultConfig(getProject());
263         }
264     }
265 }
266
Popular Tags