KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > gen > ClassGenerationInfo


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
57 package org.objectstyle.cayenne.gen;
58
59 import java.util.Iterator JavaDoc;
60
61 import org.objectstyle.cayenne.map.ObjEntity;
62 import org.objectstyle.cayenne.map.Relationship;
63 import org.objectstyle.cayenne.project.validator.MappingNamesHelper;
64 import org.objectstyle.cayenne.util.NameConverter;
65
66 /**
67  * Class generation engine for ObjEntities based on <a
68  * HREF="http://jakarta.apache.org/velocity/" target="_blank">Velocity templates
69  * </a>. Instance of ClassGenerationInfo is available inside Velocity template under
70  * the key "classGen".
71  *
72  * @author Andrei Adamchik
73  * @since 1.2
74  */

75 public class ClassGenerationInfo {
76
77     protected ObjEntity entity;
78
79     // template substitution values
80
protected String JavaDoc packageName;
81     protected String JavaDoc className;
82     protected String JavaDoc superPrefix;
83     protected String JavaDoc prop;
84     protected String JavaDoc superPackageName;
85     protected String JavaDoc superClassName;
86
87     /**
88      * Returns Java package name of the class associated with this generator.
89      */

90     public String JavaDoc getPackageName() {
91         return packageName;
92     }
93
94     /**
95      * Sets Java package name of the class associated with this generator.
96      */

97     protected void setPackageName(String JavaDoc packageName) {
98         this.packageName = packageName;
99     }
100
101     /**
102      * Returns <code>superPackageName</code> property that defines a
103      * superclass's package name.
104      */

105     public String JavaDoc getSuperPackageName() {
106         return superPackageName;
107     }
108
109     /**
110      * Sets <code>superPackageName</code> property that defines a superclass's
111      * package name.
112      */

113     protected void setSuperPackageName(String JavaDoc superPackageName) {
114         this.superPackageName = superPackageName;
115     }
116
117     /**
118      * Returns class name (without a package) of the class associated with this
119      * generator.
120      */

121     public String JavaDoc getClassName() {
122         return className;
123     }
124
125     /**
126      * Sets class name of the class associated with this
127      * generator. Class name must not include a package.
128      */

129     protected void setClassName(String JavaDoc className) {
130         this.className = className;
131     }
132
133     protected void setSuperPrefix(String JavaDoc superPrefix) {
134         this.superPrefix = superPrefix;
135     }
136
137     public String JavaDoc formatJavaType(String JavaDoc type) {
138         if (type != null) {
139             if (type.startsWith("java.lang.") && type.indexOf(10, '.') < 0) {
140                 return type.substring("java.lang.".length());
141             }
142
143             if (packageName != null
144                     && type.startsWith(packageName + '.')
145                     && type.indexOf(packageName.length() + 1, '.') < 0) {
146                 return type.substring(packageName.length() + 1);
147             }
148         }
149
150         return type;
151     }
152
153     public String JavaDoc formatVariableName(String JavaDoc variableName) {
154         if (MappingNamesHelper.getInstance().isReservedJavaKeyword(variableName)) {
155             return "_" + variableName;
156         } else {
157             return variableName;
158         }
159     }
160
161     /**
162      * Returns prefix used to distinguish between superclass and subclass when
163      * generating classes in pairs.
164      */

165     public String JavaDoc getSuperPrefix() {
166         return superPrefix;
167     }
168
169     /**
170      * Sets current class property name. This method is called during template
171      * parsing for each of the class properties.
172      */

173     public void setProp(String JavaDoc prop) {
174         this.prop = prop;
175     }
176
177     public String JavaDoc getProp() {
178         return prop;
179     }
180     
181     /**
182      * Capitalizes the first letter of the property name.
183      *
184      * @since 1.1
185      */

186     public String JavaDoc capitalized(String JavaDoc name) {
187         if (name == null || name.length() == 0)
188             return name;
189
190         char c = Character.toUpperCase(name.charAt(0));
191         return (name.length() == 1) ? Character.toString(c) : c + name.substring(1);
192     }
193     
194     /**
195      * Converts property name to Java constants naming convention.
196      *
197      * @since 1.1
198      */

199     public String JavaDoc capitalizedAsConstant(String JavaDoc name) {
200         if (name == null || name.length() == 0)
201             return name;
202
203         return NameConverter.javaToUnderscored(name);
204     }
205
206     /** Returns current property name with capitalized first letter */
207     public String JavaDoc getCappedProp() {
208         return capitalized(prop);
209     }
210     
211     /**
212      * @return a current property name converted to a format used by java static
213      * final variables - all capitalized with underscores.
214      *
215      * @since 1.0.3
216      */

217     public String JavaDoc getPropAsConstantName() {
218         return capitalizedAsConstant(prop);
219     }
220
221     /**
222      * Returns true if current entity contains at least one List property.
223      *
224      * @since 1.1
225      */

226     public boolean isContainingListProperties() {
227         if (entity == null) {
228             return false;
229         }
230         
231         Iterator JavaDoc it = entity.getRelationships().iterator();
232         while(it.hasNext()) {
233             Relationship r = (Relationship) it.next();
234             if(r.isToMany()) {
235                 return true;
236             }
237         }
238
239         return false;
240     }
241
242     /**
243      * Returns <code>true</code> if a class associated with this generator is
244      * located in a package.
245      */

246     public boolean isUsingPackage() {
247         return packageName != null;
248     }
249
250     /**
251      * Returns <code>true</code> if a superclass class associated with this
252      * generator is located in a package.
253      */

254     public boolean isUsingSuperPackage() {
255         return superPackageName != null;
256     }
257
258     /** Returns entity for the class associated with this generator. */
259     public ObjEntity getEntity() {
260         return entity;
261     }
262
263     /**
264      * @param entity The entity to set.
265      */

266     protected void setObjEntity(ObjEntity entity) {
267         this.entity = entity;
268     }
269
270     /**
271      * Returns the fully qualified super class of the data object class
272      * associated with this generator
273      */

274     public String JavaDoc getSuperClassName() {
275         return superClassName;
276     }
277
278     /**
279      * Sets the fully qualified super class of the data object class associated
280      * with this generator
281      */

282     protected void setSuperClassName(String JavaDoc value) {
283         this.superClassName = value;
284     }
285 }
Popular Tags