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

http://github.com/JetBrains/intellij-community · Java · 112 lines · 69 code · 24 blank · 19 comment · 6 complexity · 70b616c259e8d242325c97b542f4c2aa 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 com.intellij.openapi.util.text.StringUtil;
  15. import org.jetbrains.annotations.NonNls;
  16. import org.netbeans.lib.cvsclient.util.BugLog;
  17. import java.io.File;
  18. /**
  19. * @author Thomas Singer
  20. */
  21. public final class FileSystem
  22. implements IFileSystem {
  23. // Fields =================================================================
  24. private final File rootDirectory;
  25. private final String canonicalRootDirectoryName;
  26. @NonNls private static final String OS_NAME_PARAMETER = "os.name";
  27. @NonNls private static final String WINDOWS = "Windows";
  28. // Setup ==================================================================
  29. public FileSystem(File rootDirectory) {
  30. BugLog.getInstance().assertNotNull(rootDirectory);
  31. this.rootDirectory = rootDirectory;
  32. this.canonicalRootDirectoryName = getCanonicalFileName(rootDirectory);
  33. }
  34. // Implemented ============================================================
  35. @Override
  36. public File getRootDirectory() {
  37. return rootDirectory;
  38. }
  39. @Override
  40. public File getFile(String relativeFileName) {
  41. BugLog.getInstance().assertNotNull(relativeFileName);
  42. return new File(rootDirectory, relativeFileName);
  43. }
  44. @Override
  45. public File getFile(AbstractFileObject fileObject) {
  46. BugLog.getInstance().assertNotNull(fileObject);
  47. return new File(rootDirectory, fileObject.getPath().substring(1));
  48. }
  49. @Override
  50. public FileObject getFileObject(File file) throws OutOfFileSystemException {
  51. BugLog.getInstance().assertNotNull(file);
  52. if (file.isDirectory()) {
  53. throw new IllegalArgumentException(file + " isn't a file");
  54. }
  55. return FileObject.createInstance(getPath(file));
  56. }
  57. @Override
  58. public DirectoryObject getDirectoryObject(File directory) throws OutOfFileSystemException {
  59. BugLog.getInstance().assertNotNull(directory);
  60. if (directory.isFile()) {
  61. throw new IllegalArgumentException(directory + " isn't a directory");
  62. }
  63. return DirectoryObject.createInstance(getPath(directory));
  64. }
  65. // Utils ==================================================================
  66. private String getPath(File file) throws OutOfFileSystemException {
  67. final String canonicalFilePath = getCanonicalFileName(file);
  68. if (!canonicalFilePath.startsWith(canonicalRootDirectoryName)) {
  69. throw new OutOfFileSystemException(canonicalFilePath, canonicalRootDirectoryName);
  70. }
  71. final String filePath = file.getAbsolutePath();
  72. int beginIndex = canonicalRootDirectoryName.length();
  73. if (filePath.length() > beginIndex) {
  74. beginIndex++;
  75. }
  76. return '/' + filePath.substring(beginIndex).replace('\\', '/');
  77. }
  78. private static String getCanonicalFileName(File file) {
  79. String canonicalFileName = file.getAbsolutePath();
  80. if (canonicalFileName.endsWith(File.separator)) {
  81. canonicalFileName = canonicalFileName.substring(0, canonicalFileName.length() - 1);
  82. }
  83. if (System.getProperty(OS_NAME_PARAMETER).startsWith(WINDOWS)) {
  84. return StringUtil.toLowerCase(canonicalFileName);
  85. }
  86. return canonicalFileName;
  87. }
  88. }