KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > gen > ClassGenerator


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.gen;
21
22 import java.io.Writer JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import org.apache.cayenne.CayenneRuntimeException;
26 import org.apache.cayenne.map.DataMap;
27 import org.apache.cayenne.map.ObjEntity;
28 import org.apache.velocity.Template;
29 import org.apache.velocity.VelocityContext;
30 import org.apache.velocity.app.VelocityEngine;
31 import org.apache.velocity.context.Context;
32 import org.apache.velocity.runtime.RuntimeConstants;
33 import org.apache.velocity.runtime.log.NullLogSystem;
34
35 import foundrylogic.vpp.VPPConfig;
36
37 /**
38  * Class generation engine for ObjEntities based on <a
39  * HREF="http://jakarta.apache.org/velocity/" target="_blank">Velocity templates </a>.
40  * Instance of ClassGenerationInfo is available inside Velocity template under the key
41  * "classGen".
42  *
43  * @author Andrus Adamchik
44  */

45 public class ClassGenerator {
46
47     public static final String JavaDoc VERSION_1_1 = "1.1";
48     public static final String JavaDoc VERSION_1_2 = "1.2";
49
50     protected String JavaDoc versionString;
51     protected Template classTemplate;
52     protected Context velCtxt;
53     protected ClassGenerationInfo classGenerationInfo; // only used for VERSION_1_1
54

55     /**
56      * Creates a new ClassGenerationInfo that uses a specified Velocity template.
57      *
58      * @since 1.2
59      * @param template to use
60      * @param versionString of cgen
61      * @throws Exception
62      */

63     public ClassGenerator(String JavaDoc template, String JavaDoc versionString) throws Exception JavaDoc {
64         this.versionString = versionString;
65
66         if (false == VERSION_1_1.equals(versionString)) {
67             throw new IllegalStateException JavaDoc(
68                     "Illegal Version in generateClass(Writer,ObjEntity): "
69                             + versionString);
70         }
71
72         velCtxt = new VelocityContext();
73         classGenerationInfo = new ClassGenerationInfo();
74         velCtxt.put("classGen", classGenerationInfo);
75
76         initializeClassTemplate(template);
77     }
78
79     /**
80      * Creates a new ClassGenerationInfo that uses a specified Velocity template.
81      *
82      * @since 1.2
83      * @param template to use
84      * @param versionString of cgen
85      * @param vppConfig for configuring VelocityEngine and VelocityContext
86      * @throws Exception
87      */

88     public ClassGenerator(String JavaDoc template, String JavaDoc versionString, VPPConfig vppConfig)
89             throws Exception JavaDoc {
90
91         this.versionString = versionString;
92
93         if (false == VERSION_1_2.equals(versionString)) {
94             throw new IllegalStateException JavaDoc(
95                     "Illegal Version in generateClass(Writer,ObjEntity): "
96                             + versionString);
97         }
98
99         if (vppConfig != null) {
100             velCtxt = vppConfig.getVelocityContext();
101         }
102         else {
103             velCtxt = new VelocityContext();
104         }
105
106         initializeClassTemplate(template);
107     }
108
109     /**
110      * Sets up VelocityEngine properties, creates a VelocityEngine instance, and fetches a
111      * template using the VelocityEngine instance.
112      *
113      * @since 1.2
114      */

115     private void initializeClassTemplate(String JavaDoc template) throws CayenneRuntimeException {
116         VelocityEngine velocityEngine = new VelocityEngine();
117         try {
118
119             // use ClasspathResourceLoader for velocity templates lookup
120
// if Cayenne URL is not null, load resource from this URL
121
Properties JavaDoc props = new Properties JavaDoc();
122
123             // null logger that will prevent velocity.log from being generated
124
props.put(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogSystem.class
125                     .getName());
126
127             props.put("resource.loader", "cayenne");
128
129             props.put("cayenne.resource.loader.class", ClassGeneratorResourceLoader.class
130                     .getName());
131             velocityEngine.init(props);
132         }
133         catch (Exception JavaDoc ex) {
134             throw new CayenneRuntimeException("Can't initialize Velocity", ex);
135         }
136
137         try {
138             classTemplate = velocityEngine.getTemplate(template);
139         }
140         catch (Exception JavaDoc ex) {
141             throw new CayenneRuntimeException("Can't create template: " + template, ex);
142         }
143     }
144
145     /**
146      * Generates Java code for the ObjEntity. Output is written to the provided Writer.
147      */

148     public void generateClass(Writer JavaDoc out, ObjEntity entity) throws Exception JavaDoc {
149         if (false == VERSION_1_1.equals(versionString)) {
150             throw new IllegalStateException JavaDoc(
151                     "Illegal Version in generateClass(Writer,ObjEntity): "
152                             + versionString);
153         }
154
155         classGenerationInfo.setObjEntity(entity);
156         classTemplate.merge(velCtxt, out);
157     }
158
159     /**
160      * Generates Java code for the ObjEntity. Output is written to the provided Writer.
161      */

162     public void generateClass(
163             Writer JavaDoc out,
164             DataMap dataMap,
165             ObjEntity entity,
166             String JavaDoc fqnBaseClass,
167             String JavaDoc fqnSuperClass,
168             String JavaDoc fqnSubClass) throws Exception JavaDoc {
169         if (false == VERSION_1_2.equals(versionString)) {
170             throw new IllegalStateException JavaDoc(
171                     "Illegal Version in generateClass(Writer,ObjEntity,String,String,String): "
172                             + versionString);
173         }
174
175         if (null == dataMap) {
176             throw new IllegalStateException JavaDoc(
177                     "DataMap MapClassGenerator constructor required for v1.2 templating.");
178         }
179
180         velCtxt.put("objEntity", entity);
181         velCtxt.put("stringUtils", StringUtils.getInstance());
182         velCtxt.put("entityUtils", new EntityUtils(
183                 dataMap,
184                 entity,
185                 fqnBaseClass,
186                 fqnSuperClass,
187                 fqnSubClass));
188         velCtxt.put("importUtils", new ImportUtils());
189
190         classTemplate.merge(velCtxt, out);
191     }
192
193     // deprecated, delegated methods previously used internally in cayenne
194
/**
195      * @return Returns the classGenerationInfo in template.
196      */

197     public ClassGenerationInfo getClassGenerationInfo() {
198         return classGenerationInfo;
199     }
200 }
201
Popular Tags