1 56 package org.objectstyle.cayenne.tools; 57 58 import java.io.File ; 59 import java.util.ArrayList ; 60 import java.util.Collections ; 61 import java.util.List ; 62 63 import org.apache.tools.ant.BuildException; 64 import org.apache.tools.ant.types.Path; 65 import org.objectstyle.cayenne.gen.AntClassGenerator; 66 import org.objectstyle.cayenne.gen.ClassGenerator; 67 import org.objectstyle.cayenne.gen.DefaultClassGenerator; 68 import org.objectstyle.cayenne.map.DataMap; 69 import org.objectstyle.cayenne.map.EntityResolver; 70 import org.objectstyle.cayenne.map.MapLoader; 71 import org.objectstyle.cayenne.util.Util; 72 import org.xml.sax.InputSource ; 73 74 import foundrylogic.vpp.VPPConfig; 75 76 82 public class CayenneGenerator extends CayenneTask { 83 84 protected String includeEntitiesPattern; 85 protected String excludeEntitiesPattern; 86 protected VPPConfig vppConfig; 87 88 protected File map; 89 protected File additionalMaps[]; 90 protected DefaultClassGenerator generator; 91 92 public CayenneGenerator() { 93 bootstrapVelocity(); 94 generator = createGenerator(); 95 } 96 97 100 protected DefaultClassGenerator createGenerator() { 101 AntClassGenerator gen = new AntClassGenerator(); 102 gen.setParentTask(this); 103 return gen; 104 } 105 106 107 protected void bootstrapVelocity() { 108 ClassGenerator.bootstrapVelocity(this.getClass()); 109 } 110 111 114 public void execute() throws BuildException { 115 configureLogging(); 116 validateAttributes(); 117 118 try { 119 processMap(); 120 } 121 catch (Throwable th) { 122 th = Util.unwindException(th); 123 124 String thMessage = th.getLocalizedMessage(); 125 String message = "Error generating classes: "; 126 message += (!Util.isEmptyString(thMessage)) ? thMessage : th 127 .getClass() 128 .getName(); 129 130 super.log(message); 131 throw new BuildException(message, th); 132 } 133 } 134 135 protected void processMap() throws Exception { 136 137 DataMap dataMap = loadDataMap(); 138 DataMap additionalDataMaps[] = loadAdditionalDataMaps(); 139 140 EntityResolver entityResolver = new EntityResolver(Collections.singleton(dataMap)); 142 dataMap.setNamespace(entityResolver); 143 for (int i = 0; i < additionalDataMaps.length; i++) { 144 entityResolver.addDataMap(additionalDataMaps[i]); 145 additionalDataMaps[i].setNamespace(entityResolver); 146 } 147 148 List entityList = new ArrayList (dataMap.getObjEntities()); 149 150 NamePatternMatcher namePatternMatcher = new NamePatternMatcher(this, includeEntitiesPattern, excludeEntitiesPattern); 151 namePatternMatcher.filter(entityList); 152 153 if (false == ClassGenerator.VERSION_1_1.equals(generator.getVersionString())) 154 { 155 initializeVppConfig(); 156 generator.setVppConfig(vppConfig); 157 } 158 159 generator.setTimestamp(map.lastModified()); 160 generator.setDataMap(dataMap); 161 generator.setObjEntities(entityList); 162 generator.validateAttributes(); 163 generator.execute(); 164 } 165 166 167 protected DataMap loadDataMap(File mapName) throws Exception { 168 InputSource in = new InputSource (mapName.getCanonicalPath()); 169 return new MapLoader().loadDataMap(in); 170 } 171 172 173 protected DataMap loadDataMap() throws Exception { 174 return loadDataMap(map); 175 } 176 177 178 protected DataMap[] loadAdditionalDataMaps() throws Exception { 179 if (null == additionalMaps) 180 { 181 return new DataMap[0]; 182 } 183 184 DataMap dataMaps[] = new DataMap[additionalMaps.length]; 185 for (int i = 0; i < additionalMaps.length; i++) { 186 dataMaps[i] = loadDataMap(additionalMaps[i]); 187 } 188 return dataMaps; 189 } 190 191 195 protected void validateAttributes() throws BuildException { 196 if (map == null && this.getProject() == null) { 197 throw new BuildException("either 'map' or 'project' is required."); 198 } 199 } 200 201 206 public void setMap(File map) { 207 this.map = map; 208 } 209 210 215 public void setAdditionalMaps(Path additionalMapsPath) { 216 String additionalMapFilenames[] = additionalMapsPath.list(); 217 this.additionalMaps = new File [additionalMapFilenames.length]; 218 219 for (int i = 0; i < additionalMapFilenames.length; i++) { 220 additionalMaps[i] = new File (additionalMapFilenames[i]); 221 } 222 } 223 224 227 public void setDestDir(File destDir) { 228 generator.setDestDir(destDir); 229 } 230 231 234 public void setOverwrite(boolean overwrite) { 235 generator.setOverwrite(overwrite); 236 } 237 238 241 public void setMakepairs(boolean makepairs) { 242 generator.setMakePairs(makepairs); 243 } 244 245 248 public void setTemplate(File template) { 249 generator.setTemplate(template); 250 } 251 252 255 public void setSupertemplate(File supertemplate) { 256 generator.setSuperTemplate(supertemplate); 257 } 258 259 262 public void setUsepkgpath(boolean usepkgpath) { 263 generator.setUsePkgPath(usepkgpath); 264 } 265 266 269 public void setSuperpkg(String superpkg) { 270 generator.setSuperPkg(superpkg); 271 } 272 273 274 279 public void setClient(boolean client) { 280 generator.setClient(client); 281 } 282 283 288 public void setVersion(String versionString) { 289 try { 290 generator.setVersionString(versionString); 291 } catch (IllegalStateException e) { 292 throw new BuildException(e.getMessage(), e); 293 } 294 } 295 296 302 public void setEncoding(String encoding) { 303 generator.setEncoding(encoding); 304 } 305 306 311 public void setExcludeEntities(String excludeEntitiesPattern) { 312 this.excludeEntitiesPattern = excludeEntitiesPattern; 313 } 314 315 320 public void setIncludeEntities(String includeEntitiesPattern) { 321 this.includeEntitiesPattern = includeEntitiesPattern; 322 } 323 324 329 public void setOutputPattern(String outputPattern) { 330 generator.setOutputPattern(outputPattern); 331 } 332 333 338 public void setMode(String mode) { 339 generator.setMode(mode); 340 } 341 342 348 public Object createConfig() { 349 this.vppConfig = new VPPConfig(); 350 return this.vppConfig; 351 } 352 353 358 private void initializeVppConfig() { 359 if (vppConfig == null) { 360 vppConfig = VPPConfig.getDefaultConfig(getProject()); 361 } 362 } 363 364 } | Popular Tags |