1 /*
2 * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/core/IfTag.java,v 1.6 2002/05/17 15:18:08 jstrachan Exp $
3 * $Revision: 1.6 $
4 * $Date: 2002/05/17 15:18:08 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * $Id: IfTag.java,v 1.6 2002/05/17 15:18:08 jstrachan Exp $
61 */
62 package org.apache.commons.jelly.tags.ant;
63
64 import java.io.File;
65 import java.util.Iterator;
66 import java.util.NoSuchElementException;
67
68 import org.apache.tools.ant.Project;
69 import org.apache.tools.ant.DirectoryScanner;
70 import org.apache.tools.ant.types.FileSet;
71 import org.apache.tools.ant.types.FilterSet;
72
73 /***
74 * <p><code>FileIterator</code> is an iterator over a
75 * over a number of files from a colleciton of FileSet instances.
76 *
77 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
78 * @version $Revision: 1.4 $
79 */
80 public class FileIterator implements Iterator {
81
82 /*** The iterator over the FileSet objects */
83 private Iterator fileSetIterator;
84
85 /*** The Ant project */
86 private Project project;
87
88 /*** The directory scanner */
89 private DirectoryScanner ds;
90
91 /*** The file names in the current FileSet scan */
92 private String[] files;
93
94 /*** The current index into the file name array */
95 private int fileIndex = -1;
96
97 /*** The next File object we'll iterate over */
98 private File nextFile;
99
100 /*** Have we set a next object? */
101 private boolean nextObjectSet = false;
102
103 /*** Return only directories? */
104 private boolean iterateDirectories = false;
105
106 public FileIterator(Project project,
107 Iterator fileSetIterator) {
108 this( project, fileSetIterator, false);
109 }
110
111 public FileIterator(Project project,
112 Iterator fileSetIterator,
113 boolean iterateDirectories) {
114 this.project = project;
115 this.fileSetIterator = fileSetIterator;
116 this.iterateDirectories = iterateDirectories;
117 }
118
119 // Iterator interface
120 //-------------------------------------------------------------------------
121
122 /*** @return true if there is another object that matches the given predicate */
123 public boolean hasNext() {
124 if ( nextObjectSet ) {
125 return true;
126 }
127 else {
128 return setNextObject();
129 }
130 }
131
132 /*** @return the next object which matches the given predicate */
133 public Object next() {
134 if ( !nextObjectSet ) {
135 if (!setNextObject()) {
136 throw new NoSuchElementException();
137 }
138 }
139 nextObjectSet = false;
140 return nextFile;
141 }
142
143 /***
144 * throws UnsupportedOperationException
145 */
146 public void remove() {
147 throw new UnsupportedOperationException();
148 }
149
150 // Implementation methods
151 //-------------------------------------------------------------------------
152
153 /***
154 * Set nextObject to the next object. If there are no more
155 * objects then return false. Otherwise, return true.
156 */
157 private boolean setNextObject() {
158 while (true) {
159 while (ds == null) {
160 if ( ! fileSetIterator.hasNext() ) {
161 return false;
162 }
163 FileSet fs = (FileSet) fileSetIterator.next();
164 ds = fs.getDirectoryScanner(project);
165 ds.scan();
166 if (iterateDirectories) {
167 files = ds.getIncludedDirectories();
168 }
169 else {
170 files = ds.getIncludedFiles();
171 }
172 if ( files.length > 0 ) {
173 fileIndex = -1;
174 break;
175 }
176 else {
177 ds = null;
178 }
179 }
180
181 if ( ds != null && files != null ) {
182 if ( ++fileIndex < files.length ) {
183 nextFile = new File( ds.getBasedir(), files[fileIndex] );
184 nextObjectSet = true;
185 return true;
186 }
187 else {
188 ds = null;
189 }
190 }
191 }
192 }
193 }
194
195
This page was automatically generated by Maven