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.ant;
017    
018    import java.io.File;
019    import java.io.FileNotFoundException;
020    import java.io.IOException;
021    import java.util.ArrayList;
022    import java.util.Collection;
023    import java.util.Iterator;
024    
025    import org.apache.tools.ant.types.PatternSet;
026    import org.vafer.jdeb.DataConsumer;
027    import org.vafer.jdeb.DataProducer;
028    import org.vafer.jdeb.producers.DataProducerArchive;
029    import org.vafer.jdeb.producers.DataProducerDirectory;
030    import org.vafer.jdeb.producers.DataProducerFile;
031    
032    /**
033     * Ant "data" element acting as a factory for DataProducers.
034     * So far Archive and Directory producers are supported.
035     * Both support the usual ant pattern set matching.
036     * 
037     * @author Torsten Curdt <tcurdt@vafer.org>
038     */
039    public final class Data extends PatternSet implements DataProducer {
040    
041        private final Collection mapperWrapper = new ArrayList();
042    
043        private File src;
044        
045        private String type;
046            
047        public void setSrc( final File pSrc ) {
048            src = pSrc;
049        }
050        
051        public void setType( final String pType ) {
052            type = pType;
053        }
054    
055        public void addMapper( final Mapper pMapper ) {
056            mapperWrapper.add(pMapper);
057        }
058        
059        public void produce( final DataConsumer pReceiver ) throws IOException {
060            
061            if (!src.exists()) {
062                throw new FileNotFoundException("Data source not found : " + src);
063            }
064    
065            org.vafer.jdeb.mapping.Mapper[] mappers = new org.vafer.jdeb.mapping.Mapper[mapperWrapper.size()];
066            final Iterator it = mapperWrapper.iterator();
067            for (int i = 0; i < mappers.length; i++) {
068                mappers[i] = ((Mapper)it.next()).createMapper();
069            }
070    
071            if ("file".equalsIgnoreCase(type)) {
072                new DataProducerFile(
073                        src,
074                        getIncludePatterns(getProject()),
075                        getExcludePatterns(getProject()),
076                        mappers
077                        ).produce(pReceiver);
078                return;
079            }
080    
081            if ("archive".equalsIgnoreCase(type)) {
082                new DataProducerArchive(
083                        src,
084                        getIncludePatterns(getProject()),
085                        getExcludePatterns(getProject()),
086                        mappers
087                        ).produce(pReceiver);
088                return;
089            }
090            
091            if ("directory".equalsIgnoreCase(type)) {
092                new DataProducerDirectory(
093                        src,
094                        getIncludePatterns(getProject()),
095                        getExcludePatterns(getProject()),
096                        mappers
097                        ).produce(pReceiver);           
098                return;
099            }
100    
101            // @deprecated
102            
103            if (src.isFile()) {
104                new DataProducerArchive(
105                    src,
106                    getIncludePatterns(getProject()),
107                    getExcludePatterns(getProject()),
108                    mappers
109                    ).produce(pReceiver);
110            } else {
111                new DataProducerDirectory(
112                    src,
113                    getIncludePatterns(getProject()),
114                    getExcludePatterns(getProject()),
115                    mappers
116                    ).produce(pReceiver);           
117            }
118        }
119    }