1 56 57 package org.objectstyle.cayenne.gen; 58 59 import java.io.Writer ; 60 import java.util.Properties ; 61 62 import org.apache.velocity.Template; 63 import org.apache.velocity.VelocityContext; 64 import org.apache.velocity.app.Velocity; 65 import org.apache.velocity.context.Context; 66 import org.apache.velocity.runtime.RuntimeConstants; 67 import org.apache.velocity.runtime.log.NullLogSystem; 68 import org.apache.velocity.runtime.resource.loader.JarResourceLoader; 69 import org.objectstyle.cayenne.CayenneRuntimeException; 70 import org.objectstyle.cayenne.map.DataMap; 71 import org.objectstyle.cayenne.map.ObjEntity; 72 import org.objectstyle.cayenne.util.ResourceLocator; 73 74 import foundrylogic.vpp.VPPConfig; 75 76 84 public class ClassGenerator { 85 public static final String VERSION_1_1 = "1.1"; 86 public static final String VERSION_1_2 = "1.2"; 87 88 protected String versionString; 89 protected Template classTemplate; 90 protected Context velCtxt; 91 protected ClassGenerationInfo classGenerationInfo; 93 private static boolean initDone; 94 95 106 public synchronized static final void bootstrapVelocity(Class cl) { 107 if (initDone) { 108 return; 109 } 110 111 113 try { 114 String classLoaderUrl = ResourceLocator.classBaseUrl(cl); 115 116 Properties props = new Properties (); 119 120 props.put(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogSystem.class 122 .getName()); 123 124 String loaderProp = null; 125 if (classLoaderUrl != null && classLoaderUrl.startsWith("jar:")) { 127 loaderProp = "jar"; 128 props.put("jar.resource.loader.class", JarResourceLoader.class.getName()); 129 props.put("jar.resource.loader.path", classLoaderUrl); 130 } 131 else if (classLoaderUrl != null && classLoaderUrl.startsWith("file:")) { 132 loaderProp = "file"; 133 props.put("file.resource.loader.path", classLoaderUrl.substring(5)); 134 } 135 136 if (loaderProp != null) { 138 if (loaderProp.indexOf("file") < 0) { 139 loaderProp += ",file"; 140 } 141 } 142 143 props 145 .put("file.resource.loader.class", AbsFileResourceLoader.class 146 .getName()); 147 148 loaderProp = (loaderProp != null) ? loaderProp + ",class" : "class"; 150 props.put("resource.loader", loaderProp); 151 152 Velocity.init(props); 153 } 154 catch (Exception ex) { 155 throw new CayenneRuntimeException("Can't initialize VTL", ex); 156 } 157 finally { 158 initDone = true; 159 } 160 } 161 162 166 public ClassGenerator(String template) throws Exception { 167 this(template, "1.1"); 168 } 169 170 173 public ClassGenerator(String template, String versionString) throws Exception { 174 if (!initDone) { 175 bootstrapVelocity(this.getClass()); 176 } 177 178 this.versionString = versionString; 179 180 if (false == VERSION_1_1.equals(versionString)) { 181 throw new IllegalStateException ("Illegal Version in generateClass(Writer,ObjEntity): " + versionString); 182 } 183 184 velCtxt = new VelocityContext(); 185 classGenerationInfo = new ClassGenerationInfo(); 186 velCtxt.put("classGen", classGenerationInfo); 187 classTemplate = Velocity.getTemplate(template); 188 } 189 190 193 public ClassGenerator(String template, String versionString, VPPConfig vppConfig) 194 throws Exception { 195 196 if (!initDone) { 197 bootstrapVelocity(this.getClass()); 198 } 199 200 this.versionString = versionString; 201 202 if (false == VERSION_1_2.equals(versionString)) { 203 throw new IllegalStateException ( 204 "Illegal Version in generateClass(Writer,ObjEntity): " 205 + versionString); 206 } 207 208 if (vppConfig != null) { 209 velCtxt = vppConfig.getVelocityContext(); 210 } 211 else { 212 velCtxt = new VelocityContext(); 213 } 214 215 classTemplate = Velocity.getTemplate(template); 216 } 217 218 222 public void generateClass(Writer out, ObjEntity entity) throws Exception { 223 if (false == VERSION_1_1.equals(versionString)) { 224 throw new IllegalStateException ("Illegal Version in generateClass(Writer,ObjEntity): " + versionString); 225 } 226 227 classGenerationInfo.setObjEntity(entity); 228 classTemplate.merge(velCtxt, out); 229 } 230 231 235 public void generateClass(Writer out, DataMap dataMap, ObjEntity entity, String fqnBaseClass, String fqnSuperClass, String fqnSubClass) throws Exception { 236 if (false == VERSION_1_2.equals(versionString)) { 237 throw new IllegalStateException ("Illegal Version in generateClass(Writer,ObjEntity,String,String,String): " + versionString); 238 } 239 240 if (null == dataMap) { 241 throw new IllegalStateException ("DataMap MapClassGenerator constructor required for v1.2 templating."); 242 } 243 244 velCtxt.put("objEntity", entity); 245 velCtxt.put("stringUtils", StringUtils.getInstance()); 246 velCtxt.put("entityUtils", new EntityUtils(dataMap, entity, fqnBaseClass, fqnSuperClass, fqnSubClass)); 247 velCtxt.put("importUtils", new ImportUtils()); 248 249 classTemplate.merge(velCtxt, out); 250 } 251 252 256 public ClassGenerationInfo getClassGenerationInfo() { 257 return classGenerationInfo; 258 } 259 260 264 public String getPackageName() { 265 return classGenerationInfo.getPackageName(); 266 } 267 268 273 public void setPackageName(String packageName) { 274 classGenerationInfo.setPackageName(packageName); 275 } 276 277 283 public void setSuperPackageName(String superPackageName) { 284 classGenerationInfo.setSuperPackageName(superPackageName); 285 } 286 287 293 public String getClassName() { 294 return classGenerationInfo.getClassName(); 295 } 296 302 public void setClassName(String className) { 303 classGenerationInfo.setClassName(className); 304 } 305 306 311 public void setSuperClassName(String value) { 312 classGenerationInfo.setSuperClassName(value); 313 } 314 315 321 public void setSuperPrefix(String superPrefix) { 322 classGenerationInfo.setSuperPrefix(superPrefix); 323 } 324 325 327 333 public String getSuperPackageName() 334 { 335 return classGenerationInfo.getSuperPackageName(); 336 } 337 338 341 public String formatJavaType(String type) 342 { 343 return classGenerationInfo.formatJavaType(type); 344 } 345 346 349 public String formatVariableName(String variableName) 350 { 351 return classGenerationInfo.formatVariableName(variableName); 352 } 353 354 360 public String getSuperPrefix() 361 { 362 return classGenerationInfo.getSuperPrefix(); 363 } 364 365 371 public void setProp(String prop) 372 { 373 classGenerationInfo.setProp(prop); 374 } 375 376 379 public String getProp() 380 { 381 return classGenerationInfo.getProp(); 382 } 383 384 390 public String capitalized(String name) 391 { 392 return classGenerationInfo.capitalized(name); 393 } 394 395 401 public String capitalizedAsConstant(String name) 402 { 403 return classGenerationInfo.capitalizedAsConstant(name); 404 } 405 406 409 public String getCappedProp() 410 { 411 return classGenerationInfo.getCappedProp(); 412 } 413 414 421 public String getPropAsConstantName() 422 { 423 return classGenerationInfo.getPropAsConstantName(); 424 } 425 426 432 public boolean isContainingListProperties() 433 { 434 return classGenerationInfo.isContainingListProperties(); 435 } 436 437 442 public boolean isUsingPackage() 443 { 444 return classGenerationInfo.isUsingPackage(); 445 } 446 447 452 public boolean isUsingSuperPackage() 453 { 454 return classGenerationInfo.isUsingSuperPackage(); 455 } 456 457 461 public ObjEntity getEntity() 462 { 463 return classGenerationInfo.getEntity(); 464 } 465 466 471 public String getSuperClassName() 472 { 473 return classGenerationInfo.getSuperClassName(); 474 } 475 } | Popular Tags |