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.

ProjectHelperImpl2.java 46 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.helper;
  55. import org.apache.tools.ant.*;
  56. import java.io.File;
  57. import java.io.FileInputStream;
  58. import java.io.FileNotFoundException;
  59. import java.io.IOException;
  60. import java.util.Hashtable;
  61. import java.util.Vector;
  62. import java.util.Enumeration;
  63. import java.util.Locale;
  64. import org.xml.sax.Locator;
  65. import org.xml.sax.InputSource;
  66. //import org.xml.sax.HandlerBase;
  67. import org.xml.sax.SAXParseException;
  68. import org.xml.sax.XMLReader;
  69. import org.xml.sax.SAXException;
  70. import org.xml.sax.DocumentHandler;
  71. import org.xml.sax.AttributeList;
  72. import org.xml.sax.helpers.XMLReaderAdapter;
  73. import org.xml.sax.helpers.DefaultHandler;
  74. import javax.xml.parsers.SAXParserFactory;
  75. import javax.xml.parsers.SAXParser;
  76. import javax.xml.parsers.ParserConfigurationException;
  77. /**
  78. * Original helper.
  79. *
  80. * @author duncan@x180.com
  81. */
  82. public class ProjectHelperImpl2 extends ProjectHelper {
  83. /**
  84. * Parser factory to use to create parsers.
  85. * @see #getParserFactory
  86. */
  87. private static SAXParserFactory parserFactory = null;
  88. /**
  89. * SAX 2 style parser used to parse the given file.
  90. */
  91. // private org.xml.sax.Parser parser;
  92. private org.xml.sax.XMLReader parser;
  93. /** The project to configure. */
  94. private Project project;
  95. /** The configuration file to parse. */
  96. private File buildFile;
  97. /**
  98. * Parent directory of the build file. Used for resolving entities
  99. * and setting the project's base directory.
  100. */
  101. private File buildFileParent;
  102. /**
  103. * Locator for the configuration file parser.
  104. * Used for giving locations of errors etc.
  105. */
  106. private Locator locator;
  107. /**
  108. * Parses the project file, configuring the project as it goes.
  109. *
  110. * @exception BuildException if the configuration is invalid or cannot
  111. * be read
  112. */
  113. public void parse(Project project, Object source) throws BuildException {
  114. if( ! (source instanceof File) )
  115. throw new BuildException( "Only File source supported by default plugin" );
  116. File buildFile=(File)source;
  117. FileInputStream inputStream = null;
  118. InputSource inputSource = null;
  119. this.project = project;
  120. this.buildFile = new File(buildFile.getAbsolutePath());
  121. buildFileParent = new File(this.buildFile.getParent());
  122. try {
  123. SAXParser saxParser = getParserFactory().newSAXParser();
  124. parser =saxParser.getXMLReader();
  125. String uri = "file:" + buildFile.getAbsolutePath().replace('\\', '/');
  126. for (int index = uri.indexOf('#'); index != -1; index = uri.indexOf('#')) {
  127. uri = uri.substring(0, index) + "%23" + uri.substring(index+1);
  128. }
  129. inputStream = new FileInputStream(buildFile);
  130. inputSource = new InputSource(inputStream);
  131. inputSource.setSystemId(uri);
  132. project.log("parsing buildfile " + buildFile + " with URI = " + uri, Project.MSG_VERBOSE);
  133. DefaultHandler hb = new RootHandler();
  134. parser.setContentHandler(hb);
  135. parser.setEntityResolver(hb);
  136. parser.setErrorHandler(hb);
  137. parser.setDTDHandler(hb);
  138. parser.parse(inputSource);
  139. }
  140. catch(ParserConfigurationException exc) {
  141. throw new BuildException("Parser has not been configured correctly", exc);
  142. }
  143. catch(SAXParseException exc) {
  144. Location location =
  145. new Location(buildFile.toString(), exc.getLineNumber(), exc.getColumnNumber());
  146. Throwable t = exc.getException();
  147. if (t instanceof BuildException) {
  148. BuildException be = (BuildException) t;
  149. if (be.getLocation() == Location.UNKNOWN_LOCATION) {
  150. be.setLocation(location);
  151. }
  152. throw be;
  153. }
  154. throw new BuildException(exc.getMessage(), t, location);
  155. }
  156. catch(SAXException exc) {
  157. Throwable t = exc.getException();
  158. if (t instanceof BuildException) {
  159. throw (BuildException) t;
  160. }
  161. throw new BuildException(exc.getMessage(), t);
  162. }
  163. catch(FileNotFoundException exc) {
  164. throw new BuildException(exc);
  165. }
  166. catch(IOException exc) {
  167. throw new BuildException("Error reading project file", exc);
  168. }
  169. finally {
  170. if (inputStream != null) {
  171. try {
  172. inputStream.close();
  173. }
  174. catch (IOException ioe) {
  175. // ignore this
  176. }
  177. }
  178. }
  179. }
  180. /**
  181. * The common superclass for all SAX event handlers used to parse
  182. * the configuration file. Each method just throws an exception,
  183. * so subclasses should override what they can handle.
  184. *
  185. * Each type of XML element (task, target, etc.) in Ant has
  186. * a specific subclass.
  187. *
  188. * In the constructor, this class takes over the handling of SAX
  189. * events from the parent handler and returns
  190. * control back to the parent in the endElement method.
  191. */
  192. private class AbstractHandler extends DefaultHandler {
  193. /**
  194. * Previous handler for the document.
  195. * When the next element is finished, control returns
  196. * to this handler.
  197. */
  198. protected DefaultHandler parentHandler;
  199. /**
  200. * Creates a handler and sets the parser to use it
  201. * for the current element.
  202. *
  203. * @param parentHandler The handler which should be restored to the
  204. * parser at the end of the element.
  205. * Must not be <code>null</code>.
  206. */
  207. public AbstractHandler(DefaultHandler parentHandler) {
  208. this.parentHandler = parentHandler;
  209. // Start handling SAX events
  210. parser.setContentHandler(this);
  211. }
  212. /**
  213. * Handles the start of an element. This base implementation just
  214. * throws an exception.
  215. *
  216. * @param tag The name of the element being started.
  217. * Will not be <code>null</code>.
  218. * @param attrs Attributes of the element being started.
  219. * Will not be <code>null</code>.
  220. *
  221. * @exception SAXParseException if this method is not overridden, or in
  222. * case of error in an overridden version
  223. */
  224. public void startElement(String uri, String tag, String qname,
  225. AttributeList attrs)
  226. throws SAXParseException
  227. {
  228. throw new SAXParseException("Unexpected element \"" + tag + "\"", locator);
  229. }
  230. /**
  231. * Handles text within an element. This base implementation just
  232. * throws an exception.
  233. *
  234. * @param buf A character array of the text within the element.
  235. * Will not be <code>null</code>.
  236. * @param start The start element in the array.
  237. * @param count The number of characters to read from the array.
  238. *
  239. * @exception SAXParseException if this method is not overridden, or in
  240. * case of error in an overridden version
  241. */
  242. public void characters(char[] buf, int start, int count) throws SAXParseException {
  243. String s = new String(buf, start, count).trim();
  244. if (s.length() > 0) {
  245. throw new SAXParseException("Unexpected text \"" + s + "\"", locator);
  246. }
  247. }
  248. /**
  249. * Called when this element and all elements nested into it have been
  250. * handled.
  251. */
  252. protected void finished() {}
  253. /**
  254. * Handles the end of an element. Any required clean-up is performed
  255. * by the finished() method and then the original handler is restored to
  256. * the parser.
  257. *
  258. * @param name The name of the element which is ending.
  259. * Will not be <code>null</code>.
  260. *
  261. * @exception SAXException in case of error (not thrown in
  262. * this implementation)
  263. *
  264. * @see #finished()
  265. */
  266. public void endElement(String uri, String name, String qName) throws SAXException {
  267. finished();
  268. // Let parent resume handling SAX events
  269. parser.setContentHandler(parentHandler);
  270. }
  271. }
  272. /**
  273. * Handler for the root element. Its only child must be the "project" element.
  274. */
  275. private class RootHandler extends DefaultHandler {
  276. /**
  277. * Resolves file: URIs relative to the build file.
  278. *
  279. * @param publicId The public identifer, or <code>null</code>
  280. * if none is available. Ignored in this
  281. * implementation.
  282. * @param systemId The system identifier provided in the XML
  283. * document. Will not be <code>null</code>.
  284. */
  285. public InputSource resolveEntity(String publicId,
  286. String systemId) {
  287. project.log("resolving systemId: " + systemId, Project.MSG_VERBOSE);
  288. if (systemId.startsWith("file:")) {
  289. String path = systemId.substring(5);
  290. int index = path.indexOf("file:");
  291. // we only have to handle these for backward compatibility
  292. // since they are in the FAQ.
  293. while (index != -1) {
  294. path = path.substring(0, index) + path.substring(index + 5);
  295. index = path.indexOf("file:");
  296. }
  297. String entitySystemId = path;
  298. index = path.indexOf("%23");
  299. // convert these to #
  300. while (index != -1) {
  301. path = path.substring(0, index) + "#" + path.substring(index + 3);
  302. index = path.indexOf("%23");
  303. }
  304. File file = new File(path);
  305. if (!file.isAbsolute()) {
  306. file = new File(buildFileParent, path);
  307. }
  308. try {
  309. InputSource inputSource = new InputSource(new FileInputStream(file));
  310. inputSource.setSystemId("file:" + entitySystemId);
  311. return inputSource;
  312. } catch (FileNotFoundException fne) {
  313. project.log(file.getAbsolutePath()+" could not be found",
  314. Project.MSG_WARN);
  315. }
  316. }
  317. // use default if not file or file not found
  318. return null;
  319. }
  320. /**
  321. * Handles the start of a project element. A project handler is created
  322. * and initialised with the element name and attributes.
  323. *
  324. * @param tag The name of the element being started.
  325. * Will not be <code>null</code>.
  326. * @param attrs Attributes of the element being started.
  327. * Will not be <code>null</code>.
  328. *
  329. * @exception SAXParseException if the tag given is not
  330. * <code>"project"</code>
  331. */
  332. public void startElement(String uri, String tag, String qname, AttributeList attrs)
  333. throws SAXParseException
  334. {
  335. if (tag.equals("project")) {
  336. new ProjectHandler(this).init(tag, attrs);
  337. } else {
  338. throw new SAXParseException("Config file is not of expected XML type", locator);
  339. }
  340. }
  341. /**
  342. * Sets the locator in the project helper for future reference.
  343. *
  344. * @param locator The locator used by the parser.
  345. * Will not be <code>null</code>.
  346. */
  347. public void setDocumentLocator(Locator locator) {
  348. ProjectHelperImpl2.this.locator = locator;
  349. }
  350. }
  351. /**
  352. * Handler for the top level "project" element.
  353. */
  354. private class ProjectHandler extends AbstractHandler {
  355. /**
  356. * Constructor which just delegates to the superconstructor.
  357. *
  358. * @param parentHandler The handler which should be restored to the
  359. * parser at the end of the element.
  360. * Must not be <code>null</code>.
  361. */
  362. public ProjectHandler(DefaultHandler parentHandler) {
  363. super(parentHandler);
  364. }
  365. /**
  366. * Initialisation routine called after handler creation
  367. * with the element name and attributes. The attributes which
  368. * this handler can deal with are: <code>"default"</code>,
  369. * <code>"name"</code>, <code>"id"</code> and <code>"basedir"</code>.
  370. *
  371. * @param tag Name of the element which caused this handler
  372. * to be created. Should not be <code>null</code>.
  373. * Ignored in this implementation.
  374. * @param attrs Attributes of the element which caused this
  375. * handler to be created. Must not be <code>null</code>.
  376. *
  377. * @exception SAXParseException if an unexpected attribute is
  378. * encountered or if the <code>"default"</code> attribute
  379. * is missing.
  380. */
  381. public void init(String tag, AttributeList attrs) throws SAXParseException {
  382. String def = null;
  383. String name = null;
  384. String id = null;
  385. String baseDir = null;
  386. for (int i = 0; i < attrs.getLength(); i++) {
  387. String key = attrs.getName(i);
  388. String value = attrs.getValue(i);
  389. if (key.equals("default")) {
  390. def = value;
  391. } else if (key.equals("name")) {
  392. name = value;
  393. } else if (key.equals("id")) {
  394. id = value;
  395. } else if (key.equals("basedir")) {
  396. baseDir = value;
  397. } else {
  398. throw new SAXParseException("Unexpected attribute \"" + attrs.getName(i) + "\"", locator);
  399. }
  400. }
  401. if (def == null) {
  402. throw new SAXParseException("The default attribute of project is required",
  403. locator);
  404. }
  405. project.setDefaultTarget(def);
  406. if (name != null) {
  407. project.setName(name);
  408. project.addReference(name, project);
  409. }
  410. if (id != null) {
  411. project.addReference(id, project);
  412. }
  413. if (project.getProperty("basedir") != null) {
  414. project.setBasedir(project.getProperty("basedir"));
  415. } else {
  416. if (baseDir == null) {
  417. project.setBasedir(buildFileParent.getAbsolutePath());
  418. } else {
  419. // check whether the user has specified an absolute path
  420. if ((new File(baseDir)).isAbsolute()) {
  421. project.setBasedir(baseDir);
  422. } else {
  423. project.setBaseDir(project.resolveFile(baseDir, buildFileParent));
  424. }
  425. }
  426. }
  427. }
  428. /**
  429. * Handles the start of a top-level element within the project. An
  430. * appropriate handler is created and initialised with the details
  431. * of the element.
  432. *
  433. * @param tag The name of the element being started.
  434. * Will not be <code>null</code>.
  435. * @param attrs Attributes of the element being started.
  436. * Will not be <code>null</code>.
  437. *
  438. * @exception SAXParseException if the tag given is not
  439. * <code>"taskdef"</code>, <code>"typedef"</code>,
  440. * <code>"property"</code>, <code>"target"</code>
  441. * or a data type definition
  442. */
  443. public void startElement(String uri, String name, String qname, AttributeList attrs)
  444. throws SAXParseException
  445. {
  446. if (name.equals("taskdef")) {
  447. handleTaskdef(name, attrs);
  448. } else if (name.equals("typedef")) {
  449. handleTypedef(name, attrs);
  450. } else if (name.equals("property")) {
  451. handleProperty(name, attrs);
  452. } else if (name.equals("target")) {
  453. handleTarget(name, attrs);
  454. } else if (project.getDataTypeDefinitions().get(name) != null) {
  455. handleDataType(name, attrs);
  456. } else {
  457. throw new SAXParseException("Unexpected element \"" + name + "\"", locator);
  458. }
  459. }
  460. /**
  461. * Handles a task defintion element by creating a task handler
  462. * and initialising is with the details of the element.
  463. *
  464. * @param tag The name of the element to be handled.
  465. * Will not be <code>null</code>.
  466. * @param attrs Attributes of the element to be handled.
  467. * Will not be <code>null</code>.
  468. *
  469. * @exception SAXParseException if an error occurs when initialising
  470. * the task handler
  471. *
  472. */
  473. private void handleTaskdef(String name, AttributeList attrs) throws SAXParseException {
  474. (new TaskHandler(this, null, null, null)).init(name, attrs);
  475. }
  476. /**
  477. * Handles a type defintion element by creating a task handler
  478. * and initialising is with the details of the element.
  479. *
  480. * @param tag The name of the element to be handled.
  481. * Will not be <code>null</code>.
  482. * @param attrs Attributes of the element to be handled.
  483. * Will not be <code>null</code>.
  484. *
  485. * @exception SAXParseException if an error occurs initialising the
  486. * handler
  487. */
  488. private void handleTypedef(String name, AttributeList attrs) throws SAXParseException {
  489. (new TaskHandler(this, null, null, null)).init(name, attrs);
  490. }
  491. /**
  492. * Handles a property defintion element by creating a task handler
  493. * and initialising is with the details of the element.
  494. *
  495. * @param tag The name of the element to be handled.
  496. * Will not be <code>null</code>.
  497. * @param attrs Attributes of the element to be handled.
  498. * Will not be <code>null</code>.
  499. *
  500. * @exception SAXParseException if an error occurs initialising
  501. * the handler
  502. */
  503. private void handleProperty(String name, AttributeList attrs) throws SAXParseException {
  504. (new TaskHandler(this, null, null, null)).init(name, attrs);
  505. }
  506. /**
  507. * Handles a target defintion element by creating a target handler
  508. * and initialising is with the details of the element.
  509. *
  510. * @param tag The name of the element to be handled.
  511. * Will not be <code>null</code>.
  512. * @param attrs Attributes of the element to be handled.
  513. * Will not be <code>null</code>.
  514. *
  515. * @exception SAXParseException if an error occurs initialising
  516. * the handler
  517. */
  518. private void handleTarget(String tag, AttributeList attrs) throws SAXParseException {
  519. new TargetHandler(this).init(tag, attrs);
  520. }
  521. /**
  522. * Handles a data type defintion element by creating a data type
  523. * handler and initialising is with the details of the element.
  524. *
  525. * @param tag The name of the element to be handled.
  526. * Will not be <code>null</code>.
  527. * @param attrs Attributes of the element to be handled.
  528. * Will not be <code>null</code>.
  529. *
  530. * @exception SAXParseException if an error occurs initialising
  531. * the handler
  532. */
  533. private void handleDataType(String name, AttributeList attrs) throws SAXParseException {
  534. new DataTypeHandler(this).init(name, attrs);
  535. }
  536. }
  537. /**
  538. * Handler for "target" elements.
  539. */
  540. private class TargetHandler extends AbstractHandler {
  541. private Target target;
  542. /**
  543. * Constructor which just delegates to the superconstructor.
  544. *
  545. * @param parentHandler The handler which should be restored to the
  546. * parser at the end of the element.
  547. * Must not be <code>null</code>.
  548. */
  549. public TargetHandler(DefaultHandler parentHandler) {
  550. super(parentHandler);
  551. }
  552. /**
  553. * Initialisation routine called after handler creation
  554. * with the element name and attributes. The attributes which
  555. * this handler can deal with are: <code>"name"</code>,
  556. * <code>"depends"</code>, <code>"if"</code>,
  557. * <code>"unless"</code>, <code>"id"</code> and
  558. * <code>"description"</code>.
  559. *
  560. * @param tag Name of the element which caused this handler
  561. * to be created. Should not be <code>null</code>.
  562. * Ignored in this implementation.
  563. * @param attrs Attributes of the element which caused this
  564. * handler to be created. Must not be <code>null</code>.
  565. *
  566. * @exception SAXParseException if an unexpected attribute is encountered
  567. * or if the <code>"name"</code> attribute is missing.
  568. */
  569. public void init(String tag, AttributeList attrs) throws SAXParseException {
  570. String name = null;
  571. String depends = "";
  572. String ifCond = null;
  573. String unlessCond = null;
  574. String id = null;
  575. String description = null;
  576. for (int i = 0; i < attrs.getLength(); i++) {
  577. String key = attrs.getName(i);
  578. String value = attrs.getValue(i);
  579. if (key.equals("name")) {
  580. name = value;
  581. } else if (key.equals("depends")) {
  582. depends = value;
  583. } else if (key.equals("if")) {
  584. ifCond = value;
  585. } else if (key.equals("unless")) {
  586. unlessCond = value;
  587. } else if (key.equals("id")) {
  588. id = value;
  589. } else if (key.equals("description")) {
  590. description = value;
  591. } else {
  592. throw new SAXParseException("Unexpected attribute \"" + key + "\"", locator);
  593. }
  594. }
  595. if (name == null) {
  596. throw new SAXParseException("target element appears without a name attribute", locator);
  597. }
  598. target = new Target();
  599. target.setName(name);
  600. target.setIf(ifCond);
  601. target.setUnless(unlessCond);
  602. target.setDescription(description);
  603. project.addTarget(name, target);
  604. if (id != null && !id.equals("")) {
  605. project.addReference(id, target);
  606. }
  607. // take care of dependencies
  608. if (depends.length() > 0) {
  609. target.setDepends(depends);
  610. }
  611. }
  612. /**
  613. * Handles the start of an element within a target.
  614. *
  615. * @param tag The name of the element being started.
  616. * Will not be <code>null</code>.
  617. * @param attrs Attributes of the element being started.
  618. * Will not be <code>null</code>.
  619. *
  620. * @exception SAXParseException if an error occurs when initialising
  621. * the appropriate child handler
  622. */
  623. public void startElement(String uri, String name, String qname, AttributeList attrs)
  624. throws SAXParseException
  625. {
  626. if (project.getDataTypeDefinitions().get(name) != null) {
  627. new DataTypeHandler(this, target).init(name, attrs);
  628. } else {
  629. new TaskHandler(this, target, null, target).init(name, attrs);
  630. }
  631. }
  632. }
  633. /**
  634. * Handler for all task elements.
  635. */
  636. private class TaskHandler extends AbstractHandler {
  637. /** Containing target, if any. */
  638. private Target target;
  639. /**
  640. * Container for the task, if any. If target is
  641. * non-<code>null</code>, this must be too.
  642. */
  643. private TaskContainer container;
  644. /**
  645. * Task created by this handler.
  646. */
  647. private Task task;
  648. /**
  649. * Wrapper for the parent element, if any. The wrapper for this
  650. * element will be added to this wrapper as a child.
  651. */
  652. private RuntimeConfigurable parentWrapper;
  653. /**
  654. * Wrapper for this element which takes care of actually configuring
  655. * the element, if this element is contained within a target.
  656. * Otherwise the configuration is performed with the configure method.
  657. * @see ProjectHelper#configure(Object,AttributeList,Project)
  658. */
  659. private RuntimeConfigurable wrapper = null;
  660. /**
  661. * Constructor.
  662. *
  663. * @param parentHandler The handler which should be restored to the
  664. * parser at the end of the element.
  665. * Must not be <code>null</code>.
  666. *
  667. * @param container Container for the element.
  668. * May be <code>null</code> if the target is
  669. * <code>null</code> as well. If the
  670. * target is <code>null</code>, this parameter
  671. * is effectively ignored.
  672. *
  673. * @param parentWrapper Wrapper for the parent element, if any.
  674. * May be <code>null</code>. If the
  675. * target is <code>null</code>, this parameter
  676. * is effectively ignored.
  677. *
  678. * @param target Target this element is part of.
  679. * May be <code>null</code>.
  680. */
  681. public TaskHandler(DefaultHandler parentHandler, TaskContainer container, RuntimeConfigurable parentWrapper, Target target) {
  682. super(parentHandler);
  683. this.container = container;
  684. this.parentWrapper = parentWrapper;
  685. this.target = target;
  686. }
  687. /**
  688. * Initialisation routine called after handler creation
  689. * with the element name and attributes. This configures
  690. * the element with its attributes and sets it up with
  691. * its parent container (if any). Nested elements are then
  692. * added later as the parser encounters them.
  693. *
  694. * @param tag Name of the element which caused this handler
  695. * to be created. Must not be <code>null</code>.
  696. *
  697. * @param attrs Attributes of the element which caused this
  698. * handler to be created. Must not be <code>null</code>.
  699. *
  700. * @exception SAXParseException in case of error (not thrown in
  701. * this implementation)
  702. */
  703. public void init(String tag, AttributeList attrs) throws SAXParseException {
  704. try {
  705. task = project.createTask(tag);
  706. } catch (BuildException e) {
  707. // swallow here, will be thrown again in
  708. // UnknownElement.maybeConfigure if the problem persists.
  709. }
  710. if (task == null) {
  711. task = new UnknownElement(tag);
  712. task.setProject(project);
  713. //XXX task.setTaskType(tag);
  714. task.setTaskName(tag);
  715. }
  716. task.setLocation(new Location(buildFile.toString(), locator.getLineNumber(), locator.getColumnNumber()));
  717. configureId(task, attrs);
  718. // Top level tasks don't have associated targets
  719. if (target != null) {
  720. task.setOwningTarget(target);
  721. container.addTask(task);
  722. task.init();
  723. wrapper = task.getRuntimeConfigurableWrapper();
  724. wrapper.setAttributes(attrs);
  725. if (parentWrapper != null) {
  726. parentWrapper.addChild(wrapper);
  727. }
  728. } else {
  729. task.init();
  730. configure(task, attrs, project);
  731. }
  732. }
  733. /**
  734. * Executes the task if it is a top-level one.
  735. */
  736. protected void finished() {
  737. if (task != null && target == null) {
  738. task.execute();
  739. }
  740. }
  741. /**
  742. * Adds text to the task, using the wrapper if one is
  743. * available (in other words if the task is within a target)
  744. * or using addText otherwise.
  745. *
  746. * @param buf A character array of the text within the element.
  747. * Will not be <code>null</code>.
  748. * @param start The start element in the array.
  749. * @param count The number of characters to read from the array.
  750. *
  751. * @exception SAXParseException if the element doesn't support text
  752. *
  753. * @see ProjectHelper#addText(Project,Object,char[],int,int)
  754. */
  755. public void characters(char[] buf, int start, int count) throws SAXParseException {
  756. if (wrapper == null) {
  757. try {
  758. addText(project, task, buf, start, count);
  759. } catch (BuildException exc) {
  760. throw new SAXParseException(exc.getMessage(), locator, exc);
  761. }
  762. } else {
  763. wrapper.addText(buf, start, count);
  764. }
  765. }
  766. /**
  767. * Handles the start of an element within a target. Task containers
  768. * will always use another task handler, and all other tasks
  769. * will always use a nested element handler.
  770. *
  771. * @param tag The name of the element being started.
  772. * Will not be <code>null</code>.
  773. * @param attrs Attributes of the element being started.
  774. * Will not be <code>null</code>.
  775. *
  776. * @exception SAXParseException if an error occurs when initialising
  777. * the appropriate child handler
  778. */
  779. public void startElement(String uri, String name, String qname, AttributeList attrs)
  780. throws SAXParseException
  781. {
  782. if (task instanceof TaskContainer) {
  783. // task can contain other tasks - no other nested elements possible
  784. new TaskHandler(this, (TaskContainer)task, wrapper, target).init(name, attrs);
  785. }
  786. else {
  787. new NestedElementHandler(this, task, wrapper, target).init(name, attrs);
  788. }
  789. }
  790. }
  791. /**
  792. * Handler for all nested properties.
  793. */
  794. private class NestedElementHandler extends AbstractHandler {
  795. /** Parent object (task/data type/etc). */
  796. private Object parent;
  797. /** The nested element itself. */
  798. private Object child;
  799. /**
  800. * Wrapper for the parent element, if any. The wrapper for this
  801. * element will be added to this wrapper as a child.
  802. */
  803. private RuntimeConfigurable parentWrapper;
  804. /**
  805. * Wrapper for this element which takes care of actually configuring
  806. * the element, if a parent wrapper is provided.
  807. * Otherwise the configuration is performed with the configure method.
  808. * @see ProjectHelper#configure(Object,AttributeList,Project)
  809. */
  810. private RuntimeConfigurable childWrapper = null;
  811. /** Target this element is part of, if any. */
  812. private Target target;
  813. /**
  814. * Constructor.
  815. *
  816. * @param parentHandler The handler which should be restored to the
  817. * parser at the end of the element.
  818. * Must not be <code>null</code>.
  819. *
  820. * @param parent Parent of this element (task/data type/etc).
  821. * Must not be <code>null</code>.
  822. *
  823. * @param parentWrapper Wrapper for the parent element, if any.
  824. * May be <code>null</code>.
  825. *
  826. * @param target Target this element is part of.
  827. * May be <code>null</code>.
  828. */
  829. public NestedElementHandler(DefaultHandler parentHandler,
  830. Object parent,
  831. RuntimeConfigurable parentWrapper,
  832. Target target) {
  833. super(parentHandler);
  834. if (parent instanceof TaskAdapter) {
  835. this.parent = ((TaskAdapter) parent).getProxy();
  836. } else {
  837. this.parent = parent;
  838. }
  839. this.parentWrapper = parentWrapper;
  840. this.target = target;
  841. }
  842. /**
  843. * Initialisation routine called after handler creation
  844. * with the element name and attributes. This configures
  845. * the element with its attributes and sets it up with
  846. * its parent container (if any). Nested elements are then
  847. * added later as the parser encounters them.
  848. *
  849. * @param tag Name of the element which caused this handler
  850. * to be created. Must not be <code>null</code>.
  851. *
  852. * @param attrs Attributes of the element which caused this
  853. * handler to be created. Must not be <code>null</code>.
  854. *
  855. * @exception SAXParseException in case of error, such as a
  856. * BuildException being thrown during configuration.
  857. */
  858. public void init(String propType, AttributeList attrs) throws SAXParseException {
  859. Class parentClass = parent.getClass();
  860. IntrospectionHelper ih =
  861. IntrospectionHelper.getHelper(parentClass);
  862. try {
  863. String elementName = propType.toLowerCase(Locale.US);
  864. if (parent instanceof UnknownElement) {
  865. UnknownElement uc = new UnknownElement(elementName);
  866. uc.setProject(project);
  867. ((UnknownElement) parent).addChild(uc);
  868. child = uc;
  869. } else {
  870. child = ih.createElement(project, parent, elementName);
  871. }
  872. configureId(child, attrs);
  873. if (parentWrapper != null) {
  874. childWrapper = new RuntimeConfigurable(child, propType);
  875. childWrapper.setAttributes(attrs);
  876. parentWrapper.addChild(childWrapper);
  877. } else {
  878. configure(child, attrs, project);
  879. ih.storeElement(project, parent, child, elementName);
  880. }
  881. } catch (BuildException exc) {
  882. throw new SAXParseException(exc.getMessage(), locator, exc);
  883. }
  884. }
  885. /**
  886. * Adds text to the element, using the wrapper if one is
  887. * available or using addText otherwise.
  888. *
  889. * @param buf A character array of the text within the element.
  890. * Will not be <code>null</code>.
  891. * @param start The start element in the array.
  892. * @param count The number of characters to read from the array.
  893. *
  894. * @exception SAXParseException if the element doesn't support text
  895. *
  896. * @see ProjectHelper#addText(Project,Object,char[],int,int)
  897. */
  898. public void characters(char[] buf, int start, int count) throws SAXParseException {
  899. if (parentWrapper == null) {
  900. try {
  901. addText(project, child, buf, start, count);
  902. } catch (BuildException exc) {
  903. throw new SAXParseException(exc.getMessage(), locator, exc);
  904. }
  905. } else {
  906. childWrapper.addText(buf, start, count);
  907. }
  908. }
  909. /**
  910. * Handles the start of an element within this one. Task containers
  911. * will always use a task handler, and all other elements
  912. * will always use another nested element handler.
  913. *
  914. * @param tag The name of the element being started.
  915. * Will not be <code>null</code>.
  916. * @param attrs Attributes of the element being started.
  917. * Will not be <code>null</code>.
  918. *
  919. * @exception SAXParseException if an error occurs when initialising
  920. * the appropriate child handler
  921. */
  922. public void startElement(String uri, String name, String qname, AttributeList attrs)
  923. throws SAXParseException
  924. {
  925. if (child instanceof TaskContainer) {
  926. // taskcontainer nested element can contain other tasks - no other
  927. // nested elements possible
  928. new TaskHandler(this, (TaskContainer)child, childWrapper, target).init(name, attrs);
  929. }
  930. else {
  931. new NestedElementHandler(this, child, childWrapper, target).init(name, attrs);
  932. }
  933. }
  934. }
  935. /**
  936. * Handler for all data types directly subordinate to project or target.
  937. */
  938. private class DataTypeHandler extends AbstractHandler {
  939. /** Parent target, if any. */
  940. private Target target;
  941. /** The element being configured. */
  942. private Object element;
  943. /** Wrapper for this element, if it's part of a target. */
  944. private RuntimeConfigurable wrapper = null;
  945. /**
  946. * Constructor with no target specified.
  947. *
  948. * @param parentHandler The handler which should be restored to the
  949. * parser at the end of the element.
  950. * Must not be <code>null</code>.
  951. */
  952. public DataTypeHandler(DefaultHandler parentHandler) {
  953. this(parentHandler, null);
  954. }
  955. /**
  956. * Constructor with a target specified.
  957. *
  958. * @param parentHandler The handler which should be restored to the
  959. * parser at the end of the element.
  960. * Must not be <code>null</code>.
  961. *
  962. * @param target The parent target of this element.
  963. * May be <code>null</code>.
  964. */
  965. public DataTypeHandler(DefaultHandler parentHandler, Target target) {
  966. super(parentHandler);
  967. this.target = target;
  968. }
  969. /**
  970. * Initialisation routine called after handler creation
  971. * with the element name and attributes. This configures
  972. * the element with its attributes and sets it up with
  973. * its parent container (if any). Nested elements are then
  974. * added later as the parser encounters them.
  975. *
  976. * @param tag Name of the element which caused this handler
  977. * to be created. Must not be <code>null</code>.
  978. *
  979. * @param attrs Attributes of the element which caused this
  980. * handler to be created. Must not be <code>null</code>.
  981. *
  982. * @exception SAXParseException in case of error, such as a
  983. * BuildException being thrown during configuration.
  984. */
  985. public void init(String propType, AttributeList attrs) throws SAXParseException {
  986. try {
  987. element = project.createDataType(propType);
  988. if (element == null) {
  989. throw new BuildException("Unknown data type "+propType);
  990. }
  991. if (target != null) {
  992. wrapper = new RuntimeConfigurable(element, propType);
  993. wrapper.setAttributes(attrs);
  994. target.addDataType(wrapper);
  995. } else {
  996. configure(element, attrs, project);
  997. configureId(element, attrs);
  998. }
  999. } catch (BuildException exc) {
  1000. throw new SAXParseException(exc.getMessage(), locator, exc);
  1001. }
  1002. }
  1003. // XXX: (Jon Skeet) Any reason why this doesn't use the wrapper
  1004. // if one is available, whereas NestedElementHandler.characters does?
  1005. /**
  1006. * Adds text to the element.
  1007. *
  1008. * @param buf A character array of the text within the element.
  1009. * Will not be <code>null</code>.
  1010. * @param start The start element in the array.
  1011. * @param count The number of characters to read from the array.
  1012. *
  1013. * @exception SAXParseException if the element doesn't support text
  1014. *
  1015. * @see ProjectHelper#addText(Project,Object,char[],int,int)
  1016. */
  1017. public void characters(char[] buf, int start, int count) throws SAXParseException {
  1018. try {
  1019. addText(project, element, buf, start, count);
  1020. } catch (BuildException exc) {
  1021. throw new SAXParseException(exc.getMessage(), locator, exc);
  1022. }
  1023. }
  1024. /**
  1025. * Handles the start of an element within this one.
  1026. * This will always use a nested element handler.
  1027. *
  1028. * @param tag The name of the element being started.
  1029. * Will not be <code>null</code>.
  1030. * @param attrs Attributes of the element being started.
  1031. * Will not be <code>null</code>.
  1032. *
  1033. * @exception SAXParseException if an error occurs when initialising
  1034. * the child handler
  1035. */
  1036. public void startElement(String uri, String name, String qname, AttributeList attrs)
  1037. throws SAXParseException
  1038. {
  1039. new NestedElementHandler(this, element, wrapper, target).init(name, attrs);
  1040. }
  1041. }
  1042. /**
  1043. * Returns the parser factory to use. Only one parser
  1044. * factory is ever created by this method (multi-threading
  1045. * issues aside) and is then cached for future use.
  1046. *
  1047. * @return a SAXParserFactory to use within this class
  1048. */
  1049. private static SAXParserFactory getParserFactory() {
  1050. if (parserFactory == null) {
  1051. parserFactory = SAXParserFactory.newInstance();
  1052. }
  1053. return parserFactory;
  1054. }
  1055. /**
  1056. * Scans an attribute list for the <code>id</code> attribute and
  1057. * stores a reference to the target object in the project if an
  1058. * id is found.
  1059. * <p>
  1060. * This method was moved out of the configure method to allow
  1061. * it to be executed at parse time.
  1062. *
  1063. * @see #configure(Object,AttributeList,Project)
  1064. */
  1065. private void configureId(Object target, AttributeList attr) {
  1066. String id = attr.getValue("id");
  1067. if (id != null) {
  1068. project.addReference(id, target);
  1069. }
  1070. }
  1071. }