Lines 17-51
Link Here
|
17 |
*/ |
17 |
*/ |
18 |
package org.apache.tools.ant.property; |
18 |
package org.apache.tools.ant.property; |
19 |
|
19 |
|
20 |
|
|
|
21 |
import java.util.LinkedList; |
22 |
import java.util.HashMap; |
20 |
import java.util.HashMap; |
|
|
21 |
import java.util.HashSet; |
23 |
import java.util.Iterator; |
22 |
import java.util.Iterator; |
|
|
23 |
import java.util.LinkedList; |
24 |
import java.util.Map; |
24 |
import java.util.Map; |
|
|
25 |
import java.util.Set; |
25 |
|
26 |
|
26 |
import org.apache.tools.ant.PropertyHelper; |
27 |
import org.apache.tools.ant.PropertyHelper; |
27 |
|
28 |
|
28 |
/** |
29 |
/** |
29 |
* A stack of local property maps. |
30 |
* A stack of local property maps. There is a map for each scope (target, |
30 |
* There is a map for each scope (target, sequential, macro). |
31 |
* sequential, macro). |
|
|
32 |
* |
31 |
* @since Ant 1.8.0 |
33 |
* @since Ant 1.8.0 |
32 |
*/ |
34 |
*/ |
33 |
public class LocalPropertyStack { |
35 |
public class LocalPropertyStack { |
34 |
private LinkedList stack = new LinkedList(); |
36 |
|
|
|
37 |
/** |
38 |
* An entry in the property stack. |
39 |
*/ |
40 |
private static class StackEntry { |
41 |
/** Map of local properties. */ |
42 |
private Map<String, Object> propertyMap; |
43 |
/** |
44 |
* Set of local prefixes. All variables with such a prefix are local. |
45 |
*/ |
46 |
private Set<String> localPrefixSet; |
47 |
/** If true, all properties in the current scope are local. */ |
48 |
private boolean allLocal; |
49 |
/** |
50 |
* Properties named in this set are not local to this scope, even if |
51 |
* they are named in propertyMap or have a prefix contained in |
52 |
* localPrefixSet or allLocal is set. |
53 |
*/ |
54 |
private Set<String> globalProperties; |
55 |
/** |
56 |
* Properties with a prefix contained in this set are not local to this |
57 |
* scope, even if they are named in propertyMap or have a prefix |
58 |
* contained in localPrefixSet or allLocal is set. |
59 |
*/ |
60 |
private Set<String> globalPrefixSet; |
61 |
|
62 |
private StackEntry() { |
63 |
propertyMap = new HashMap<String, Object>(); |
64 |
localPrefixSet = new HashSet<String>(); |
65 |
globalProperties = new HashSet<String>(); |
66 |
globalPrefixSet = new HashSet<String>(); |
67 |
} |
68 |
|
69 |
private void clear() { |
70 |
propertyMap.clear(); |
71 |
localPrefixSet.clear(); |
72 |
globalPrefixSet.clear(); |
73 |
globalProperties.clear(); |
74 |
propertyMap = null; |
75 |
localPrefixSet = null; |
76 |
globalPrefixSet = null; |
77 |
globalProperties = null; |
78 |
} |
79 |
|
80 |
/** |
81 |
* Determine if the given property is local to this scope. |
82 |
* |
83 |
* @param property |
84 |
* property name to check |
85 |
* @return true if it is local, else false |
86 |
*/ |
87 |
private boolean isLocalProperty(String property) { |
88 |
if (globalProperties.contains(property)) { |
89 |
return false; |
90 |
} |
91 |
for (Iterator<String> prefixIter = globalPrefixSet.iterator(); prefixIter |
92 |
.hasNext();) { |
93 |
if (property.startsWith(prefixIter.next())) { |
94 |
return false; |
95 |
} |
96 |
} |
97 |
|
98 |
if (allLocal) { |
99 |
return true; |
100 |
} |
101 |
if (propertyMap.get(property) != null) { |
102 |
return true; |
103 |
} |
104 |
for (Iterator<String> prefixIter = localPrefixSet.iterator(); prefixIter |
105 |
.hasNext();) { |
106 |
if (property.startsWith(prefixIter.next())) { |
107 |
return true; |
108 |
} |
109 |
} |
110 |
|
111 |
return false; |
112 |
} |
113 |
|
114 |
} |
115 |
|
116 |
private LinkedList<StackEntry> stack = new LinkedList<StackEntry>(); |
35 |
|
117 |
|
36 |
// -------------------------------------------------- |
118 |
// -------------------------------------------------- |
37 |
// |
119 |
// |
38 |
// Local property adding and scoping |
120 |
// Local property adding and scoping |
39 |
// |
121 |
// |
40 |
// -------------------------------------------------- |
122 |
// -------------------------------------------------- |
41 |
|
123 |
|
42 |
/** |
124 |
/** |
43 |
* Add a local property. |
125 |
* Add a local property. |
44 |
* @param property the name of the local property. |
126 |
* |
|
|
127 |
* @param property |
128 |
* the name of the local property. |
45 |
*/ |
129 |
*/ |
46 |
public void addLocal(String property) { |
130 |
public void addLocal(String property) { |
47 |
if (!stack.isEmpty()) { |
131 |
if (!stack.isEmpty()) { |
48 |
((Map) stack.getFirst()).put(property, NullReturn.NULL); |
132 |
stack.getFirst().propertyMap.put(property, NullReturn.NULL); |
|
|
133 |
} |
134 |
} |
135 |
|
136 |
/** |
137 |
* Add a local prefix. All properties with this prefix are handled as local |
138 |
* property. |
139 |
* |
140 |
* @param prefix |
141 |
* the prefix to detect a local property. |
142 |
*/ |
143 |
public void addLocalPrefix(String prefix) { |
144 |
if (!stack.isEmpty()) { |
145 |
stack.getFirst().localPrefixSet.add(prefix); |
146 |
} |
147 |
} |
148 |
|
149 |
/** |
150 |
* Set all properties in the current scope as local. |
151 |
*/ |
152 |
public void setAllLocal() { |
153 |
if (!stack.isEmpty()) { |
154 |
stack.getFirst().allLocal = true; |
155 |
} |
156 |
} |
157 |
|
158 |
/** |
159 |
* Add a global property. |
160 |
* |
161 |
* This is useful if the visibility of properties were restricted with |
162 |
* {@link #addLocalPrefix(String)} or {@link #setAllLocal()} but a certain |
163 |
* property should be global. |
164 |
* |
165 |
* @param property |
166 |
* the name of the global property. |
167 |
*/ |
168 |
public void addGlobal(String property) { |
169 |
if (!stack.isEmpty()) { |
170 |
stack.getFirst().globalProperties.add(property); |
171 |
} |
172 |
} |
173 |
|
174 |
/** |
175 |
* Add a global prefix. |
176 |
* |
177 |
* This is useful if the visibility of properties were restricted with |
178 |
* {@link #setAllLocal()} but properties with the given prefix should be |
179 |
* global. |
180 |
* |
181 |
* @param prefix |
182 |
* the prefix to detect a global property. |
183 |
*/ |
184 |
public void addGlobalPrefix(String prefix) { |
185 |
if (!stack.isEmpty()) { |
186 |
stack.getFirst().globalPrefixSet.add(prefix); |
49 |
} |
187 |
} |
50 |
} |
188 |
} |
51 |
|
189 |
|
Lines 53-76
Link Here
|
53 |
* Enter the local scope. |
191 |
* Enter the local scope. |
54 |
*/ |
192 |
*/ |
55 |
public void enterScope() { |
193 |
public void enterScope() { |
56 |
stack.addFirst(new HashMap()); |
194 |
stack.addFirst(new StackEntry()); |
57 |
} |
195 |
} |
58 |
|
196 |
|
59 |
/** |
197 |
/** |
60 |
* Exit the local scope. |
198 |
* Exit the local scope. |
61 |
*/ |
199 |
*/ |
62 |
public void exitScope() { |
200 |
public void exitScope() { |
63 |
((HashMap) stack.removeFirst()).clear(); |
201 |
((StackEntry) stack.removeFirst()).clear(); |
64 |
} |
202 |
} |
65 |
|
203 |
|
66 |
// -------------------------------------------------- |
204 |
// -------------------------------------------------- |
67 |
// |
205 |
// |
68 |
// Copy - used in parallel to make a new stack |
206 |
// Copy - used in parallel to make a new stack |
69 |
// |
207 |
// |
70 |
// -------------------------------------------------- |
208 |
// -------------------------------------------------- |
71 |
|
209 |
|
72 |
/** |
210 |
/** |
73 |
* Copy the stack for a parallel thread. |
211 |
* Copy the stack for a parallel thread. |
|
|
212 |
* |
74 |
* @return a copy. |
213 |
* @return a copy. |
75 |
*/ |
214 |
*/ |
76 |
public LocalPropertyStack copy() { |
215 |
public LocalPropertyStack copy() { |
Lines 81-122
Link Here
|
81 |
|
220 |
|
82 |
// -------------------------------------------------- |
221 |
// -------------------------------------------------- |
83 |
// |
222 |
// |
84 |
// PropertyHelper delegate methods |
223 |
// PropertyHelper delegate methods |
85 |
// |
224 |
// |
86 |
// -------------------------------------------------- |
225 |
// -------------------------------------------------- |
87 |
|
226 |
|
88 |
/** |
227 |
/** |
89 |
* Evaluate a property. |
228 |
* Evaluate a property. |
90 |
* @param property the property's String "identifier". |
229 |
* |
91 |
* @param helper the invoking PropertyHelper. |
230 |
* @param property |
|
|
231 |
* the property's String "identifier". |
232 |
* @param helper |
233 |
* the invoking PropertyHelper. |
92 |
* @return Object value. |
234 |
* @return Object value. |
93 |
*/ |
235 |
*/ |
94 |
public Object evaluate(String property, PropertyHelper helper) { |
236 |
public Object evaluate(String property, PropertyHelper helper) { |
95 |
for (Iterator i = stack.iterator(); i.hasNext();) { |
237 |
Map<String, Object> map = getMapForProperty(property); |
96 |
Map map = (Map) i.next(); |
238 |
if (map == null) { |
97 |
Object ret = map.get(property); |
239 |
return null; |
98 |
if (ret != null) { |
240 |
} |
99 |
return ret; |
241 |
Object ret = map.get(property); |
100 |
} |
242 |
if (ret != null) { |
|
|
243 |
// Note: NullReturn.NULL is handled on upper level. |
244 |
return ret; |
101 |
} |
245 |
} |
102 |
return null; |
246 |
return null; |
103 |
} |
247 |
} |
104 |
|
248 |
|
105 |
/** |
249 |
/** |
106 |
* Set a *new" property. |
250 |
* Set a *new" property. |
107 |
* @param property the property's String "identifier". |
251 |
* |
108 |
* @param value the value to set. |
252 |
* @param property |
109 |
* @param propertyHelper the invoking PropertyHelper. |
253 |
* the property's String "identifier". |
|
|
254 |
* @param value |
255 |
* the value to set. |
256 |
* @param propertyHelper |
257 |
* the invoking PropertyHelper. |
110 |
* @return true if this entity 'owns' the property. |
258 |
* @return true if this entity 'owns' the property. |
111 |
*/ |
259 |
*/ |
112 |
public boolean setNew( |
260 |
public boolean setNew(String property, Object value, |
113 |
String property, Object value, PropertyHelper propertyHelper) { |
261 |
PropertyHelper propertyHelper) { |
114 |
Map map = getMapForProperty(property); |
262 |
Map<String, Object> map = getMapForProperty(property); |
115 |
if (map == null) { |
263 |
if (map == null) { |
116 |
return false; |
264 |
return false; |
117 |
} |
265 |
} |
118 |
Object currValue = map.get(property); |
266 |
Object currValue = map.get(property); |
119 |
if (currValue == NullReturn.NULL) { |
267 |
if (currValue == NullReturn.NULL || !map.containsKey(property)) { |
120 |
map.put(property, value); |
268 |
map.put(property, value); |
121 |
} |
269 |
} |
122 |
return true; |
270 |
return true; |
Lines 124-136
Link Here
|
124 |
|
272 |
|
125 |
/** |
273 |
/** |
126 |
* Set a property. |
274 |
* Set a property. |
127 |
* @param property the property's String "identifier". |
275 |
* |
128 |
* @param value the value to set. |
276 |
* @param property |
129 |
* @param propertyHelper the invoking PropertyHelper. |
277 |
* the property's String "identifier". |
|
|
278 |
* @param value |
279 |
* the value to set. |
280 |
* @param propertyHelper |
281 |
* the invoking PropertyHelper. |
130 |
* @return true if this entity 'owns' the property. |
282 |
* @return true if this entity 'owns' the property. |
131 |
*/ |
283 |
*/ |
132 |
public boolean set(String property, Object value, PropertyHelper propertyHelper) { |
284 |
public boolean set(String property, Object value, |
133 |
Map map = getMapForProperty(property); |
285 |
PropertyHelper propertyHelper) { |
|
|
286 |
Map<String, Object> map = getMapForProperty(property); |
134 |
if (map == null) { |
287 |
if (map == null) { |
135 |
return false; |
288 |
return false; |
136 |
} |
289 |
} |
Lines 138-151
Link Here
|
138 |
return true; |
291 |
return true; |
139 |
} |
292 |
} |
140 |
|
293 |
|
141 |
private Map getMapForProperty(String property) { |
294 |
private Map<String, Object> getMapForProperty(String property) { |
142 |
for (Iterator i = stack.iterator(); i.hasNext();) { |
295 |
for (Iterator<StackEntry> i = stack.iterator(); i.hasNext();) { |
143 |
Map map = (Map) i.next(); |
296 |
StackEntry se = i.next(); |
144 |
if (map.get(property) != null) { |
297 |
if (se.isLocalProperty(property)) { |
145 |
return map; |
298 |
return se.propertyMap; |
146 |
} |
299 |
} |
147 |
} |
300 |
} |
148 |
return null; |
301 |
return null; |
149 |
} |
302 |
} |
150 |
} |
303 |
} |
151 |
|
|
|