001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 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 */
020 package org.sonar.batch.scan.filesystem;
021
022 import org.sonar.api.batch.fs.InputFile;
023 import org.sonar.api.batch.fs.InputFileFilter;
024 import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile;
025 import org.sonar.api.scan.filesystem.FileSystemFilter;
026 import org.sonar.api.scan.filesystem.FileType;
027 import org.sonar.api.scan.filesystem.ModuleFileSystem;
028
029 import java.io.File;
030
031 public class DeprecatedFileFilters implements InputFileFilter {
032 private final FileSystemFilter[] filters;
033
034 public DeprecatedFileFilters(FileSystemFilter[] filters) {
035 this.filters = filters;
036 }
037
038 public DeprecatedFileFilters() {
039 this(new FileSystemFilter[0]);
040 }
041
042 @Override
043 public boolean accept(InputFile inputFile) {
044 if (filters.length > 0) {
045 DeprecatedContext context = new DeprecatedContext(inputFile);
046 for (FileSystemFilter filter : filters) {
047 if (!filter.accept(inputFile.file(), context)) {
048 return false;
049 }
050 }
051 }
052 return true;
053 }
054
055 static class DeprecatedContext implements FileSystemFilter.Context {
056 private final InputFile inputFile;
057
058 DeprecatedContext(InputFile inputFile) {
059 this.inputFile = inputFile;
060 }
061
062 @Override
063 public ModuleFileSystem fileSystem() {
064 throw new UnsupportedOperationException("Not supported since 4.0");
065 }
066
067 @Override
068 public FileType type() {
069 String type = inputFile.type().name();
070 return FileType.valueOf(type);
071 }
072
073 @Override
074 public File relativeDir() {
075 return new File(((DeprecatedDefaultInputFile)inputFile).sourceDirAbsolutePath());
076 }
077
078 @Override
079 public String relativePath() {
080 return ((DeprecatedDefaultInputFile)inputFile).pathRelativeToSourceDir();
081 }
082
083 @Override
084 public String canonicalPath() {
085 return inputFile.absolutePath();
086 }
087 }
088 }