1 16 package org.apache.commons.vfs.provider; 17 18 import org.apache.commons.vfs.Capability; 19 import org.apache.commons.vfs.FileContent; 20 import org.apache.commons.vfs.FileContentInfoFactory; 21 import org.apache.commons.vfs.FileName; 22 import org.apache.commons.vfs.FileObject; 23 import org.apache.commons.vfs.FileSelector; 24 import org.apache.commons.vfs.FileSystem; 25 import org.apache.commons.vfs.FileSystemException; 26 import org.apache.commons.vfs.FileType; 27 import org.apache.commons.vfs.FileUtil; 28 import org.apache.commons.vfs.NameScope; 29 import org.apache.commons.vfs.RandomAccessContent; 30 import org.apache.commons.vfs.Selectors; 31 import org.apache.commons.vfs.util.RandomAccessMode; 32 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.io.OutputStream ; 36 import java.net.MalformedURLException ; 37 import java.net.URL ; 38 import java.security.AccessController ; 39 import java.security.PrivilegedActionException ; 40 import java.security.PrivilegedExceptionAction ; 41 import java.security.cert.Certificate ; 42 import java.util.ArrayList ; 43 import java.util.Arrays ; 44 import java.util.Collections ; 45 import java.util.List ; 46 import java.util.Map ; 47 48 58 public abstract class AbstractFileObject implements FileObject 59 { 60 private static final FileName[] EMPTY_FILE_ARRAY = {}; 62 63 private final AbstractFileName name; 64 private final AbstractFileSystem fs; 65 66 private DefaultFileContent content; 67 68 private boolean attached; 70 private FileType type; 71 private AbstractFileObject parent; 72 73 private FileName[] children; 77 private List objects; 78 79 protected AbstractFileObject(final FileName name, 80 final AbstractFileSystem fs) 81 { 82 this.name = (AbstractFileName) name; 83 this.fs = fs; 84 } 85 86 93 protected void doAttach() throws Exception 94 { 95 } 96 97 105 protected void doDetach() throws Exception 106 { 107 } 108 109 113 protected abstract FileType doGetType() throws Exception ; 114 115 121 protected boolean doIsHidden() throws Exception 122 { 123 return false; 124 } 125 126 132 protected boolean doIsReadable() throws Exception 133 { 134 return true; 135 } 136 137 143 protected boolean doIsWriteable() throws Exception 144 { 145 return true; 146 } 147 148 153 protected abstract String [] doListChildren() throws Exception ; 154 155 162 protected FileObject[] doListChildrenResolved() throws Exception 163 { 164 return null; 165 } 166 167 177 protected void doDelete() throws Exception 178 { 179 throw new FileSystemException("vfs.provider/delete-not-supported.error"); 180 } 181 182 190 protected void doRename(FileObject newfile) throws Exception 191 { 192 throw new FileSystemException("vfs.provider/rename-not-supported.error"); 193 } 194 195 205 protected void doCreateFolder() throws Exception 206 { 207 throw new FileSystemException("vfs.provider/create-folder-not-supported.error"); 208 } 209 210 216 protected void onChildrenChanged(FileName child, FileType newType) throws Exception 217 { 218 } 219 220 225 protected void onChange() throws Exception 226 { 227 } 228 229 235 protected long doGetLastModifiedTime() throws Exception 236 { 237 throw new FileSystemException("vfs.provider/get-last-modified-not-supported.error"); 238 } 239 240 246 protected void doSetLastModifiedTime(final long modtime) 247 throws Exception 248 { 249 throw new FileSystemException("vfs.provider/set-last-modified-not-supported.error"); 250 } 251 252 258 protected Map doGetAttributes() 259 throws Exception 260 { 261 return Collections.EMPTY_MAP; 262 } 263 264 270 protected void doSetAttribute(final String atttrName, final Object value) 271 throws Exception 272 { 273 throw new FileSystemException("vfs.provider/set-attribute-not-supported.error"); 274 } 275 276 282 protected Certificate [] doGetCertificates() throws Exception 283 { 284 return null; 285 } 286 287 291 protected abstract long doGetContentSize() throws Exception ; 292 293 302 protected abstract InputStream doGetInputStream() throws Exception ; 303 304 312 protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception 313 { 314 throw new FileSystemException("vfs.provider/random-access-not-supported.error"); 315 } 316 317 334 protected OutputStream doGetOutputStream(boolean bAppend) throws Exception 335 { 336 throw new FileSystemException("vfs.provider/write-not-supported.error"); 337 } 338 339 342 public String toString() 343 { 344 return name.getURI(); 345 } 346 347 350 public FileName getName() 351 { 352 return name; 353 } 354 355 358 public FileSystem getFileSystem() 359 { 360 return fs; 361 } 362 363 366 public URL getURL() throws FileSystemException 367 { 368 final StringBuffer buf = new StringBuffer (); 369 try 370 { 371 return (URL ) AccessController.doPrivileged(new PrivilegedExceptionAction () 372 { 373 public Object run() throws MalformedURLException 374 { 375 return new URL (UriParser.extractScheme(name.getURI(), buf), "", -1, 376 buf.toString(), new DefaultURLStreamHandler(fs.getContext(), fs.getFileSystemOptions())); 377 } 378 }); 379 } 380 catch (final PrivilegedActionException e) 381 { 382 throw new FileSystemException("vfs.provider/get-url.error", name, e.getException()); 383 } 384 } 385 386 389 public boolean exists() throws FileSystemException 390 { 391 attach(); 392 return (type != FileType.IMAGINARY); 393 } 394 395 398 public FileType getType() throws FileSystemException 399 { 400 attach(); 401 return type; 402 } 403 404 407 public boolean isHidden() throws FileSystemException 408 { 409 try 410 { 411 attach(); 412 if (exists()) 413 { 414 return doIsHidden(); 415 } 416 else 417 { 418 return false; 419 } 420 } 421 catch (final Exception exc) 422 { 423 throw new FileSystemException("vfs.provider/check-is-hidden.error", name, exc); 424 } 425 } 426 427 430 public boolean isReadable() throws FileSystemException 431 { 432 try 433 { 434 attach(); 435 if (exists()) 436 { 437 return doIsReadable(); 438 } 439 else 440 { 441 return false; 442 } 443 } 444 catch (final Exception exc) 445 { 446 throw new FileSystemException("vfs.provider/check-is-readable.error", name, exc); 447 } 448 } 449 450 453 public boolean isWriteable() throws FileSystemException 454 { 455 try 456 { 457 attach(); 458 if (exists()) 459 { 460 return doIsWriteable(); 461 } 462 else 463 { 464 final FileObject parent = getParent(); 465 if (parent != null) 466 { 467 return parent.isWriteable(); 468 } 469 return true; 470 } 471 } 472 catch (final Exception exc) 473 { 474 throw new FileSystemException("vfs.provider/check-is-writeable.error", name, exc); 475 } 476 } 477 478 481 public FileObject getParent() throws FileSystemException 482 { 483 if (this == fs.getRoot()) 484 { 485 if (fs.getParentLayer() != null) 486 { 487 return fs.getParentLayer().getParent(); 489 } 490 else 491 { 492 return null; 494 } 495 } 496 497 synchronized (this) 498 { 499 if (parent == null) 501 { 502 parent = (AbstractFileObject) fs.resolveFile(name.getParent()); 503 } 504 } 505 return parent; 506 } 507 508 511 public FileObject[] getChildren() throws FileSystemException 512 { 513 synchronized (this) 514 { 515 attach(); 516 if (!type.hasChildren()) 517 { 518 throw new FileSystemException("vfs.provider/list-children-not-folder.error", name); 519 } 520 521 if (children != null) 523 { 524 return resolveFiles(children); 525 } 526 527 FileObject[] childrenObjects; 529 try 530 { 531 childrenObjects = doListChildrenResolved(); 532 children = extractNames(childrenObjects); 533 } 534 catch (Exception exc) 535 { 536 throw new FileSystemException("vfs.provider/list-children.error", new Object []{name}, exc); 537 } 538 539 if (childrenObjects != null) 540 { 541 return childrenObjects; 542 } 543 544 final String [] files; 546 try 547 { 548 files = doListChildren(); 549 } 550 catch (Exception exc) 551 { 552 throw new FileSystemException("vfs.provider/list-children.error", new Object []{name}, exc); 553 } 554 555 if (files == null || files.length == 0) 556 { 557 children = EMPTY_FILE_ARRAY; 559 } 560 else 561 { 562 children = new FileName[files.length]; 565 for (int i = 0; i < files.length; i++) 566 { 567 final String file = files[i]; 568 children[i] = getFileSystem().getFileSystemManager().resolveName(name, file, NameScope.CHILD); 571 } 572 } 573 574 return resolveFiles(children); 575 } 576 } 577 578 private FileName[] extractNames(FileObject[] objects) 579 { 580 if (objects == null) 581 { 582 return null; 583 } 584 585 FileName[] names = new FileName[objects.length]; 586 for (int iterObjects = 0; iterObjects < objects.length; iterObjects++) 587 { 588 names[iterObjects] = objects[iterObjects].getName(); 589 } 590 591 return names; 592 } 593 594 private FileObject[] resolveFiles(FileName[] children) throws FileSystemException 595 { 596 if (children == null) 597 { 598 return null; 599 } 600 601 FileObject[] objects = new FileObject[children.length]; 602 for (int iterChildren = 0; iterChildren < children.length; iterChildren++) 603 { 604 objects[iterChildren] = resolveFile(children[iterChildren]); 605 } 606 607 return objects; 608 } 609 610 private FileObject resolveFile(FileName child) throws FileSystemException 611 { 612 return fs.resolveFile(child); 613 } 614 615 618 public FileObject getChild(final String name) throws FileSystemException 619 { 620 FileObject[] children = getChildren(); 622 for (int i = 0; i < children.length; i++) 623 { 624 final FileName child = children[i].getName(); 626 if (child.getBaseName().equals(name)) 629 { 630 return resolveFile(child); 631 } 632 } 633 return null; 634 } 635 636 639 public FileObject resolveFile(final String name, final NameScope scope) 640 throws FileSystemException 641 { 642 return fs.resolveFile(getFileSystem().getFileSystemManager().resolveName(this.name, name, scope)); 644 } 645 646 654 public FileObject resolveFile(final String path) throws FileSystemException 655 { 656 final FileName otherName = getFileSystem().getFileSystemManager().resolveName(name, path); 657 return fs.resolveFile(otherName); 658 } 659 660 665 private boolean deleteSelf() throws FileSystemException 666 { 667 synchronized (this) 668 { 669 675 676 if (getType() == FileType.IMAGINARY) 677 { 678 return false; 680 } 681 682 try 683 { 684 doDelete(); 686 687 handleDelete(); 689 } 690 catch (final RuntimeException re) 691 { 692 throw re; 693 } 694 catch (final Exception exc) 695 { 696 throw new FileSystemException("vfs.provider/delete.error", new Object []{name}, exc); 697 } 698 699 return true; 700 } 701 } 702 703 709 public boolean delete() throws FileSystemException 710 { 711 return delete(Selectors.SELECT_SELF) > 0; 712 } 713 714 719 public int delete(final FileSelector selector) throws FileSystemException 720 { 721 int nuofDeleted = 0; 722 723 if (getType() == FileType.IMAGINARY) 724 { 725 return nuofDeleted; 727 } 728 729 ArrayList files = new ArrayList (); 731 findFiles(selector, true, files); 732 733 final int count = files.size(); 735 for (int i = 0; i < count; i++) 736 { 737 final AbstractFileObject file = (AbstractFileObject) files.get(i); 738 740 if (file.getType() == FileType.FOLDER && file.getChildren().length != 0) 742 { 743 continue; 745 } 746 747 boolean deleted = file.deleteSelf(); 749 if (deleted) 750 { 751 nuofDeleted++; 752 } 753 } 754 755 return nuofDeleted; 756 } 757 758 761 public void createFile() throws FileSystemException 762 { 763 synchronized (this) 764 { 765 try 766 { 767 getOutputStream().close(); 768 endOutput(); 769 } 770 catch (final RuntimeException re) 771 { 772 throw re; 773 } 774 catch (final Exception e) 775 { 776 throw new FileSystemException("vfs.provider/create-file.error", name, e); 777 } 778 } 779 } 780 781 785 public void createFolder() throws FileSystemException 786 { 787 synchronized (this) 788 { 789 if (getType() == FileType.FOLDER) 790 { 791 return; 793 } 794 if (getType() != FileType.IMAGINARY) 795 { 796 throw new FileSystemException("vfs.provider/create-folder-mismatched-type.error", name); 797 } 798 if (!isWriteable()) 799 { 800 throw new FileSystemException("vfs.provider/create-folder-read-only.error", name); 801 } 802 803 final FileObject parent = getParent(); 805 if (parent != null) 806 { 807 parent.createFolder(); 808 } 809 810 try 811 { 812 doCreateFolder(); 814 815 handleCreate(FileType.FOLDER); 817 } 818 catch (final RuntimeException re) 819 { 820 throw re; 821 } 822 catch (final Exception exc) 823 { 824 throw new FileSystemException("vfs.provider/create-folder.error", name, exc); 825 } 826 } 827 } 828 829 832 public void copyFrom(final FileObject file, final FileSelector selector) 833 throws FileSystemException 834 { 835 if (!file.exists()) 836 { 837 throw new FileSystemException("vfs.provider/copy-missing-file.error", file); 838 } 839 if (!isWriteable()) 840 { 841 throw new FileSystemException("vfs.provider/copy-read-only.error", new Object []{file.getType(), file.getName(), this}, null); 842 } 843 844 final ArrayList files = new ArrayList (); 846 file.findFiles(selector, false, files); 847 848 final int count = files.size(); 850 for (int i = 0; i < count; i++) 851 { 852 final FileObject srcFile = (FileObject) files.get(i); 853 854 final String relPath = file.getName().getRelativeName(srcFile.getName()); 856 final FileObject destFile = resolveFile(relPath, NameScope.DESCENDENT_OR_SELF); 857 858 if (destFile.exists() && destFile.getType() != srcFile.getType()) 860 { 861 destFile.delete(Selectors.SELECT_ALL); 865 } 866 867 try 869 { 870 if (srcFile.getType().hasContent()) 871 { 872 FileUtil.copyContent(srcFile, destFile); 873 } 874 else if (srcFile.getType().hasChildren()) 875 { 876 destFile.createFolder(); 877 } 878 } 879 catch (final IOException e) 880 { 881 throw new FileSystemException("vfs.provider/copy-file.error", new Object []{srcFile, destFile}, e); 882 } 883 } 884 } 885 886 889 public void moveTo(FileObject destFile) throws FileSystemException 890 { 891 if (!isWriteable()) 892 { 893 throw new FileSystemException("vfs.provider/rename-read-only.error", getName()); 894 } 895 if (destFile.exists()) 896 { 897 destFile.delete(Selectors.SELECT_ALL); 898 } 900 901 if (canRenameTo(destFile)) 902 { 903 try 905 { 906 doRename(destFile); 907 908 ((AbstractFileObject) destFile).handleCreate(getType()); 909 910 destFile.close(); 912 handleDelete(); } 914 catch (final RuntimeException re) 915 { 916 throw re; 917 } 918 catch (final Exception exc) 919 { 920 throw new FileSystemException("vfs.provider/rename.error", new Object [] 921 { 922 getName(), 923 destFile.getName() 924 }, exc); 925 } 926 } 927 else 928 { 929 931 destFile.copyFrom(this, Selectors.SELECT_SELF); 932 933 if (((destFile.getType() == FileType.FILE && destFile.getFileSystem().hasCapability(Capability.SET_LAST_MODIFIED_FILE)) || 934 (destFile.getType() == FileType.FOLDER && destFile.getFileSystem().hasCapability(Capability.SET_LAST_MODIFIED_FOLDER))) && 935 getFileSystem().hasCapability(Capability.GET_LAST_MODIFIED)) 936 { 937 destFile.getContent().setLastModifiedTime(this.getContent().getLastModifiedTime()); 938 } 939 940 deleteSelf(); 941 } 942 943 } 944 945 952 public boolean canRenameTo(FileObject newfile) 953 { 954 if (getFileSystem() == newfile.getFileSystem()) 955 { 956 return true; 957 } 958 959 return false; 960 } 961 962 968 public FileObject[] findFiles(final FileSelector selector) throws FileSystemException 969 { 970 if (!exists()) 971 { 972 return null; 973 } 974 975 final ArrayList list = new ArrayList (); 976 findFiles(selector, true, list); 977 return (FileObject[]) list.toArray(new FileObject[list.size()]); 978 } 979 980 983 public FileContent getContent() throws FileSystemException 984 { 985 attach(); 986 if (content == null) 987 { 988 content = new DefaultFileContent(this, getFileContentInfoFactory()); 989 } 990 return content; 991 } 992 993 996 public void close() throws FileSystemException 997 { 998 FileSystemException exc = null; 999 1000 if (content != null) 1002 { 1003 try 1004 { 1005 content.close(); 1006 } 1007 catch (FileSystemException e) 1008 { 1009 exc = e; 1010 } 1011 } 1012 1013 try 1015 { 1016 detach(); 1017 } 1018 catch (final Exception e) 1019 { 1020 exc = new FileSystemException("vfs.provider/close.error", name, e); 1021 } 1022 1023 if (exc != null) 1024 { 1025 throw exc; 1026 } 1027 } 1028 1029 1032 public InputStream getInputStream() throws FileSystemException 1033 { 1034 attach(); 1035 if (!type.hasContent()) 1036 { 1037 throw new FileSystemException("vfs.provider/read-not-file.error", name); 1038 } 1039 if (!isReadable()) 1040 { 1041 throw new FileSystemException("vfs.provider/read-not-readable.error", name); 1042 } 1043 1044 try 1046 { 1047 return doGetInputStream(); 1048 } 1049 catch (final Exception exc) 1050 { 1051 throw new FileSystemException("vfs.provider/read.error", name, exc); 1052 } 1053 } 1054 1055 1059 public RandomAccessContent getRandomAccessContent(final RandomAccessMode mode) throws FileSystemException 1060 { 1061 attach(); 1062 if (!type.hasContent()) 1063 { 1064 throw new FileSystemException("vfs.provider/read-not-file.error", name); 1065 } 1066 1067 if (mode.requestRead()) 1068 { 1069 if (!getFileSystem().hasCapability(Capability.RANDOM_ACCESS_READ)) 1070 { 1071 throw new FileSystemException("vfs.provider/random-access-read-not-supported.error"); 1072 } 1073 if (!isReadable()) 1074 { 1075 throw new FileSystemException("vfs.provider/read-not-readable.error", name); 1076 } 1077 } 1078 1079 if (mode.requestWrite()) 1080 { 1081 if (!getFileSystem().hasCapability(Capability.RANDOM_ACCESS_WRITE)) 1082 { 1083 throw new FileSystemException("vfs.provider/random-access-write-not-supported.error"); 1084 } 1085 if (!isWriteable()) 1086 { 1087 throw new FileSystemException("vfs.provider/write-read-only.error", name); 1088 } 1089 } 1090 1091 try 1093 { 1094 return doGetRandomAccessContent(mode); 1095 } 1096 catch (final Exception exc) 1097 { 1098 throw new FileSystemException("vfs.provider/random-access.error", name, exc); 1099 } 1100 } 1101 1102 1107 public OutputStream getOutputStream() throws FileSystemException 1108 { 1109 return getOutputStream(false); 1110 } 1111 1112 1120 public OutputStream getOutputStream(boolean bAppend) throws FileSystemException 1121 { 1122 if (getType() != FileType.IMAGINARY && !getType().hasContent()) 1123 { 1124 throw new FileSystemException("vfs.provider/write-not-file.error", name); 1125 } 1126 if (!isWriteable()) 1127 { 1128 throw new FileSystemException("vfs.provider/write-read-only.error", name); 1129 } 1130 if (bAppend && !getFileSystem().hasCapability(Capability.APPEND_CONTENT)) 1131 { 1132 throw new FileSystemException("vfs.provider/write-append-not-supported.error", name); 1133 } 1134 1135 if (getType() == FileType.IMAGINARY) 1136 { 1137 FileObject parent = getParent(); 1139 if (parent != null) 1140 { 1141 parent.createFolder(); 1142 } 1143 } 1144 1145 try 1147 { 1148 return doGetOutputStream(bAppend); 1149 } 1150 catch (RuntimeException re) 1151 { 1152 throw re; 1153 } 1154 catch (Exception exc) 1155 { 1156 throw new FileSystemException("vfs.provider/write.error", new Object []{name}, exc); 1157 } 1158 } 1159 1160 1164 private void detach() throws Exception 1165 { 1166 synchronized (this) 1167 { 1168 if (attached) 1169 { 1170 try 1171 { 1172 doDetach(); 1173 } 1174 finally 1175 { 1176 attached = false; 1177 setFileType(null); 1178 parent = null; 1179 1180 fs.fileDetached(this); 1181 1182 removeChildrenCache(); 1183 } 1185 } 1186 } 1187 } 1188 1189 private void removeChildrenCache() 1190 { 1191 1202 children = null; 1203 } 1204 1205 1208 private void attach() throws FileSystemException 1209 { 1210 synchronized (this) 1211 { 1212 if (attached) 1213 { 1214 return; 1215 } 1216 1217 try 1218 { 1219 doAttach(); 1221 attached = true; 1222 if (type == null) 1224 { 1225 setFileType(doGetType()); 1226 } 1227 if (type == null) 1228 { 1229 setFileType(FileType.IMAGINARY); 1230 } 1231 } 1232 catch (Exception exc) 1233 { 1234 throw new FileSystemException("vfs.provider/get-type.error", new Object []{name}, exc); 1235 } 1236 1237 fs.fileAttached(this); 1238 } 1239 } 1240 1241 1244 protected void endOutput() throws Exception 1245 { 1246 if (getType() == FileType.IMAGINARY) 1247 { 1248 handleCreate(FileType.FILE); 1250 } 1251 else 1252 { 1253 onChange(); 1255 } 1256 } 1257 1258 1262 protected void handleCreate(final FileType newType) throws Exception 1263 { 1264 synchronized (this) 1265 { 1266 if (attached) 1267 { 1268 injectType(newType); 1270 1271 removeChildrenCache(); 1272 children = EMPTY_FILE_ARRAY; 1273 1274 onChange(); 1276 } 1277 1278 notifyParent(this.getName(), newType); 1280 1281 fs.fireFileCreated(this); 1283 } 1284 } 1285 1286 1290 protected void handleDelete() throws Exception 1291 { 1292 synchronized (this) 1293 { 1294 if (attached) 1295 { 1296 injectType(FileType.IMAGINARY); 1298 removeChildrenCache(); 1299 1301 onChange(); 1303 } 1304 1305 notifyParent(this.getName(), FileType.IMAGINARY); 1307 1308 fs.fireFileDeleted(this); 1310 } 1311 } 1312 1313 1317 protected void handleChanged() throws Exception 1318 { 1319 fs.fireFileChanged(this); 1321 } 1322 1323 1328 protected void childrenChanged() throws Exception 1329 { 1330 childrenChanged(null, null); 1331 } 1332 1333 1336 protected void childrenChanged(FileName childName, FileType newType) throws Exception 1337 { 1338 1340 if (children != null) 1341 { 1342 if (childName != null && newType != null) 1343 { 1344 ArrayList list = new ArrayList (Arrays.asList(children)); 1346 if (newType.equals(FileType.IMAGINARY)) 1347 { 1348 list.remove(childName); 1349 } 1350 else 1351 { 1352 list.add(childName); 1353 } 1354 children = new FileName[list.size()]; 1355 list.toArray(children); 1356 } 1357 } 1358 1359 onChildrenChanged(childName, newType); 1361 } 1362 1363 1367 private void notifyParent(FileName childName, FileType newType) throws Exception 1368 { 1369 if (parent == null) 1370 { 1371 FileName parentName = name.getParent(); 1372 if (parentName != null) 1373 { 1374 parent = (AbstractFileObject) fs.getFileFromCache(parentName); 1376 } 1377 } 1378 1379 if (parent != null) 1380 { 1381 parent.childrenChanged(childName, newType); 1382 } 1383 } 1384 1385 1389 public void findFiles(final FileSelector selector, 1390 final boolean depthwise, 1391 final List selected) throws FileSystemException 1392 { 1393 try 1394 { 1395 if (exists()) 1396 { 1397 final DefaultFileSelectorInfo info = new DefaultFileSelectorInfo(); 1399 info.setBaseFolder(this); 1400 info.setDepth(0); 1401 info.setFile(this); 1402 traverse(info, selector, depthwise, selected); 1403 } 1404 } 1405 catch (final Exception e) 1406 { 1407 throw new FileSystemException("vfs.provider/find-files.error", name, e); 1408 } 1409 } 1410 1411 1414 private static void traverse(final DefaultFileSelectorInfo fileInfo, 1415 final FileSelector selector, 1416 final boolean depthwise, 1417 final List selected) 1418 throws Exception 1419 { 1420 final FileObject file = fileInfo.getFile(); 1422 final int index = selected.size(); 1423 1424 if (file.getType().hasChildren() && selector.traverseDescendents(fileInfo)) 1426 { 1427 final int curDepth = fileInfo.getDepth(); 1428 fileInfo.setDepth(curDepth + 1); 1429 1430 final FileObject[] children = file.getChildren(); 1432 for (int i = 0; i < children.length; i++) 1433 { 1434 final FileObject child = children[i]; 1435 fileInfo.setFile(child); 1436 traverse(fileInfo, selector, depthwise, selected); 1437 } 1438 1439 fileInfo.setFile(file); 1440 fileInfo.setDepth(curDepth); 1441 } 1442 1443 if (selector.includeFile(fileInfo)) 1445 { 1446 if (depthwise) 1447 { 1448 selected.add(file); 1450 } 1451 else 1452 { 1453 selected.add(index, file); 1455 } 1456 } 1457 } 1458 1459 1464 public boolean isContentOpen() 1465 { 1466 if (content == null) 1467 { 1468 return false; 1469 } 1470 1471 return content.isOpen(); 1472 } 1473 1474 1479 public boolean isAttached() 1480 { 1481 return attached; 1482 } 1483 1484 1487 protected FileContentInfoFactory getFileContentInfoFactory() 1488 { 1489 return getFileSystem().getFileSystemManager().getFileContentInfoFactory(); 1490 } 1491 1492 protected void injectType(FileType fileType) 1493 { 1494 setFileType(fileType); 1495 } 1496 1497 private void setFileType(FileType type) 1498 { 1499 if (type != null && type != FileType.IMAGINARY) 1500 { 1501 try 1502 { 1503 name.setType(type); 1504 } 1505 catch (FileSystemException e) 1506 { 1507 throw new RuntimeException (e.getMessage()); 1508 } 1509 } 1510 this.type = type; 1511 } 1512 1513 1520 public void holdObject(Object strongRef) 1521 { 1522 if (objects == null) 1523 { 1524 objects = new ArrayList (5); 1525 } 1526 objects.add(strongRef); 1527 } 1528 1529 1532 protected void notifyAllStreamsClosed() 1533 { 1534 } 1535} 1536 | Popular Tags |