diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-02-14 09:12:24 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-02-14 09:12:24 +0100 |
commit | 60dda2492f3af037bee478ac49e12a8c4366493c (patch) | |
tree | 94ee5e2a25e3472f755b6c8c5891c3c7f764db5a | |
parent | 7cf621d59f61bf2a654ca05b053c9132209050f5 (diff) | |
download | sonarqube-60dda2492f3af037bee478ac49e12a8c4366493c.tar.gz sonarqube-60dda2492f3af037bee478ac49e12a8c4366493c.zip |
Fix backward-compatibility of DefaultProjectFileSystem
-rw-r--r-- | sonar-deprecated/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/sonar-deprecated/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java b/sonar-deprecated/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java new file mode 100644 index 00000000000..e323abc2df4 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java @@ -0,0 +1,73 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.resources; + +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.lang.StringUtils; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @deprecated replaced by {@link org.sonar.api.scan.filesystem.ModuleFileSystem} and {@link org.sonar.api.scan.filesystem.PathResolver} in 3.5 + */ +@Deprecated +public class DefaultProjectFileSystem { + /** + * getRelativePath("c:/foo/src/my/package/Hello.java", "c:/foo/src") is "my/package/Hello.java" + * + * @return null if file is not in dir (including recursive subdirectories) + */ + public static String getRelativePath(java.io.File file, java.io.File dir) { + return getRelativePath(file, Arrays.asList(dir)); + } + + /** + * getRelativePath("c:/foo/src/my/package/Hello.java", ["c:/bar", "c:/foo/src"]) is "my/package/Hello.java". + * <p> + * Relative path is composed of slashes. Windows backslaches are replaced by / + * </p> + * + * @return null if file is not in dir (including recursive subdirectories) + */ + public static String getRelativePath(java.io.File file, List<java.io.File> dirs) { + List<String> stack = new ArrayList<String>(); + String path = FilenameUtils.normalize(file.getAbsolutePath()); + java.io.File cursor = new java.io.File(path); + while (cursor != null) { + if (containsFile(dirs, cursor)) { + return StringUtils.join(stack, "/"); + } + stack.add(0, cursor.getName()); + cursor = cursor.getParentFile(); + } + return null; + } + + private static boolean containsFile(List<java.io.File> dirs, java.io.File cursor) { + for (java.io.File dir : dirs) { + if (FilenameUtils.equalsNormalizedOnSystem(dir.getAbsolutePath(), cursor.getAbsolutePath())) { + return true; + } + } + return false; + } +} |