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.

ProjectHelperImpl.java 47 KiB

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