1 19 20 package org.openide.loaders; 21 22 import java.io.*; 23 import org.openide.filesystems.*; 24 import org.openide.util.Lookup; 25 import org.openide.util.NbBundle; 26 27 32 public class FileEntry extends MultiDataObject.Entry { 33 34 static final long serialVersionUID = 5972727204237511983L; 35 36 40 public FileEntry(MultiDataObject obj, FileObject fo) { 41 obj.super (fo); 42 } 43 44 48 public FileObject copy (FileObject f, String suffix) throws IOException { 49 FileObject fo = getFile(); 50 String newName = fo.getName() + suffix; 51 return fo.copy (f, newName, fo.getExt ()); 52 } 53 54 60 public FileObject rename (String name) throws IOException { 61 boolean locked = isLocked (); 62 63 FileLock lock = takeLock(); 64 try { 65 getFile().rename(lock, name, getFile().getExt()); 66 } finally { 67 if (!locked) 68 lock.releaseLock(); 69 } 70 return getFile (); 71 } 72 73 78 public FileObject move (FileObject f, String suffix) throws IOException { 79 boolean locked = isLocked (); 80 81 FileObject fo = getFile(); 82 FileLock lock = takeLock (); 83 try { 84 String newName = fo.getName() + suffix; 85 FileObject dest = fo.move (lock, f, newName, fo.getExt ()); 86 return dest; 87 } finally { 88 if (!locked) 89 lock.releaseLock (); 90 } 91 } 92 93 95 public void delete () throws IOException { 96 102 boolean locked = isLocked (); 103 104 FileLock lock = takeLock(); 105 try { 106 getFile().delete(lock); 107 } 108 finally { 109 if (!locked) 110 lock.releaseLock(); 111 } 112 } 113 114 118 public FileObject createFromTemplate (FileObject f, String name) throws IOException { 119 if (name == null) { 120 name = FileUtil.findFreeFileName( 121 f, 122 getFile ().getName (), getFile ().getExt () 123 ); 124 } 125 126 127 FileObject fo = null; 128 for (CreateFromTemplateHandler h : Lookup.getDefault().lookupAll(CreateFromTemplateHandler.class)) { 129 if (h.accept(getFile())) { 130 fo = h.createFromTemplate(getFile(), f, name, DataObject.CreateAction.findParameters(name)); 131 assert fo != null; 132 break; 133 } 134 } 135 136 if (fo == null) { 137 fo = getFile().copy (f, name, getFile().getExt ()); 138 } 139 140 141 DataObject.setTemplate (fo, false); 143 144 return fo; 145 } 146 147 154 public abstract static class Format extends FileEntry { 155 static final long serialVersionUID =8896750589709521197L; 156 160 public Format (MultiDataObject obj, FileObject fo) { 161 super (obj, fo); 162 } 163 164 170 public FileObject createFromTemplate (FileObject f, String name) throws IOException { 171 String ext = getFile ().getExt (); 172 173 if (name == null) { 174 name = FileUtil.findFreeFileName( 175 f, 176 getFile ().getName (), ext 177 ); 178 } 179 180 181 FileObject fo = null; 182 for (CreateFromTemplateHandler h : Lookup.getDefault().lookupAll(CreateFromTemplateHandler.class)) { 183 if (h.accept(getFile())) { 184 fo = h.createFromTemplate(getFile(), f, name, DataObject.CreateAction.findParameters(name)); 185 assert fo != null; 186 break; 187 } 188 } 189 190 if (fo != null) { 191 DataObject.setTemplate (fo, false); 193 return fo; 194 } 195 196 fo = f.createData (name, ext); 197 198 java.text.Format frm = createFormat (f, name, ext); 199 200 BufferedReader r = new BufferedReader (new InputStreamReader (getFile ().getInputStream ())); 201 try { 202 FileLock lock = fo.lock (); 203 try { 204 BufferedWriter w = new BufferedWriter (new OutputStreamWriter (fo.getOutputStream (lock))); 205 206 try { 207 String current; 208 while ((current = r.readLine ()) != null) { 209 w.write (frm.format (current)); 210 w.newLine (); 212 } 213 } finally { 214 w.close (); 215 } 216 } finally { 217 lock.releaseLock (); 218 } 219 } finally { 220 r.close (); 221 } 222 223 FileUtil.copyAttributes (getFile (), fo); 225 226 DataObject.setTemplate (fo, false); 228 229 return fo; 230 } 231 232 240 protected abstract java.text.Format createFormat (FileObject target, String n, String e); 241 242 } 243 244 245 251 public final static class Numb extends MultiDataObject.Entry { 252 253 static final long serialVersionUID = -6572157492885890612L; 254 255 260 public Numb (MultiDataObject obj, FileObject fo) { 261 obj.super (fo); 262 } 263 264 267 public boolean isImportant () { 268 return false; 269 } 270 271 276 public FileObject copy (FileObject f, String suffix) { 277 return null; 278 } 279 280 285 public FileObject rename (String name) throws IOException { 286 stdBehaving(); 287 return null; 288 } 289 290 296 public FileObject move (FileObject f, String suffix) throws IOException { 297 stdBehaving(); 298 return null; 299 } 300 301 304 public void delete () throws IOException { 305 stdBehaving(); 306 } 307 308 311 private void stdBehaving () throws IOException { 312 if (getFile() == null) 313 return; 314 315 if (isLocked()) 316 throw new IOException (NbBundle.getBundle (FileEntry.class).getString ("EXC_SharedAccess")); 317 318 FileLock lock = takeLock(); 319 try { 320 getFile().delete(lock); 321 } finally { 322 if (lock != null) 323 lock.releaseLock(); 324 } 325 } 326 327 332 public FileObject createFromTemplate (FileObject f, String name) { 333 return null; 334 } 335 } 336 342 public final static class Folder extends MultiDataObject.Entry { 343 344 345 public Folder (MultiDataObject obj, FileObject fo) { 346 obj.super (fo); 347 } 348 349 355 public FileObject copy (FileObject f, String suffix) throws IOException { 356 String add = suffix + ((getFile ().getExt ().length () > 0) ? "." + getFile ().getExt () : ""); 357 358 FileObject fo = FileUtil.createFolder (f, getFile ().getName () + add); 359 FileUtil.copyAttributes (getFile (), fo); 360 361 return fo; 362 } 363 364 370 public FileObject move (FileObject f, String suffix) throws IOException { 371 return copy (f, suffix); 372 } 373 374 380 public FileObject createFromTemplate (FileObject f, String name) throws IOException { 381 if (name == null) { 382 name = FileUtil.findFreeFileName( 383 f, 384 getFile ().getName (), getFile ().getExt () 385 ); 386 } 387 FileObject fo = FileUtil.createFolder (f, name); 388 389 FileUtil.copyAttributes (getFile (), fo); 390 DataObject.setTemplate (fo, false); 391 392 return fo; 393 } 394 395 400 public FileObject rename (String name) throws IOException { 401 boolean locked = isLocked (); 402 FileLock lock = takeLock (); 403 try { 404 getFile ().rename (lock, name, null); 405 } finally { 406 if (!locked) 407 lock.releaseLock (); 408 } 409 return getFile (); 410 } 411 412 417 public void delete () throws IOException { 418 boolean locked = isLocked (); 419 FileLock lock = takeLock (); 420 try { 421 getFile ().delete (lock); 422 } finally { 423 if (!locked) 424 lock.releaseLock(); 425 } 426 } 427 428 } 429 } 430 | Popular Tags |