KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.tools;
57
58 import java.io.File JavaDoc;
59 import java.util.ArrayList JavaDoc;
60 import java.util.Collections JavaDoc;
61 import java.util.List JavaDoc;
62
63 import org.apache.tools.ant.BuildException;
64 import org.apache.tools.ant.types.Path;
65 import org.objectstyle.cayenne.gen.AntClassGenerator;
66 import org.objectstyle.cayenne.gen.ClassGenerator;
67 import org.objectstyle.cayenne.gen.DefaultClassGenerator;
68 import org.objectstyle.cayenne.map.DataMap;
69 import org.objectstyle.cayenne.map.EntityResolver;
70 import org.objectstyle.cayenne.map.MapLoader;
71 import org.objectstyle.cayenne.util.Util;
72 import org.xml.sax.InputSource JavaDoc;
73
74 import foundrylogic.vpp.VPPConfig;
75
76 /**
77  * Ant task to perform class generation from data map. This class is an Ant adapter to
78  * DefaultClassGenerator class.
79  *
80  * @author Andrei Adamchik
81  */

82 public class CayenneGenerator extends CayenneTask {
83     
84     protected String JavaDoc includeEntitiesPattern;
85     protected String JavaDoc excludeEntitiesPattern;
86     protected VPPConfig vppConfig;
87
88     protected File JavaDoc map;
89     protected File JavaDoc additionalMaps[];
90     protected DefaultClassGenerator generator;
91
92     public CayenneGenerator() {
93         bootstrapVelocity();
94         generator = createGenerator();
95     }
96
97     /**
98      * Factory method to create internal class generator. Called from constructor.
99      */

100     protected DefaultClassGenerator createGenerator() {
101         AntClassGenerator gen = new AntClassGenerator();
102         gen.setParentTask(this);
103         return gen;
104     }
105
106     /** Initialize Velocity with class loader of the right class. */
107     protected void bootstrapVelocity() {
108         ClassGenerator.bootstrapVelocity(this.getClass());
109     }
110
111     /**
112      * Executes the task. It will be called by ant framework.
113      */

114     public void execute() throws BuildException {
115         configureLogging();
116         validateAttributes();
117
118         try {
119             processMap();
120         }
121         catch (Throwable JavaDoc th) {
122             th = Util.unwindException(th);
123
124             String JavaDoc thMessage = th.getLocalizedMessage();
125             String JavaDoc message = "Error generating classes: ";
126             message += (!Util.isEmptyString(thMessage)) ? thMessage : th
127                     .getClass()
128                     .getName();
129
130             super.log(message);
131             throw new BuildException(message, th);
132         }
133     }
134
135     protected void processMap() throws Exception JavaDoc {
136
137         DataMap dataMap = loadDataMap();
138         DataMap additionalDataMaps[] = loadAdditionalDataMaps();
139
140         // Create MappingNamespace for maps.
141
EntityResolver entityResolver = new EntityResolver(Collections.singleton(dataMap));
142         dataMap.setNamespace(entityResolver);
143         for (int i = 0; i < additionalDataMaps.length; i++) {
144             entityResolver.addDataMap(additionalDataMaps[i]);
145             additionalDataMaps[i].setNamespace(entityResolver);
146         }
147
148         List JavaDoc entityList = new ArrayList JavaDoc(dataMap.getObjEntities());
149
150         NamePatternMatcher namePatternMatcher = new NamePatternMatcher(this, includeEntitiesPattern, excludeEntitiesPattern);
151         namePatternMatcher.filter(entityList);
152
153         if (false == ClassGenerator.VERSION_1_1.equals(generator.getVersionString()))
154         {
155             initializeVppConfig();
156             generator.setVppConfig(vppConfig);
157         }
158
159         generator.setTimestamp(map.lastModified());
160         generator.setDataMap(dataMap);
161         generator.setObjEntities(entityList);
162         generator.validateAttributes();
163         generator.execute();
164     }
165
166     /** Loads and returns a DataMap by File. */
167     protected DataMap loadDataMap(File JavaDoc mapName) throws Exception JavaDoc {
168         InputSource JavaDoc in = new InputSource JavaDoc(mapName.getCanonicalPath());
169         return new MapLoader().loadDataMap(in);
170     }
171
172     /** Loads and returns DataMap based on <code>map</code> attribute. */
173     protected DataMap loadDataMap() throws Exception JavaDoc {
174         return loadDataMap(map);
175     }
176
177     /** Loads and returns DataMap based on <code>map</code> attribute. */
178     protected DataMap[] loadAdditionalDataMaps() throws Exception JavaDoc {
179         if (null == additionalMaps)
180         {
181             return new DataMap[0];
182         }
183         
184         DataMap dataMaps[] = new DataMap[additionalMaps.length];
185         for (int i = 0; i < additionalMaps.length; i++) {
186             dataMaps[i] = loadDataMap(additionalMaps[i]);
187         }
188         return dataMaps;
189     }
190
191     /**
192      * Validates atttributes that are not related to internal DefaultClassGenerator.
193      * Throws BuildException if attributes are invalid.
194      */

195     protected void validateAttributes() throws BuildException {
196         if (map == null && this.getProject() == null) {
197             throw new BuildException("either 'map' or 'project' is required.");
198         }
199     }
200
201     /**
202      * Sets the map.
203      *
204      * @param map The map to set
205      */

206     public void setMap(File JavaDoc map) {
207         this.map = map;
208     }
209
210     /**
211      * Sets the additional DataMaps.
212      *
213      * @param additionalMaps The additional DataMaps to set
214      */

215     public void setAdditionalMaps(Path additionalMapsPath) {
216         String JavaDoc additionalMapFilenames[] = additionalMapsPath.list();
217         this.additionalMaps = new File JavaDoc[additionalMapFilenames.length];
218
219         for (int i = 0; i < additionalMapFilenames.length; i++) {
220             additionalMaps[i] = new File JavaDoc(additionalMapFilenames[i]);
221         }
222     }
223
224     /**
225      * Sets the destDir.
226      */

227     public void setDestDir(File JavaDoc destDir) {
228         generator.setDestDir(destDir);
229     }
230
231     /**
232      * Sets <code>overwrite</code> property.
233      */

234     public void setOverwrite(boolean overwrite) {
235         generator.setOverwrite(overwrite);
236     }
237
238     /**
239      * Sets <code>makepairs</code> property.
240      */

241     public void setMakepairs(boolean makepairs) {
242         generator.setMakePairs(makepairs);
243     }
244
245     /**
246      * Sets <code>template</code> property.
247      */

248     public void setTemplate(File JavaDoc template) {
249         generator.setTemplate(template);
250     }
251
252     /**
253      * Sets <code>supertemplate</code> property.
254      */

255     public void setSupertemplate(File JavaDoc supertemplate) {
256         generator.setSuperTemplate(supertemplate);
257     }
258
259     /**
260      * Sets <code>usepkgpath</code> property.
261      */

262     public void setUsepkgpath(boolean usepkgpath) {
263         generator.setUsePkgPath(usepkgpath);
264     }
265
266     /**
267      * Sets <code>superpkg</code> property.
268      */

269     public void setSuperpkg(String JavaDoc superpkg) {
270         generator.setSuperPkg(superpkg);
271     }
272     
273
274     /**
275      * Sets <code>client</code> property.
276      *
277      * @since 1.2
278      */

279     public void setClient(boolean client) {
280         generator.setClient(client);
281     }
282
283     /**
284      * Sets <code>version</code> property.
285      *
286      * @since 1.2
287      */

288     public void setVersion(String JavaDoc versionString) {
289         try {
290             generator.setVersionString(versionString);
291         } catch (IllegalStateException JavaDoc e) {
292             throw new BuildException(e.getMessage(), e);
293         }
294     }
295     
296     /**
297      * Sets <code>encoding</code> property that allows to generate files using
298      * non-default encoding.
299      *
300      * @since 1.2
301      */

302     public void setEncoding(String JavaDoc encoding) {
303         generator.setEncoding(encoding);
304     }
305     
306     /**
307      * Sets <code>excludeEntitiesPattern</code> property.
308      *
309      * @since 1.2
310      */

311     public void setExcludeEntities(String JavaDoc excludeEntitiesPattern) {
312         this.excludeEntitiesPattern = excludeEntitiesPattern;
313     }
314     
315     /**
316      * Sets <code>includeEntitiesPattern</code> property.
317      *
318      * @since 1.2
319      */

320     public void setIncludeEntities(String JavaDoc includeEntitiesPattern) {
321         this.includeEntitiesPattern = includeEntitiesPattern;
322     }
323
324     /**
325      * Sets <code>outputPattern</code> property.
326      *
327      * @since 1.2
328      */

329     public void setOutputPattern(String JavaDoc outputPattern) {
330         generator.setOutputPattern(outputPattern);
331     }
332
333     /**
334      * Sets <code>outputPattern</code> property.
335      *
336      * @since 1.2
337      */

338     public void setMode(String JavaDoc mode) {
339         generator.setMode(mode);
340     }
341
342     /**
343      * Provides a <code>VPPConfig</code> objec to configure.
344      * (Written with createConfig() instead of addConfig() to avoid run-time dependency on VPP).
345      *
346      * @since 1.2
347      */

348     public Object JavaDoc createConfig() {
349         this.vppConfig = new VPPConfig();
350         return this.vppConfig;
351     }
352
353     /**
354      * If no VppConfig element specified, use the default one.
355      *
356      * @since 1.2
357      */

358     private void initializeVppConfig() {
359         if (vppConfig == null) {
360             vppConfig = VPPConfig.getDefaultConfig(getProject());
361         }
362     }
363
364 }
Popular Tags