KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > views > elements > ProjectNode


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.views.elements;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Comparator JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.apache.tools.ant.Project;
20 import org.apache.tools.ant.Target;
21 import org.eclipse.ant.core.ProjectInfo;
22 import org.eclipse.ant.core.TargetInfo;
23 import org.eclipse.ant.internal.ui.model.AntUtil;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26
27 /**
28  * Representation of an ant build project.
29  */

30 public class ProjectNode extends AntNode {
31
32     private List JavaDoc targets= null;
33     private TargetNode defaultTarget= null;
34     private String JavaDoc buildFileName;
35     private boolean isErrorNode= false;
36     
37     private String JavaDoc defaultTargetName;
38     /**
39      * Creates a new project node with the given name and the given build file
40      * name.
41      *
42      * @param name the project's name or <code>null</code> if the project's
43      * name is not known. If this value is <code>null</code>, the file will be
44      * parsed the first time a value is requested that requires it.
45      * @param buildFileName
46      */

47     public ProjectNode(String JavaDoc name, String JavaDoc buildFileName) {
48         super(name);
49         this.buildFileName= buildFileName;
50     }
51     
52     /**
53      * Creates a new project node on the given build file.
54      */

55     public ProjectNode(String JavaDoc buildFileName) {
56         this(null, buildFileName);
57     }
58
59     /**
60      * Returns the targets in this project
61      *
62      * @return TargetNode[] the targets in this project
63      */

64     public TargetNode[] getTargets() {
65         if (targets == null) {
66             // Lazily parse the file to populate the targets
67
parseBuildFile();
68         }
69         return (TargetNode[])targets.toArray(new TargetNode[targets.size()]);
70     }
71     
72     /**
73      * Adds the given target to this project
74      *
75      * @param target the target to add
76      */

77     private void addTarget(TargetNode target) {
78         targets.add(target);
79         target.setParent(this);
80     }
81     
82     /**
83      * Sets this project's default target to the given target
84      *
85      * @param target this project's default target
86      */

87     public void setDefaultTarget(TargetNode target) {
88         defaultTarget= target;
89         if (target != null) {
90             defaultTargetName= target.getName();
91         }
92     }
93     
94     /**
95      * Returns the name of the build file containing this project
96      *
97      * @return String the name of this project's build file
98      */

99     public String JavaDoc getBuildFileName() {
100         return buildFileName;
101     }
102     
103     /**
104      * Returns the default target in this project or <code>null</code> if none
105      * has been set
106      *
107      * @return TargetNode the default target or <code>null</code> if none has
108      * been set
109      */

110     public TargetNode getDefaultTarget() {
111         if (targets == null) {
112             // Lazily parse the file to populate the targets
113
parseBuildFile();
114         }
115         return defaultTarget;
116     }
117     
118     /**
119      * Returns the default target name. If this project has not been parsed yet, this
120      * method will return the default target name specified via setDefaultTargetName(String)
121      * or <code>null</code> if no default target name has been specified.
122      *
123      * This method is intended to be used by clients who want to access the name of this
124      * project's default target without forcing the build file to be parsed.
125      *
126      * @return String the name of the default target in this project.
127      */

128     public String JavaDoc getDefaultTargetName() {
129         return defaultTargetName;
130     }
131     
132     /**
133      * Sets the name of this project node's default target.
134      *
135      * @param name the name of this project node's default target
136      * @see ProjectNode#getDefaultTargetName()
137      */

138     public void setDefaultTargetName(String JavaDoc name) {
139         defaultTargetName= name;
140     }
141     
142     /**
143      * Sets this project's error node state
144      *
145      * @param isErrorNode whether or not an error occurred while parsing this node
146      */

147     public void setIsErrorNode(boolean isErrorNode) {
148         this.isErrorNode= isErrorNode;
149     }
150     
151     /**
152      * Returns whether an error occurred while parsing this Ant node
153      *
154      * @return whether an error occurred while parsing this Ant node
155      */

156     public boolean isErrorNode() {
157         return isErrorNode;
158     }
159     
160     /**
161      * Parses the given build file and populates the targets contained in the
162      * build file. If an error occurs while parsing the file, the error
163      * state will be set and a target error node will be added
164      */

165     public void parseBuildFile() {
166         clear();
167         TargetInfo[] infos = null;
168         IPath buildFilePath= AntUtil.getFile(getBuildFileName()).getLocation();
169         if (buildFilePath == null) {
170             setErrorMessage(AntViewElementsMessages.getString("ProjectNode.Build_file_not_found_1")); //$NON-NLS-1$
171
return;
172         }
173         try {
174             infos = AntUtil.getTargets(buildFilePath.toString());
175         } catch (CoreException e) {
176             setErrorMessage(e.getMessage());
177             return;
178         }
179         if (infos.length < 1) {
180             setErrorMessage(AntViewElementsMessages.getString("ProjectNode.No_targets")); //$NON-NLS-1$
181
return;
182         }
183         ProjectInfo projectInfo= infos[0].getProject();
184         // Create Apache Ant objects
185
Project project = new Project();
186         if (projectInfo.getName() != null) {
187             project.setName(projectInfo.getName());
188         }
189         
190         for (int i = 0; i < infos.length; i++) {
191             TargetInfo info = infos[i];
192             if (info.isDefault()) {
193                 project.setDefault(info.getName());
194             }
195             Target target = new Target();
196             target.setName(info.getName());
197             String JavaDoc[] dependencies = info.getDependencies();
198             StringBuffer JavaDoc depends = new StringBuffer JavaDoc();
199             int numDependencies = dependencies.length;
200             if (numDependencies > 0) {
201                 // Onroll the loop to avoid trailing comma
202
depends.append(dependencies[0]);
203             }
204             for (int j = 1; j < numDependencies; j++) {
205                 depends.append(',').append(dependencies[j]);
206             }
207             target.setDepends(depends.toString());
208             target.setDescription(info.getDescription());
209             project.addTarget(target);
210         }
211         if (project.getDefaultTarget() == null) {
212             setErrorMessage(AntViewElementsMessages.getString("ProjectNode.No_default")); //$NON-NLS-1$
213
return;
214         }
215         // Set the project node data based on the Apache Ant data
216
String JavaDoc projectName = project.getName();
217         if (projectName == null) {
218             projectName = AntViewElementsMessages.getString("ProjectNode.<name_unspecified>_1"); //$NON-NLS-1$
219
}
220         // Update the project name
221
setName(projectName);
222         setDescription(projectInfo.getDescription());
223         Enumeration JavaDoc projTargets = project.getTargets().elements();
224         while (projTargets.hasMoreElements()) {
225             Target target = (Target) projTargets.nextElement();
226             // Target Node -----------------
227
Enumeration JavaDoc targetDependencies = target.getDependencies();
228             TargetNode targetNode = new TargetNode(target.getName(), target.getDescription());
229             while (targetDependencies.hasMoreElements()) {
230                 targetNode.addDependency((String JavaDoc) targetDependencies.nextElement());
231             }
232             addTarget(targetNode);
233             if (target.getName().equals(project.getDefaultTarget())) {
234                 setDefaultTarget(targetNode);
235             }
236         }
237         Collections.sort(targets, new Comparator JavaDoc() {
238             /**
239              * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
240              */

241             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
242                 String JavaDoc name1=""; //$NON-NLS-1$
243
String JavaDoc name2= ""; //$NON-NLS-1$
244
if (o1 instanceof TargetNode) {
245                     name1= ((TargetNode)o1).getName();
246                 }
247                 if (o2 instanceof TargetNode) {
248                     name2= ((TargetNode)o2).getName();
249                 }
250                 return name1.compareToIgnoreCase(name2);
251             }
252         });
253     }
254     
255     /**
256      * Clear's this node's internally stored data
257      */

258     private void clear() {
259         targets= new ArrayList JavaDoc();
260         setIsErrorNode(false);
261         setDefaultTarget(null);
262         setDefaultTargetName(null);
263     }
264     
265     /**
266      * Sets the error message of this project and creates a new target child
267      * node with the message
268      *
269      * @param errorMessage the error message generated while parsing this
270      * project
271      */

272     private void setErrorMessage(String JavaDoc errorMessage) {
273         setName(getBuildFileName());
274         setIsErrorNode(true);
275         TargetNode target= new TargetNode(errorMessage, errorMessage);
276         target.setIsErrorNode(true);
277         addTarget(target);
278     }
279
280     /**
281      * Returns the name of this project, parsing the build file first if
282      * necessary.
283      *
284      * @return String this project's name
285      */

286     public String JavaDoc getName() {
287         if (super.getName() == null) {
288             parseBuildFile();
289         }
290         String JavaDoc name= super.getName();
291         if (name == null || name.length() == 0) {
292             name= AntViewElementsMessages.getString("ProjectNode.<name_unspecified>_1"); //$NON-NLS-1$
293
}
294         return name;
295     }
296
297     /**
298      * @see java.lang.Object#toString()
299      */

300     public String JavaDoc toString() {
301         return getName();
302     }
303 }
304
Popular Tags