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.producers;
17  
18  import org.apache.tools.ant.types.selectors.SelectorUtils;
19  import org.apache.tools.tar.TarEntry;
20  import org.vafer.jdeb.DataProducer;
21  import org.vafer.jdeb.mapping.Mapper;
22  
23  /**
24   * Base Producer class providing including/excluding.
25   * 
26   * @author Torsten Curdt <tcurdt@vafer.org>
27   */
28  public abstract class AbstractDataProducer implements DataProducer {
29  
30  	private final String[] includes;
31  	private final String[] excludes;
32  	private final Mapper[] mappers;
33  	
34  	
35  	public AbstractDataProducer( final String[] pIncludes, final String[] pExcludes, final Mapper[] pMapper ) {
36  		excludes = (pExcludes != null) ? pExcludes : new String[0];
37  		includes = (pIncludes != null) ? pIncludes : new String[] { "**" };
38  		mappers = (pMapper != null) ? pMapper : new Mapper[0];
39  	}
40  	
41  	public boolean isIncluded( final String pName ) {
42  		if (!isIncluded(pName, includes)) {
43  			return false;
44  		}
45  		if (isExcluded(pName, excludes)) {
46  			return false;
47  		}
48  		return true;
49  	}
50  
51      private boolean isIncluded( String name, String[] includes ) {
52          for (int i = 0; i < includes.length; i++) {
53              if (SelectorUtils.matchPath(includes[i], name)) {
54                  return true;
55              }
56          }
57          return false;
58      }
59  
60      
61      private boolean isExcluded( String name, String[] excludes ) {
62          for (int i = 0; i < excludes.length; i++) {
63              if (SelectorUtils.matchPath(excludes[i], name)) {            
64                  return true;
65              }
66          }
67          return false;
68      }
69  	
70  	public TarEntry map( final TarEntry pEntry ) {
71  		
72  		TarEntry entry = pEntry;
73  
74  		for (int i = 0; i < mappers.length; i++) {
75  			entry = mappers[i].map(entry);
76  		}
77  		
78  		return entry;
79  	}
80  }