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.component.file; 018 019 import java.io.File; 020 import java.io.FileFilter; 021 022 import org.apache.camel.util.AntPathMatcher; 023 import org.slf4j.Logger; 024 import org.slf4j.LoggerFactory; 025 026 /** 027 * File filter using {@link AntPathMatcher}. 028 * <p/> 029 * Exclude take precedence over includes. If a file match both exclude and include it will be regarded as excluded. 030 */ 031 public class AntPathMatcherFileFilter implements FileFilter { 032 private static final transient Logger LOG = LoggerFactory.getLogger(AntPathMatcherFileFilter.class); 033 034 private AntPathMatcher matcher = new AntPathMatcher(); 035 private String[] excludes; 036 private String[] includes; 037 038 public boolean accept(File pathname) { 039 return acceptPathName(pathname.getPath()); 040 } 041 042 /** 043 * Accepts the given file by the path name 044 * 045 * @param path the path 046 * @return <tt>true</tt> if accepted, <tt>false</tt> if not 047 */ 048 public boolean acceptPathName(String path) { 049 // must use single / as path separators 050 path = path.replace(File.separatorChar, '/'); 051 052 LOG.trace("Filtering file: {}", path); 053 054 // excludes take precedence 055 if (excludes != null) { 056 for (String exclude : excludes) { 057 if (matcher.match(exclude, path)) { 058 // something to exclude so we cant accept it 059 LOG.trace("File is excluded: {}", path); 060 return false; 061 } 062 } 063 } 064 065 if (includes != null) { 066 for (String include : includes) { 067 if (matcher.match(include, path)) { 068 // something to include so we accept it 069 LOG.trace("File is included: {}", path); 070 return true; 071 } 072 } 073 } 074 075 if (excludes != null && includes == null) { 076 // if the user specified excludes but no includes, presumably we should include by default 077 return true; 078 } 079 080 // nothing to include so we can't accept it 081 return false; 082 } 083 084 public String[] getExcludes() { 085 return excludes; 086 } 087 088 public void setExcludes(String[] excludes) { 089 this.excludes = excludes; 090 } 091 092 public String[] getIncludes() { 093 return includes; 094 } 095 096 public void setIncludes(String[] includes) { 097 this.includes = includes; 098 } 099 100 /** 101 * Sets excludes using a single string where each element can be separated with comma 102 */ 103 public void setExcludes(String excludes) { 104 setExcludes(excludes.split(",")); 105 } 106 107 /** 108 * Sets includes using a single string where each element can be separated with comma 109 */ 110 public void setIncludes(String includes) { 111 setIncludes(includes.split(",")); 112 } 113 114 }