1 11 package org.eclipse.ant.internal.ui.views.elements; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.Comparator ; 16 import java.util.Enumeration ; 17 import java.util.List ; 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 30 public class ProjectNode extends AntNode { 31 32 private List targets= null; 33 private TargetNode defaultTarget= null; 34 private String buildFileName; 35 private boolean isErrorNode= false; 36 37 private String defaultTargetName; 38 47 public ProjectNode(String name, String buildFileName) { 48 super(name); 49 this.buildFileName= buildFileName; 50 } 51 52 55 public ProjectNode(String buildFileName) { 56 this(null, buildFileName); 57 } 58 59 64 public TargetNode[] getTargets() { 65 if (targets == null) { 66 parseBuildFile(); 68 } 69 return (TargetNode[])targets.toArray(new TargetNode[targets.size()]); 70 } 71 72 77 private void addTarget(TargetNode target) { 78 targets.add(target); 79 target.setParent(this); 80 } 81 82 87 public void setDefaultTarget(TargetNode target) { 88 defaultTarget= target; 89 if (target != null) { 90 defaultTargetName= target.getName(); 91 } 92 } 93 94 99 public String getBuildFileName() { 100 return buildFileName; 101 } 102 103 110 public TargetNode getDefaultTarget() { 111 if (targets == null) { 112 parseBuildFile(); 114 } 115 return defaultTarget; 116 } 117 118 128 public String getDefaultTargetName() { 129 return defaultTargetName; 130 } 131 132 138 public void setDefaultTargetName(String name) { 139 defaultTargetName= name; 140 } 141 142 147 public void setIsErrorNode(boolean isErrorNode) { 148 this.isErrorNode= isErrorNode; 149 } 150 151 156 public boolean isErrorNode() { 157 return isErrorNode; 158 } 159 160 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")); 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")); return; 182 } 183 ProjectInfo projectInfo= infos[0].getProject(); 184 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 [] dependencies = info.getDependencies(); 198 StringBuffer depends = new StringBuffer (); 199 int numDependencies = dependencies.length; 200 if (numDependencies > 0) { 201 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")); return; 214 } 215 String projectName = project.getName(); 217 if (projectName == null) { 218 projectName = AntViewElementsMessages.getString("ProjectNode.<name_unspecified>_1"); } 220 setName(projectName); 222 setDescription(projectInfo.getDescription()); 223 Enumeration projTargets = project.getTargets().elements(); 224 while (projTargets.hasMoreElements()) { 225 Target target = (Target) projTargets.nextElement(); 226 Enumeration targetDependencies = target.getDependencies(); 228 TargetNode targetNode = new TargetNode(target.getName(), target.getDescription()); 229 while (targetDependencies.hasMoreElements()) { 230 targetNode.addDependency((String ) targetDependencies.nextElement()); 231 } 232 addTarget(targetNode); 233 if (target.getName().equals(project.getDefaultTarget())) { 234 setDefaultTarget(targetNode); 235 } 236 } 237 Collections.sort(targets, new Comparator () { 238 241 public int compare(Object o1, Object o2) { 242 String name1=""; String name2= ""; 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 258 private void clear() { 259 targets= new ArrayList (); 260 setIsErrorNode(false); 261 setDefaultTarget(null); 262 setDefaultTargetName(null); 263 } 264 265 272 private void setErrorMessage(String errorMessage) { 273 setName(getBuildFileName()); 274 setIsErrorNode(true); 275 TargetNode target= new TargetNode(errorMessage, errorMessage); 276 target.setIsErrorNode(true); 277 addTarget(target); 278 } 279 280 286 public String getName() { 287 if (super.getName() == null) { 288 parseBuildFile(); 289 } 290 String name= super.getName(); 291 if (name == null || name.length() == 0) { 292 name= AntViewElementsMessages.getString("ProjectNode.<name_unspecified>_1"); } 294 return name; 295 } 296 297 300 public String toString() { 301 return getName(); 302 } 303 } 304 | Popular Tags |