1 56 57 package org.objectstyle.cayenne.gen; 58 59 import java.io.File ; 60 import java.io.FileOutputStream ; 61 import java.io.IOException ; 62 import java.io.OutputStreamWriter ; 63 import java.io.Writer ; 64 import java.util.ArrayList ; 65 import java.util.List ; 66 67 import org.objectstyle.cayenne.map.DataMap; 68 import org.objectstyle.cayenne.map.ObjEntity; 69 import org.objectstyle.cayenne.tools.NamePatternMatcher; 70 71 78 public class DefaultClassGenerator extends MapClassGenerator { 79 80 protected File destDir; 81 protected boolean overwrite; 82 protected boolean usePkgPath = true; 83 protected boolean makePairs = true; 84 protected File template; 85 protected File superTemplate; 86 protected long timestamp = System.currentTimeMillis(); 87 private static final String WILDCARD = "*"; 88 protected String outputPattern = "*.java"; 89 90 95 protected String encoding; 96 97 public DefaultClassGenerator() { 98 } 99 100 104 public DefaultClassGenerator(DataMap dataMap) { 105 this(dataMap, new ArrayList (dataMap.getObjEntities())); 106 } 107 108 112 public DefaultClassGenerator(DataMap dataMap, List selectedObjEntities) { 113 super(dataMap, selectedObjEntities); 114 } 115 116 121 public DefaultClassGenerator(List selectedObjEntities) { 122 super(selectedObjEntities); 123 } 124 125 126 public void execute() throws Exception { 127 validateAttributes(); 128 129 if (makePairs) { 130 String t = getTemplateForPairs(); 131 String st = getSupertemplateForPairs(); 132 generateClassPairs(t, st, MapClassGenerator.SUPERCLASS_PREFIX); 133 } 134 else { 135 generateSingleClasses(getTemplateForSingles(), MapClassGenerator.SUPERCLASS_PREFIX); 136 } 137 } 138 139 143 public void validateAttributes() throws Exception { 144 if (destDir == null) { 145 throw new Exception ("'destDir' attribute is missing."); 146 } 147 148 if (!destDir.isDirectory()) { 149 throw new Exception ("'destDir' is not a directory."); 150 } 151 152 if (!destDir.canWrite()) { 153 throw new Exception ("Do not have write permissions for " + destDir); 154 } 155 156 if (template != null && !template.canRead()) { 157 throw new Exception ("Can't read template from " + template); 158 } 159 160 if (makePairs && superTemplate != null && !superTemplate.canRead()) { 161 throw new Exception ("Can't read super template from " + superTemplate); 162 } 163 164 if ( (false == VERSION_1_1.equals(versionString)) && (false == VERSION_1_2.equals(versionString)) ) { 165 throw new Exception ("'version' must be '" + VERSION_1_1 + "' or '" + VERSION_1_2 + "'."); 166 } 167 } 168 169 172 public void setDestDir(File destDir) { 173 this.destDir = destDir; 174 } 175 176 179 public void setOverwrite(boolean overwrite) { 180 this.overwrite = overwrite; 181 } 182 183 186 public void setMakePairs(boolean makePairs) { 187 this.makePairs = makePairs; 188 } 189 190 193 public void setTemplate(File template) { 194 this.template = template; 195 } 196 197 200 public void setSuperTemplate(File superTemplate) { 201 this.superTemplate = superTemplate; 202 } 203 204 207 public void setUsePkgPath(boolean usePkgPath) { 208 this.usePkgPath = usePkgPath; 209 } 210 211 214 public void setOutputPattern(String outputPattern) { 215 this.outputPattern = outputPattern; 216 } 217 218 public void closeWriter(Writer out) throws Exception { 219 out.close(); 220 } 221 222 226 public Writer openWriter(ObjEntity entity, String pkgName, String className) 227 throws Exception { 228 File outFile = (className.startsWith(SUPERCLASS_PREFIX)) 229 ? fileForSuperclass(pkgName, className) 230 : fileForClass(pkgName, className); 231 232 if (outFile == null) { 233 return null; 234 } 235 236 FileOutputStream out = new FileOutputStream (outFile); 238 239 return (getEncoding() != null) 240 ? new OutputStreamWriter (out, getEncoding()) 241 : new OutputStreamWriter (out); 242 } 243 244 248 protected File fileForSuperclass(String pkgName, String className) throws Exception { 249 250 String filename = NamePatternMatcher.replaceWildcardInStringWithString(WILDCARD, outputPattern, className); 251 File dest = new File (mkpath(destDir, pkgName), filename); 252 253 if (dest.exists() 257 && !isOld(dest) 258 && (superTemplate == null || superTemplate.lastModified() < dest 259 .lastModified())) { 260 return null; 261 } 262 263 return dest; 264 } 265 266 270 protected File fileForClass(String pkgName, String className) throws Exception { 271 272 String filename = NamePatternMatcher.replaceWildcardInStringWithString(WILDCARD, outputPattern, className); 273 File dest = new File (mkpath(destDir, pkgName), filename); 274 275 if (dest.exists()) { 276 if (makePairs) { 278 return null; 279 } 280 281 if (!overwrite) { 283 return null; 284 } 285 286 if (!isOld(dest) 290 && (template == null || template.lastModified() < dest.lastModified())) { 291 return null; 292 } 293 } 294 295 return dest; 296 } 297 298 302 protected boolean isOld(File file) { 303 return file.lastModified() <= getTimestamp(); 304 } 305 306 311 protected File mkpath(File dest, String pkgName) throws Exception { 312 313 if (!usePkgPath || pkgName == null) { 314 return dest; 315 } 316 317 String path = pkgName.replace('.', File.separatorChar); 318 File fullPath = new File (dest, path); 319 if (!fullPath.isDirectory() && !fullPath.mkdirs()) { 320 throw new Exception ("Error making path: " + fullPath); 321 } 322 323 return fullPath; 324 } 325 326 329 protected String getTemplateForSingles() throws IOException { 330 return (template != null) ? template.getPath() : defaultSingleClassTemplate(); 331 } 332 333 336 protected String getTemplateForPairs() throws IOException { 337 return (template != null) ? template.getPath() : defaultSubclassTemplate(); 338 } 339 340 343 protected String getSupertemplateForPairs() throws IOException { 344 return (superTemplate != null) 345 ? superTemplate.getPath() 346 : defaultSuperclassTemplate(); 347 } 348 349 353 public long getTimestamp() { 354 return timestamp; 355 } 356 357 public void setTimestamp(long timestamp) { 358 this.timestamp = timestamp; 359 } 360 361 366 public String getEncoding() { 367 return encoding; 368 } 369 370 375 public void setEncoding(String encoding) { 376 this.encoding = encoding; 377 } 378 } | Popular Tags |