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.

ProjectHelper.java 30 kB

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