KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 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.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19 import org.apache.tools.ant.BuildException;
20 import org.apache.tools.ant.RuntimeConfigurable;
21 import org.apache.tools.ant.Task;
22 import org.eclipse.ant.core.AntSecurityException;
23 import org.eclipse.ant.internal.ui.AntUIImages;
24 import org.eclipse.ant.internal.ui.IAntUIConstants;
25 import org.eclipse.ant.internal.ui.preferences.AntEditorPreferenceConstants;
26 import org.eclipse.jface.resource.ImageDescriptor;
27
28
29 public class AntTaskNode extends AntElementNode {
30
31     private Task fTask= null;
32     protected String JavaDoc fBaseLabel= null;
33     protected String JavaDoc fLabel;
34     private String JavaDoc fId= null;
35     protected boolean fConfigured= false;
36     
37     public AntTaskNode(Task task) {
38         super(task.getTaskName());
39         fTask= task;
40     }
41     
42     public AntTaskNode(Task task, String JavaDoc label) {
43         super(task.getTaskName());
44         fTask= task;
45         fBaseLabel= label;
46     }
47     
48     public String JavaDoc getLabel() {
49         if (fLabel == null) {
50             StringBuffer JavaDoc label= new StringBuffer JavaDoc();
51             if (fBaseLabel != null) {
52                 label.append(fBaseLabel);
53             } else if (fId != null) {
54                 label.append(fId);
55             } else {
56                 label.append(fTask.getTaskName());
57             }
58             if (isExternal()) {
59                 appendEntityName(label);
60             }
61             fLabel= label.toString();
62         }
63         return fLabel;
64     }
65     
66     public void setBaseLabel(String JavaDoc label) {
67         fBaseLabel= label;
68     }
69     
70     public Task getTask() {
71         return fTask;
72     }
73     
74     public void setTask(Task task) {
75         fTask= task;
76     }
77     
78     protected ImageDescriptor getBaseImageDescriptor() {
79         if (fId != null) {
80             return AntUIImages.getImageDescriptor(IAntUIConstants.IMG_ANT_TYPE);
81         }
82         
83         return super.getBaseImageDescriptor();
84     }
85
86     /**
87      * The reference id for this task
88      * @param id The reference id for this task
89      */

90     public void setId(String JavaDoc id) {
91         fId= id;
92     }
93     
94     /**
95      * Returns the reference id for this task or <code>null</code>
96      * if it has no reference id.
97      * @return The reference id for this task
98      */

99     public String JavaDoc getId() {
100         return fId;
101     }
102     
103     /**
104      * Configures the associated task if required.
105      * Allows subclasses to do specific configuration (such as executing the task) by
106      * calling <code>nodeSpecificConfigure</code>
107      *
108      * @return whether the configuration of this node could have impact on other nodes
109      */

110     public boolean configure(boolean validateFully) {
111         if (getId() != null) {
112             //ensure that references are set...new for Ant 1.7
113
try {
114                 getProjectNode().getProject().getReference(getId());
115             } catch (BuildException e) {
116                 handleBuildException(e, AntEditorPreferenceConstants.PROBLEM_TASKS);
117             }
118         }
119         if (!validateFully || (getParentNode() instanceof AntTaskNode)) {
120             return false;
121         }
122         if (fConfigured) {
123             return false;
124         }
125         int severity= AntModelProblem.getSeverity(AntEditorPreferenceConstants.PROBLEM_TASKS);
126         if (severity != AntModelProblem.NO_PROBLEM) {
127             //only configure if the user cares about the problems
128
try {
129                 getTask().maybeConfigure();
130                 fConfigured= true;
131                 return true;
132             } catch (BuildException be) {
133                 handleBuildException(be, AntEditorPreferenceConstants.PROBLEM_TASKS);
134             } catch (AntSecurityException se) {
135                 //either a system exit or setting of system property was attempted
136
handleBuildException(new BuildException(AntModelMessages.AntTaskNode_0), AntEditorPreferenceConstants.PROBLEM_SECURITY);
137             }
138         }
139         return false;
140     }
141
142     protected void handleBuildException(BuildException be, String JavaDoc preferenceKey) {
143         int severity= AntModelProblem.getSeverity(preferenceKey);
144         if (severity != AntModelProblem.NO_PROBLEM) {
145             getAntModel().handleBuildException(be, this, severity);
146         }
147     }
148     
149     public boolean containsOccurrence(String JavaDoc identifier) {
150         RuntimeConfigurable wrapper= getTask().getRuntimeConfigurableWrapper();
151         Map JavaDoc attributeMap= wrapper.getAttributeMap();
152         Set JavaDoc keys= attributeMap.keySet();
153         boolean lookingForProperty= identifier.startsWith("${") && identifier.endsWith("}"); //$NON-NLS-1$ //$NON-NLS-2$
154
for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext(); ) {
155             String JavaDoc key= (String JavaDoc) iter.next();
156             String JavaDoc value= (String JavaDoc) attributeMap.get(key);
157             if (lookingForProperty && (key.equals("if") || key.equals("unless"))) { //$NON-NLS-1$ //$NON-NLS-2$
158
if (value.indexOf(identifier.substring(2, identifier.length() - 1)) != -1) {
159                     return true;
160                 }
161             } else if (value.indexOf(identifier) != -1) {
162                 return true;
163             }
164         }
165         StringBuffer JavaDoc text= wrapper.getText();
166         if (text.length() > 0) {
167             if (lookingForProperty && text.indexOf(identifier) != -1) {
168                 return true; //property ref in text
169
}
170         }
171     
172         return false;
173     }
174
175     public List JavaDoc computeIdentifierOffsets(String JavaDoc identifier) {
176         String JavaDoc textToSearch= getAntModel().getText(getOffset(), getLength());
177         if (textToSearch == null || textToSearch.length() == 0 || identifier.length() ==0) {
178             return null;
179         }
180         List JavaDoc results= new ArrayList JavaDoc();
181         RuntimeConfigurable wrapper= getTask().getRuntimeConfigurableWrapper();
182         Map JavaDoc attributeMap= wrapper.getAttributeMap();
183         Set JavaDoc keys= attributeMap.keySet();
184         String JavaDoc lineSep= System.getProperty("line.separator"); //$NON-NLS-1$
185
for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext(); ) {
186             String JavaDoc key = (String JavaDoc) iter.next();
187             String JavaDoc value= (String JavaDoc) attributeMap.get(key);
188             int identifierCorrection= 1;
189             if (value.indexOf(identifier) != -1) {
190                 int keyOffset= textToSearch.indexOf(key);
191                 while (keyOffset > 0 && !Character.isWhitespace(textToSearch.charAt(keyOffset - 1))) {
192                     keyOffset= textToSearch.indexOf(key, keyOffset + 1);
193                 }
194                 int valueOffset= textToSearch.indexOf('"', keyOffset);
195                 int valueLine= ((AntModel)getAntModel()).getLine(getOffset() + valueOffset);
196                 
197                 int withinValueOffset= value.indexOf(identifier);
198                 while (withinValueOffset != -1) {
199                     int resultLine= ((AntModel)getAntModel()).getLine(getOffset() + valueOffset + withinValueOffset);
200                     //the value stored in the attribute map seems to be modified to not contain control characters
201
//new lines, carriage returns and these are replaced with spaces
202
//so if the line separator is greater than 1 in length we need to correct for this
203
int resultOffset= getOffset() + valueOffset + withinValueOffset + identifierCorrection + ((resultLine - valueLine) * (lineSep.length() - 1));
204                     results.add(new Integer JavaDoc(resultOffset));
205                     withinValueOffset= value.indexOf(identifier, withinValueOffset + 1);
206                 }
207             }
208         }
209         
210         String JavaDoc text= wrapper.getText().toString().trim();
211         if (text.length() > 0) {
212             int offset= textToSearch.indexOf(text.toString());
213             offset= textToSearch.indexOf(identifier, offset);
214             results.add(new Integer JavaDoc(offset + getOffset()));
215         }
216         return results;
217     }
218 }
219
Popular Tags