You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

AntXMLContext.java 11 kB

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