KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > ownhelpers > FileSetsIterator


1 /**
2  * $Id: FileSetsIterator.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004-2005 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The GNU LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.ownhelpers;
30
31 import java.io.File JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.NoSuchElementException JavaDoc;
35
36 import org.apache.tools.ant.BuildException;
37 import org.apache.tools.ant.DirectoryScanner;
38 import org.apache.tools.ant.Project;
39 import org.apache.tools.ant.types.FileSet;
40
41 import com.idaremedia.antx.AntX;
42 import com.idaremedia.antx.helpers.ArrayIterator;
43
44 /**
45  * Helper that can iterate a heterogeneous collection of Ant
46  * <span class="src">FileSet</span>s, <span class="src">File</span>s, and
47  * path strings. The iterator returns absolute path <em>names</em>.
48  *
49  * @since JWare/AntX 0.5
50  * @author ssmc, &copy;2004-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
51  * @version 0.5
52  * @.safety single
53  * @.group impl,helper
54  * @.caveat Both file and pathnames are returned <em>as-is</em>.
55  **/

56
57 public class FileSetsIterator extends ProjectDependentSkeleton implements Iterator JavaDoc
58 {
59     private static final String JavaDoc IAM_ = AntX.utilities+"FileSetsIterator:";
60
61
62     /**
63      * Initializes new iterator for given collection of file
64      * declarations.
65      * @param filesets list of filesets, Files, and path names(strings) (non-null)
66      * @param project project from which base directory should be determined (non-null)
67      **/

68     public FileSetsIterator(List JavaDoc filesets, Project project)
69     {
70         super();
71         AntX.require_(filesets!=null,IAM_,"ctor- nonzro fileset list");
72         setProject(project);
73         m_includesItr = filesets.isEmpty() ? null : filesets.iterator();
74     }
75
76
77
78     /**
79      * Returns <i>true</i> if there is at least one path left
80      * to iterate.
81      **/

82     public boolean hasNext()
83     {
84         if (m_includesItr==null) {
85             return false;
86         }
87
88         boolean gotOne = (m_nextItem!=null);
89
90         if (!gotOne) {
91             if (m_innerItr!=null) {
92                 if (m_innerItr.hasNext()) {
93                     m_nextItem = m_innerItr.next();
94                     return true;
95                 }
96                 m_innerItr = null;
97                 if (m_privateCall) {
98                     return false;
99                 }
100             }
101             while (m_includesItr.hasNext()) {
102                 Object JavaDoc candidate = m_includesItr.next();
103
104                 if (candidate instanceof FileSet) {
105                     DirectoryScanner ds = ((FileSet)candidate).getDirectoryScanner(getProject());
106                     String JavaDoc[] list = ds.getIncludedFiles();
107                     LocalTk.resolveAllFilesOrDirs(list,ds,false);
108                     m_innerItr = new ArrayIterator(list);
109                     m_privateCall=true;
110                     gotOne = this.hasNext();//get 1st one setup!
111
m_privateCall=false;
112                     if (gotOne) {
113                         return true;
114                     }
115                     candidate = null;
116                 }
117                 else if (candidate instanceof File JavaDoc) {
118                     candidate = ((File JavaDoc)candidate).getPath();
119                 }
120                 else if (!(candidate instanceof String JavaDoc)) {
121                     String JavaDoc error = AntX.uistrs().get("task.bad.path.from",
122                                                      String.valueOf(candidate));
123                     getProject().log(error,Project.MSG_ERR);
124                     throw new BuildException(error);
125                 }
126
127                 if (candidate!=null) {
128                     m_nextItem = candidate;
129                     gotOne = true;
130                     break;
131                 }
132             }
133         }
134
135         return gotOne;
136     }
137
138
139
140     /**
141      * Returns the next file path to be handled as a
142      * <span class="src">String</span>.
143      * @throws NoSuchElementException if nothing left to iterate
144      **/

145     public Object JavaDoc next()
146     {
147         if (m_nextItem==null) {
148             throw new NoSuchElementException JavaDoc();
149         }
150         Object JavaDoc result = m_nextItem;
151         m_nextItem = null;
152         return result;
153     }
154
155
156
157     /**
158      * Always generates an
159      * <span class="src">UnsupportedOperationException</span>.
160      * @throws UnsupportedOperationException always
161      **/

162     public void remove()
163     {
164         throw new UnsupportedOperationException JavaDoc();
165     }
166
167
168     private Iterator JavaDoc m_includesItr; //NB:list of filesets,files,pathnames,etc.
169
private Iterator JavaDoc m_innerItr;//NB:list of a particular fileset's included bits
170
private Object JavaDoc m_nextItem;//NB:next String
171
private boolean m_privateCall;
172 }
173
174
175 /* end-of-FileSetsIterator.java */
Popular Tags