001    /*
002     * Copyright 2010 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.vafer.jdeb.producers;
017    
018    import org.apache.tools.ant.types.selectors.SelectorUtils;
019    import org.apache.tools.tar.TarEntry;
020    import org.vafer.jdeb.DataProducer;
021    import org.vafer.jdeb.mapping.Mapper;
022    
023    /**
024     * Base Producer class providing including/excluding.
025     * 
026     * @author Torsten Curdt <tcurdt@vafer.org>
027     */
028    public abstract class AbstractDataProducer implements DataProducer {
029    
030        private final String[] includes;
031        private final String[] excludes;
032        private final Mapper[] mappers;
033        
034        
035        public AbstractDataProducer( final String[] pIncludes, final String[] pExcludes, final Mapper[] pMapper ) {
036            excludes = (pExcludes != null) ? pExcludes : new String[0];
037            includes = (pIncludes != null) ? pIncludes : new String[] { "**" };
038            mappers = (pMapper != null) ? pMapper : new Mapper[0];
039        }
040        
041        public boolean isIncluded( final String pName ) {
042            if (!isIncluded(pName, includes)) {
043                return false;
044            }
045            if (isExcluded(pName, excludes)) {
046                return false;
047            }
048            return true;
049        }
050    
051        private boolean isIncluded( String name, String[] includes ) {
052            for (int i = 0; i < includes.length; i++) {
053                if (SelectorUtils.matchPath(includes[i], name)) {
054                    return true;
055                }
056            }
057            return false;
058        }
059    
060        
061        private boolean isExcluded( String name, String[] excludes ) {
062            for (int i = 0; i < excludes.length; i++) {
063                if (SelectorUtils.matchPath(excludes[i], name)) {            
064                    return true;
065                }
066            }
067            return false;
068        }
069        
070        public TarEntry map( final TarEntry pEntry ) {
071            
072            TarEntry entry = pEntry;
073    
074            for (int i = 0; i < mappers.length; i++) {
075                entry = mappers[i].map(entry);
076            }
077            
078            return entry;
079        }
080    }