1 // ========================================================================
2 // Copyright 2006-2007 Sabre Holdings.
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 // http://www.apache.org/licenses/LICENSE-2.0
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 // ========================================================================
14 
15 package org.mortbay.jetty.ant.types;
16 
17 import java.io.File;
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21 
22 import org.apache.tools.ant.DirectoryScanner;
23 
24 /**
25  * Describes set of files matched by <fileset/> elements in ant configuration
26  * file. It is used to group application classes, libraries, and scannedTargets
27  * elements.
28  *
29  * @author Jakub Pawlowicz
30  */
31 public class FileMatchingConfiguration
32 {
33 
34     private List directoryScanners;
35 
FileMatchingConfiguration()36     public FileMatchingConfiguration()
37     {
38         this.directoryScanners = new ArrayList();
39     }
40 
41     /**
42      * @param directoryScanner new directory scanner retrieved from the
43      *            <fileset/> element.
44      */
addDirectoryScanner(DirectoryScanner directoryScanner)45     public void addDirectoryScanner(DirectoryScanner directoryScanner)
46     {
47         this.directoryScanners.add(directoryScanner);
48     }
49 
50     /**
51      * @return a list of base directories denoted by a list of directory
52      *         scanners.
53      */
getBaseDirectories()54     public List getBaseDirectories()
55     {
56         List baseDirs = new ArrayList();
57         Iterator scanners = directoryScanners.iterator();
58         while (scanners.hasNext())
59         {
60             DirectoryScanner scanner = (DirectoryScanner) scanners.next();
61             baseDirs.add(scanner.getBasedir());
62         }
63 
64         return baseDirs;
65     }
66 
67     /**
68      * Checks if passed file is scanned by any of the directory scanners.
69      *
70      * @param pathToFile a fully qualified path to tested file.
71      * @return true if so, false otherwise.
72      */
isIncluded(String pathToFile)73     public boolean isIncluded(String pathToFile)
74     {
75         Iterator scanners = directoryScanners.iterator();
76         while (scanners.hasNext())
77         {
78             DirectoryScanner scanner = (DirectoryScanner) scanners.next();
79             scanner.scan();
80             String[] includedFiles = scanner.getIncludedFiles();
81 
82             for (int i = 0; i < includedFiles.length; i++)
83             {
84                 File includedFile = new File(scanner.getBasedir(), includedFiles[i]);
85                 if (pathToFile.equalsIgnoreCase(includedFile.getAbsolutePath()))
86                 {
87                     return true;
88                 }
89             }
90         }
91 
92         return false;
93     }
94 }
95