1 19 20 package org.apache.cayenne.project; 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.PrintWriter ; 27 28 import org.apache.cayenne.util.Util; 29 30 36 public abstract class ProjectFile { 37 38 protected String location; 39 protected File tempFile; 40 protected Project projectObj; 41 42 public ProjectFile() {} 43 44 47 public ProjectFile(Project project, String location) { 48 this.location = location; 49 this.projectObj = project; 50 } 51 52 55 public String getLocation() { 56 String oName = getObjectName(); 57 if (oName == null) { 58 throw new NullPointerException ("Null name."); 59 } 60 61 return oName + getLocationSuffix(); 62 } 63 64 67 public String getOldLocation() { 68 return location; 69 } 70 71 76 public String getLocationSuffix() { 77 return ""; 78 } 79 80 83 public abstract Object getObject(); 84 85 89 public abstract String getObjectName(); 90 91 96 public abstract void save(PrintWriter out) throws Exception ; 97 98 102 public abstract boolean canHandle(Object obj); 103 104 108 public boolean canHandleObject() { 109 return canHandle(getObject()); 110 } 111 112 115 public void synchronizeLocation() { 116 location = getLocation(); 117 } 118 119 123 public void willSave() {} 124 125 130 public void saveTemp() throws Exception { 131 if (tempFile != null && tempFile.isFile()) { 133 tempFile.delete(); 134 tempFile = null; 135 } 136 137 File finalFile = resolveFile(); 139 checkWritePermissions(finalFile); 140 141 tempFile = tempFileForFile(finalFile); 143 144 FileOutputStream fout = new FileOutputStream (tempFile); 146 OutputStreamWriter fw = new OutputStreamWriter (fout, "UTF-8"); 147 148 try { 149 PrintWriter pw = new PrintWriter (fw); 150 try { 151 save(pw); 152 } finally { 153 pw.close(); 154 } 155 } finally { 156 fw.close(); 157 } 158 } 159 160 165 public File resolveFile() { 166 return getProject().resolveFile(getLocation()); 167 } 168 169 175 public File resolveOldFile() { 176 String oldLocation = getOldLocation(); 177 return (oldLocation != null) ? getProject().resolveFile(oldLocation) : null; 178 } 179 180 183 public File saveCommit() throws ProjectException { 184 File finalFile = resolveFile(); 185 186 if (tempFile != null) { 187 if (finalFile.exists()) { 188 if (!finalFile.delete()) { 189 throw new ProjectException( 190 "Unable to remove old master file : " + finalFile); 191 } 192 } 193 194 if (!tempFile.renameTo(finalFile)) { 195 throw new ProjectException( 196 "Unable to move " + tempFile + " to " + finalFile); 197 } 198 199 tempFile = null; 200 } 201 202 return finalFile; 203 } 204 205 208 public void saveUndo() { 209 if (tempFile != null && tempFile.isFile()) { 210 tempFile.delete(); 211 tempFile = null; 212 } 213 } 214 215 219 public Project getProject() { 220 return projectObj; 221 } 222 223 public boolean isRenamed() { 224 return !Util.nullSafeEquals(location, getLocation()); 225 } 226 227 230 protected File tempFileForFile(File f) throws IOException { 231 File parent = f.getParentFile(); 232 String name = f.getName(); 233 234 if (name == null || name.length() < 3) { 235 name = "cayenne-project"; 236 } 237 238 if(!parent.exists()) { 239 if(!parent.mkdirs()) { 240 throw new IOException ("Error creating directory tree: " + parent); 241 } 242 } 243 244 return File.createTempFile(name, null, parent); 245 } 246 247 protected void checkWritePermissions(File file) throws IOException { 248 if (file.isDirectory()) { 249 throw new IOException ("Target file is a directory: " + file); 250 } 251 252 if (file.exists() && !file.canWrite()) { 253 throw new IOException ("Can't write to file: " + file); 254 } 255 } 256 257 public String toString() { 258 StringBuffer buf = new StringBuffer (); 259 buf.append("ProjectFile [").append(getClass().getName()).append("]: name = "); 260 if (getObject() != null) { 261 buf.append("*null*"); 262 } else { 263 buf.append(getObjectName()); 264 } 265 266 return buf.toString(); 267 } 268 } 269 | Popular Tags |