1 19 20 package org.apache.cayenne.gen; 21 22 import java.io.Writer ; 23 import java.util.Properties ; 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 45 public class ClassGenerator { 46 47 public static final String VERSION_1_1 = "1.1"; 48 public static final String VERSION_1_2 = "1.2"; 49 50 protected String versionString; 51 protected Template classTemplate; 52 protected Context velCtxt; 53 protected ClassGenerationInfo classGenerationInfo; 55 63 public ClassGenerator(String template, String versionString) throws Exception { 64 this.versionString = versionString; 65 66 if (false == VERSION_1_1.equals(versionString)) { 67 throw new IllegalStateException ( 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 88 public ClassGenerator(String template, String versionString, VPPConfig vppConfig) 89 throws Exception { 90 91 this.versionString = versionString; 92 93 if (false == VERSION_1_2.equals(versionString)) { 94 throw new IllegalStateException ( 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 115 private void initializeClassTemplate(String template) throws CayenneRuntimeException { 116 VelocityEngine velocityEngine = new VelocityEngine(); 117 try { 118 119 Properties props = new Properties (); 122 123 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 ex) { 134 throw new CayenneRuntimeException("Can't initialize Velocity", ex); 135 } 136 137 try { 138 classTemplate = velocityEngine.getTemplate(template); 139 } 140 catch (Exception ex) { 141 throw new CayenneRuntimeException("Can't create template: " + template, ex); 142 } 143 } 144 145 148 public void generateClass(Writer out, ObjEntity entity) throws Exception { 149 if (false == VERSION_1_1.equals(versionString)) { 150 throw new IllegalStateException ( 151 "Illegal Version in generateClass(Writer,ObjEntity): " 152 + versionString); 153 } 154 155 classGenerationInfo.setObjEntity(entity); 156 classTemplate.merge(velCtxt, out); 157 } 158 159 162 public void generateClass( 163 Writer out, 164 DataMap dataMap, 165 ObjEntity entity, 166 String fqnBaseClass, 167 String fqnSuperClass, 168 String fqnSubClass) throws Exception { 169 if (false == VERSION_1_2.equals(versionString)) { 170 throw new IllegalStateException ( 171 "Illegal Version in generateClass(Writer,ObjEntity,String,String,String): " 172 + versionString); 173 } 174 175 if (null == dataMap) { 176 throw new IllegalStateException ( 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 197 public ClassGenerationInfo getClassGenerationInfo() { 198 return classGenerationInfo; 199 } 200 } 201 | Popular Tags |