View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.vafer.jdeb.ant;
17  
18  import java.io.File;
19  import java.io.FileNotFoundException;
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Iterator;
24  
25  import org.apache.tools.ant.types.PatternSet;
26  import org.vafer.jdeb.DataConsumer;
27  import org.vafer.jdeb.DataProducer;
28  import org.vafer.jdeb.producers.DataProducerArchive;
29  import org.vafer.jdeb.producers.DataProducerDirectory;
30  
31  
32  /**
33   * Ant "data" elment acting as a factory for DataProducers.
34   * So far Archive and Directory producers are supported.
35   * Both support the usual ant pattern set matching.
36   * 
37   * @author Torsten Curdt <tcurdt@vafer.org>
38   */
39  public final class Data extends PatternSet implements DataProducer {
40  
41  	private final Collection mapperWrapper = new ArrayList();
42  
43  	private File src;
44  		
45  	public void setSrc( final File pSrc ) {
46  		src = pSrc;
47  	}
48  
49  	public void addMapper( final Mapper pMapper ) {
50  		mapperWrapper.add(pMapper);
51  	}
52  	
53  	public void produce( final DataConsumer pReceiver ) throws IOException {
54  		
55  		if (!src.exists()) {
56  			throw new FileNotFoundException("Data source not found : " + src);
57  		}
58  
59  		org.vafer.jdeb.mapping.Mapper[] mappers = new org.vafer.jdeb.mapping.Mapper[mapperWrapper.size()];
60  		final Iterator it = mapperWrapper.iterator();
61  		for (int i = 0; i < mappers.length; i++) {
62  			mappers[i] = ((Mapper)it.next()).createMapper();
63  		}
64  		
65  		if (src.isFile()) {
66  			new DataProducerArchive(
67  				src,
68  				getIncludePatterns(getProject()),
69  				getExcludePatterns(getProject()),
70  				mappers
71  				).produce(pReceiver);
72  		} else {
73  			new DataProducerDirectory(
74  				src,
75  				getIncludePatterns(getProject()),
76  				getExcludePatterns(getProject()),
77  				mappers
78  				).produce(pReceiver);			
79  		}
80  	}
81  }