View Javadoc

1   /*
2    * Copyright 2002,2004 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.apache.commons.jelly.tags.ant;
17  
18  import java.io.File;
19  import java.util.Iterator;
20  import java.util.NoSuchElementException;
21  
22  import org.apache.tools.ant.DirectoryScanner;
23  import org.apache.tools.ant.Project;
24  import org.apache.tools.ant.types.FileSet;
25  
26  /***
27   * <p><code>FileIterator</code> is an iterator over a
28   * over a number of files from a colleciton of FileSet instances.
29   *
30   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31   * @version $Revision: 155420 $
32   */
33  public class FileIterator implements Iterator {
34  
35      /*** The iterator over the FileSet objects */
36      private Iterator fileSetIterator;
37  
38      /*** The Ant project */
39      private Project project;
40  
41      /*** The directory scanner */
42      private DirectoryScanner ds;
43  
44      /*** The file names in the current FileSet scan */
45      private String[] files;
46  
47      /*** The current index into the file name array */
48      private int fileIndex = -1;
49  
50      /*** The next File object we'll iterate over */
51      private File nextFile;
52  
53      /*** Have we set a next object? */
54      private boolean nextObjectSet = false;
55  
56      /*** Return only directories? */
57      private boolean iterateDirectories = false;
58  
59      public FileIterator(Project project,
60                          Iterator fileSetIterator) {
61          this( project, fileSetIterator, false);
62      }
63  
64      public FileIterator(Project project,
65                          Iterator fileSetIterator,
66                          boolean iterateDirectories) {
67          this.project = project;
68          this.fileSetIterator = fileSetIterator;
69          this.iterateDirectories = iterateDirectories;
70      }
71  
72      // Iterator interface
73      //-------------------------------------------------------------------------
74  
75      /*** @return true if there is another object that matches the given predicate */
76      public boolean hasNext() {
77          if ( nextObjectSet ) {
78              return true;
79          }
80          else {
81              return setNextObject();
82          }
83      }
84  
85      /*** @return the next object which matches the given predicate */
86      public Object next() {
87          if ( !nextObjectSet ) {
88              if (!setNextObject()) {
89                  throw new NoSuchElementException();
90              }
91          }
92          nextObjectSet = false;
93          return nextFile;
94      }
95  
96      /***
97       * throws UnsupportedOperationException
98       */
99      public void remove() {
100         throw new UnsupportedOperationException();
101     }
102 
103     // Implementation methods
104     //-------------------------------------------------------------------------
105 
106     /***
107      * Set nextObject to the next object. If there are no more
108      * objects then return false. Otherwise, return true.
109      */
110     private boolean setNextObject() {
111         while (true) {
112             while (ds == null) {
113                 if ( ! fileSetIterator.hasNext() ) {
114                     return false;
115                 }
116                 FileSet fs = (FileSet) fileSetIterator.next();
117                 ds = fs.getDirectoryScanner(project);
118                 ds.scan();
119                 if (iterateDirectories) {
120                     files = ds.getIncludedDirectories();
121                 }
122                 else {
123                     files = ds.getIncludedFiles();
124                 }
125                 if ( files.length > 0 ) {
126                     fileIndex = -1;
127                     break;
128                 }
129                 else {
130                     ds = null;
131                 }
132             }
133 
134             if ( ds != null && files != null ) {
135                 if ( ++fileIndex < files.length ) {
136                     nextFile = new File( ds.getBasedir(), files[fileIndex] );
137                     nextObjectSet = true;
138                     return true;
139                 }
140                 else {
141                     ds = null;
142                 }
143             }
144         }
145     }
146 }
147 
148