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.

ProjectBuilder.java 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // -------------------------------------------------------------------------------
  2. // Copyright (c)2000 Apache Software Foundation
  3. // -------------------------------------------------------------------------------
  4. package org.apache.ant;
  5. import java.io.*;
  6. import java.util.*;
  7. import javax.xml.parsers.*;
  8. import org.xml.sax.*;
  9. /**
  10. * Helper class to build Project object trees.
  11. *
  12. * XXX right now this class only deals with the primary levels (project/target/task)
  13. * and nothing else. Also, it only supports attributes....
  14. *
  15. * @author James Duncan Davidson (duncan@apache.org)
  16. */
  17. public class ProjectBuilder {
  18. // -----------------------------------------------------------------
  19. // PRIVATE MEMBERS
  20. // -----------------------------------------------------------------
  21. /**
  22. *
  23. */
  24. private AntFrontEnd frontEnd;
  25. /**
  26. *
  27. */
  28. private SAXParserFactory parserFactory;
  29. /**
  30. *
  31. */
  32. private TaskManager taskManager;
  33. // -----------------------------------------------------------------
  34. // CONSTRUCTORS
  35. // -----------------------------------------------------------------
  36. /**
  37. * Creates a new project builder that will build projects for the given
  38. * Ant.
  39. */
  40. public ProjectBuilder(AntFrontEnd frontEnd) {
  41. this.frontEnd = frontEnd;
  42. taskManager = new TaskManager(frontEnd);
  43. parserFactory = SAXParserFactory.newInstance();
  44. parserFactory.setValidating(false);
  45. }
  46. // -----------------------------------------------------------------
  47. // PUBLIC METHODS
  48. // -----------------------------------------------------------------
  49. /**
  50. * Builds a project from the given file.
  51. */
  52. public Project buildFromFile(File file) throws AntException {
  53. try {
  54. SAXParser parser = parserFactory.newSAXParser();
  55. BuilderHandlerBase bhb = new BuilderHandlerBase();
  56. bhb.setProjectFileLocation(file);
  57. parser.parse(file, bhb);
  58. Project project = bhb.getProject();
  59. project.setFrontEnd(frontEnd);
  60. return project;
  61. } catch (ParserConfigurationException pce) {
  62. throw new AntException(pce);
  63. } catch (SAXException se) {
  64. Exception e = se.getException();
  65. if (e != null && e instanceof AntException) {
  66. // it's one of our own thrown from inside the parser to stop it
  67. throw (AntException)e;
  68. }
  69. throw new AntException(se);
  70. } catch (IOException ioe) {
  71. throw new AntException(ioe);
  72. }
  73. }
  74. /**
  75. * Returns the TaskManager associated with this ProjectBuilder and
  76. * the projects that it builds
  77. */
  78. public TaskManager getTaskManager() {
  79. return taskManager;
  80. }
  81. // -----------------------------------------------------------------
  82. // INNER CLASSES
  83. // -----------------------------------------------------------------
  84. /**
  85. * Inner class that implements the needed SAX methods to get all the
  86. * data needed out of a build file.
  87. */
  88. class BuilderHandlerBase extends HandlerBase {
  89. private static final int STATE_START = 0;
  90. private static final int STATE_PROJECT = 1;
  91. private static final int STATE_TARGET = 2;
  92. private static final int STATE_TASK = 3;
  93. private static final int STATE_DESCRIPTION = 4;
  94. private static final int STATE_PROPERTY = 5;
  95. private static final int STATE_FINISHED = 99;
  96. private int state = STATE_START;
  97. private Vector tagCharDataStack = new Vector();
  98. private Target currentTarget;
  99. private Task currentTask;
  100. Project project = new Project(frontEnd, taskManager);
  101. Project getProject() {
  102. return project;
  103. }
  104. void setProjectFileLocation(File file) {
  105. project.setBaseDir(file.getParentFile());
  106. }
  107. public void startElement(String name, AttributeList atts) throws SAXException {
  108. StringBuffer tagCharData = new StringBuffer();
  109. tagCharDataStack.insertElementAt(tagCharData, 0);
  110. switch (state) {
  111. case STATE_START:
  112. if (name.equals("project")) {
  113. state = STATE_PROJECT;
  114. String projectName = atts.getValue("name");
  115. if (projectName != null) {
  116. project.setName(projectName);
  117. } else {
  118. String msg = "Project element doesn't contain a name attribute";
  119. AntException ae = new AntException(msg);
  120. throw new SAXException(ae);
  121. }
  122. String defaultTarget = atts.getValue("default");
  123. if (defaultTarget != null) {
  124. project.setDefaultTargetName(defaultTarget);
  125. }
  126. String baseDirName = atts.getValue("basedir");
  127. if (baseDirName != null) {
  128. // XXX need to check to see if base dir exists
  129. project.setBaseDir(new File(baseDirName));
  130. }
  131. } else {
  132. String msg = "Project file doesn't contain a project element as " +
  133. "its root node";
  134. AntException ae = new AntException(msg);
  135. throw new SAXException(ae);
  136. }
  137. break;
  138. case STATE_PROJECT:
  139. // valid tags in a project object are: description, property, and target
  140. if (name.equals("description")) {
  141. state = STATE_DESCRIPTION;
  142. } else if (name.equals("property")) {
  143. state = STATE_PROPERTY;
  144. String propertyName = atts.getValue("name");
  145. String propertyValue = atts.getValue("value");
  146. if (propertyName == null) {
  147. String msg = "Name attribute must be present on property";
  148. AntException ae = new AntException(msg);
  149. throw new SAXException(ae);
  150. } else if (propertyValue == null) {
  151. String msg = "Value attribute must be present on property";
  152. AntException ae = new AntException(msg);
  153. throw new SAXException(ae);
  154. } else {
  155. project.setProperty(propertyName, propertyValue);
  156. }
  157. } else if (name.equals("target")) {
  158. state = STATE_TARGET;
  159. String targetName = atts.getValue("name");
  160. if (targetName != null) {
  161. currentTarget = new Target(targetName);
  162. project.addTarget(currentTarget);
  163. } else {
  164. // XXX figure out which target we're talking about!
  165. // Like a location
  166. String msg = "Target element doesn't contain a name attribute";
  167. AntException ae = new AntException(msg);
  168. throw new SAXException(ae);
  169. }
  170. String depends = atts.getValue("depends");
  171. if (depends != null) {
  172. StringTokenizer tok = new StringTokenizer(depends, ",", false);
  173. while(tok.hasMoreTokens()) {
  174. currentTarget.addDependancy(tok.nextToken().trim());
  175. }
  176. }
  177. // XXX add dependency checks
  178. } else {
  179. System.out.println("Expecting target, got: " + name);
  180. // XXX exception out
  181. }
  182. break;
  183. case STATE_TARGET:
  184. // Valid tags inside target: task
  185. state = STATE_TASK;
  186. //System.out.println("Getting task: " + name + " for target " +
  187. // currentTarget);
  188. // XXX need to validate that task type (name) exists in system
  189. // else exception out.
  190. currentTask = new Task(name);
  191. currentTarget.addTask(currentTask);
  192. for (int i = 0; i < atts.getLength(); i++) {
  193. String atName = atts.getName(i);
  194. String atValue = atts.getValue(i);
  195. currentTask.addAttribute(atName, atValue);
  196. }
  197. break;
  198. case STATE_TASK:
  199. // data in here needs to be reflected into tasks
  200. System.out.println("Not yet supporting tags inside of tasks!");
  201. System.out.println("The project build will probably bust right here");
  202. break;
  203. default:
  204. System.out.println("I'm not sure, but we're off base here: " + name);
  205. // XXX exception out
  206. }
  207. }
  208. public void characters(char ch[], int start, int length) throws SAXException {
  209. StringBuffer buf = (StringBuffer)tagCharDataStack.elementAt(0);
  210. buf.append(ch, start, length);
  211. }
  212. public void endElement(String name) throws SAXException {
  213. StringBuffer elementData = (StringBuffer)tagCharDataStack.elementAt(0);
  214. tagCharDataStack.removeElementAt(0);
  215. switch (state) {
  216. case STATE_TASK:
  217. state = STATE_TARGET;
  218. break;
  219. case STATE_TARGET:
  220. if (name.equals("target")) {
  221. state = STATE_PROJECT;
  222. } else {
  223. System.out.println("Expecting to get an end of target, got: " + name);
  224. // XXX exception out.
  225. }
  226. break;
  227. case STATE_DESCRIPTION:
  228. if (name.equals("description")) {
  229. state = STATE_PROJECT;
  230. project.setDescription(elementData.toString().trim());
  231. } else {
  232. System.out.println("Expecting to get an end of description, got: " +
  233. name);
  234. // XXX exception out.
  235. }
  236. break;
  237. case STATE_PROPERTY:
  238. if (name.equals("property")) {
  239. state = STATE_PROJECT;
  240. } else {
  241. System.out.println("Expecting to get end of property, got: " + name);
  242. // XXX exception out
  243. }
  244. break;
  245. case STATE_PROJECT:
  246. if (name.equals("project")) {
  247. state = STATE_FINISHED;
  248. } else {
  249. System.out.println("Expecting to get end of project, got: " + name);
  250. // XXX exception out;
  251. }
  252. break;
  253. default:
  254. System.out.println("I'm not sure what we are ending here: " + name);
  255. // XXX exception out;
  256. }
  257. }
  258. }
  259. }