001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.spring.util;
018
019 import java.io.File;
020 import java.io.FileFilter;
021
022 import org.slf4j.Logger;
023 import org.slf4j.LoggerFactory;
024 import org.springframework.util.AntPathMatcher;
025 import org.springframework.util.StringUtils;
026
027 /**
028 * File filter using Spring's {@link AntPathMatcher}.
029 * <p/>
030 * Exclude take precedence over includes. If a file match both exclude and include it will be regarded as excluded.
031 */
032 public class SpringAntPathMatcherFileFilter implements FileFilter {
033 private static final transient Logger LOG = LoggerFactory.getLogger(SpringAntPathMatcherFileFilter.class);
034
035 private AntPathMatcher matcher = new AntPathMatcher();
036 private String[] excludes;
037 private String[] includes;
038
039 public boolean accept(File pathname) {
040 return acceptPathName(pathname.getPath());
041 }
042
043 /**
044 * Accepts the given file by the path name
045 *
046 * @param path the path
047 * @return <tt>true</tt> if accepted, <tt>false</tt> if not
048 */
049 public boolean acceptPathName(String path) {
050 // must use single / as path separators
051 path = StringUtils.replace(path, File.separator, "/");
052
053 LOG.trace("Filtering file: {}", path);
054
055 // excludes take precedence
056 if (excludes != null) {
057 for (String exclude : excludes) {
058 if (matcher.match(exclude, path)) {
059 // something to exclude so we cant accept it
060 LOG.trace("File is excluded: {}", path);
061 return false;
062 }
063 }
064 }
065
066 if (includes != null) {
067 for (String include : includes) {
068 if (matcher.match(include, path)) {
069 // something to include so we accept it
070 LOG.trace("File is included: {}", path);
071 return true;
072 }
073 }
074 }
075
076 // nothing to include so we cant accept it
077 return false;
078 }
079
080 public String[] getExcludes() {
081 return excludes;
082 }
083
084 public void setExcludes(String[] excludes) {
085 this.excludes = excludes;
086 }
087
088 public String[] getIncludes() {
089 return includes;
090 }
091
092 public void setIncludes(String[] includes) {
093 this.includes = includes;
094 }
095
096 /**
097 * Sets excludes using a single string where each element can be separated with comma
098 */
099 public void setExcludes(String excludes) {
100 setExcludes(excludes.split(","));
101 }
102
103 /**
104 * Sets includes using a single string where each element can be separated with comma
105 */
106 public void setIncludes(String includes) {
107 setIncludes(includes.split(","));
108 }
109
110 }