You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExclusionFilters.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2016 SonarSource SA
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.scan.filesystem;
  21. import org.apache.commons.lang.ArrayUtils;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import org.sonar.api.batch.ScannerSide;
  25. import org.sonar.api.batch.fs.InputFile;
  26. import org.sonar.api.batch.fs.internal.PathPattern;
  27. import org.sonar.api.scan.filesystem.FileExclusions;
  28. @ScannerSide
  29. public class ExclusionFilters {
  30. private static final Logger LOG = LoggerFactory.getLogger(ExclusionFilters.class);
  31. private final FileExclusions exclusionSettings;
  32. private PathPattern[] mainInclusions;
  33. private PathPattern[] mainExclusions;
  34. private PathPattern[] testInclusions;
  35. private PathPattern[] testExclusions;
  36. public ExclusionFilters(FileExclusions exclusions) {
  37. this.exclusionSettings = exclusions;
  38. }
  39. public void prepare() {
  40. mainInclusions = prepareMainInclusions();
  41. mainExclusions = prepareMainExclusions();
  42. testInclusions = prepareTestInclusions();
  43. testExclusions = prepareTestExclusions();
  44. log("Included sources: ", mainInclusions);
  45. log("Excluded sources: ", mainExclusions);
  46. log("Included tests: ", testInclusions);
  47. log("Excluded tests: ", testExclusions);
  48. }
  49. public boolean hasPattern() {
  50. return mainInclusions.length > 0 || mainExclusions.length > 0 || testInclusions.length > 0 || testExclusions.length > 0;
  51. }
  52. private static void log(String title, PathPattern[] patterns) {
  53. if (patterns.length > 0) {
  54. LOG.info(title);
  55. for (PathPattern pattern : patterns) {
  56. LOG.info(" {}", pattern);
  57. }
  58. }
  59. }
  60. public boolean accept(InputFile inputFile, InputFile.Type type) {
  61. PathPattern[] inclusionPatterns;
  62. PathPattern[] exclusionPatterns;
  63. if (InputFile.Type.MAIN == type) {
  64. inclusionPatterns = mainInclusions;
  65. exclusionPatterns = mainExclusions;
  66. } else if (InputFile.Type.TEST == type) {
  67. inclusionPatterns = testInclusions;
  68. exclusionPatterns = testExclusions;
  69. } else {
  70. throw new IllegalArgumentException("Unknown file type: " + type);
  71. }
  72. if (inclusionPatterns.length > 0) {
  73. boolean matchInclusion = false;
  74. for (PathPattern pattern : inclusionPatterns) {
  75. matchInclusion |= pattern.match(inputFile);
  76. }
  77. if (!matchInclusion) {
  78. return false;
  79. }
  80. }
  81. if (exclusionPatterns.length > 0) {
  82. for (PathPattern pattern : exclusionPatterns) {
  83. if (pattern.match(inputFile)) {
  84. return false;
  85. }
  86. }
  87. }
  88. return true;
  89. }
  90. PathPattern[] prepareMainInclusions() {
  91. if (exclusionSettings.sourceInclusions().length > 0) {
  92. // User defined params
  93. return PathPattern.create(exclusionSettings.sourceInclusions());
  94. }
  95. return new PathPattern[0];
  96. }
  97. PathPattern[] prepareTestInclusions() {
  98. return PathPattern.create(computeTestInclusions());
  99. }
  100. private String[] computeTestInclusions() {
  101. if (exclusionSettings.testInclusions().length > 0) {
  102. // User defined params
  103. return exclusionSettings.testInclusions();
  104. }
  105. return ArrayUtils.EMPTY_STRING_ARRAY;
  106. }
  107. PathPattern[] prepareMainExclusions() {
  108. String[] patterns = (String[]) ArrayUtils.addAll(
  109. exclusionSettings.sourceExclusions(), computeTestInclusions());
  110. return PathPattern.create(patterns);
  111. }
  112. PathPattern[] prepareTestExclusions() {
  113. return PathPattern.create(exclusionSettings.testExclusions());
  114. }
  115. }