KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 package org.apache.cayenne.gen;
22
23 import java.util.Iterator JavaDoc;
24
25 import org.apache.cayenne.map.ObjEntity;
26 import org.apache.cayenne.map.Relationship;
27 import org.apache.cayenne.project.validator.MappingNamesHelper;
28 import org.apache.cayenne.util.NameConverter;
29
30 /**
31  * Class generation engine for ObjEntities based on <a
32  * HREF="http://jakarta.apache.org/velocity/" target="_blank">Velocity templates
33  * </a>. Instance of ClassGenerationInfo is available inside Velocity template under
34  * the key "classGen".
35  *
36  * @author Andrus Adamchik
37  * @since 1.2
38  */

39 public class ClassGenerationInfo {
40
41     protected ObjEntity entity;
42
43     // template substitution values
44
protected String JavaDoc packageName;
45     protected String JavaDoc className;
46     protected String JavaDoc superPrefix;
47     protected String JavaDoc prop;
48     protected String JavaDoc superPackageName;
49     protected String JavaDoc superClassName;
50
51     /**
52      * Returns Java package name of the class associated with this generator.
53      */

54     public String JavaDoc getPackageName() {
55         return packageName;
56     }
57
58     /**
59      * Sets Java package name of the class associated with this generator.
60      */

61     protected void setPackageName(String JavaDoc packageName) {
62         this.packageName = packageName;
63     }
64
65     /**
66      * Returns <code>superPackageName</code> property that defines a
67      * superclass's package name.
68      */

69     public String JavaDoc getSuperPackageName() {
70         return superPackageName;
71     }
72
73     /**
74      * Sets <code>superPackageName</code> property that defines a superclass's
75      * package name.
76      */

77     protected void setSuperPackageName(String JavaDoc superPackageName) {
78         this.superPackageName = superPackageName;
79     }
80
81     /**
82      * Returns class name (without a package) of the class associated with this
83      * generator.
84      */

85     public String JavaDoc getClassName() {
86         return className;
87     }
88
89     /**
90      * Sets class name of the class associated with this
91      * generator. Class name must not include a package.
92      */

93     protected void setClassName(String JavaDoc className) {
94         this.className = className;
95     }
96
97     protected void setSuperPrefix(String JavaDoc superPrefix) {
98         this.superPrefix = superPrefix;
99     }
100
101     public String JavaDoc formatJavaType(String JavaDoc type) {
102         if (type != null) {
103             if (type.startsWith("java.lang.") && type.indexOf('.', 10) < 0) {
104                 return type.substring("java.lang.".length());
105             }
106
107             if (packageName != null
108                     && type.startsWith(packageName + '.')
109                     && type.indexOf(packageName.length() + 1, '.') < 0) {
110                 return type.substring(packageName.length() + 1);
111             }
112         }
113
114         return type;
115     }
116
117     public String JavaDoc formatVariableName(String JavaDoc variableName) {
118         if (MappingNamesHelper.getInstance().isReservedJavaKeyword(variableName)) {
119             return "_" + variableName;
120         } else {
121             return variableName;
122         }
123     }
124
125     /**
126      * Returns prefix used to distinguish between superclass and subclass when
127      * generating classes in pairs.
128      */

129     public String JavaDoc getSuperPrefix() {
130         return superPrefix;
131     }
132
133     /**
134      * Sets current class property name. This method is called during template
135      * parsing for each of the class properties.
136      */

137     public void setProp(String JavaDoc prop) {
138         this.prop = prop;
139     }
140
141     public String JavaDoc getProp() {
142         return prop;
143     }
144     
145     /**
146      * Capitalizes the first letter of the property name.
147      *
148      * @since 1.1
149      */

150     public String JavaDoc capitalized(String JavaDoc name) {
151         if (name == null || name.length() == 0)
152             return name;
153
154         char c = Character.toUpperCase(name.charAt(0));
155         return (name.length() == 1) ? Character.toString(c) : c + name.substring(1);
156     }
157     
158     /**
159      * Converts property name to Java constants naming convention.
160      *
161      * @since 1.1
162      */

163     public String JavaDoc capitalizedAsConstant(String JavaDoc name) {
164         if (name == null || name.length() == 0)
165             return name;
166
167         return NameConverter.javaToUnderscored(name);
168     }
169
170     /** Returns current property name with capitalized first letter */
171     public String JavaDoc getCappedProp() {
172         return capitalized(prop);
173     }
174     
175     /**
176      * @return a current property name converted to a format used by java static
177      * final variables - all capitalized with underscores.
178      *
179      * @since 1.0.3
180      */

181     public String JavaDoc getPropAsConstantName() {
182         return capitalizedAsConstant(prop);
183     }
184
185     /**
186      * Returns true if current entity contains at least one Declared List property.
187      *
188      * @since 1.2
189      */

190     public boolean isContainingDeclaredListProperties() {
191         if (entity == null) {
192             return false;
193         }
194         
195         Iterator JavaDoc it = entity.getDeclaredRelationships().iterator();
196         while(it.hasNext()) {
197             Relationship r = (Relationship) it.next();
198             if(r.isToMany()) {
199                 return true;
200             }
201         }
202
203         return false;
204     }
205
206     /**
207      * Returns true if current entity contains at least one List property.
208      *
209      * @since 1.1
210      */

211     public boolean isContainingListProperties() {
212         if (entity == null) {
213             return false;
214         }
215         
216         Iterator JavaDoc it = entity.getRelationships().iterator();
217         while(it.hasNext()) {
218             Relationship r = (Relationship) it.next();
219             if(r.isToMany()) {
220                 return true;
221             }
222         }
223
224         return false;
225     }
226
227     /**
228      * Returns <code>true</code> if a class associated with this generator is
229      * located in a package.
230      */

231     public boolean isUsingPackage() {
232         return packageName != null;
233     }
234
235     /**
236      * Returns <code>true</code> if a superclass class associated with this
237      * generator is located in a package.
238      */

239     public boolean isUsingSuperPackage() {
240         return superPackageName != null;
241     }
242
243     /** Returns entity for the class associated with this generator. */
244     public ObjEntity getEntity() {
245         return entity;
246     }
247
248     /**
249      * @param entity The entity to set.
250      */

251     protected void setObjEntity(ObjEntity entity) {
252         this.entity = entity;
253     }
254
255     /**
256      * Returns the fully qualified super class of the data object class
257      * associated with this generator
258      */

259     public String JavaDoc getSuperClassName() {
260         return superClassName;
261     }
262
263     /**
264      * Sets the fully qualified super class of the data object class associated
265      * with this generator
266      */

267     protected void setSuperClassName(String JavaDoc value) {
268         this.superClassName = value;
269     }
270 }
271
Popular Tags