| 1 19 20 package org.netbeans.bluej.upgrade; 21 22 import java.io.*; 23 import java.util.*; 24 import org.openide.util.*; 25 import java.util.jar.*; 26 import org.w3c.dom.*; 27 import org.xml.sax.*; 28 import org.openide.xml.XMLUtil; 29 30 import org.openide.filesystems.*; 31 import org.openide.filesystems.FileSystem; 32 33 34 38 final class Copy extends Object { 39 40 41 48 public static void copyDeep (FileObject source, FileObject target, Set thoseToCopy) 49 throws IOException { 50 copyDeep (source, target, thoseToCopy, null); 51 } 52 53 private static void copyDeep ( 54 FileObject source, FileObject target, Set thoseToCopy, String prefix 55 ) throws IOException { 56 FileObject src = prefix == null ? source : FileUtil.createFolder (source, prefix); 57 58 FileObject[] arr = src.getChildren(); 59 for (int i = 0; i < arr.length; i++) { 60 String fullname; 61 if (prefix == null) { 62 fullname = arr[i].getNameExt (); 63 } else { 64 fullname = prefix + "/" + arr[i].getNameExt (); 65 } 66 if (arr[i].isData ()) { 67 if (!thoseToCopy.contains (fullname)) { 68 continue; 69 } 70 } 71 72 73 if (arr[i].isFolder()) { 74 copyDeep (source, target, thoseToCopy, fullname); 75 if (thoseToCopy.contains (fullname) && arr[i].getAttributes ().hasMoreElements ()) { 76 FileObject tg = FileUtil.createFolder (target, fullname); 77 FileUtil.copyAttributes (arr[i], tg); 78 } 79 } else { 80 FileObject folder = prefix == null ? target : FileUtil.createFolder (target, prefix); 81 FileObject tg = folder.getFileObject (arr[i].getNameExt ()); 82 try { 83 if (tg == null) { 84 tg = FileUtil.copyFile (arr[i], folder, arr[i].getName(), arr[i].getExt ()); 86 } 87 } catch (IOException ex) { 88 if (arr[i].getNameExt().endsWith("_hidden")) { 89 continue; 90 } 91 throw ex; 92 } 93 FileUtil.copyAttributes (arr[i], tg); 94 } 95 } 96 97 98 } 99 100 101 102 241 private void recursiveCopy (FileObject sourceFolder, FileObject destFolder) throws IOException { 242 FileObject childrens [] = sourceFolder.getChildren(); 243 for (int i = 0 ; i < childrens.length ; i++ ) { 244 final FileObject subSourceFo = childrens[i]; 245 FileObject subTargetFo = null; 246 247 if (subSourceFo.isFolder()) { 248 subTargetFo = destFolder.getFileObject(subSourceFo.getName()); 249 if (subTargetFo == null) { 250 subTargetFo = destFolder.createFolder(subSourceFo.getName()); 251 252 } 253 copyAttributes(subSourceFo,subTargetFo); 254 recursiveCopy(subSourceFo,subTargetFo); 255 } else { 256 subTargetFo = destFolder.getFileObject(subSourceFo.getNameExt()); 257 if (subTargetFo == null) { 258 if ( Utilities.getOperatingSystem () == Utilities.OS_VMS 259 && subSourceFo.getNameExt ().equalsIgnoreCase ( "_nbattrs.") ) 260 subTargetFo = FileUtil.copyFile(subSourceFo, destFolder, subSourceFo.getNameExt(), subSourceFo.getExt()); 261 else 262 subTargetFo = FileUtil.copyFile(subSourceFo, destFolder, subSourceFo.getName(), subSourceFo.getExt()); 263 } 264 copyAttributes(subSourceFo,subTargetFo); 265 } 266 } 267 } 268 269 private void message (String s) { 270 271 } 272 private void progress (int x, int y) { 273 274 } 275 private int maxItems; 276 private int items; 277 private int timeDev; 278 279 283 private void recursiveCopyWithFilter ( 284 FileObject source, FileObject dest, Object [] filter, long basicTime 285 ) throws IOException { 286 FileObject childrens [] = source.getChildren(); 287 if (source.isFolder() == false ) { 288 message (getString("MSG_IS_NOT_FOLDER", source.getName())); 289 } 290 291 maxItems += childrens.length; 293 294 for (int i = 0 ; i < childrens.length ; i++ ) { 295 FileObject subSourceFo = childrens[i]; 296 297 items++; 299 progress(items, maxItems); 300 301 if (!canCopy (subSourceFo, filter, basicTime)) 302 continue; 303 304 FileObject subTargetFo = null; 305 if (subSourceFo.isFolder ()) { 306 subTargetFo = dest.getFileObject (subSourceFo.getNameExt ()); 307 if (subTargetFo == null) { 308 subTargetFo = dest.createFolder (subSourceFo.getNameExt ()); 309 310 } 311 copyAttributes (subSourceFo, subTargetFo); 312 recursiveCopyWithFilter (subSourceFo, subTargetFo, filter, basicTime); 313 } else { 314 subTargetFo = dest.getFileObject (subSourceFo.getName (), subSourceFo.getExt ()); 315 316 if (subTargetFo != null) { 317 FileLock lock = subTargetFo.lock (); 318 subTargetFo.delete (lock); 319 lock.releaseLock (); 320 } 321 322 if ( Utilities.getOperatingSystem () == Utilities.OS_VMS 323 && subSourceFo.getNameExt ().equalsIgnoreCase ( "_nbattrs.") ) 324 subTargetFo = copyFile (subSourceFo, dest, subSourceFo.getNameExt ()); 325 else 326 subTargetFo = copyFile (subSourceFo, dest, subSourceFo.getName ()); 327 copyAttributes (subSourceFo, subTargetFo); 328 } 329 } 330 } 331 332 private FileObject copyFile (FileObject src, FileObject trg, String newName) throws IOException { 333 return FileUtil.copyFile (src, trg, newName); 334 } 335 336 private static void copyAttributes (FileObject source, FileObject dest) throws IOException { 337 Enumeration attrKeys = source.getAttributes(); 338 while (attrKeys.hasMoreElements()) { 339 String key = (String ) attrKeys.nextElement(); 340 Object value = source.getAttribute(key); 341 if (value != null) { 342 dest.setAttribute(key, value); 343 } 344 } 345 } 346 348 private boolean canCopy (FileObject fo, Object [] filter, long basicTime) throws IOException { 349 String nonCopiedFiles [] = (String []) filter [0]; 350 String wildcards [] = (String []) filter [1]; 351 String name = fo.getPath(); 352 353 if (fo.isFolder ()) { 354 return Arrays.binarySearch (nonCopiedFiles, name + "/*") < 0; } 356 357 for (int i = 0; i < wildcards.length; i++) { 358 if (name.endsWith (wildcards [i])) { 359 return false; 360 } 361 } 362 363 long time = fo.lastModified().getTime(); 364 365 boolean canCopy = Arrays.binarySearch (nonCopiedFiles, name) < 0 && 366 basicTime + timeDev <= time; 367 if (!canCopy) { 368 return false; 369 } 370 371 if (fo.getExt().equals("settings")) { boolean tag1 = false; 378 boolean tag2 = false; 379 BufferedReader reader = null; 380 try { 381 reader = new BufferedReader(new InputStreamReader(fo.getInputStream())); 382 String line; 383 while (null != (line = reader.readLine())) { 384 if (line.indexOf("<module name=") != -1) { if (line.indexOf("<module name=\"org.netbeans.modules.java/1\"") != -1) { tag1 = true; } else { 388 break; } 390 } 391 if (line.indexOf("<serialdata class=") != -1) { if (line.indexOf("<serialdata class=\"org.netbeans.modules.java.FastJavacCompilerType\">") != -1) { tag2 = true; if (tag1) { 395 break; 396 } 397 } else { 398 break; } 400 } 401 } 402 } catch (IOException ex) { 403 } finally { 406 if (reader != null) { 407 reader.close(); 408 } 409 } 410 if (tag1 && tag2) { 411 return false; } 413 } 414 415 return true; 416 } 417 419 420 424 public static String getIdeVersion (File dir) { 425 String version = null; 426 String dirType = null; 427 String branding = null; 428 429 if (new File (dir, "system").exists ()) { 430 return "3.6"; 431 } 432 return null; 433 } 434 435 437 protected static String getString (String key) { 438 return NbBundle.getMessage (Copy.class, key); 439 } 440 441 protected static String getString (String key,String param) { 442 return NbBundle.getMessage(Copy.class,key,param); 443 } 444 445 private static class AttrslessLocalFileSystem extends AbstractFileSystem implements AbstractFileSystem.Attr { 446 public AttrslessLocalFileSystem (LocalFileSystem fs) { 447 super (); 448 this.change = new LocalFileSystem.Impl (fs); 449 this.info = (AbstractFileSystem.Info) this.change; 450 this.list = (AbstractFileSystem.List) this.change; 451 this.attr = this; 452 } 453 public boolean isReadOnly () { 454 return false; 455 } 456 public String getDisplayName () { 457 return getClass ().toString (); } 459 460 462 public void deleteAttributes (String name) { 463 } 464 public Enumeration attributes (String name) { 465 return org.openide.util.Enumerations.empty (); 466 } 467 public void renameAttributes (String oldName, String newName) { 468 } 469 public void writeAttribute (String name, String attrName, Object value) throws IOException { 470 } 471 public Object readAttribute (String name, String attrName) { 472 return null; 473 } 474 } 475 } 476 | Popular Tags |