KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > model > AntDefiningTaskNode


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

11
12 package org.eclipse.ant.internal.ui.model;
13
14 import java.io.File JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Hashtable JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.ComponentHelper;
24 import org.apache.tools.ant.Task;
25 import org.apache.tools.ant.UnknownElement;
26 import org.eclipse.ant.core.AntCorePlugin;
27 import org.eclipse.ant.core.AntCorePreferences;
28 import org.eclipse.ant.core.AntSecurityException;
29 import org.eclipse.ant.internal.ui.AntUIImages;
30 import org.eclipse.ant.internal.ui.AntUIPlugin;
31 import org.eclipse.ant.internal.ui.IAntUIConstants;
32 import org.eclipse.ant.internal.ui.preferences.AntEditorPreferenceConstants;
33 import org.eclipse.core.runtime.FileLocator;
34 import org.eclipse.jface.preference.IPreferenceStore;
35 import org.eclipse.jface.resource.ImageDescriptor;
36 import org.xml.sax.Attributes JavaDoc;
37
38 public class AntDefiningTaskNode extends AntTaskNode {
39     private String JavaDoc fIdentifier= null;
40     
41     public AntDefiningTaskNode(Task task, Attributes JavaDoc attributes) {
42         super(task);
43         String JavaDoc label= attributes.getValue(IAntModelConstants.ATTR_NAME);
44         if (label == null) {
45             label= task.getTaskName();
46         
47             String JavaDoc file= attributes.getValue(IAntModelConstants.ATTR_FILE);
48             if(file != null) {
49                 label= label + " " + file; //$NON-NLS-1$
50
fIdentifier= file;
51             } else {
52                 String JavaDoc resource= attributes.getValue(IAntModelConstants.ATTR_RESOURCE);
53                 if (resource != null) {
54                     label= label + " " + resource; //$NON-NLS-1$
55
fIdentifier= resource;
56                 }
57             }
58         } else {
59             fIdentifier= label;
60         }
61         
62         setBaseLabel(label);
63     }
64     
65     protected ImageDescriptor getBaseImageDescriptor() {
66         String JavaDoc taskName= getTask().getTaskName();
67         if ("taskdef".equalsIgnoreCase(taskName) || "typedef".equalsIgnoreCase(taskName)) { //$NON-NLS-1$//$NON-NLS-2$
68
return AntUIImages.getImageDescriptor(IAntUIConstants.IMG_ANT_TASKDEF);
69         }
70         return AntUIImages.getImageDescriptor(IAntUIConstants.IMG_ANT_MACRODEF);
71     }
72     
73     /**
74      * Execute the defining task.
75      */

76     public boolean configure(boolean validateFully) {
77         if (fConfigured) {
78             return false;
79         }
80         if (shouldConfigure()) {
81             try {
82                 ComponentHelper helper= ComponentHelper.getComponentHelper(getProjectNode().getProject());
83                 ((AntModel) getAntModel()).removeDefinerTasks(getIdentifier(), helper.getAntTypeTable());
84                 Hashtable JavaDoc old= new Hashtable JavaDoc(helper.getAntTypeTable());
85                 getTask().maybeConfigure();
86                 getTask().execute();
87                 Iterator JavaDoc newNames= helper.getAntTypeTable().keySet().iterator();
88                 List JavaDoc defined= new ArrayList JavaDoc();
89                 while (newNames.hasNext()) {
90                     String JavaDoc name = (String JavaDoc) newNames.next();
91                     if (old.get(name) == null) {
92                         defined.add(name);
93                     }
94                 }
95                 ((AntModel) getAntModel()).addDefinedTasks(defined, this);
96                 return false;
97             } catch (BuildException be) {
98                 ((AntModel)getAntModel()).removeDefiningTaskNodeInfo(this);
99                 handleBuildException(be, AntEditorPreferenceConstants.PROBLEM_CLASSPATH);
100             } catch (LinkageError JavaDoc e) {
101                 //A classpath problem with the definer. Possible causes are having multiple
102
//versions of the same JAR on the Ant runtime classpath (either explicitly or via the plugin
103
//classloaders. See bug 71888
104
((AntModel)getAntModel()).removeDefiningTaskNodeInfo(this);
105                 handleBuildException(new BuildException(AntModelMessages.AntDefiningTaskNode_0), AntEditorPreferenceConstants.PROBLEM_CLASSPATH);
106             } catch (AntSecurityException se) {
107                 //either a system exit or setting of system property was attempted
108
((AntModel)getAntModel()).removeDefiningTaskNodeInfo(this);
109                 handleBuildException(new BuildException(AntModelMessages.AntDefiningTaskNode_1), AntEditorPreferenceConstants.PROBLEM_SECURITY);
110             }
111         }
112         return false;
113     }
114     
115     public Object JavaDoc getRealTask() {
116         Task task= getTask();
117         if (task instanceof UnknownElement) {
118             task.maybeConfigure();
119             return ((UnknownElement)task).getRealThing();
120         }
121         return task;
122     }
123     
124     protected String JavaDoc getIdentifier() {
125         return fIdentifier;
126     }
127     
128     /*
129      * Sets the Java class path in org.apache.tools.ant.types.Path
130      * so that the classloaders defined by these "definer" tasks will have the
131      * correct classpath.
132      */

133     public static void setJavaClassPath() {
134         
135         AntCorePreferences prefs= AntCorePlugin.getPlugin().getPreferences();
136         URL JavaDoc[] antClasspath= prefs.getURLs();
137         
138         setJavaClassPath(antClasspath);
139     }
140     
141     /*
142      * Sets the Java class path in org.apache.tools.ant.types.Path
143      * so that the classloaders defined by these "definer" tasks will have the
144      * correct classpath.
145      */

146     public static void setJavaClassPath(URL JavaDoc[] antClasspath) {
147         
148         StringBuffer JavaDoc buff= new StringBuffer JavaDoc();
149         File JavaDoc file= null;
150         for (int i = 0; i < antClasspath.length; i++) {
151             try {
152                 file = new File JavaDoc(FileLocator.toFileURL(antClasspath[i]).getPath());
153             } catch (IOException JavaDoc e) {
154                 continue;
155             }
156             buff.append(file.getAbsolutePath());
157             buff.append("; "); //$NON-NLS-1$
158
}
159
160         org.apache.tools.ant.types.Path systemClasspath= new org.apache.tools.ant.types.Path(null, buff.substring(0, buff.length() - 2));
161         org.apache.tools.ant.types.Path.systemClasspath= systemClasspath;
162     }
163     
164     public boolean collapseProjection() {
165         IPreferenceStore store= AntUIPlugin.getDefault().getPreferenceStore();
166         if (store.getBoolean(AntEditorPreferenceConstants.EDITOR_FOLDING_DEFINING)) {
167             return true;
168         }
169         return false;
170     }
171
172     /* (non-Javadoc)
173      * @see org.eclipse.ant.internal.ui.model.AntElementNode#setLength(int)
174      */

175     public void setLength(int length) {
176         super.setLength(length);
177         if (shouldConfigure()) {
178             getAntModel().setDefiningTaskNodeText(this);
179         }
180     }
181
182     private boolean shouldConfigure() {
183         IPreferenceStore store= AntUIPlugin.getDefault().getPreferenceStore();
184         return store.getBoolean(AntEditorPreferenceConstants.CODEASSIST_USER_DEFINED_TASKS);
185     }
186
187     protected void setNeedsToBeConfigured(boolean configure) {
188        fConfigured= !configure;
189     }
190 }
191
Popular Tags