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

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