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 12 kB

11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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.apache.tools.ant.BuildException;
  28. import org.apache.tools.ant.Location;
  29. import org.apache.tools.ant.Project;
  30. import org.apache.tools.ant.RuntimeConfigurable;
  31. import org.apache.tools.ant.Target;
  32. import org.apache.tools.ant.util.FileUtils;
  33. import org.xml.sax.Attributes;
  34. import org.xml.sax.Locator;
  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<Target> targetVector = new Vector<Target>();
  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<RuntimeConfigurable> 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<String, List<String>> prefixMapping = new HashMap<String, List<String>>();
  89. /** Keeps track of targets in files */
  90. private Map<String, Target> 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. if (buildFile != null) {
  108. this.buildFileParent = new File(buildFile.getParent());
  109. implicitTarget.setLocation(new Location(buildFile.getAbsolutePath()));
  110. try {
  111. setBuildFile(FileUtils.getFileUtils().getFileURL(buildFile));
  112. } catch (MalformedURLException ex) {
  113. throw new BuildException(ex);
  114. }
  115. } else {
  116. this.buildFileParent = null;
  117. }
  118. }
  119. /**
  120. * sets the build file to which the XML context belongs
  121. * @param buildFile Ant build file
  122. * @throws MalformedURLException if parent URL cannot be constructed
  123. * @since Ant 1.8.0
  124. */
  125. public void setBuildFile(URL buildFile) throws MalformedURLException {
  126. this.buildFileURL = buildFile;
  127. this.buildFileParentURL = new URL(buildFile, ".");
  128. if (implicitTarget.getLocation() == null) {
  129. implicitTarget.setLocation(new Location(buildFile.toString()));
  130. }
  131. }
  132. /**
  133. * find out the build file
  134. * @return the build file to which the XML context belongs
  135. */
  136. public File getBuildFile() {
  137. return buildFile;
  138. }
  139. /**
  140. * find out the parent build file of this build file
  141. * @return the parent build file of this build file
  142. */
  143. public File getBuildFileParent() {
  144. return buildFileParent;
  145. }
  146. /**
  147. * find out the build file
  148. * @return the build file to which the xml context belongs
  149. * @since Ant 1.8.0
  150. */
  151. public URL getBuildFileURL() {
  152. return buildFileURL;
  153. }
  154. /**
  155. * find out the parent build file of this build file
  156. * @return the parent build file of this build file
  157. * @since Ant 1.8.0
  158. */
  159. public URL getBuildFileParentURL() {
  160. return buildFileParentURL;
  161. }
  162. /**
  163. * find out the project to which this antxml context belongs
  164. * @return project
  165. */
  166. public Project getProject() {
  167. return project;
  168. }
  169. /**
  170. * find out the current project name
  171. * @return current project name
  172. */
  173. public String getCurrentProjectName() {
  174. return currentProjectName;
  175. }
  176. /**
  177. * set the name of the current project
  178. * @param name name of the current project
  179. */
  180. public void setCurrentProjectName(String name) {
  181. this.currentProjectName = name;
  182. }
  183. /**
  184. * get the current runtime configurable wrapper
  185. * can return null
  186. * @return runtime configurable wrapper
  187. */
  188. public RuntimeConfigurable currentWrapper() {
  189. if (wStack.size() < 1) {
  190. return null;
  191. }
  192. return (RuntimeConfigurable) wStack.elementAt(wStack.size() - 1);
  193. }
  194. /**
  195. * get the runtime configurable wrapper of the parent project
  196. * can return null
  197. * @return runtime configurable wrapper of the parent project
  198. */
  199. public RuntimeConfigurable parentWrapper() {
  200. if (wStack.size() < 2) {
  201. return null;
  202. }
  203. return (RuntimeConfigurable) wStack.elementAt(wStack.size() - 2);
  204. }
  205. /**
  206. * add a runtime configurable wrapper to the internal stack
  207. * @param wrapper runtime configurable wrapper
  208. */
  209. public void pushWrapper(RuntimeConfigurable wrapper) {
  210. wStack.addElement(wrapper);
  211. }
  212. /**
  213. * remove a runtime configurable wrapper from the stack
  214. */
  215. public void popWrapper() {
  216. if (wStack.size() > 0) {
  217. wStack.removeElementAt(wStack.size() - 1);
  218. }
  219. }
  220. /**
  221. * access the stack of wrappers
  222. * @return the stack of wrappers
  223. */
  224. public Vector<RuntimeConfigurable> getWrapperStack() {
  225. return wStack;
  226. }
  227. /**
  228. * add a new target
  229. * @param target target to add
  230. */
  231. public void addTarget(Target target) {
  232. targetVector.addElement(target);
  233. currentTarget = target;
  234. }
  235. /**
  236. * get the current target
  237. * @return current target
  238. */
  239. public Target getCurrentTarget() {
  240. return currentTarget;
  241. }
  242. /**
  243. * get the implicit target
  244. * @return implicit target
  245. */
  246. public Target getImplicitTarget() {
  247. return implicitTarget;
  248. }
  249. /**
  250. * sets the current target
  251. * @param target current target
  252. */
  253. public void setCurrentTarget(Target target) {
  254. this.currentTarget = target;
  255. }
  256. /**
  257. * sets the implicit target
  258. * @param target the implicit target
  259. */
  260. public void setImplicitTarget(Target target) {
  261. this.implicitTarget = target;
  262. }
  263. /**
  264. * access the vector of targets
  265. * @return vector of targets
  266. */
  267. public Vector<Target> getTargets() {
  268. return targetVector;
  269. }
  270. /**
  271. * Scans an attribute list for the <code>id</code> attribute and
  272. * stores a reference to the target object in the project if an
  273. * id is found.
  274. * <p>
  275. * This method was moved out of the configure method to allow
  276. * it to be executed at parse time.
  277. * @param element the current element
  278. * @param attr attributes of the current element
  279. */
  280. public void configureId(Object element, Attributes attr) {
  281. String id = attr.getValue("id");
  282. if (id != null) {
  283. project.addIdReference(id, element);
  284. }
  285. }
  286. /**
  287. * access the locator
  288. * @return locator
  289. */
  290. public Locator getLocator() {
  291. return locator;
  292. }
  293. /**
  294. * sets the locator
  295. * @param locator locator
  296. */
  297. public void setLocator(Locator locator) {
  298. this.locator = locator;
  299. }
  300. /**
  301. * tells whether the project tag is being ignored
  302. * @return whether the project tag is being ignored
  303. */
  304. public boolean isIgnoringProjectTag() {
  305. return ignoreProjectTag;
  306. }
  307. /**
  308. * sets the flag to ignore the project tag
  309. * @param flag to ignore the project tag
  310. */
  311. public void setIgnoreProjectTag(boolean flag) {
  312. this.ignoreProjectTag = flag;
  313. }
  314. /**
  315. * Called during parsing, stores the prefix to uri mapping.
  316. *
  317. * @param prefix a namespace prefix
  318. * @param uri a namespace uri
  319. */
  320. public void startPrefixMapping(String prefix, String uri) {
  321. List<String> list = prefixMapping.get(prefix);
  322. if (list == null) {
  323. list = new ArrayList<String>();
  324. prefixMapping.put(prefix, list);
  325. }
  326. list.add(uri);
  327. }
  328. /**
  329. * End of prefix to uri mapping.
  330. *
  331. * @param prefix the namespace prefix
  332. */
  333. public void endPrefixMapping(String prefix) {
  334. List<String> list = prefixMapping.get(prefix);
  335. if (list == null || list.size() == 0) {
  336. return; // Should not happen
  337. }
  338. list.remove(list.size() - 1);
  339. }
  340. /**
  341. * prefix to namespace uri mapping
  342. *
  343. * @param prefix the prefix to map
  344. * @return the uri for this prefix, null if not present
  345. */
  346. public String getPrefixMapping(String prefix) {
  347. List<String> list = prefixMapping.get(prefix);
  348. if (list == null || list.size() == 0) {
  349. return null;
  350. }
  351. return (String) list.get(list.size() - 1);
  352. }
  353. /**
  354. * Get the targets in the current source file.
  355. * @return the current targets.
  356. */
  357. public Map<String, Target> getCurrentTargets() {
  358. return currentTargets;
  359. }
  360. /**
  361. * Set the map of the targets in the current source file.
  362. * @param currentTargets a map of targets.
  363. */
  364. public void setCurrentTargets(Map<String, Target> currentTargets) {
  365. this.currentTargets = currentTargets;
  366. }
  367. }