View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 Sabre Holdings.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  
20  package org.eclipse.jetty.ant.types;
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import org.apache.tools.ant.DirectoryScanner;
28  
29  /**
30   * Describes set of files matched by <fileset/> elements in ant configuration
31   * file. It is used to group application classes, libraries, and scannedTargets
32   * elements.
33   * 
34   */
35  public class FileMatchingConfiguration
36  {
37  
38      private List directoryScanners;
39  
40      public FileMatchingConfiguration()
41      {
42          this.directoryScanners = new ArrayList();
43      }
44  
45      /**
46       * @param directoryScanner new directory scanner retrieved from the
47       *            <fileset/> element.
48       */
49      public void addDirectoryScanner(DirectoryScanner directoryScanner)
50      {
51          this.directoryScanners.add(directoryScanner);
52      }
53  
54      /**
55       * @return a list of base directories denoted by a list of directory
56       *         scanners.
57       */
58      public List getBaseDirectories()
59      {
60          List baseDirs = new ArrayList();
61          Iterator scanners = directoryScanners.iterator();
62          while (scanners.hasNext())
63          {
64              DirectoryScanner scanner = (DirectoryScanner) scanners.next();
65              baseDirs.add(scanner.getBasedir());
66          }
67  
68          return baseDirs;
69      }
70  
71      /**
72       * Checks if passed file is scanned by any of the directory scanners.
73       * 
74       * @param pathToFile a fully qualified path to tested file.
75       * @return true if so, false otherwise.
76       */
77      public boolean isIncluded(String pathToFile)
78      {
79          Iterator scanners = directoryScanners.iterator();
80          while (scanners.hasNext())
81          {
82              DirectoryScanner scanner = (DirectoryScanner) scanners.next();
83              scanner.scan();
84              String[] includedFiles = scanner.getIncludedFiles();
85  
86              for (int i = 0; i < includedFiles.length; i++)
87              {
88                  File includedFile = new File(scanner.getBasedir(), includedFiles[i]);
89                  if (pathToFile.equalsIgnoreCase(includedFile.getAbsolutePath()))
90                  {
91                      return true;
92                  }
93              }
94          }
95  
96          return false;
97      }
98  }