1 56 package org.objectstyle.cayenne.project; 57 58 import java.io.File ; 59 import java.io.FileOutputStream ; 60 import java.io.IOException ; 61 import java.io.OutputStreamWriter ; 62 import java.io.PrintWriter ; 63 64 import org.objectstyle.cayenne.util.Util; 65 66 72 public abstract class ProjectFile { 73 74 protected String location; 75 protected File tempFile; 76 protected Project projectObj; 77 78 public ProjectFile() {} 79 80 83 public ProjectFile(Project project, String location) { 84 this.location = location; 85 this.projectObj = project; 86 } 87 88 91 public String getLocation() { 92 String oName = getObjectName(); 93 if (oName == null) { 94 throw new NullPointerException ("Null name."); 95 } 96 97 return oName + getLocationSuffix(); 98 } 99 100 103 public String getOldLocation() { 104 return location; 105 } 106 107 112 public String getLocationSuffix() { 113 return ""; 114 } 115 116 119 public abstract Object getObject(); 120 121 125 public abstract String getObjectName(); 126 127 132 public abstract void save(PrintWriter out) throws Exception ; 133 134 138 public abstract boolean canHandle(Object obj); 139 140 144 public boolean canHandleObject() { 145 return canHandle(getObject()); 146 } 147 148 151 public void synchronizeLocation() { 152 location = getLocation(); 153 } 154 155 159 public void willSave() {} 160 161 166 public void saveTemp() throws Exception { 167 if (tempFile != null && tempFile.isFile()) { 169 tempFile.delete(); 170 tempFile = null; 171 } 172 173 File finalFile = resolveFile(); 175 checkWritePermissions(finalFile); 176 177 tempFile = tempFileForFile(finalFile); 179 180 FileOutputStream fout = new FileOutputStream (tempFile); 182 OutputStreamWriter fw = new OutputStreamWriter (fout, "UTF-8"); 183 184 try { 185 PrintWriter pw = new PrintWriter (fw); 186 try { 187 save(pw); 188 } finally { 189 pw.close(); 190 } 191 } finally { 192 fw.close(); 193 } 194 } 195 196 201 public File resolveFile() { 202 return getProject().resolveFile(getLocation()); 203 } 204 205 211 public File resolveOldFile() { 212 String oldLocation = getOldLocation(); 213 return (oldLocation != null) ? getProject().resolveFile(oldLocation) : null; 214 } 215 216 219 public File saveCommit() throws ProjectException { 220 File finalFile = resolveFile(); 221 222 if (tempFile != null) { 223 if (finalFile.exists()) { 224 if (!finalFile.delete()) { 225 throw new ProjectException( 226 "Unable to remove old master file : " + finalFile); 227 } 228 } 229 230 if (!tempFile.renameTo(finalFile)) { 231 throw new ProjectException( 232 "Unable to move " + tempFile + " to " + finalFile); 233 } 234 235 tempFile = null; 236 } 237 238 return finalFile; 239 } 240 241 244 public void saveUndo() { 245 if (tempFile != null && tempFile.isFile()) { 246 tempFile.delete(); 247 tempFile = null; 248 } 249 } 250 251 255 public Project getProject() { 256 return projectObj; 257 } 258 259 public boolean isRenamed() { 260 return !Util.nullSafeEquals(location, getLocation()); 261 } 262 263 266 protected File tempFileForFile(File f) throws IOException { 267 File parent = f.getParentFile(); 268 String name = f.getName(); 269 270 if (name == null || name.length() < 3) { 271 name = "cayenne-project"; 272 } 273 274 if(!parent.exists()) { 275 if(!parent.mkdirs()) { 276 throw new IOException ("Error creating directory tree: " + parent); 277 } 278 } 279 280 return File.createTempFile(name, null, parent); 281 } 282 283 protected void checkWritePermissions(File file) throws IOException { 284 if (file.isDirectory()) { 285 throw new IOException ("Target file is a directory: " + file); 286 } 287 288 if (file.exists() && !file.canWrite()) { 289 throw new IOException ("Can't write to file: " + file); 290 } 291 } 292 293 public String toString() { 294 StringBuffer buf = new StringBuffer (); 295 buf.append("ProjectFile [").append(getClass().getName()).append("]: name = "); 296 if (getObject() != null) { 297 buf.append("*null*"); 298 } else { 299 buf.append(getObjectName()); 300 } 301 302 return buf.toString(); 303 } 304 } 305 | Popular Tags |