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 <code>&lt;fileset/&gt;</code> elements in ant configuration
31   * file. It is used to group application classes, libraries, and scannedTargets
32   * elements.
33   */
34  public class FileMatchingConfiguration
35  {
36  
37      private List directoryScanners;
38  
39      public FileMatchingConfiguration()
40      {
41          this.directoryScanners = new ArrayList();
42      }
43  
44      /**
45       * @param directoryScanner new directory scanner retrieved from the
46       *            <code>&lt;fileset/&gt;</code> element.
47       */
48      public void addDirectoryScanner(DirectoryScanner directoryScanner)
49      {
50          this.directoryScanners.add(directoryScanner);
51      }
52  
53      /**
54       * @return a list of base directories denoted by a list of directory
55       *         scanners.
56       */
57      public List getBaseDirectories()
58      {
59          List baseDirs = new ArrayList();
60          Iterator scanners = directoryScanners.iterator();
61          while (scanners.hasNext())
62          {
63              DirectoryScanner scanner = (DirectoryScanner) scanners.next();
64              baseDirs.add(scanner.getBasedir());
65          }
66  
67          return baseDirs;
68      }
69  
70      /**
71       * Checks if passed file is scanned by any of the directory scanners.
72       * 
73       * @param pathToFile a fully qualified path to tested file.
74       * @return true if so, false otherwise.
75       */
76      public boolean isIncluded(String pathToFile)
77      {
78          Iterator scanners = directoryScanners.iterator();
79          while (scanners.hasNext())
80          {
81              DirectoryScanner scanner = (DirectoryScanner) scanners.next();
82              scanner.scan();
83              String[] includedFiles = scanner.getIncludedFiles();
84  
85              for (int i = 0; i < includedFiles.length; i++)
86              {
87                  File includedFile = new File(scanner.getBasedir(), includedFiles[i]);
88                  if (pathToFile.equalsIgnoreCase(includedFile.getAbsolutePath()))
89                  {
90                      return true;
91                  }
92              }
93          }
94  
95          return false;
96      }
97  }