%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.ant.FileIterator |
|
|
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: 1.4 $ |
|
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 | 0 | 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 | 0 | private boolean nextObjectSet = false; |
55 | ||
56 | /** Return only directories? */ |
|
57 | 0 | private boolean iterateDirectories = false; |
58 | ||
59 | public FileIterator(Project project, |
|
60 | Iterator fileSetIterator) { |
|
61 | 0 | this( project, fileSetIterator, false); |
62 | 0 | } |
63 | ||
64 | public FileIterator(Project project, |
|
65 | Iterator fileSetIterator, |
|
66 | 0 | boolean iterateDirectories) { |
67 | 0 | this.project = project; |
68 | 0 | this.fileSetIterator = fileSetIterator; |
69 | 0 | this.iterateDirectories = iterateDirectories; |
70 | 0 | } |
71 | ||
72 | // Iterator interface |
|
73 | //------------------------------------------------------------------------- |
|
74 | ||
75 | /** @return true if there is another object that matches the given predicate */ |
|
76 | public boolean hasNext() { |
|
77 | 0 | if ( nextObjectSet ) { |
78 | 0 | return true; |
79 | } |
|
80 | else { |
|
81 | 0 | return setNextObject(); |
82 | } |
|
83 | } |
|
84 | ||
85 | /** @return the next object which matches the given predicate */ |
|
86 | public Object next() { |
|
87 | 0 | if ( !nextObjectSet ) { |
88 | 0 | if (!setNextObject()) { |
89 | 0 | throw new NoSuchElementException(); |
90 | } |
|
91 | } |
|
92 | 0 | nextObjectSet = false; |
93 | 0 | return nextFile; |
94 | } |
|
95 | ||
96 | /** |
|
97 | * throws UnsupportedOperationException |
|
98 | */ |
|
99 | public void remove() { |
|
100 | 0 | 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 | 0 | while (true) { |
112 | 0 | while (ds == null) { |
113 | 0 | if ( ! fileSetIterator.hasNext() ) { |
114 | 0 | return false; |
115 | } |
|
116 | 0 | FileSet fs = (FileSet) fileSetIterator.next(); |
117 | 0 | ds = fs.getDirectoryScanner(project); |
118 | 0 | ds.scan(); |
119 | 0 | if (iterateDirectories) { |
120 | 0 | files = ds.getIncludedDirectories(); |
121 | } |
|
122 | else { |
|
123 | 0 | files = ds.getIncludedFiles(); |
124 | } |
|
125 | 0 | if ( files.length > 0 ) { |
126 | 0 | fileIndex = -1; |
127 | 0 | break; |
128 | } |
|
129 | else { |
|
130 | 0 | ds = null; |
131 | } |
|
132 | } |
|
133 | ||
134 | 0 | if ( ds != null && files != class="keyword">null ) { |
135 | 0 | if ( ++fileIndex < files.length ) { |
136 | 0 | nextFile = new File( ds.getBasedir(), files[fileIndex] ); |
137 | 0 | nextObjectSet = true; |
138 | 0 | return true; |
139 | } |
|
140 | else { |
|
141 | 0 | ds = null; |
142 | } |
|
143 | } |
|
144 | } |
|
145 | } |
|
146 | } |
|
147 | ||
148 |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |