1 | /*
|
---|
2 | * Copyright 2003-2005 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 | */
|
---|
17 | package org.apache.tools.ant.helper;
|
---|
18 |
|
---|
19 | import java.io.File;
|
---|
20 | import java.util.ArrayList;
|
---|
21 | import java.util.HashMap;
|
---|
22 | import java.util.List;
|
---|
23 | import java.util.Map;
|
---|
24 | import java.util.Vector;
|
---|
25 |
|
---|
26 | import org.xml.sax.Locator;
|
---|
27 | import org.xml.sax.Attributes;
|
---|
28 |
|
---|
29 |
|
---|
30 | import org.apache.tools.ant.Project;
|
---|
31 | import org.apache.tools.ant.Target;
|
---|
32 | import org.apache.tools.ant.Location;
|
---|
33 | import org.apache.tools.ant.RuntimeConfigurable;
|
---|
34 |
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Context information for the ant processing.
|
---|
38 | *
|
---|
39 | */
|
---|
40 | public class AntXMLContext {
|
---|
41 | /** The project to configure. */
|
---|
42 | private Project project;
|
---|
43 |
|
---|
44 | /** The configuration file to parse. */
|
---|
45 | private File buildFile;
|
---|
46 |
|
---|
47 | /** Vector with all the targets, in the order they are
|
---|
48 | * defined. Project maintains a Hashtable, which is not ordered.
|
---|
49 | * This will allow description to know the original order.
|
---|
50 | */
|
---|
51 | private Vector targetVector = new Vector();
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Parent directory of the build file. Used for resolving entities
|
---|
55 | * and setting the project's base directory.
|
---|
56 | */
|
---|
57 | private File buildFileParent;
|
---|
58 |
|
---|
59 | /** Name of the current project */
|
---|
60 | private String currentProjectName;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Locator for the configuration file parser.
|
---|
64 | * Used for giving locations of errors etc.
|
---|
65 | */
|
---|
66 | private Locator locator;
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Target that all other targets will depend upon implicitly.
|
---|
70 | *
|
---|
71 | * <p>This holds all tasks and data type definitions that have
|
---|
72 | * been placed outside of targets.</p>
|
---|
73 | */
|
---|
74 | private Target implicitTarget = new Target();
|
---|
75 |
|
---|
76 | /** Current target ( no need for a stack as the processing model
|
---|
77 | allows only one level of target ) */
|
---|
78 | private Target currentTarget = null;
|
---|
79 |
|
---|
80 | /** The stack of RuntimeConfigurable2 wrapping the
|
---|
81 | objects.
|
---|
82 | */
|
---|
83 | private Vector wStack = new Vector();
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Indicates whether the project tag attributes are to be ignored
|
---|
87 | * when processing a particular build file.
|
---|
88 | */
|
---|
89 | private boolean ignoreProjectTag = false;
|
---|
90 |
|
---|
91 | /** Keeps track of prefix -> uri mapping during parsing */
|
---|
92 | private Map prefixMapping = new HashMap();
|
---|
93 |
|
---|
94 |
|
---|
95 | /** Keeps track of targets in files */
|
---|
96 | private Map currentTargets = null;
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * constructor
|
---|
100 | * @param project the project to which this antxml context belongs to
|
---|
101 | */
|
---|
102 | public AntXMLContext(Project project) {
|
---|
103 | this.project = project;
|
---|
104 | implicitTarget.setProject(project);
|
---|
105 | implicitTarget.setName("");
|
---|
106 | targetVector.addElement(implicitTarget);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * sets the build file to which the XML context belongs
|
---|
111 | * @param buildFile ant build file
|
---|
112 | */
|
---|
113 | public void setBuildFile(File buildFile) {
|
---|
114 | this.buildFile = buildFile;
|
---|
115 | this.buildFileParent = new File(buildFile.getParent());
|
---|
116 | implicitTarget.setLocation(new Location(buildFile.getAbsolutePath()));
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * find out the build file
|
---|
121 | * @return the build file to which the xml context belongs
|
---|
122 | */
|
---|
123 | public File getBuildFile() {
|
---|
124 | return buildFile;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * find out the parent build file of this build file
|
---|
129 | * @return the parent build file of this build file
|
---|
130 | */
|
---|
131 | public File getBuildFileParent() {
|
---|
132 | return buildFileParent;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * find out the project to which this antxml context belongs
|
---|
137 | * @return project
|
---|
138 | */
|
---|
139 | public Project getProject() {
|
---|
140 | return project;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * find out the current project name
|
---|
145 | * @return current project name
|
---|
146 | */
|
---|
147 | public String getCurrentProjectName() {
|
---|
148 | return currentProjectName;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * set the name of the current project
|
---|
153 | * @param name name of the current project
|
---|
154 | */
|
---|
155 | public void setCurrentProjectName(String name) {
|
---|
156 | this.currentProjectName = name;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * get the current runtime configurable wrapper
|
---|
161 | * can return null
|
---|
162 | * @return runtime configurable wrapper
|
---|
163 | */
|
---|
164 | public RuntimeConfigurable currentWrapper() {
|
---|
165 | if (wStack.size() < 1) {
|
---|
166 | return null;
|
---|
167 | }
|
---|
168 | return (RuntimeConfigurable) wStack.elementAt(wStack.size() - 1);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * get the runtime configurable wrapper of the parent project
|
---|
173 | * can return null
|
---|
174 | * @return runtime configurable wrapper of the parent project
|
---|
175 | */
|
---|
176 | public RuntimeConfigurable parentWrapper() {
|
---|
177 | if (wStack.size() < 2) {
|
---|
178 | return null;
|
---|
179 | }
|
---|
180 | return (RuntimeConfigurable) wStack.elementAt(wStack.size() - 2);
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * add a runtime configurable wrapper to the internal stack
|
---|
185 | * @param wrapper runtime configurable wrapper
|
---|
186 | */
|
---|
187 | public void pushWrapper(RuntimeConfigurable wrapper) {
|
---|
188 | wStack.addElement(wrapper);
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * remove a runtime configurable wrapper from the stack
|
---|
193 | */
|
---|
194 | public void popWrapper() {
|
---|
195 | if (wStack.size() > 0) {
|
---|
196 | wStack.removeElementAt(wStack.size() - 1);
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * access the stack of wrappers
|
---|
202 | * @return the stack of wrappers
|
---|
203 | */
|
---|
204 | public Vector getWrapperStack() {
|
---|
205 | return wStack;
|
---|
206 | }
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * add a new target
|
---|
210 | * @param target target to add
|
---|
211 | */
|
---|
212 | public void addTarget(Target target) {
|
---|
213 | targetVector.addElement(target);
|
---|
214 | currentTarget = target;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * get the current target
|
---|
219 | * @return current target
|
---|
220 | */
|
---|
221 | public Target getCurrentTarget() {
|
---|
222 | return currentTarget;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * get the implicit target
|
---|
227 | * @return implicit target
|
---|
228 | */
|
---|
229 | public Target getImplicitTarget() {
|
---|
230 | return implicitTarget;
|
---|
231 | }
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * sets the current target
|
---|
235 | * @param target current target
|
---|
236 | */
|
---|
237 | public void setCurrentTarget(Target target) {
|
---|
238 | this.currentTarget = target;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * sets the implicit target
|
---|
243 | * @param target the implicit target
|
---|
244 | */
|
---|
245 | public void setImplicitTarget(Target target) {
|
---|
246 | this.implicitTarget = target;
|
---|
247 | }
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * access the vector of targets
|
---|
251 | * @return vector of targets
|
---|
252 | */
|
---|
253 | public Vector getTargets() {
|
---|
254 | return targetVector;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Scans an attribute list for the <code>id</code> attribute and
|
---|
259 | * stores a reference to the target object in the project if an
|
---|
260 | * id is found.
|
---|
261 | * <p>
|
---|
262 | * This method was moved out of the configure method to allow
|
---|
263 | * it to be executed at parse time.
|
---|
264 | * @param element the current element
|
---|
265 | * @param attr attributes of the current element
|
---|
266 | */
|
---|
267 | public void configureId(Object element, Attributes attr) {
|
---|
268 | String id = attr.getValue("id");
|
---|
269 | if (id != null) {
|
---|
270 | project.addReference(id, element);
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * access the locator
|
---|
276 | * @return locator
|
---|
277 | */
|
---|
278 | public Locator getLocator() {
|
---|
279 | return locator;
|
---|
280 | }
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * sets the locator
|
---|
284 | * @param locator locator
|
---|
285 | */
|
---|
286 | public void setLocator(Locator locator) {
|
---|
287 | this.locator = locator;
|
---|
288 | }
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * tells whether the project tag is being ignored
|
---|
292 | * @return whether the project tag is being ignored
|
---|
293 | */
|
---|
294 | public boolean isIgnoringProjectTag() {
|
---|
295 | return ignoreProjectTag;
|
---|
296 | }
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * sets the flag to ignore the project tag
|
---|
300 | * @param flag to ignore the project tag
|
---|
301 | */
|
---|
302 | public void setIgnoreProjectTag(boolean flag) {
|
---|
303 | this.ignoreProjectTag = flag;
|
---|
304 | }
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * Called during parsing, stores the prefix to uri mapping.
|
---|
308 | *
|
---|
309 | * @param prefix a namespace prefix
|
---|
310 | * @param uri a namespace uri
|
---|
311 | */
|
---|
312 | public void startPrefixMapping(String prefix, String uri) {
|
---|
313 | List list = (List) prefixMapping.get(prefix);
|
---|
314 | if (list == null) {
|
---|
315 | list = new ArrayList();
|
---|
316 | prefixMapping.put(prefix, list);
|
---|
317 | }
|
---|
318 | list.add(uri);
|
---|
319 | }
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * End of prefix to uri mapping.
|
---|
323 | *
|
---|
324 | * @param prefix the namespace prefix
|
---|
325 | */
|
---|
326 | public void endPrefixMapping(String prefix) {
|
---|
327 | List list = (List) prefixMapping.get(prefix);
|
---|
328 | if (list == null || list.size() == 0) {
|
---|
329 | return; // Should not happen
|
---|
330 | }
|
---|
331 | list.remove(list.size() - 1);
|
---|
332 | }
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * prefix to namespace uri mapping
|
---|
336 | *
|
---|
337 | * @param prefix the prefix to map
|
---|
338 | * @return the uri for this prefix, null if not present
|
---|
339 | */
|
---|
340 | public String getPrefixMapping(String prefix) {
|
---|
341 | List list = (List) prefixMapping.get(prefix);
|
---|
342 | if (list == null || list.size() == 0) {
|
---|
343 | return null;
|
---|
344 | }
|
---|
345 | return (String) list.get(list.size() - 1);
|
---|
346 | }
|
---|
347 |
|
---|
348 | /**
|
---|
349 | * Get the targets in the current source file.
|
---|
350 | * @return the current targets.
|
---|
351 | */
|
---|
352 | public Map getCurrentTargets() {
|
---|
353 | return currentTargets;
|
---|
354 | }
|
---|
355 |
|
---|
356 | /**
|
---|
357 | * Set the map of the targets in the current source file.
|
---|
358 | * @param currentTargets a map of targets.
|
---|
359 | */
|
---|
360 | public void setCurrentTargets(Map currentTargets) {
|
---|
361 | this.currentTargets = currentTargets;
|
---|
362 | }
|
---|
363 |
|
---|
364 | }
|
---|
365 |
|
---|
366 |
|
---|