1 19 20 package org.netbeans.upgrade; 21 22 import java.io.*; 23 import java.util.*; 24 import java.util.regex.Matcher ; 25 import java.util.regex.Pattern ; 26 import org.openide.util.*; 27 28 import org.openide.filesystems.*; 29 30 34 final class Copy extends Object { 35 36 37 44 public static void copyDeep (FileObject source, FileObject target, Set thoseToCopy) 45 throws IOException { 46 copyDeep (source, target, thoseToCopy, null); 47 } 48 49 private static void copyDeep ( 50 FileObject source, FileObject target, Set thoseToCopy, String prefix 51 ) throws IOException { 52 FileObject src = prefix == null ? source : FileUtil.createFolder (source, prefix); 53 54 FileObject[] arr = src.getChildren(); 55 for (int i = 0; i < arr.length; i++) { 56 String fullname; 57 if (prefix == null) { 58 fullname = arr[i].getNameExt (); 59 } else { 60 fullname = prefix + "/" + arr[i].getNameExt (); 61 } 62 if (arr[i].isData ()) { 63 if (!thoseToCopy.contains (fullname)) { 64 continue; 65 } 66 } 67 68 69 if (arr[i].isFolder()) { 70 copyDeep (source, target, thoseToCopy, fullname); 71 if (thoseToCopy.contains (fullname) && arr[i].getAttributes ().hasMoreElements ()) { 72 FileObject tg = FileUtil.createFolder (target, fullname); 73 FileUtil.copyAttributes (arr[i], tg); 74 } 75 } else { 76 FileObject folder = prefix == null ? target : FileUtil.createFolder (target, prefix); 77 FileObject tg = folder.getFileObject (arr[i].getNameExt ()); 78 try { 79 if (tg == null) { 80 tg = FileUtil.copyFile (arr[i], folder, arr[i].getName(), arr[i].getExt ()); 82 } 83 } catch (IOException ex) { 84 if (arr[i].getNameExt().endsWith("_hidden")) { 85 continue; 86 } 87 throw ex; 88 } 89 FileUtil.copyAttributes (arr[i], tg); 90 } 91 } 92 93 94 } 95 96 public static void appendSelectedLines(File sourceFile, File targetFolder, String [] regexForSelection) 97 throws IOException { 98 if (!sourceFile.exists()) { 99 return; 100 } 101 Pattern [] linePattern = new Pattern [regexForSelection.length]; 102 for (int i = 0; i < linePattern.length; i++) { 103 linePattern[i] = Pattern.compile(regexForSelection[i]); 104 } 105 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 106 File targetFile = new File(targetFolder,sourceFile.getName()); 107 if (!targetFolder.exists()) { 108 targetFolder.mkdirs(); 109 } 110 assert targetFolder.exists(); 111 112 if (!targetFile.exists()) { 113 targetFile.createNewFile(); 114 } else { 115 FileInputStream targetIS = new FileInputStream(targetFile); 117 try { 118 FileUtil.copy(targetIS, bos); 119 } finally { 120 targetIS.close(); 121 } 122 } 123 assert targetFile.exists(); 124 125 126 String line = null; 128 BufferedReader sourceReader = new BufferedReader(new FileReader(sourceFile)); 129 try { 130 while ((line = sourceReader.readLine()) != null) { 131 if (linePattern != null) { 132 for (int i = 0; i < linePattern.length; i++) { 133 Matcher m = linePattern[i].matcher(line); 134 if (m.matches()) { 135 bos.write(line.getBytes()); 136 bos.write('\n'); 137 break; 138 } 139 } 140 } else { 141 bos.write(line.getBytes()); 142 bos.write('\n'); 143 } 144 } 145 } finally { 146 sourceReader.close(); 147 } 148 149 ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray()); 150 FileOutputStream targetOS = new FileOutputStream(targetFile); 151 try { 152 FileUtil.copy(bin, targetOS); 153 } finally { 154 bin.close(); 155 targetOS.close(); 156 } 157 } 158 159 160 161 162 301 private void recursiveCopy (FileObject sourceFolder, FileObject destFolder) throws IOException { 302 FileObject childrens [] = sourceFolder.getChildren(); 303 for (int i = 0 ; i < childrens.length ; i++ ) { 304 final FileObject subSourceFo = childrens[i]; 305 FileObject subTargetFo = null; 306 307 if (subSourceFo.isFolder()) { 308 subTargetFo = destFolder.getFileObject(subSourceFo.getName()); 309 if (subTargetFo == null) { 310 subTargetFo = destFolder.createFolder(subSourceFo.getName()); 311 312 } 313 copyAttributes(subSourceFo,subTargetFo); 314 recursiveCopy(subSourceFo,subTargetFo); 315 } else { 316 subTargetFo = destFolder.getFileObject(subSourceFo.getNameExt()); 317 if (subTargetFo == null) { 318 if ( Utilities.getOperatingSystem () == Utilities.OS_VMS 319 && subSourceFo.getNameExt ().equalsIgnoreCase ( "_nbattrs.") ) 320 subTargetFo = FileUtil.copyFile(subSourceFo, destFolder, subSourceFo.getNameExt(), subSourceFo.getExt()); 321 else 322 subTargetFo = FileUtil.copyFile(subSourceFo, destFolder, subSourceFo.getName(), subSourceFo.getExt()); 323 } 324 copyAttributes(subSourceFo,subTargetFo); 325 } 326 } 327 } 328 329 private void message (String s) { 330 331 } 332 private void progress (int x, int y) { 333 334 } 335 private int maxItems; 336 private int items; 337 private int timeDev; 338 339 343 private void recursiveCopyWithFilter ( 344 FileObject source, FileObject dest, Object [] filter, long basicTime 345 ) throws IOException { 346 FileObject childrens [] = source.getChildren(); 347 if (source.isFolder() == false ) { 348 message (getString("MSG_IS_NOT_FOLDER", source.getName())); 349 } 350 351 maxItems += childrens.length; 353 354 for (int i = 0 ; i < childrens.length ; i++ ) { 355 FileObject subSourceFo = childrens[i]; 356 357 items++; 359 progress(items, maxItems); 360 361 if (!canCopy (subSourceFo, filter, basicTime)) 362 continue; 363 364 FileObject subTargetFo = null; 365 if (subSourceFo.isFolder ()) { 366 subTargetFo = dest.getFileObject (subSourceFo.getNameExt ()); 367 if (subTargetFo == null) { 368 subTargetFo = dest.createFolder (subSourceFo.getNameExt ()); 369 370 } 371 copyAttributes (subSourceFo, subTargetFo); 372 recursiveCopyWithFilter (subSourceFo, subTargetFo, filter, basicTime); 373 } else { 374 subTargetFo = dest.getFileObject (subSourceFo.getName (), subSourceFo.getExt ()); 375 376 if (subTargetFo != null) { 377 FileLock lock = subTargetFo.lock (); 378 subTargetFo.delete (lock); 379 lock.releaseLock (); 380 } 381 382 if ( Utilities.getOperatingSystem () == Utilities.OS_VMS 383 && subSourceFo.getNameExt ().equalsIgnoreCase ( "_nbattrs.") ) 384 subTargetFo = copyFile (subSourceFo, dest, subSourceFo.getNameExt ()); 385 else 386 subTargetFo = copyFile (subSourceFo, dest, subSourceFo.getName ()); 387 copyAttributes (subSourceFo, subTargetFo); 388 } 389 } 390 } 391 392 private FileObject copyFile (FileObject src, FileObject trg, String newName) throws IOException { 393 return FileUtil.copyFile (src, trg, newName); 394 } 395 396 private static void copyAttributes (FileObject source, FileObject dest) throws IOException { 397 Enumeration attrKeys = source.getAttributes(); 398 while (attrKeys.hasMoreElements()) { 399 String key = (String ) attrKeys.nextElement(); 400 Object value = source.getAttribute(key); 401 if (value != null) { 402 dest.setAttribute(key, value); 403 } 404 } 405 } 406 408 private boolean canCopy (FileObject fo, Object [] filter, long basicTime) throws IOException { 409 String nonCopiedFiles [] = (String []) filter [0]; 410 String wildcards [] = (String []) filter [1]; 411 String name = fo.getPath(); 412 413 if (fo.isFolder ()) { 414 return Arrays.binarySearch (nonCopiedFiles, name + "/*") < 0; } 416 417 for (int i = 0; i < wildcards.length; i++) { 418 if (name.endsWith (wildcards [i])) { 419 return false; 420 } 421 } 422 423 long time = fo.lastModified().getTime(); 424 425 boolean canCopy = Arrays.binarySearch (nonCopiedFiles, name) < 0 && 426 basicTime + timeDev <= time; 427 if (!canCopy) { 428 return false; 429 } 430 431 if (fo.getExt().equals("settings")) { boolean tag1 = false; 438 boolean tag2 = false; 439 BufferedReader reader = null; 440 try { 441 reader = new BufferedReader(new InputStreamReader(fo.getInputStream())); 442 String line; 443 while (null != (line = reader.readLine())) { 444 if (line.indexOf("<module name=") != -1) { if (line.indexOf("<module name=\"org.netbeans.modules.java/1\"") != -1) { tag1 = true; } else { 448 break; } 450 } 451 if (line.indexOf("<serialdata class=") != -1) { if (line.indexOf("<serialdata class=\"org.netbeans.modules.java.FastJavacCompilerType\">") != -1) { tag2 = true; if (tag1) { 455 break; 456 } 457 } else { 458 break; } 460 } 461 } 462 } catch (IOException ex) { 463 } finally { 466 if (reader != null) { 467 reader.close(); 468 } 469 } 470 if (tag1 && tag2) { 471 return false; } 473 } 474 475 return true; 476 } 477 479 480 484 public static String getIdeVersion (File dir) { 485 String version = null; 486 String dirType = null; 487 String branding = null; 488 489 if (new File (dir, "system").exists ()) { 490 return "3.6"; 491 } 492 return null; 493 } 494 495 497 protected static String getString (String key) { 498 return NbBundle.getMessage (Copy.class, key); 499 } 500 501 protected static String getString (String key,String param) { 502 return NbBundle.getMessage(Copy.class,key,param); 503 } 504 505 private static class AttrslessLocalFileSystem extends AbstractFileSystem implements AbstractFileSystem.Attr { 506 public AttrslessLocalFileSystem (LocalFileSystem fs) { 507 super (); 508 this.change = new LocalFileSystem.Impl (fs); 509 this.info = (AbstractFileSystem.Info) this.change; 510 this.list = (AbstractFileSystem.List) this.change; 511 this.attr = this; 512 } 513 public boolean isReadOnly () { 514 return false; 515 } 516 public String getDisplayName () { 517 return getClass ().toString (); } 519 520 522 public void deleteAttributes (String name) { 523 } 524 public Enumeration<String > attributes (String name) { 525 return org.openide.util.Enumerations.empty (); 526 } 527 public void renameAttributes (String oldName, String newName) { 528 } 529 public void writeAttribute (String name, String attrName, Object value) throws IOException { 530 } 531 public Object readAttribute (String name, String attrName) { 532 return null; 533 } 534 } 535 } 536 | Popular Tags |