KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > condition > FlexIncludesIterator


1 /**
2  * $Id: FlexIncludesIterator.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004 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.condition;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.NoSuchElementException JavaDoc;
35
36 import com.idaremedia.antx.AntX;
37 import com.idaremedia.antx.ownhelpers.InnerProperties;
38 import com.idaremedia.antx.ownhelpers.ProjectDependentSkeleton;
39 import com.idaremedia.antx.ownhelpers.PropertiesIterator;
40 import com.idaremedia.antx.ownhelpers.UnresolvedProperty;
41
42 /**
43  * Special iterator of the set conditions (like &lt;allset&gt;) nested elements. This
44  * iterator returns a {@linkplain com.idaremedia.antx.FlexString FlexString} for each
45  * iteration including the collections of properties returned by nested
46  * <span class="src">&lt;properties&gt</span> elements. For unresolved property values,
47  * this iterator will substitute the marker {@linkplain UnresolvedProperty#VALUE unresolved}
48  * value as the returned item's value.
49  *
50  * @since JWare/AntX 0.4
51  * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
52  * @version 0.5
53  * @.safety single
54  * @.group impl,helper
55  **/

56
57 public final class FlexIncludesIterator extends ProjectDependentSkeleton
58     implements Iterator JavaDoc
59 {
60     private static final String JavaDoc IAM_ = AntX.conditions+"FlexIncludesIterator:";
61
62
63     /**
64      * Initializes a new value iterator for an AntX set-based
65      * condition.
66      * @param owner the set condition (non-null)
67      **/

68     public FlexIncludesIterator(FlexOuterCondition owner)
69     {
70         AntX.require_(owner!=null,IAM_,"ctor- nonzro owning set");
71         m_owner = owner;
72         m_scratchFV = new ExternalPropertyDef();
73     }
74
75
76     /**
77      * Triggered on first call to {@linkplain #hasNext}. Will
78      * setup iteration parameters. Returns <i>false</i> if nothing
79      * to iterate.
80      **/

81     private boolean prepareToIterate()
82     {
83         List JavaDoc l= m_owner.getIncludes();
84         if (l.isEmpty()) {
85             return false;//NB:don't waste time+memory
86
}
87
88         setProject(m_owner.getProject());
89         m_scratchFV.setProject(getProjectNoNull());
90         m_scratchFV.setTrim(m_owner.willTrim());
91         m_includesItr = l.iterator();
92         return true;
93     }
94
95
96     /**
97      * Returns <i>true</i> if there is at least one item left
98      * to iterate.
99      **/

100     public boolean hasNext()
101     {
102         if (m_includesItr==null) {
103             if (!prepareToIterate()) {
104                 return false;
105             }
106         }
107
108         boolean gotOne = (m_nextItem!=null);
109
110         if (!gotOne) {
111             if (m_innerItr!=null) {
112                 if (m_innerItr.hasNext()) {
113                     Map.Entry JavaDoc entry= (Map.Entry JavaDoc)m_innerItr.next();
114                     m_scratchFV.set(entry);
115                     m_nextItem = m_scratchFV;
116                     return true;
117                 }
118                 m_innerItr.dispose();
119                 m_innerItr = null;
120                 if (m_privateCall) {
121                     return false;
122                 }
123             }
124             while (m_includesItr.hasNext()) {
125                 Object JavaDoc candidate = m_includesItr.next();
126
127                 if (candidate instanceof InnerProperties) {
128                     m_innerItr = new PropertiesIterator((InnerProperties)candidate);
129                     m_innerItr.setCheckBrokenSubstitution();
130                     m_innerItr.setBrokenSubstitutionProxy(UnresolvedProperty.VALUE);
131                     m_privateCall=true;
132                     gotOne = hasNext();//get 1st one setup!
133
m_privateCall=false;
134                     if (gotOne) {
135                         return true;
136                     }
137                     candidate = null;
138                 }
139
140                 if (candidate!=null) {
141                     m_nextItem = candidate;
142                     gotOne = true;
143                     break;
144                 }
145             }
146         }
147
148         return gotOne;
149     }
150
151
152     /**
153      * Returns the next <span class="src">FlexString</span> to
154      * be handled. Note that the returned value may be part of the
155      * property collection of a nested property set.
156      * @throws NoSuchElementException if nothing left to iterate
157      **/

158     public Object JavaDoc next()
159     {
160         if (m_nextItem==null) {
161             throw new NoSuchElementException JavaDoc();
162         }
163         Object JavaDoc result = m_nextItem;
164         m_nextItem = null;
165         return result;
166     }
167
168
169     /**
170      * Always generates an
171      * <span class="src">UnsupportedOperationException</span>.
172      * @throws UnsupportedOperationException always
173      **/

174     public void remove()
175     {
176         throw new UnsupportedOperationException JavaDoc();
177     }
178
179
180
181     private final FlexOuterCondition m_owner;
182     private Iterator JavaDoc m_includesItr;
183     private PropertiesIterator m_innerItr;
184     private Object JavaDoc m_nextItem;
185     private final ExternalPropertyDef m_scratchFV;
186     private boolean m_privateCall;
187 }
188
189 /* end-of-FlexIncludesIterator.java */
190
Popular Tags