/plugins/cvs/smartcvs-src/org/netbeans/lib/cvsclient/file/FileSystem.java

https://github.com/jexp/idea2 · Java · 106 lines · 63 code · 24 blank · 19 comment · 6 complexity · 1ba9bc37891c9923bebf1b566f92be3f MD5 · raw file

  1. /*
  2. * Sun Public License Notice
  3. *
  4. * The contents of this file are subject to the Sun Public License
  5. * Version 1.0 (the "License"). You may not use this file except in
  6. * compliance with the License. A copy of the License is available at
  7. * http://www.sun.com/
  8. *
  9. * The Original Code is NetBeans. The Initial Developer of the Original
  10. * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
  11. * Microsystems, Inc. All Rights Reserved.
  12. */
  13. package org.netbeans.lib.cvsclient.file;
  14. import org.netbeans.lib.cvsclient.util.BugLog;
  15. import org.jetbrains.annotations.NonNls;
  16. import java.io.File;
  17. /**
  18. * @author Thomas Singer
  19. */
  20. public final class FileSystem
  21. implements IFileSystem {
  22. // Fields =================================================================
  23. private final File rootDirectory;
  24. private final String canonicalRootDirectoryName;
  25. @NonNls private static final String OS_NAME_PARAMETER = "os.name";
  26. @NonNls private static final String WINDOWS = "Windows";
  27. // Setup ==================================================================
  28. public FileSystem(File rootDirectory) {
  29. BugLog.getInstance().assertNotNull(rootDirectory);
  30. this.rootDirectory = rootDirectory;
  31. this.canonicalRootDirectoryName = getCanonicalFileName(rootDirectory);
  32. }
  33. // Implemented ============================================================
  34. public File getRootDirectory() {
  35. return rootDirectory;
  36. }
  37. public File getFile(String relativeFileName) {
  38. BugLog.getInstance().assertNotNull(relativeFileName);
  39. return new File(rootDirectory, relativeFileName);
  40. }
  41. public File getFile(AbstractFileObject fileObject) {
  42. BugLog.getInstance().assertNotNull(fileObject);
  43. return new File(rootDirectory, fileObject.getPath().substring(1));
  44. }
  45. public FileObject getFileObject(File file) throws OutOfFileSystemException {
  46. BugLog.getInstance().assertNotNull(file);
  47. if (file.isDirectory()) {
  48. throw new IllegalArgumentException(file + " isn't a file");
  49. }
  50. return FileObject.createInstance(getPath(file));
  51. }
  52. public DirectoryObject getDirectoryObject(File directory) throws OutOfFileSystemException {
  53. BugLog.getInstance().assertNotNull(directory);
  54. if (directory.isFile()) {
  55. throw new IllegalArgumentException(directory + " isn't a directory");
  56. }
  57. return DirectoryObject.createInstance(getPath(directory));
  58. }
  59. // Utils ==================================================================
  60. private String getPath(File file) throws OutOfFileSystemException {
  61. final String canonicalFilePath = getCanonicalFileName(file);
  62. if (!canonicalFilePath.startsWith(canonicalRootDirectoryName)) {
  63. throw new OutOfFileSystemException(canonicalFilePath, canonicalRootDirectoryName);
  64. }
  65. final String filePath = file.getAbsolutePath();
  66. int beginIndex = canonicalRootDirectoryName.length();
  67. if (filePath.length() > beginIndex) {
  68. beginIndex++;
  69. }
  70. return '/' + filePath.substring(beginIndex).replace('\\', '/');
  71. }
  72. private static String getCanonicalFileName(File file) {
  73. String canonicalFileName = file.getAbsolutePath();
  74. if (canonicalFileName.endsWith(File.separator)) {
  75. canonicalFileName = canonicalFileName.substring(0, canonicalFileName.length() - 1);
  76. }
  77. if (System.getProperty(OS_NAME_PARAMETER).startsWith(WINDOWS)) {
  78. return canonicalFileName.toLowerCase();
  79. }
  80. return canonicalFileName;
  81. }
  82. }