001/* 002 * SonarQube, open source software quality management tool. 003 * Copyright (C) 2008-2013 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * SonarQube is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * SonarQube is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 */ 020package org.sonar.api.resources; 021 022import org.apache.commons.io.FilenameUtils; 023import org.apache.commons.lang.StringUtils; 024 025import java.util.ArrayList; 026import java.util.Arrays; 027import java.util.List; 028 029/** 030 * @deprecated replaced by {@link org.sonar.api.scan.filesystem.ModuleFileSystem} and {@link org.sonar.api.scan.filesystem.PathResolver} in 3.5 031 */ 032@Deprecated 033public class DefaultProjectFileSystem { 034 /** 035 * getRelativePath("c:/foo/src/my/package/Hello.java", "c:/foo/src") is "my/package/Hello.java" 036 * 037 * @return null if file is not in dir (including recursive subdirectories) 038 */ 039 public static String getRelativePath(java.io.File file, java.io.File dir) { 040 return getRelativePath(file, Arrays.asList(dir)); 041 } 042 043 /** 044 * getRelativePath("c:/foo/src/my/package/Hello.java", ["c:/bar", "c:/foo/src"]) is "my/package/Hello.java". 045 * <p> 046 * Relative path is composed of slashes. Windows backslaches are replaced by / 047 * </p> 048 * 049 * @return null if file is not in dir (including recursive subdirectories) 050 */ 051 public static String getRelativePath(java.io.File file, List<java.io.File> dirs) { 052 List<String> stack = new ArrayList<String>(); 053 String path = FilenameUtils.normalize(file.getAbsolutePath()); 054 java.io.File cursor = new java.io.File(path); 055 while (cursor != null) { 056 if (containsFile(dirs, cursor)) { 057 return StringUtils.join(stack, "/"); 058 } 059 stack.add(0, cursor.getName()); 060 cursor = cursor.getParentFile(); 061 } 062 return null; 063 } 064 065 private static boolean containsFile(List<java.io.File> dirs, java.io.File cursor) { 066 for (java.io.File dir : dirs) { 067 if (FilenameUtils.equalsNormalizedOnSystem(dir.getAbsolutePath(), cursor.getAbsolutePath())) { 068 return true; 069 } 070 } 071 return false; 072 } 073}