1 11 12 package org.eclipse.ant.internal.ui.model; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.apache.tools.ant.BuildException; 20 import org.apache.tools.ant.Task; 21 import org.eclipse.ant.core.AntSecurityException; 22 import org.eclipse.ant.internal.ui.AntUIImages; 23 import org.eclipse.ant.internal.ui.IAntUIConstants; 24 import org.eclipse.ant.internal.ui.editor.AntEditorCompletionProcessor; 25 import org.eclipse.ant.internal.ui.preferences.AntEditorPreferenceConstants; 26 import org.eclipse.jface.resource.ImageDescriptor; 27 import org.eclipse.jface.text.IRegion; 28 import org.xml.sax.Attributes ; 29 30 public class AntPropertyNode extends AntTaskNode { 31 32 private String fValue= null; 33 private String fReferencedName; 34 private String fOccurrencesStartingPoint= IAntModelConstants.ATTR_VALUE; 35 private String fOccurrencesIdentifier; 36 37 41 private Map fProperties= null; 42 43 public AntPropertyNode(Task task, Attributes attributes) { 44 super(task); 45 String label = attributes.getValue(IAntModelConstants.ATTR_NAME); 46 if (label == null) { 47 label = attributes.getValue(IAntModelConstants.ATTR_FILE); 48 if (label != null) { 49 fReferencedName= label; 50 label= "file="+label; } else { 52 label = attributes.getValue(IAntModelConstants.ATTR_RESOURCE); 53 if (label != null) { 54 fReferencedName= label; 55 label= "resource="+label; } else { 57 label = attributes.getValue(IAntModelConstants.ATTR_ENVIRONMENT); 58 if (label != null) { 59 label= "environment=" + label; } else { 61 label = attributes.getValue("srcFile"); if (label != null) { 63 fReferencedName= label; 64 label= "srcFile=" + label; } 66 } 67 } 68 } 69 } else { 70 fValue= attributes.getValue(IAntModelConstants.ATTR_VALUE); 71 if (fValue == null) { 72 fOccurrencesStartingPoint= IAntModelConstants.ATTR_LOCATION; 73 fValue= attributes.getValue(fOccurrencesStartingPoint); 74 } 75 } 76 setBaseLabel(label); 77 } 78 79 public String getProperty(String propertyName) { 80 if (fProperties != null) { 81 return (String )fProperties.get(propertyName); 82 } 83 return null; 84 } 85 86 89 protected ImageDescriptor getBaseImageDescriptor() { 90 return AntUIImages.getImageDescriptor(IAntUIConstants.IMG_PROPERTY); 91 } 92 93 96 public boolean configure(boolean validateFully) { 97 if (fConfigured) { 98 return false; 99 } 100 try { 101 getProjectNode().setCurrentConfiguringProperty(this); 102 getTask().maybeConfigure(); 103 getTask().execute(); 104 fConfigured= true; 105 } catch (BuildException be) { 106 handleBuildException(be, AntEditorPreferenceConstants.PROBLEM_PROPERTIES); 107 } catch (LinkageError le) { 108 handleBuildException(new BuildException(AntModelMessages.AntPropertyNode_0), AntEditorPreferenceConstants.PROBLEM_PROPERTIES); 114 } catch (AntSecurityException se) { 115 handleBuildException(new BuildException(AntModelMessages.AntPropertyNode_1), AntEditorPreferenceConstants.PROBLEM_SECURITY); 117 } finally { 118 getProjectNode().setCurrentConfiguringProperty(null); 119 } 120 return false; 121 } 122 123 128 public void addProperty(String propertyName, String value) { 129 if (fProperties == null) { 130 fProperties= new HashMap (1); 131 } 132 fProperties.put(propertyName, value); 133 } 134 135 138 public String getReferencedElement(int offset) { 139 if (fReferencedName != null) { 140 String textToSearch= getAntModel().getText(getOffset(), offset - getOffset()); 141 if (textToSearch != null && textToSearch.length() != 0) { 142 String attributeString = AntEditorCompletionProcessor.getAttributeStringFromDocumentStringToPrefix(textToSearch); 143 if ("file".equals(attributeString) || "resource".equals(attributeString) || "srcFile".equals(attributeString)) { return fReferencedName; 145 } 146 } 147 } 148 return null; 149 } 150 151 154 public boolean containsOccurrence(String identifier) { 155 if (!getTask().getTaskName().equals("property")) { return super.containsOccurrence(identifier); 157 } 158 159 if (fValue != null) { 160 return fValue.indexOf(identifier) != -1; 161 } 162 return false; 163 } 164 165 168 public String getOccurrencesIdentifier() { 169 if (fOccurrencesIdentifier == null) { 170 fOccurrencesIdentifier= new StringBuffer ("${").append(fBaseLabel).append('}').toString(); } 172 return fOccurrencesIdentifier; 173 } 174 175 178 public boolean isRegionPotentialReference(IRegion region) { 179 boolean superOK= super.isRegionPotentialReference(region); 180 if (!getTask().getTaskName().equals("property") || !superOK) { return superOK; 182 } 183 184 String textToSearch= getAntModel().getText(getOffset(), getLength()); 185 if (textToSearch == null) { 186 return false; 187 } 188 int valueOffset= textToSearch.indexOf(fOccurrencesStartingPoint); 189 if (valueOffset > -1) { 190 valueOffset= textToSearch.indexOf('"', valueOffset); 191 if (valueOffset > -1) { 192 boolean inValue= region.getOffset() >= (getOffset() + valueOffset); 193 if (inValue) { 194 if ("{".equals(getAntModel().getText(region.getOffset() - 1, 1)) || "}".equals(getAntModel().getText(region.getOffset() + region.getLength(), 1))) { return true; 196 } 197 return false; 199 } 200 return true; 201 } 202 } 203 return false; 204 } 205 206 public List computeIdentifierOffsets(String identifier) { 207 if (!getTask().getTaskName().equals("property")) { return super.computeIdentifierOffsets(identifier); 209 } 210 String textToSearch= getAntModel().getText(getOffset(), getLength()); 211 if (textToSearch == null || textToSearch.length() == 0 || identifier.length() == 0) { 212 return null; 213 } 214 List results= new ArrayList (); 215 if (fBaseLabel != null) { 216 if (fBaseLabel.equals(identifier)) { 217 int nameOffset= textToSearch.indexOf("name"); nameOffset= textToSearch.indexOf(identifier, nameOffset + 1); 219 results.add(new Integer (getOffset() + nameOffset)); 220 } 221 } 222 if (fValue != null) { 223 int valueOffset= textToSearch.indexOf(fOccurrencesStartingPoint); 224 int endOffset= getOffset() + getLength(); 225 while (valueOffset < endOffset) { 226 valueOffset= textToSearch.indexOf(identifier, valueOffset); 227 if (valueOffset == -1 || valueOffset > endOffset) { 228 break; 229 } 230 results.add(new Integer (getOffset() + valueOffset)); 231 valueOffset+= identifier.length(); 232 } 233 } 234 return results; 235 } 236 237 240 public boolean isFromDeclaration(IRegion region) { 241 if (fBaseLabel == null) { 242 return false; 243 } 244 if (fBaseLabel.length() != region.getLength()) { 245 return false; 246 } 247 int offset= getOffset(); 248 String textToSearch= getAntModel().getText(getOffset(), getLength()); 249 if (textToSearch == null || textToSearch.length() == 0) { 250 return false; 251 } 252 int nameStartOffset= textToSearch.indexOf("name"); nameStartOffset= textToSearch.indexOf("\"", nameStartOffset); int nameEndOffset= textToSearch.indexOf("\"", nameStartOffset + 1); nameEndOffset+=offset; 256 nameStartOffset+=offset; 257 return nameStartOffset <= region.getOffset() && region.getOffset() + region.getLength() <= nameEndOffset; 258 } 259 } 260 | Popular Tags |