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.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
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 Log LOG = LogFactory.getLog(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            if (LOG.isTraceEnabled()) {
054                LOG.trace("Filtering file: " + path);
055            }
056    
057            // excludes take precedence
058            if (excludes != null) {
059                for (String exclude : excludes) {
060                    if (matcher.match(exclude, path)) {
061                        // something to exclude so we cant accept it
062                        if (LOG.isTraceEnabled()) {
063                            LOG.trace("File is excluded: " + path);
064                        }
065                        return false;
066                    }
067                }
068            }
069    
070            if (includes != null) {
071                for (String include : includes) {
072                    if (matcher.match(include, path)) {
073                        // something to include so we accept it
074                        if (LOG.isTraceEnabled()) {
075                            LOG.trace("File is included: " + path);
076                        }
077                        return true;
078                    }
079                }
080            }
081    
082            // nothing to include so we cant accept it
083            return false;
084        }
085    
086        public String[] getExcludes() {
087            return excludes;
088        }
089    
090        public void setExcludes(String[] excludes) {
091            this.excludes = excludes;
092        }
093    
094        public String[] getIncludes() {
095            return includes;
096        }
097    
098        public void setIncludes(String[] includes) {
099            this.includes = includes;
100        }
101    
102        /**
103         * Sets excludes using a single string where each element can be separated with comma
104         */
105        public void setExcludes(String excludes) {
106            setExcludes(excludes.split(","));
107        }
108    
109        /**
110         * Sets includes using a single string where each element can be separated with comma
111         */
112        public void setIncludes(String includes) {
113            setIncludes(includes.split(","));
114        }
115    
116    }