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 28 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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;
  55. import java.io.File;
  56. import java.io.FileInputStream;
  57. import java.io.FileNotFoundException;
  58. import java.io.IOException;
  59. import java.util.Hashtable;
  60. import java.util.Vector;
  61. import java.util.Enumeration;
  62. import java.util.Locale;
  63. import org.xml.sax.Locator;
  64. import org.xml.sax.InputSource;
  65. import org.xml.sax.HandlerBase;
  66. import org.xml.sax.SAXParseException;
  67. import org.xml.sax.SAXException;
  68. import org.xml.sax.DocumentHandler;
  69. import org.xml.sax.AttributeList;
  70. import org.xml.sax.helpers.XMLReaderAdapter;
  71. import javax.xml.parsers.SAXParserFactory;
  72. import javax.xml.parsers.SAXParser;
  73. import javax.xml.parsers.ParserConfigurationException;
  74. // Note: Use of local classes can create problems with various compilers
  75. // like gcc or even jikes. In addition it makes the code harder to read
  76. // for many beginners ( at least for me - Costin ).
  77. // I changed the code to avoid the tricks that the compiler does, now
  78. // jikes works ( for me ). Note that declaring the classes 'private'
  79. // is probably overriden by the compiler - a feature of the internal class impl.
  80. /**
  81. * "Original" implementation of the project helper. Or at least
  82. * what is present in ant1.4.
  83. *
  84. * @author duncan@x180.com
  85. */
  86. public class ProjectHelperImpl extends ProjectHelper {
  87. private static SAXParserFactory parserFactory = null;
  88. protected Project project;
  89. protected Object source;
  90. protected File buildFile;
  91. protected File buildFileParent;
  92. private org.xml.sax.Parser parser;
  93. private Locator locator;
  94. /** Return a handler for project. This can be used by xml helpers to fallback to
  95. the original behavior ( non-namespace aware ).
  96. When the <project> callback is received, if no namespaces are used ( or no
  97. new attributes, etc ) then the easiest way to achieve backward compatibility
  98. is to use the original.
  99. A helper needs to call this method, which will switch the HandlerBase in
  100. the SAX parser and return it to the original on </project>
  101. @experimental This is likely to change
  102. */
  103. public HandlerBase defaultProjectHandler( Project project,
  104. org.xml.sax.Parser parser,
  105. String tag, AttributeList attrs,
  106. DocumentHandler parent )
  107. throws SAXParseException
  108. {
  109. this.project=project;
  110. this.parser=parser;
  111. ProjectHandler h=new ProjectHandler(this, parent);
  112. h.init( tag, attrs );
  113. return h;
  114. }
  115. /**
  116. * Parses the project file.
  117. */
  118. public void parse(Project project, Object source) throws BuildException {
  119. if( ! (source instanceof File) )
  120. throw new BuildException( "Only File source is supported by the default helper");
  121. File buildFile=(File)source;
  122. this.project = project;
  123. this.buildFile = new File(buildFile.getAbsolutePath());
  124. buildFileParent = new File(this.buildFile.getParent());
  125. FileInputStream inputStream = null;
  126. InputSource inputSource = null;
  127. try {
  128. SAXParser saxParser = getParserFactory().newSAXParser();
  129. try {
  130. parser = saxParser.getParser();
  131. } catch (SAXException exc) {
  132. parser = new XMLReaderAdapter(saxParser.getXMLReader());
  133. }
  134. String uri = "file:" + buildFile.getAbsolutePath().replace('\\', '/');
  135. for (int index = uri.indexOf('#'); index != -1; index = uri.indexOf('#')) {
  136. uri = uri.substring(0, index) + "%23" + uri.substring(index+1);
  137. }
  138. inputStream = new FileInputStream(buildFile);
  139. inputSource = new InputSource(inputStream);
  140. inputSource.setSystemId(uri);
  141. project.log("parsing buildfile " + buildFile + " with URI = " + uri, Project.MSG_VERBOSE);
  142. HandlerBase hb = new RootHandler(this);
  143. parser.setDocumentHandler(hb);
  144. parser.setEntityResolver(hb);
  145. parser.setErrorHandler(hb);
  146. parser.setDTDHandler(hb);
  147. parser.parse(inputSource);
  148. }
  149. catch(ParserConfigurationException exc) {
  150. throw new BuildException("Parser has not been configured correctly", exc);
  151. }
  152. catch(SAXParseException exc) {
  153. Location location =
  154. new Location(buildFile.toString(), exc.getLineNumber(), exc.getColumnNumber());
  155. Throwable t = exc.getException();
  156. if (t instanceof BuildException) {
  157. BuildException be = (BuildException) t;
  158. if (be.getLocation() == Location.UNKNOWN_LOCATION) {
  159. be.setLocation(location);
  160. }
  161. throw be;
  162. }
  163. throw new BuildException(exc.getMessage(), t, location);
  164. }
  165. catch(SAXException exc) {
  166. Throwable t = exc.getException();
  167. if (t instanceof BuildException) {
  168. throw (BuildException) t;
  169. }
  170. throw new BuildException(exc.getMessage(), t);
  171. }
  172. catch(FileNotFoundException exc) {
  173. throw new BuildException(exc);
  174. }
  175. catch(IOException exc) {
  176. throw new BuildException("Error reading project file", exc);
  177. }
  178. finally {
  179. if (inputStream != null) {
  180. try {
  181. inputStream.close();
  182. }
  183. catch (IOException ioe) {
  184. // ignore this
  185. }
  186. }
  187. }
  188. }
  189. /**
  190. * The common superclass for all sax event handlers in Ant. Basically
  191. * throws an exception in each method, so subclasses should override
  192. * what they can handle.
  193. *
  194. * Each type of xml element (task, target, etc) in ant will
  195. * have its own subclass of AbstractHandler.
  196. *
  197. * In the constructor, this class takes over the handling of sax
  198. * events from the parent handler, and returns
  199. * control back to the parent in the endElement method.
  200. */
  201. private static class AbstractHandler extends HandlerBase {
  202. protected DocumentHandler parentHandler;
  203. protected ProjectHelperImpl helper;
  204. public AbstractHandler(ProjectHelperImpl helper, DocumentHandler parentHandler) {
  205. this.parentHandler = parentHandler;
  206. this.helper=helper;
  207. // Start handling SAX events
  208. helper.parser.setDocumentHandler(this);
  209. }
  210. public void startElement(String tag, AttributeList attrs) throws SAXParseException {
  211. throw new SAXParseException("Unexpected element \"" + tag + "\"", helper.locator);
  212. }
  213. public void characters(char[] buf, int start, int end) throws SAXParseException {
  214. String s = new String(buf, start, end).trim();
  215. if (s.length() > 0) {
  216. throw new SAXParseException("Unexpected text \"" + s + "\"", helper.locator);
  217. }
  218. }
  219. /**
  220. * Called when this element and all elements nested into it have been
  221. * handled.
  222. */
  223. protected void finished() {}
  224. public void endElement(String name) throws SAXException {
  225. finished();
  226. // Let parent resume handling SAX events
  227. helper.parser.setDocumentHandler(parentHandler);
  228. }
  229. }
  230. /**
  231. * Handler for the root element. It's only child must be the "project" element.
  232. */
  233. private static class RootHandler extends HandlerBase {
  234. private ProjectHelperImpl helper;
  235. public RootHandler( ProjectHelperImpl helper ) {
  236. this.helper=helper;
  237. }
  238. /**
  239. * resolve file: URIs as relative to the build file.
  240. */
  241. public InputSource resolveEntity(String publicId,
  242. String systemId) {
  243. helper.project.log("resolving systemId: " + systemId, Project.MSG_VERBOSE);
  244. if (systemId.startsWith("file:")) {
  245. String path = systemId.substring(5);
  246. int index = path.indexOf("file:");
  247. // we only have to handle these for backward compatibility
  248. // since they are in the FAQ.
  249. while (index != -1) {
  250. path = path.substring(0, index) + path.substring(index + 5);
  251. index = path.indexOf("file:");
  252. }
  253. String entitySystemId = path;
  254. index = path.indexOf("%23");
  255. // convert these to #
  256. while (index != -1) {
  257. path = path.substring(0, index) + "#" + path.substring(index + 3);
  258. index = path.indexOf("%23");
  259. }
  260. File file = new File(path);
  261. if (!file.isAbsolute()) {
  262. file = new File(helper.buildFileParent, path);
  263. }
  264. try {
  265. InputSource inputSource = new InputSource(new FileInputStream(file));
  266. inputSource.setSystemId("file:" + entitySystemId);
  267. return inputSource;
  268. } catch (FileNotFoundException fne) {
  269. helper.project.log(file.getAbsolutePath()+" could not be found",
  270. Project.MSG_WARN);
  271. }
  272. }
  273. // use default if not file or file not found
  274. return null;
  275. }
  276. public void startElement(String tag, AttributeList attrs) throws SAXParseException {
  277. if (tag.equals("project")) {
  278. new ProjectHandler(helper, this).init(tag, attrs);
  279. } else {
  280. throw new SAXParseException("Config file is not of expected XML type", helper.locator);
  281. }
  282. }
  283. public void setDocumentLocator(Locator locator) {
  284. helper.locator = locator;
  285. }
  286. }
  287. /**
  288. * Handler for the top level "project" element.
  289. */
  290. private static class ProjectHandler extends AbstractHandler {
  291. public ProjectHandler(ProjectHelperImpl helper, DocumentHandler parentHandler) {
  292. super(helper, parentHandler);
  293. }
  294. public void init(String tag, AttributeList attrs) throws SAXParseException {
  295. String def = null;
  296. String name = null;
  297. String id = null;
  298. String baseDir = null;
  299. for (int i = 0; i < attrs.getLength(); i++) {
  300. String key = attrs.getName(i);
  301. String value = attrs.getValue(i);
  302. if (key.equals("default")) {
  303. def = value;
  304. } else if (key.equals("name")) {
  305. name = value;
  306. } else if (key.equals("id")) {
  307. id = value;
  308. } else if (key.equals("basedir")) {
  309. baseDir = value;
  310. } else {
  311. throw new SAXParseException("Unexpected attribute \"" + attrs.getName(i) + "\"", helper.locator);
  312. }
  313. }
  314. if (def == null) {
  315. throw new SAXParseException("The default attribute of project is required",
  316. helper.locator);
  317. }
  318. helper.project.setDefaultTarget(def);
  319. if (name != null) {
  320. helper.project.setName(name);
  321. helper.project.addReference(name, helper.project);
  322. }
  323. if (id != null) {
  324. helper.project.addReference(id, helper.project);
  325. }
  326. if (helper.project.getProperty("basedir") != null) {
  327. helper.project.setBasedir(helper.project.getProperty("basedir"));
  328. } else {
  329. if (baseDir == null) {
  330. helper.project.setBasedir(helper.buildFileParent.getAbsolutePath());
  331. } else {
  332. // check whether the user has specified an absolute path
  333. if ((new File(baseDir)).isAbsolute()) {
  334. helper.project.setBasedir(baseDir);
  335. } else {
  336. helper.project.setBaseDir(helper.project.resolveFile(baseDir, helper.buildFileParent));
  337. }
  338. }
  339. }
  340. }
  341. public void startElement(String name, AttributeList attrs) throws SAXParseException {
  342. if (name.equals("taskdef")) {
  343. handleTaskdef(name, attrs);
  344. } else if (name.equals("typedef")) {
  345. handleTypedef(name, attrs);
  346. } else if (name.equals("property")) {
  347. handleProperty(name, attrs);
  348. } else if (name.equals("target")) {
  349. handleTarget(name, attrs);
  350. } else if (helper.project.getDataTypeDefinitions().get(name) != null) {
  351. handleDataType(name, attrs);
  352. } else {
  353. throw new SAXParseException("Unexpected element \"" + name + "\"", helper.locator);
  354. }
  355. }
  356. private void handleTaskdef(String name, AttributeList attrs) throws SAXParseException {
  357. (new TaskHandler(helper, this, null, null, null)).init(name, attrs);
  358. }
  359. private void handleTypedef(String name, AttributeList attrs) throws SAXParseException {
  360. (new TaskHandler(helper, this, null, null, null)).init(name, attrs);
  361. }
  362. private void handleProperty(String name, AttributeList attrs) throws SAXParseException {
  363. (new TaskHandler(helper, this, null, null, null)).init(name, attrs);
  364. }
  365. private void handleTarget(String tag, AttributeList attrs) throws SAXParseException {
  366. new TargetHandler(helper, this).init(tag, attrs);
  367. }
  368. private void handleDataType(String name, AttributeList attrs) throws SAXParseException {
  369. new DataTypeHandler(helper, this).init(name, attrs);
  370. }
  371. }
  372. /**
  373. * Handler for "target" elements.
  374. */
  375. private static class TargetHandler extends AbstractHandler {
  376. private Target target;
  377. public TargetHandler(ProjectHelperImpl helper, DocumentHandler parentHandler) {
  378. super(helper, parentHandler);
  379. }
  380. public void init(String tag, AttributeList attrs) throws SAXParseException {
  381. String name = null;
  382. String depends = "";
  383. String ifCond = null;
  384. String unlessCond = null;
  385. String id = null;
  386. String description = null;
  387. for (int i = 0; i < attrs.getLength(); i++) {
  388. String key = attrs.getName(i);
  389. String value = attrs.getValue(i);
  390. if (key.equals("name")) {
  391. name = value;
  392. } else if (key.equals("depends")) {
  393. depends = value;
  394. } else if (key.equals("if")) {
  395. ifCond = value;
  396. } else if (key.equals("unless")) {
  397. unlessCond = value;
  398. } else if (key.equals("id")) {
  399. id = value;
  400. } else if (key.equals("description")) {
  401. description = value;
  402. } else {
  403. throw new SAXParseException("Unexpected attribute \"" + key + "\"", helper.locator);
  404. }
  405. }
  406. if (name == null) {
  407. throw new SAXParseException("target element appears without a name attribute", helper.locator);
  408. }
  409. target = new Target();
  410. target.setName(name);
  411. target.setIf(ifCond);
  412. target.setUnless(unlessCond);
  413. target.setDescription(description);
  414. helper.project.addTarget(name, target);
  415. if (id != null && !id.equals("")) {
  416. helper.project.addReference(id, target);
  417. }
  418. // take care of dependencies
  419. if (depends.length() > 0) {
  420. target.setDepends(depends);
  421. }
  422. }
  423. public void startElement(String name, AttributeList attrs) throws SAXParseException {
  424. if (helper.project.getDataTypeDefinitions().get(name) != null) {
  425. new DataTypeHandler(helper, this, target).init(name, attrs);
  426. } else {
  427. new TaskHandler(helper, this, target, null, target).init(name, attrs);
  428. }
  429. }
  430. }
  431. /**
  432. * Handler for all task elements.
  433. */
  434. private static class TaskHandler extends AbstractHandler {
  435. private Target target;
  436. private TaskContainer container;
  437. private Task task;
  438. private RuntimeConfigurable parentWrapper;
  439. private RuntimeConfigurable wrapper = null;
  440. public TaskHandler(ProjectHelperImpl helper,
  441. DocumentHandler parentHandler,
  442. TaskContainer container,
  443. RuntimeConfigurable parentWrapper,
  444. Target target)
  445. {
  446. super(helper, parentHandler);
  447. this.container = container;
  448. this.parentWrapper = parentWrapper;
  449. this.target = target;
  450. }
  451. public void init(String tag, AttributeList attrs) throws SAXParseException {
  452. try {
  453. task = helper.project.createTask(tag);
  454. } catch (BuildException e) {
  455. // swallow here, will be thrown again in
  456. // UnknownElement.maybeConfigure if the problem persists.
  457. }
  458. if (task == null) {
  459. task = new UnknownElement(tag);
  460. task.setProject(helper.project);
  461. task.setTaskType(tag);
  462. task.setTaskName(tag);
  463. }
  464. task.setLocation(new Location(helper.buildFile.toString(),
  465. helper.locator.getLineNumber(),
  466. helper.locator.getColumnNumber()));
  467. helper.configureId(task, attrs);
  468. // Top level tasks don't have associated targets
  469. if (target != null) {
  470. task.setOwningTarget(target);
  471. container.addTask(task);
  472. task.init();
  473. wrapper = task.getRuntimeConfigurableWrapper();
  474. wrapper.setAttributes(attrs);
  475. if (parentWrapper != null) {
  476. parentWrapper.addChild(wrapper);
  477. }
  478. } else {
  479. task.init();
  480. configure(task, attrs, helper.project);
  481. }
  482. }
  483. protected void finished() {
  484. if (task != null && target == null) {
  485. task.execute();
  486. }
  487. }
  488. public void characters(char[] buf, int start, int end) throws SAXParseException {
  489. if (wrapper == null) {
  490. try {
  491. addText(helper.project, task, buf, start, end);
  492. } catch (BuildException exc) {
  493. throw new SAXParseException(exc.getMessage(), helper.locator, exc);
  494. }
  495. } else {
  496. wrapper.addText(buf, start, end);
  497. }
  498. }
  499. public void startElement(String name, AttributeList attrs) throws SAXParseException {
  500. if (task instanceof TaskContainer) {
  501. // task can contain other tasks - no other nested elements possible
  502. new TaskHandler(helper, this, (TaskContainer)task, wrapper, target).init(name, attrs);
  503. }
  504. else {
  505. new NestedElementHandler(helper, this, task, wrapper, target).init(name, attrs);
  506. }
  507. }
  508. }
  509. /**
  510. * Handler for all nested properties.
  511. */
  512. private static class NestedElementHandler extends AbstractHandler {
  513. private Object parent;
  514. private Object child;
  515. private RuntimeConfigurable parentWrapper;
  516. private RuntimeConfigurable childWrapper = null;
  517. private TaskAdapter adapter=null;
  518. private Target target;
  519. public NestedElementHandler(ProjectHelperImpl helper,
  520. DocumentHandler parentHandler,
  521. Object parent,
  522. RuntimeConfigurable parentWrapper,
  523. Target target) {
  524. super(helper, parentHandler);
  525. if (parent instanceof TaskAdapter) {
  526. this.adapter= (TaskAdapter)parent;
  527. this.parent = adapter.getProxy();
  528. } else {
  529. this.parent = parent;
  530. }
  531. this.parentWrapper = parentWrapper;
  532. this.target = target;
  533. }
  534. public void init(String propType, AttributeList attrs) throws SAXParseException {
  535. Class parentClass = parent.getClass();
  536. IntrospectionHelper ih =
  537. IntrospectionHelper.getHelper(parentClass);
  538. if( adapter!=null ) {
  539. adapter.setIntrospectionHelper( ih );
  540. }
  541. try {
  542. String elementName = propType.toLowerCase(Locale.US);
  543. if (parent instanceof UnknownElement) {
  544. UnknownElement uc = new UnknownElement(elementName);
  545. uc.setProject(helper.project);
  546. ((UnknownElement) parent).addChild(uc);
  547. child = uc;
  548. } else {
  549. child = ih.createElement(helper.project, parent, elementName);
  550. }
  551. helper.configureId(child, attrs);
  552. if (parentWrapper != null) {
  553. childWrapper = new RuntimeConfigurable(child, propType);
  554. childWrapper.setAttributes(attrs);
  555. parentWrapper.addChild(childWrapper);
  556. } else {
  557. configure(child, attrs, helper.project);
  558. ih.storeElement(helper.project, parent, child, elementName);
  559. }
  560. } catch (BuildException exc) {
  561. throw new SAXParseException(exc.getMessage(), helper.locator, exc);
  562. }
  563. }
  564. public void characters(char[] buf, int start, int end) throws SAXParseException {
  565. if (parentWrapper == null) {
  566. try {
  567. addText(helper.project, child, buf, start, end);
  568. } catch (BuildException exc) {
  569. throw new SAXParseException(exc.getMessage(), helper.locator, exc);
  570. }
  571. } else {
  572. childWrapper.addText(buf, start, end);
  573. }
  574. }
  575. public void startElement(String name, AttributeList attrs) throws SAXParseException {
  576. if (child instanceof TaskContainer) {
  577. // taskcontainer nested element can contain other tasks - no other
  578. // nested elements possible
  579. new TaskHandler(helper, this, (TaskContainer)child, childWrapper, target).init(name, attrs);
  580. }
  581. else {
  582. new NestedElementHandler(helper, this, child, childWrapper, target).init(name, attrs);
  583. }
  584. }
  585. }
  586. /**
  587. * Handler for all data types at global level.
  588. */
  589. private static class DataTypeHandler extends AbstractHandler {
  590. private Target target;
  591. private Object element;
  592. private RuntimeConfigurable wrapper = null;
  593. public DataTypeHandler(ProjectHelperImpl helper, DocumentHandler parentHandler) {
  594. this(helper, parentHandler, null);
  595. }
  596. public DataTypeHandler(ProjectHelperImpl helper,
  597. DocumentHandler parentHandler,
  598. Target target)
  599. {
  600. super(helper, parentHandler);
  601. this.target = target;
  602. }
  603. public void init(String propType, AttributeList attrs) throws SAXParseException {
  604. try {
  605. element = helper.project.createDataType(propType);
  606. if (element == null) {
  607. throw new BuildException("Unknown data type "+propType);
  608. }
  609. if (target != null) {
  610. wrapper = new RuntimeConfigurable(element, propType);
  611. wrapper.setAttributes(attrs);
  612. target.addDataType(wrapper);
  613. } else {
  614. configure(element, attrs, helper.project);
  615. helper.configureId(element, attrs);
  616. }
  617. } catch (BuildException exc) {
  618. throw new SAXParseException(exc.getMessage(), helper.locator, exc);
  619. }
  620. }
  621. public void characters(char[] buf, int start, int end) throws SAXParseException {
  622. try {
  623. addText(helper.project, element, buf, start, end);
  624. } catch (BuildException exc) {
  625. throw new SAXParseException(exc.getMessage(), helper.locator, exc);
  626. }
  627. }
  628. public void startElement(String name, AttributeList attrs) throws SAXParseException {
  629. new NestedElementHandler(helper, this, element, wrapper, target).init(name, attrs);
  630. }
  631. }
  632. public ProjectHelperImpl() {
  633. }
  634. private static SAXParserFactory getParserFactory() {
  635. if (parserFactory == null) {
  636. parserFactory = SAXParserFactory.newInstance();
  637. }
  638. return parserFactory;
  639. }
  640. /**
  641. * Scan AttributeList for the id attribute and maybe add a
  642. * reference to project.
  643. *
  644. * <p>Moved out of {@link #configure configure} to make it happen
  645. * at parser time.</p>
  646. */
  647. private void configureId(Object target, AttributeList attr) {
  648. String id = attr.getValue("id");
  649. if (id != null) {
  650. project.addReference(id, target);
  651. }
  652. }
  653. }