1 11 12 package org.eclipse.ant.internal.ui.model; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.Set ; 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 fBaseLabel= null; 33 protected String fLabel; 34 private String 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 label) { 43 super(task.getTaskName()); 44 fTask= task; 45 fBaseLabel= label; 46 } 47 48 public String getLabel() { 49 if (fLabel == null) { 50 StringBuffer label= new StringBuffer (); 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 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 90 public void setId(String id) { 91 fId= id; 92 } 93 94 99 public String getId() { 100 return fId; 101 } 102 103 110 public boolean configure(boolean validateFully) { 111 if (getId() != null) { 112 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 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 handleBuildException(new BuildException(AntModelMessages.AntTaskNode_0), AntEditorPreferenceConstants.PROBLEM_SECURITY); 137 } 138 } 139 return false; 140 } 141 142 protected void handleBuildException(BuildException be, String 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 identifier) { 150 RuntimeConfigurable wrapper= getTask().getRuntimeConfigurableWrapper(); 151 Map attributeMap= wrapper.getAttributeMap(); 152 Set keys= attributeMap.keySet(); 153 boolean lookingForProperty= identifier.startsWith("${") && identifier.endsWith("}"); for (Iterator iter = keys.iterator(); iter.hasNext(); ) { 155 String key= (String ) iter.next(); 156 String value= (String ) attributeMap.get(key); 157 if (lookingForProperty && (key.equals("if") || key.equals("unless"))) { 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 text= wrapper.getText(); 166 if (text.length() > 0) { 167 if (lookingForProperty && text.indexOf(identifier) != -1) { 168 return true; } 170 } 171 172 return false; 173 } 174 175 public List computeIdentifierOffsets(String identifier) { 176 String textToSearch= getAntModel().getText(getOffset(), getLength()); 177 if (textToSearch == null || textToSearch.length() == 0 || identifier.length() ==0) { 178 return null; 179 } 180 List results= new ArrayList (); 181 RuntimeConfigurable wrapper= getTask().getRuntimeConfigurableWrapper(); 182 Map attributeMap= wrapper.getAttributeMap(); 183 Set keys= attributeMap.keySet(); 184 String lineSep= System.getProperty("line.separator"); for (Iterator iter = keys.iterator(); iter.hasNext(); ) { 186 String key = (String ) iter.next(); 187 String value= (String ) 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 int resultOffset= getOffset() + valueOffset + withinValueOffset + identifierCorrection + ((resultLine - valueLine) * (lineSep.length() - 1)); 204 results.add(new Integer (resultOffset)); 205 withinValueOffset= value.indexOf(identifier, withinValueOffset + 1); 206 } 207 } 208 } 209 210 String 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 (offset + getOffset())); 215 } 216 return results; 217 } 218 } 219 | Popular Tags |