1 19 20 package org.apache.cayenne.gen; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.OutputStreamWriter ; 26 import java.io.Writer ; 27 import java.util.ArrayList ; 28 import java.util.List ; 29 30 import org.apache.cayenne.map.DataMap; 31 import org.apache.cayenne.map.ObjEntity; 32 import org.apache.cayenne.tools.NamePatternMatcher; 33 34 41 public class DefaultClassGenerator extends MapClassGenerator { 42 43 protected File destDir; 44 protected boolean overwrite; 45 protected boolean usePkgPath = true; 46 protected boolean makePairs = true; 47 protected String template; 48 protected String superTemplate; 49 protected long timestamp = System.currentTimeMillis(); 50 private static final String WILDCARD = "*"; 51 protected String outputPattern = "*.java"; 52 53 58 protected String encoding; 59 60 public DefaultClassGenerator() { 61 } 62 63 67 public DefaultClassGenerator(DataMap dataMap) { 68 this(dataMap, new ArrayList (dataMap.getObjEntities())); 69 } 70 71 75 public DefaultClassGenerator(DataMap dataMap, List selectedObjEntities) { 76 super(dataMap, selectedObjEntities); 77 } 78 79 80 public void execute() throws Exception { 81 validateAttributes(); 82 83 if (makePairs) { 84 String t = getTemplateForPairs(); 85 String st = getSupertemplateForPairs(); 86 generateClassPairs(t, st, MapClassGenerator.SUPERCLASS_PREFIX); 87 } 88 else { 89 generateSingleClasses( 90 getTemplateForSingles(), 91 MapClassGenerator.SUPERCLASS_PREFIX); 92 } 93 } 94 95 99 public void validateAttributes() throws Exception { 100 if (destDir == null) { 101 throw new Exception ("'destDir' attribute is missing."); 102 } 103 104 if (!destDir.isDirectory()) { 105 throw new Exception ("'destDir' is not a directory."); 106 } 107 108 if (!destDir.canWrite()) { 109 throw new Exception ("Do not have write permissions for " + destDir); 110 } 111 112 if ((false == VERSION_1_1.equals(versionString)) 113 && (false == VERSION_1_2.equals(versionString))) { 114 throw new Exception ("'version' must be '" 115 + VERSION_1_1 116 + "' or '" 117 + VERSION_1_2 118 + "'."); 119 } 120 } 121 122 125 public void setDestDir(File destDir) { 126 this.destDir = destDir; 127 } 128 129 132 public void setOverwrite(boolean overwrite) { 133 this.overwrite = overwrite; 134 } 135 136 139 public void setMakePairs(boolean makePairs) { 140 this.makePairs = makePairs; 141 } 142 143 146 public void setTemplate(String template) { 147 this.template = template; 148 } 149 150 153 public void setSuperTemplate(String superTemplate) { 154 this.superTemplate = superTemplate; 155 } 156 157 160 public void setUsePkgPath(boolean usePkgPath) { 161 this.usePkgPath = usePkgPath; 162 } 163 164 167 public void setOutputPattern(String outputPattern) { 168 this.outputPattern = outputPattern; 169 } 170 171 public void closeWriter(Writer out) throws Exception { 172 out.close(); 173 } 174 175 179 public Writer openWriter(ObjEntity entity, String pkgName, String className) 180 throws Exception { 181 File outFile = (className.startsWith(SUPERCLASS_PREFIX)) ? fileForSuperclass( 182 pkgName, 183 className) : fileForClass(pkgName, className); 184 185 if (outFile == null) { 186 return null; 187 } 188 189 FileOutputStream out = new FileOutputStream (outFile); 191 192 return (getEncoding() != null) 193 ? new OutputStreamWriter (out, getEncoding()) 194 : new OutputStreamWriter (out); 195 } 196 197 201 protected File fileForSuperclass(String pkgName, String className) throws Exception { 202 203 String filename = NamePatternMatcher.replaceWildcardInStringWithString( 204 WILDCARD, 205 outputPattern, 206 className); 207 File dest = new File (mkpath(destDir, pkgName), filename); 208 209 if (dest.exists() && !isOld(dest)) { 213 214 if (superTemplate == null) { 215 return null; 216 } 217 218 File superTemplateFile = new File (superTemplate); 219 if (superTemplateFile.lastModified() < dest.lastModified()) { 220 return null; 221 } 222 } 223 224 return dest; 225 } 226 227 231 protected File fileForClass(String pkgName, String className) throws Exception { 232 233 String filename = NamePatternMatcher.replaceWildcardInStringWithString( 234 WILDCARD, 235 outputPattern, 236 className); 237 File dest = new File (mkpath(destDir, pkgName), filename); 238 239 if (dest.exists()) { 240 if (makePairs) { 242 return null; 243 } 244 245 if (!overwrite) { 247 return null; 248 } 249 250 if (!isOld(dest)) { 254 255 if (template == null) { 256 return null; 257 } 258 259 File templateFile = new File (template); 260 if (templateFile.lastModified() < dest.lastModified()) { 261 return null; 262 } 263 } 264 } 265 266 return dest; 267 } 268 269 273 protected boolean isOld(File file) { 274 return file.lastModified() <= getTimestamp(); 275 } 276 277 282 protected File mkpath(File dest, String pkgName) throws Exception { 283 284 if (!usePkgPath || pkgName == null) { 285 return dest; 286 } 287 288 String path = pkgName.replace('.', File.separatorChar); 289 File fullPath = new File (dest, path); 290 if (!fullPath.isDirectory() && !fullPath.mkdirs()) { 291 throw new Exception ("Error making path: " + fullPath); 292 } 293 294 return fullPath; 295 } 296 297 300 protected String getTemplateForSingles() throws IOException { 301 return (template != null) ? template : defaultSingleClassTemplate(); 302 } 303 304 307 protected String getTemplateForPairs() throws IOException { 308 return (template != null) ? template : defaultSubclassTemplate(); 309 } 310 311 314 protected String getSupertemplateForPairs() throws IOException { 315 return (superTemplate != null) ? superTemplate : defaultSuperclassTemplate(); 316 } 317 318 322 public long getTimestamp() { 323 return timestamp; 324 } 325 326 public void setTimestamp(long timestamp) { 327 this.timestamp = timestamp; 328 } 329 330 335 public String getEncoding() { 336 return encoding; 337 } 338 339 344 public void setEncoding(String encoding) { 345 this.encoding = encoding; 346 } 347 } 348 | Popular Tags |