001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar 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 * Sonar 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
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.batch;
021
022 import org.slf4j.Logger;
023 import org.slf4j.LoggerFactory;
024 import org.sonar.api.batch.ResourceFilter;
025 import org.sonar.api.resources.Resource;
026
027 /**
028 * @since 1.12
029 */
030 public class ResourceFilters {
031
032 private static final Logger LOG = LoggerFactory.getLogger(ResourceFilters.class);
033
034 private ResourceFilter[] filters;
035
036 public ResourceFilters(ResourceFilter[] filters) {
037 this.filters = (filters==null ? new ResourceFilter[0] : filters);
038 }
039
040 public ResourceFilters() {
041 this(null);
042 }
043
044 public ResourceFilter[] getFilters() {
045 return filters;
046 }
047
048 /**
049 * Return true if the violation must be saved. If false then it is ignored.
050 */
051 public boolean isExcluded(Resource resource) {
052 boolean ignored = false;
053 int index = 0;
054 while (!ignored && index < filters.length) {
055 ResourceFilter filter = filters[index];
056 ignored = filter.isIgnored(resource);
057 if (ignored && LOG.isDebugEnabled()) {
058 LOG.debug("Resource {} is excluded by the filter {}", resource, filter);
059 }
060 index++;
061 }
062 return ignored;
063 }
064
065 }