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

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