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

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