KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > Description


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.types;
20
21 import org.apache.tools.ant.Project;
22 import org.apache.tools.ant.ProjectHelper;
23 import org.apache.tools.ant.Task;
24 import org.apache.tools.ant.UnknownElement;
25 import org.apache.tools.ant.Target;
26 import org.apache.tools.ant.helper.ProjectHelperImpl;
27
28 import java.util.Vector JavaDoc;
29
30
31 /**
32  * Description is used to provide a project-wide description element
33  * (that is, a description that applies to a buildfile as a whole).
34  * If present, the <description> element is printed out before the
35  * target descriptions.
36  *
37  * Description has no attributes, only text. There can only be one
38  * project description per project. A second description element will
39  * overwrite the first.
40  *
41  *
42  * @ant.datatype ignore="true"
43  */

44 public class Description extends DataType {
45
46     /**
47      * Adds descriptive text to the project.
48      *
49      * @param text the descriptive text
50      */

51     public void addText(String JavaDoc text) {
52
53         ProjectHelper ph = ProjectHelper.getProjectHelper();
54         if (!(ph instanceof ProjectHelperImpl)) {
55             // New behavior for delayed task creation. Description
56
// will be evaluated in Project.getDescription()
57
return;
58         }
59         String JavaDoc currentDescription = getProject().getDescription();
60         if (currentDescription == null) {
61             getProject().setDescription(text);
62         } else {
63             getProject().setDescription(currentDescription + text);
64         }
65     }
66
67     /**
68      * Return the descriptions from all the targets of
69      * a project.
70      *
71      * @param project the project to get the descriptions for.
72      * @return a string containing the concatenated descriptions of
73      * the targets.
74      */

75     public static String JavaDoc getDescription(Project project) {
76         Vector JavaDoc targets = (Vector JavaDoc) project.getReference("ant.targets");
77         if (targets == null) {
78             return null;
79         }
80         StringBuffer JavaDoc description = new StringBuffer JavaDoc();
81         for (int i = 0; i < targets.size(); i++) {
82             Target t = (Target) targets.elementAt(i);
83             concatDescriptions(project, t, description);
84         }
85         return description.toString();
86     }
87
88     private static void concatDescriptions(Project project, Target t,
89                                            StringBuffer JavaDoc description) {
90         if (t == null) {
91             return;
92         }
93         Vector JavaDoc tasks = findElementInTarget(project, t, "description");
94         if (tasks == null) {
95             return;
96         }
97         for (int i = 0; i < tasks.size(); i++) {
98             Task task = (Task) tasks.elementAt(i);
99             if (!(task instanceof UnknownElement)) {
100                 continue;
101             }
102             UnknownElement ue = ((UnknownElement) task);
103             String JavaDoc descComp = ue.getWrapper().getText().toString();
104             if (descComp != null) {
105                 description.append(project.replaceProperties(descComp));
106             }
107         }
108     }
109
110     private static Vector JavaDoc findElementInTarget(Project project,
111                                               Target t, String JavaDoc name) {
112         Task[] tasks = t.getTasks();
113         Vector JavaDoc elems = new Vector JavaDoc();
114         for (int i = 0; i < tasks.length; i++) {
115             if (name.equals(tasks[i].getTaskName())) {
116                 elems.addElement(tasks[i]);
117             }
118         }
119         return elems;
120     }
121
122 }
123
Popular Tags