001    /*
002     * Copyright 2005 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    
031    
032    /**
033     * Ant "data" elment 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            public void setSrc( final File pSrc ) {
046                    src = pSrc;
047            }
048    
049            public void addMapper( final Mapper pMapper ) {
050                    mapperWrapper.add(pMapper);
051            }
052            
053            public void produce( final DataConsumer pReceiver ) throws IOException {
054                    
055                    if (!src.exists()) {
056                            throw new FileNotFoundException("Data source not found : " + src);
057                    }
058    
059                    org.vafer.jdeb.mapping.Mapper[] mappers = new org.vafer.jdeb.mapping.Mapper[mapperWrapper.size()];
060                    final Iterator it = mapperWrapper.iterator();
061                    for (int i = 0; i < mappers.length; i++) {
062                            mappers[i] = ((Mapper)it.next()).createMapper();
063                    }
064                    
065                    if (src.isFile()) {
066                            new DataProducerArchive(
067                                    src,
068                                    getIncludePatterns(getProject()),
069                                    getExcludePatterns(getProject()),
070                                    mappers
071                                    ).produce(pReceiver);
072                    } else {
073                            new DataProducerDirectory(
074                                    src,
075                                    getIncludePatterns(getProject()),
076                                    getExcludePatterns(getProject()),
077                                    mappers
078                                    ).produce(pReceiver);                   
079                    }
080            }
081    }