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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // -------------------------------------------------------------------------------
  2. // Copyright (c)2000 Apache Software Foundation
  3. // -------------------------------------------------------------------------------
  4. package org.apache.ant;
  5. import java.io.*;
  6. import javax.xml.parsers.*;
  7. import org.xml.sax.*;
  8. /**
  9. * Helper class to build Project object trees.
  10. *
  11. * XXX right now this class only deals with the primary levels (project/target/task)
  12. * and nothing else. Also, it only supports attributes....
  13. *
  14. * @author James Duncan Davidson (duncan@apache.org)
  15. */
  16. class ProjectBuilder {
  17. private SAXParserFactory parserFactory;
  18. // -----------------------------------------------------------------
  19. // CONSTRUCTORS
  20. // -----------------------------------------------------------------
  21. ProjectBuilder() {
  22. parserFactory = SAXParserFactory.newInstance();
  23. parserFactory.setValidating(false);
  24. }
  25. Project buildFromFile(File file) throws AntException {
  26. try {
  27. SAXParser parser = parserFactory.newSAXParser();
  28. BuilderHandlerBase bhb = new BuilderHandlerBase();
  29. parser.parse(file, bhb);
  30. return bhb.getProject();
  31. } catch (ParserConfigurationException pce) {
  32. throw new AntException(pce.getMessage());
  33. } catch (SAXException se) {
  34. System.out.println(se);
  35. System.out.println(se.getMessage());
  36. throw new AntException(se.getMessage());
  37. } catch (IOException ioe) {
  38. throw new AntException(ioe.getMessage());
  39. }
  40. }
  41. class BuilderHandlerBase extends HandlerBase {
  42. private static final int STATE_START = 0;
  43. private static final int STATE_PROJECT = 1;
  44. private static final int STATE_TARGET = 2;
  45. private static final int STATE_TASK = 3;
  46. private static final int STATE_FINISHED = 99;
  47. private int state = STATE_START;
  48. private Target currentTarget;
  49. private Task currentTask;
  50. Project project = new Project();
  51. Project getProject() {
  52. return project;
  53. }
  54. public void startElement(String name, AttributeList atts) throws SAXException {
  55. //System.out.println("element: " + name);
  56. switch (state) {
  57. case STATE_START:
  58. if (name.equals("project")) {
  59. state = STATE_PROJECT;
  60. String projectName = atts.getValue("name");
  61. if (projectName == null) {
  62. System.out.println("Projects *must* have names");
  63. // XXX exception out
  64. }
  65. project.setName(projectName);
  66. } else {
  67. System.out.println("Expecting project, got: " + name);
  68. // XXX exception out
  69. }
  70. break;
  71. case STATE_PROJECT:
  72. if (name.equals("target")) {
  73. state = STATE_TARGET;
  74. String targetName = atts.getValue("name");
  75. if (targetName == null) {
  76. System.out.println("Targets *must* have names");
  77. // XXX exception out
  78. }
  79. currentTarget = new Target(targetName);
  80. project.addTarget(currentTarget);
  81. // XXX add dependency checks
  82. } else {
  83. System.out.println("Expecting target, got: " + name);
  84. // XXX exception out
  85. }
  86. break;
  87. case STATE_TARGET:
  88. state = STATE_TASK;
  89. //System.out.println("Getting task: " + name + " for target " +
  90. // currentTarget);
  91. // XXX need to validate that task type (name) exists in system
  92. // else exception out.
  93. currentTask = new Task(name);
  94. currentTarget.addTask(currentTask);
  95. for (int i = 0; i < atts.getLength(); i++) {
  96. String atName = atts.getName(i);
  97. String atValue = atts.getValue(i);
  98. currentTask.addAttribute(atName, atValue);
  99. }
  100. break;
  101. default:
  102. System.out.println("I'm not sure, but we're off base here: " + name);
  103. // XXX exception out
  104. }
  105. }
  106. public void characters(char ch[], int start, int length) throws SAXException {
  107. }
  108. public void endElement(String name) throws SAXException {
  109. // System.out.println("end: " + name);
  110. switch (state) {
  111. case STATE_TASK:
  112. state = STATE_TARGET;
  113. break;
  114. case STATE_TARGET:
  115. if (name.equals("target")) {
  116. state = STATE_PROJECT;
  117. } else {
  118. System.out.println("Expecting to get an end of target, got: " + name);
  119. // XXX exception out.
  120. }
  121. break;
  122. case STATE_PROJECT:
  123. if (name.equals("project")) {
  124. state = STATE_FINISHED;
  125. } else {
  126. System.out.println("Expecting to get end of project, got: " + name);
  127. // XXX exception out;
  128. }
  129. break;
  130. default:
  131. System.out.println("I'm not sure what we are ending here: " + name);
  132. // XXX exception out;
  133. }
  134. }
  135. }
  136. }