1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.types;
  18. import org.apache.tools.ant.Project;
  19. import org.apache.tools.ant.ProjectHelper;
  20. import org.apache.tools.ant.Task;
  21. import org.apache.tools.ant.UnknownElement;
  22. import org.apache.tools.ant.Target;
  23. import org.apache.tools.ant.helper.ProjectHelperImpl;
  24. import java.util.Vector;
  25. /**
  26. * Description is used to provide a project-wide description element
  27. * (that is, a description that applies to a buildfile as a whole).
  28. * If present, the <description> element is printed out before the
  29. * target descriptions.
  30. *
  31. * Description has no attributes, only text. There can only be one
  32. * project description per project. A second description element will
  33. * overwrite the first.
  34. *
  35. * @version $Revision: 1.15.2.4 $ $Date: 2004/03/09 17:01:54 $
  36. *
  37. * @ant.datatype ignore="true"
  38. */
  39. public class Description extends DataType {
  40. /**
  41. * Adds descriptive text to the project.
  42. *
  43. * @param text the descriptive text
  44. */
  45. public void addText(String text) {
  46. ProjectHelper ph = ProjectHelper.getProjectHelper();
  47. if (!(ph instanceof ProjectHelperImpl)) {
  48. // New behavior for delayed task creation. Description
  49. // will be evaluated in Project.getDescription()
  50. return;
  51. }
  52. String currentDescription = getProject().getDescription();
  53. if (currentDescription == null) {
  54. getProject().setDescription(text);
  55. } else {
  56. getProject().setDescription(currentDescription + text);
  57. }
  58. }
  59. /**
  60. * return the descriptions from all the targets of
  61. * a project.
  62. *
  63. * @param project the project to get the descriptions for.
  64. * @return a string containing the concatenated descriptions of
  65. * the targets.
  66. */
  67. public static String getDescription(Project project) {
  68. StringBuffer description = new StringBuffer();
  69. Vector targets = (Vector) project.getReference("ant.targets");
  70. for (int i = 0; i < targets.size(); i++) {
  71. Target t = (Target) targets.elementAt(i);
  72. concatDescriptions(project, t, description);
  73. }
  74. return description.toString();
  75. }
  76. private static void concatDescriptions(Project project, Target t,
  77. StringBuffer description) {
  78. if (t == null) {
  79. return;
  80. }
  81. Vector tasks = findElementInTarget(project, t, "description");
  82. if (tasks == null) {
  83. return;
  84. }
  85. for (int i = 0; i < tasks.size(); i++) {
  86. Task task = (Task) tasks.elementAt(i);
  87. if (!(task instanceof UnknownElement)) {
  88. continue;
  89. }
  90. UnknownElement ue = ((UnknownElement) task);
  91. StringBuffer descComp = ue.getWrapper().getText();
  92. if (descComp != null) {
  93. description.append((Object) descComp);
  94. }
  95. }
  96. }
  97. private static Vector findElementInTarget(Project project,
  98. Target t, String name) {
  99. Task[] tasks = t.getTasks();
  100. Vector elems = new Vector();
  101. for (int i = 0; i < tasks.length; i++) {
  102. if (name.equals(tasks[i].getTaskName())) {
  103. elems.addElement(tasks[i]);
  104. }
  105. }
  106. return elems;
  107. }
  108. }