KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > helper > AntXMLContext


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.helper;
19
20 import java.io.File JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import org.xml.sax.Locator JavaDoc;
28 import org.xml.sax.Attributes JavaDoc;
29
30
31 import org.apache.tools.ant.Project;
32 import org.apache.tools.ant.Target;
33 import org.apache.tools.ant.Location;
34 import org.apache.tools.ant.RuntimeConfigurable;
35
36
37 /**
38  * Context information for the ant processing.
39  *
40  */

41 public class AntXMLContext {
42     /** The project to configure. */
43     private Project project;
44
45     /** The configuration file to parse. */
46     private File JavaDoc buildFile;
47
48     /** Vector with all the targets, in the order they are
49      * defined. Project maintains a Hashtable, which is not ordered.
50      * This will allow description to know the original order.
51      */

52     private Vector JavaDoc targetVector = new Vector JavaDoc();
53
54     /**
55      * Parent directory of the build file. Used for resolving entities
56      * and setting the project's base directory.
57      */

58     private File JavaDoc buildFileParent;
59
60     /** Name of the current project */
61     private String JavaDoc currentProjectName;
62
63     /**
64      * Locator for the configuration file parser.
65      * Used for giving locations of errors etc.
66      */

67     private Locator JavaDoc locator;
68
69      /**
70       * Target that all other targets will depend upon implicitly.
71       *
72       * <p>This holds all tasks and data type definitions that have
73       * been placed outside of targets.</p>
74       */

75     private Target implicitTarget = new Target();
76
77     /** Current target ( no need for a stack as the processing model
78         allows only one level of target ) */

79     private Target currentTarget = null;
80
81     /** The stack of RuntimeConfigurable2 wrapping the
82         objects.
83     */

84     private Vector JavaDoc wStack = new Vector JavaDoc();
85
86     /**
87      * Indicates whether the project tag attributes are to be ignored
88      * when processing a particular build file.
89      */

90     private boolean ignoreProjectTag = false;
91
92     /** Keeps track of prefix -> uri mapping during parsing */
93     private Map JavaDoc prefixMapping = new HashMap JavaDoc();
94
95
96     /** Keeps track of targets in files */
97     private Map JavaDoc currentTargets = null;
98
99     /**
100      * constructor
101      * @param project the project to which this antxml context belongs to
102      */

103     public AntXMLContext(Project project) {
104         this.project = project;
105         implicitTarget.setProject(project);
106         implicitTarget.setName("");
107         targetVector.addElement(implicitTarget);
108     }
109
110     /**
111      * sets the build file to which the XML context belongs
112      * @param buildFile ant build file
113      */

114     public void setBuildFile(File JavaDoc buildFile) {
115         this.buildFile = buildFile;
116         this.buildFileParent = new File JavaDoc(buildFile.getParent());
117         implicitTarget.setLocation(new Location(buildFile.getAbsolutePath()));
118     }
119
120     /**
121      * find out the build file
122      * @return the build file to which the xml context belongs
123      */

124     public File JavaDoc getBuildFile() {
125         return buildFile;
126     }
127
128     /**
129      * find out the parent build file of this build file
130      * @return the parent build file of this build file
131      */

132     public File JavaDoc getBuildFileParent() {
133         return buildFileParent;
134     }
135
136     /**
137      * find out the project to which this antxml context belongs
138      * @return project
139      */

140     public Project getProject() {
141         return project;
142     }
143
144     /**
145      * find out the current project name
146      * @return current project name
147      */

148     public String JavaDoc getCurrentProjectName() {
149         return currentProjectName;
150     }
151
152     /**
153      * set the name of the current project
154      * @param name name of the current project
155      */

156     public void setCurrentProjectName(String JavaDoc name) {
157         this.currentProjectName = name;
158     }
159
160     /**
161      * get the current runtime configurable wrapper
162      * can return null
163      * @return runtime configurable wrapper
164      */

165     public RuntimeConfigurable currentWrapper() {
166         if (wStack.size() < 1) {
167             return null;
168         }
169         return (RuntimeConfigurable) wStack.elementAt(wStack.size() - 1);
170     }
171
172     /**
173      * get the runtime configurable wrapper of the parent project
174      * can return null
175      * @return runtime configurable wrapper of the parent project
176      */

177     public RuntimeConfigurable parentWrapper() {
178         if (wStack.size() < 2) {
179             return null;
180         }
181         return (RuntimeConfigurable) wStack.elementAt(wStack.size() - 2);
182     }
183
184     /**
185      * add a runtime configurable wrapper to the internal stack
186      * @param wrapper runtime configurable wrapper
187      */

188     public void pushWrapper(RuntimeConfigurable wrapper) {
189         wStack.addElement(wrapper);
190     }
191
192     /**
193      * remove a runtime configurable wrapper from the stack
194      */

195     public void popWrapper() {
196         if (wStack.size() > 0) {
197             wStack.removeElementAt(wStack.size() - 1);
198         }
199     }
200
201     /**
202      * access the stack of wrappers
203      * @return the stack of wrappers
204      */

205     public Vector JavaDoc getWrapperStack() {
206         return wStack;
207     }
208
209     /**
210      * add a new target
211      * @param target target to add
212      */

213     public void addTarget(Target target) {
214         targetVector.addElement(target);
215         currentTarget = target;
216     }
217
218     /**
219      * get the current target
220      * @return current target
221      */

222     public Target getCurrentTarget() {
223         return currentTarget;
224     }
225
226     /**
227      * get the implicit target
228      * @return implicit target
229      */

230     public Target getImplicitTarget() {
231         return implicitTarget;
232     }
233
234     /**
235      * sets the current target
236      * @param target current target
237      */

238     public void setCurrentTarget(Target target) {
239         this.currentTarget = target;
240     }
241
242     /**
243      * sets the implicit target
244      * @param target the implicit target
245      */

246     public void setImplicitTarget(Target target) {
247         this.implicitTarget = target;
248     }
249
250     /**
251      * access the vector of targets
252      * @return vector of targets
253      */

254     public Vector JavaDoc getTargets() {
255         return targetVector;
256     }
257
258     /**
259      * Scans an attribute list for the <code>id</code> attribute and
260      * stores a reference to the target object in the project if an
261      * id is found.
262      * <p>
263      * This method was moved out of the configure method to allow
264      * it to be executed at parse time.
265      * @param element the current element
266      * @param attr attributes of the current element
267      */

268     public void configureId(Object JavaDoc element, Attributes JavaDoc attr) {
269         String JavaDoc id = attr.getValue("id");
270         if (id != null) {
271             project.addIdReference(id, element);
272         }
273     }
274
275     /**
276      * access the locator
277      * @return locator
278      */

279     public Locator JavaDoc getLocator() {
280         return locator;
281     }
282
283     /**
284      * sets the locator
285      * @param locator locator
286      */

287     public void setLocator(Locator JavaDoc locator) {
288         this.locator = locator;
289     }
290
291     /**
292      * tells whether the project tag is being ignored
293      * @return whether the project tag is being ignored
294      */

295     public boolean isIgnoringProjectTag() {
296         return ignoreProjectTag;
297     }
298
299     /**
300      * sets the flag to ignore the project tag
301      * @param flag to ignore the project tag
302      */

303     public void setIgnoreProjectTag(boolean flag) {
304         this.ignoreProjectTag = flag;
305     }
306
307     /**
308      * Called during parsing, stores the prefix to uri mapping.
309      *
310      * @param prefix a namespace prefix
311      * @param uri a namespace uri
312      */

313     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) {
314         List JavaDoc list = (List JavaDoc) prefixMapping.get(prefix);
315         if (list == null) {
316             list = new ArrayList JavaDoc();
317             prefixMapping.put(prefix, list);
318         }
319         list.add(uri);
320     }
321
322     /**
323      * End of prefix to uri mapping.
324      *
325      * @param prefix the namespace prefix
326      */

327     public void endPrefixMapping(String JavaDoc prefix) {
328         List JavaDoc list = (List JavaDoc) prefixMapping.get(prefix);
329         if (list == null || list.size() == 0) {
330             return; // Should not happen
331
}
332         list.remove(list.size() - 1);
333     }
334
335     /**
336      * prefix to namespace uri mapping
337      *
338      * @param prefix the prefix to map
339      * @return the uri for this prefix, null if not present
340      */

341     public String JavaDoc getPrefixMapping(String JavaDoc prefix) {
342         List JavaDoc list = (List JavaDoc) prefixMapping.get(prefix);
343         if (list == null || list.size() == 0) {
344             return null;
345         }
346         return (String JavaDoc) list.get(list.size() - 1);
347     }
348
349     /**
350      * Get the targets in the current source file.
351      * @return the current targets.
352      */

353     public Map JavaDoc getCurrentTargets() {
354         return currentTargets;
355     }
356
357     /**
358      * Set the map of the targets in the current source file.
359      * @param currentTargets a map of targets.
360      */

361     public void setCurrentTargets(Map JavaDoc currentTargets) {
362         this.currentTargets = currentTargets;
363     }
364
365 }
366
367
368
Popular Tags