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

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