1 19 20 package org.apache.cayenne.tools; 21 22 import java.io.File ; 23 24 import org.apache.cayenne.gen.AntClassGenerator; 25 import org.apache.cayenne.gen.ClassGenerator; 26 import org.apache.cayenne.gen.DefaultClassGenerator; 27 import org.apache.tools.ant.BuildException; 28 import org.apache.tools.ant.Task; 29 import org.apache.tools.ant.types.Path; 30 31 import foundrylogic.vpp.VPPConfig; 32 33 39 public class CayenneGenerator extends Task { 40 41 protected String includeEntitiesPattern; 42 protected String excludeEntitiesPattern; 43 protected VPPConfig vppConfig; 44 45 protected File map; 46 protected File additionalMaps[]; 47 protected DefaultClassGenerator generator; 48 49 protected CayenneGeneratorUtil generatorUtil; 50 51 public CayenneGenerator() { 52 generator = createGenerator(); 53 generatorUtil = new CayenneGeneratorUtil(); 54 } 55 56 59 protected DefaultClassGenerator createGenerator() { 60 AntClassGenerator gen = new AntClassGenerator(); 61 gen.setParentTask(this); 62 return gen; 63 } 64 65 68 public void execute() throws BuildException { 69 validateAttributes(); 70 71 if (false == ClassGenerator.VERSION_1_1.equals(generator.getVersionString())) { 73 initializeVppConfig(); 74 generator.setVppConfig(vppConfig); 75 } 76 77 generatorUtil.setAdditionalMaps(additionalMaps); 79 generatorUtil.setExcludeEntitiesPattern(excludeEntitiesPattern); 80 generatorUtil.setGenerator(generator); 81 generatorUtil.setIncludeEntitiesPattern(includeEntitiesPattern); 82 generatorUtil.setLogger(new AntTaskLogger(this)); 83 generatorUtil.setMap(map); 84 85 try { 86 generatorUtil.execute(); 87 } 88 catch (Exception e) { 89 throw new BuildException(e); 90 } 91 } 92 93 97 protected void validateAttributes() throws BuildException { 98 if (map == null && this.getProject() == null) { 99 throw new BuildException("either 'map' or 'project' is required."); 100 } 101 } 102 103 108 public void setMap(File map) { 109 this.map = map; 110 } 111 112 117 public void setAdditionalMaps(Path additionalMapsPath) { 118 String additionalMapFilenames[] = additionalMapsPath.list(); 119 this.additionalMaps = new File [additionalMapFilenames.length]; 120 121 for (int i = 0; i < additionalMapFilenames.length; i++) { 122 additionalMaps[i] = new File (additionalMapFilenames[i]); 123 } 124 } 125 126 129 public void setDestDir(File destDir) { 130 generator.setDestDir(destDir); 131 } 132 133 136 public void setOverwrite(boolean overwrite) { 137 generator.setOverwrite(overwrite); 138 } 139 140 143 public void setMakepairs(boolean makepairs) { 144 generator.setMakePairs(makepairs); 145 } 146 147 150 public void setTemplate(String template) { 151 generator.setTemplate(template); 152 } 153 154 157 public void setSupertemplate(String supertemplate) { 158 generator.setSuperTemplate(supertemplate); 159 } 160 161 164 public void setUsepkgpath(boolean usepkgpath) { 165 generator.setUsePkgPath(usepkgpath); 166 } 167 168 171 public void setSuperpkg(String superpkg) { 172 generator.setSuperPkg(superpkg); 173 } 174 175 180 public void setClient(boolean client) { 181 generator.setClient(client); 182 } 183 184 189 public void setVersion(String versionString) { 190 try { 191 generator.setVersionString(versionString); 192 } 193 catch (IllegalStateException e) { 194 throw new BuildException(e.getMessage(), e); 195 } 196 } 197 198 204 public void setEncoding(String encoding) { 205 generator.setEncoding(encoding); 206 } 207 208 213 public void setExcludeEntities(String excludeEntitiesPattern) { 214 this.excludeEntitiesPattern = excludeEntitiesPattern; 215 } 216 217 222 public void setIncludeEntities(String includeEntitiesPattern) { 223 this.includeEntitiesPattern = includeEntitiesPattern; 224 } 225 226 231 public void setOutputPattern(String outputPattern) { 232 generator.setOutputPattern(outputPattern); 233 } 234 235 240 public void setMode(String mode) { 241 generator.setMode(mode); 242 } 243 244 250 public Object createConfig() { 251 this.vppConfig = new VPPConfig(); 252 return this.vppConfig; 253 } 254 255 260 private void initializeVppConfig() { 261 if (vppConfig == null) { 262 vppConfig = VPPConfig.getDefaultConfig(getProject()); 263 } 264 } 265 } 266 | Popular Tags |