1 12 13 package org.eclipse.ant.internal.ui.model; 14 15 import java.io.File ; 16 import java.io.IOException ; 17 import java.net.URLClassLoader ; 18 import java.util.ArrayList ; 19 import java.util.Arrays ; 20 import java.util.Collection ; 21 import java.util.Enumeration ; 22 import java.util.HashMap ; 23 import java.util.Hashtable ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 import java.util.Properties ; 28 import java.util.Set ; 29 import java.util.Stack ; 30 31 import org.apache.tools.ant.AntTypeDefinition; 32 import org.apache.tools.ant.BuildException; 33 import org.apache.tools.ant.ComponentHelper; 34 import org.apache.tools.ant.IntrospectionHelper; 35 import org.apache.tools.ant.Location; 36 import org.apache.tools.ant.Main; 37 import org.apache.tools.ant.Project; 38 import org.apache.tools.ant.RuntimeConfigurable; 39 import org.apache.tools.ant.Target; 40 import org.apache.tools.ant.Task; 41 import org.apache.tools.ant.TaskAdapter; 42 import org.apache.tools.ant.UnknownElement; 43 import org.eclipse.ant.core.AntCorePlugin; 44 import org.eclipse.ant.core.AntCorePreferences; 45 import org.eclipse.ant.core.AntSecurityException; 46 import org.eclipse.ant.core.Property; 47 import org.eclipse.ant.core.Type; 48 import org.eclipse.ant.internal.core.AntClassLoader; 49 import org.eclipse.ant.internal.core.AntCoreUtil; 50 import org.eclipse.ant.internal.core.AntSecurityManager; 51 import org.eclipse.ant.internal.core.IAntCoreConstants; 52 import org.eclipse.ant.internal.ui.AntUIPlugin; 53 import org.eclipse.ant.internal.ui.AntUtil; 54 import org.eclipse.ant.internal.ui.editor.DecayCodeCompletionDataStructuresThread; 55 import org.eclipse.ant.internal.ui.editor.outline.AntEditorMarkerUpdater; 56 import org.eclipse.ant.internal.ui.editor.utils.ProjectHelper; 57 import org.eclipse.ant.internal.ui.preferences.AntEditorPreferenceConstants; 58 import org.eclipse.core.resources.IFile; 59 import org.eclipse.core.resources.ResourcesPlugin; 60 import org.eclipse.core.runtime.CoreException; 61 import org.eclipse.core.runtime.IPath; 62 import org.eclipse.core.runtime.Preferences; 63 import org.eclipse.core.variables.VariablesPlugin; 64 import org.eclipse.jface.text.BadLocationException; 65 import org.eclipse.jface.text.DocumentEvent; 66 import org.eclipse.jface.text.IDocument; 67 import org.eclipse.jface.text.IDocumentListener; 68 import org.eclipse.jface.text.ISynchronizable; 69 import org.xml.sax.Attributes ; 70 import org.xml.sax.SAXParseException ; 71 72 import com.ibm.icu.text.MessageFormat; 73 74 public class AntModel implements IAntModel { 75 76 private static ClassLoader fgClassLoader; 77 private static int fgInstanceCount= 0; 78 79 private IDocument fDocument; 80 private IProblemRequestor fProblemRequestor; 81 private LocationProvider fLocationProvider; 82 83 private AntProjectNode fProjectNode; 84 private AntTargetNode fCurrentTargetNode; 85 private AntElementNode fLastNode; 86 private AntElementNode fNodeBeingResolved; 87 private int fNodeBeingResolvedIndex= -1; 88 89 private Map fEntityNameToPath; 90 91 96 private Stack fStillOpenElements = new Stack (); 97 98 private Map fTaskToNode= new HashMap (); 99 100 private List fTaskNodes= new ArrayList (); 101 102 private final Object fDirtyLock= new Object (); 103 private boolean fIsDirty= true; 104 private File fEditedFile= null; 105 106 private ClassLoader fLocalClassLoader= null; 107 108 private boolean fHasLexicalInfo= true; 109 private boolean fHasPositionInfo= true; 110 private boolean fHasTaskInfo= true; 111 112 private IDocumentListener fListener; 113 private AntEditorMarkerUpdater fMarkerUpdater= null; 114 private List fNonStructuralNodes= new ArrayList (1); 115 116 private Preferences.IPropertyChangeListener fCorePropertyChangeListener; 117 118 private Preferences.IPropertyChangeListener fUIPropertyChangeListener; 119 120 private Map fProperties= null; 121 private List fPropertyFiles= null; 122 123 private Map fDefinersToText; 124 private Map fPreviousDefinersToText; 125 private Map fDefinerNodeIdentifierToDefinedTasks; 126 private Map fTaskNameToDefiningNode; 127 private Map fCurrentNodeIdentifiers; 128 129 private boolean fReportingProblemsCurrent= false; 130 private boolean fDoNotReportProblems= false; 131 private boolean fShouldReconcile= true; 132 private HashMap fNamespacePrefixMappings; 133 134 public AntModel(IDocument document, IProblemRequestor problemRequestor, LocationProvider locationProvider) { 135 init(document, problemRequestor, locationProvider); 136 137 fMarkerUpdater= new AntEditorMarkerUpdater(); 138 fMarkerUpdater.setModel(this); 139 fCorePropertyChangeListener= new Preferences.IPropertyChangeListener() { 140 143 public void propertyChange(Preferences.PropertyChangeEvent event) { 144 if (event.getProperty().equals(IAntCoreConstants.PREFERENCE_CLASSPATH_CHANGED)) { 145 if (((Boolean )event.getNewValue()) == Boolean.TRUE) { 146 reconcileForPropertyChange(true); 147 } 148 } 149 } 150 }; 151 AntCorePlugin.getPlugin().getPluginPreferences().addPropertyChangeListener(fCorePropertyChangeListener); 152 153 fUIPropertyChangeListener= new Preferences.IPropertyChangeListener() { 154 157 public void propertyChange(Preferences.PropertyChangeEvent event) { 158 String property= event.getProperty(); 159 if (property.equals(AntEditorPreferenceConstants.PROBLEM)) { 160 AntUIPlugin.getDefault().getPluginPreferences().removePropertyChangeListener(fUIPropertyChangeListener); 161 reconcileForPropertyChange(false); 162 AntUIPlugin.getDefault().getPluginPreferences().setToDefault(AntEditorPreferenceConstants.PROBLEM); 163 AntUIPlugin.getDefault().getPluginPreferences().addPropertyChangeListener(fUIPropertyChangeListener); 164 } else if (property.equals(AntEditorPreferenceConstants.CODEASSIST_USER_DEFINED_TASKS)) { 165 reconcileForPropertyChange(false); 166 } else if (property.equals(AntEditorPreferenceConstants.BUILDFILE_NAMES_TO_IGNORE) || property.equals(AntEditorPreferenceConstants.BUILDFILE_IGNORE_ALL)) { 167 fReportingProblemsCurrent= false; 168 reconcileForPropertyChange(false); 169 } 170 } 171 }; 172 AntUIPlugin.getDefault().getPluginPreferences().addPropertyChangeListener(fUIPropertyChangeListener); 173 } 174 175 public AntModel(IDocument document, IProblemRequestor problemRequestor, LocationProvider locationProvider, boolean resolveLexicalInfo, boolean resolvePositionInfo, boolean resolveTaskInfo) { 176 init(document, problemRequestor, locationProvider); 177 178 fHasLexicalInfo= resolveLexicalInfo; 179 fHasPositionInfo= resolvePositionInfo; 180 fHasTaskInfo= resolveTaskInfo; 181 } 182 183 private void init(IDocument document, IProblemRequestor problemRequestor, LocationProvider locationProvider) { 184 fDocument= document; 185 fProblemRequestor= problemRequestor; 186 fLocationProvider= locationProvider; 187 if (fgInstanceCount == 0) { 188 AntDefiningTaskNode.setJavaClassPath(); 191 AntModelCore.getDefault().startBreakpointListening(); 192 } 193 fgInstanceCount++; 194 DecayCodeCompletionDataStructuresThread.cancel(); 195 } 196 197 200 public void dispose() { 201 synchronized (getLockObject()) { 202 if (fDocument != null && fListener != null) { 203 fDocument.removeDocumentListener(fListener); 204 } 205 fDocument= null; 206 fLocationProvider= null; 207 ProjectHelper.setAntModel(null); 208 } 209 210 if (fCorePropertyChangeListener != null) { 211 AntCorePlugin.getPlugin().getPluginPreferences().removePropertyChangeListener(fCorePropertyChangeListener); 212 } 213 if (fUIPropertyChangeListener != null) { 214 AntUIPlugin.getDefault().getPluginPreferences().removePropertyChangeListener(fUIPropertyChangeListener); 215 } 216 fgInstanceCount--; 217 if (fgInstanceCount == 0) { 218 fgClassLoader= null; 219 DecayCodeCompletionDataStructuresThread.getDefault().start(); 220 AntModelCore.getDefault().stopBreakpointListening(); 221 cleanup(); 222 } 223 } 224 225 private Object getLockObject() { 226 if (fDocument instanceof ISynchronizable) { 227 Object lock= ((ISynchronizable)fDocument).getLockObject(); 228 if (lock != null) { 229 return lock; 230 } 231 } 232 return this; 233 } 234 235 private void cleanup() { 236 AntProjectNode projectNode= getProjectNode(); 237 if (projectNode != null) { 238 IntrospectionHelper.getHelper(projectNode.getProject(), Small.class); 240 projectNode.getProject().fireBuildFinished(null); 241 } 242 } 243 244 247 public void reconcile() { 248 synchronized (fDirtyLock) { 249 if (!fShouldReconcile || !fIsDirty) { 250 return; 251 } 252 fIsDirty= false; 253 } 254 255 synchronized (getLockObject()) { 256 if (fLocationProvider == null) { 257 return; 259 } 260 261 if (fDocument == null) { 262 fProjectNode= null; 263 } else { 264 reset(); 265 parseDocument(fDocument); 266 reconcileTaskAndTypes(); 267 } 268 AntModelCore.getDefault().notifyAntModelListeners(new AntModelChangeEvent(this)); 269 } 270 } 271 272 private void reset() { 273 fCurrentTargetNode= null; 274 fStillOpenElements= new Stack (); 275 fTaskToNode= new HashMap (); 276 fTaskNodes= new ArrayList (); 277 fNodeBeingResolved= null; 278 fNodeBeingResolvedIndex= -1; 279 fLastNode= null; 280 fCurrentNodeIdentifiers= null; 281 fNamespacePrefixMappings= null; 282 283 fNonStructuralNodes= new ArrayList (1); 284 if (fDefinersToText != null) { 285 fPreviousDefinersToText= new HashMap (fDefinersToText); 286 fDefinersToText= null; 287 } 288 } 289 290 private void parseDocument(IDocument input) { 291 boolean parsed= true; 292 if (input.getLength() == 0) { 293 fProjectNode= null; 294 parsed= false; 295 return; 296 } 297 ClassLoader originalClassLoader= Thread.currentThread().getContextClassLoader(); 298 ClassLoader parsingClassLoader= getClassLoader(originalClassLoader); 299 Thread.currentThread().setContextClassLoader(parsingClassLoader); 300 Project project= null; 301 try { 302 ProjectHelper projectHelper= null; 303 String textToParse= input.get(); 304 if (fProjectNode == null || !fProjectNode.hasChildren()) { 305 fProjectNode= null; 306 project = new AntModelProject(); 307 projectHelper= prepareForFullParse(project, parsingClassLoader); 308 } else { 309 project= fProjectNode.getProject(); 310 projectHelper= (ProjectHelper)project.getReference("ant.projectHelper"); projectHelper.setBuildFile(getEditedFile()); 312 prepareForFullIncremental(); 313 } 314 beginReporting(); 315 Map references= project.getReferences(); 316 references.remove("ant.parsing.context"); ProjectHelper.setAntModel(this); 318 projectHelper.parse(project, textToParse); 319 320 } catch(BuildException e) { 321 handleBuildException(e, null); 322 } finally { 323 if (parsed) { 324 SecurityManager origSM= System.getSecurityManager(); 325 processAntHome(true); 326 try { 327 System.setSecurityManager(new AntSecurityManager(origSM, Thread.currentThread(), false)); 329 resolveBuildfile(); 330 endReporting(); 331 } catch (AntSecurityException e) { 332 333 } finally { 334 Thread.currentThread().setContextClassLoader(originalClassLoader); 335 getClassLoader(null); 336 System.setSecurityManager(origSM); 337 project.fireBuildFinished(null); } 339 } 340 } 341 } 342 343 private ProjectHelper prepareForFullParse(Project project, ClassLoader parsingClassLoader) { 344 initializeProject(project, parsingClassLoader); 345 File file = getEditedFile(); 349 String filePath= ""; if (file != null) { 351 filePath= file.getAbsolutePath(); 352 } 353 project.setUserProperty("ant.file", filePath); project.setUserProperty("ant.version", Main.getAntVersion()); 356 ProjectHelper projectHelper= new ProjectHelper(this); 357 projectHelper.setBuildFile(file); 358 project.addReference("ant.projectHelper", projectHelper); return projectHelper; 360 } 361 362 private void prepareForFullIncremental() { 363 fProjectNode.reset(); 364 fTaskToNode= new HashMap (); 365 fTaskNodes= new ArrayList (); 366 } 367 368 private void initializeProject(Project project, ClassLoader loader) { 369 processAntHome(false); 370 project.init(); 371 setProperties(project); 372 setTasks(project, loader); 373 setTypes(project, loader); 374 } 375 376 private void setTasks(Project project, ClassLoader loader) { 377 List tasks = AntCorePlugin.getPlugin().getPreferences().getTasks(); 378 for (Iterator iterator = tasks.iterator(); iterator.hasNext();) { 379 org.eclipse.ant.core.Task task = (org.eclipse.ant.core.Task) iterator.next(); 380 AntTypeDefinition def= new AntTypeDefinition(); 381 def.setName(task.getTaskName()); 382 def.setClassName(task.getClassName()); 383 def.setClassLoader(loader); 384 def.setAdaptToClass(Task.class); 385 def.setAdapterClass(TaskAdapter.class); 386 ComponentHelper.getComponentHelper(project).addDataTypeDefinition(def); 387 } 388 } 389 390 private void setTypes(Project project, ClassLoader loader) { 391 List types = AntCorePlugin.getPlugin().getPreferences().getTypes(); 392 for (Iterator iterator = types.iterator(); iterator.hasNext();) { 393 Type type = (Type) iterator.next(); 394 AntTypeDefinition def = new AntTypeDefinition(); 395 def.setName(type.getTypeName()); 396 def.setClassName(type.getClassName()); 397 def.setClassLoader(loader); 398 ComponentHelper.getComponentHelper(project).addDataTypeDefinition(def); 399 } 400 } 401 402 private void setProperties(Project project) { 403 setBuiltInProperties(project); 404 setExtraProperties(project); 405 setGlobalProperties(project); 406 loadExtraPropertyFiles(project); 407 loadPropertyFiles(project); 408 } 409 410 private void setExtraProperties(Project project) { 411 if (fProperties != null) { 412 for (Iterator iter = fProperties.keySet().iterator(); iter.hasNext();) { 413 String name = (String ) iter.next(); 414 String value= (String ) fProperties.get(name); 415 try { 416 value= VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(value); 417 } catch (CoreException e) { 418 } 419 if (value != null) { 420 project.setUserProperty(name, value); 421 } 422 } 423 } 424 } 425 426 private void loadExtraPropertyFiles(Project project) { 427 if (fPropertyFiles != null) { 428 try { 429 List allProperties= AntCoreUtil.loadPropertyFiles(fPropertyFiles, project.getUserProperty("basedir"), getEditedFile().getAbsolutePath()); setPropertiesFromFiles(project, allProperties); 431 } catch (IOException e1) { 432 AntUIPlugin.log(e1); 433 } 434 } 435 } 436 437 440 private void loadPropertyFiles(Project project) { 441 List fileNames= Arrays.asList(AntCorePlugin.getPlugin().getPreferences().getCustomPropertyFiles()); 442 try { 443 List allProperties= AntCoreUtil.loadPropertyFiles(fileNames, project.getUserProperty("basedir"), getEditedFile().getAbsolutePath()); setPropertiesFromFiles(project, allProperties); 445 } catch (IOException e1) { 446 AntUIPlugin.log(e1); 447 } 448 } 449 450 private void setPropertiesFromFiles(Project project, List allProperties) { 451 Iterator iter= allProperties.iterator(); 452 while (iter.hasNext()) { 453 Properties props = (Properties ) iter.next(); 454 Enumeration propertyNames = props.propertyNames(); 455 while (propertyNames.hasMoreElements()) { 456 String name = (String ) propertyNames.nextElement(); 457 if (project.getUserProperty(name) == null) { 459 project.setUserProperty(name, props.getProperty(name)); 460 } 461 } 462 } 463 } 464 465 private void setBuiltInProperties(Project project) { 466 project.setUserProperty("ant.file", getEditedFile().getAbsolutePath()); project.setUserProperty("ant.version", Main.getAntVersion()); } 470 471 private void processAntHome(boolean finished) { 472 AntCorePreferences prefs= AntCorePlugin.getPlugin().getPreferences(); 473 String antHome= prefs.getAntHome(); 474 if (finished) { 475 System.getProperties().remove("ant.home"); System.getProperties().remove("ant.library.dir"); } else { 478 System.setProperty("ant.home", antHome); File antLibDir= new File (antHome, "lib"); System.setProperty("ant.library.dir", antLibDir.getAbsolutePath()); } 482 } 483 484 private void setGlobalProperties(Project project) { 485 List properties= AntCorePlugin.getPlugin().getPreferences().getProperties(); 486 if (properties != null) { 487 for (Iterator iter = properties.iterator(); iter.hasNext();) { 488 Property property = (Property) iter.next(); 489 String value= property.getValue(true); 490 if (value != null) { 491 project.setUserProperty(property.getName(), value); 492 } 493 } 494 } 495 } 496 497 private void resolveBuildfile() { 498 Collection nodeCopy= new ArrayList (fTaskNodes); 499 Iterator iter= nodeCopy.iterator(); 500 while (iter.hasNext()) { 501 AntTaskNode node = (AntTaskNode) iter.next(); 502 fNodeBeingResolved= node; 503 fNodeBeingResolvedIndex= -1; 504 if (node.configure(false)) { 505 resolveBuildfile(); 507 } 508 } 509 fNodeBeingResolved= null; 510 fNodeBeingResolvedIndex= -1; 511 checkTargets(); 512 } 513 514 518 private void checkTargets() { 519 if (fProjectNode == null || doNotReportProblems()) { 520 return; 521 } 522 String defaultTargetName= fProjectNode.getDefaultTargetName(); 523 if (defaultTargetName != null && fProjectNode.getProject().getTargets().get(defaultTargetName) == null) { 524 String message= MessageFormat.format(AntModelMessages.AntModel_43, new String []{defaultTargetName}); 526 IProblem problem= createProblem(message, fProjectNode.getOffset(), fProjectNode.getSelectionLength(), AntModelProblem.SEVERITY_ERROR); 527 acceptProblem(problem); 528 markHierarchy(fProjectNode, AntModelProblem.SEVERITY_ERROR, message); 529 } 530 if (!fProjectNode.hasChildren()) { 531 return; 532 } 533 List children= fProjectNode.getChildNodes(); 534 Iterator iter= children.iterator(); 535 boolean checkCircularDependencies= true; 536 while (iter.hasNext()) { 537 AntElementNode node = (AntElementNode) iter.next(); 538 AntElementNode originalNode= node; 539 if (node instanceof AntTargetNode) { 540 if (checkCircularDependencies) { 541 checkCircularDependencies= false; 542 checkCircularDependencies(node); 543 } 544 checkMissingDependencies(node, originalNode); 545 } 546 } 547 } 548 549 552 private void checkMissingDependencies(AntElementNode node, AntElementNode originalNode) { 553 String missing= ((AntTargetNode)node).checkDependencies(); 554 if (missing != null) { 555 String message= MessageFormat.format(AntModelMessages.AntModel_44, new String []{missing}); 556 AntElementNode importNode= node.getImportNode(); 557 if (importNode != null) { 558 node= importNode; 559 } 560 IProblem problem= createProblem(message, node.getOffset(), node.getSelectionLength(), AntModelProblem.SEVERITY_ERROR); 561 acceptProblem(problem); 562 markHierarchy(originalNode, AntModelProblem.SEVERITY_ERROR, message); 563 } 564 } 565 566 private boolean doNotReportProblems() { 567 if (fReportingProblemsCurrent) { 568 return fDoNotReportProblems; 569 } 570 571 fReportingProblemsCurrent= true; 572 fDoNotReportProblems= false; 573 574 if (AntUIPlugin.getDefault().getCombinedPreferenceStore().getBoolean(AntEditorPreferenceConstants.BUILDFILE_IGNORE_ALL)) { 575 fDoNotReportProblems= true; 576 return fDoNotReportProblems; 577 } 578 String buildFileNames= AntUIPlugin.getDefault().getCombinedPreferenceStore().getString(AntEditorPreferenceConstants.BUILDFILE_NAMES_TO_IGNORE); 579 if (buildFileNames.length() > 0) { 580 String [] names= AntUtil.parseString(buildFileNames, ","); String editedFileName= getEditedFile().getName(); 582 for (int i = 0; i < names.length; i++) { 583 String string = names[i]; 584 if (string.trim().equals(editedFileName)) { 585 fDoNotReportProblems= true; 586 return fDoNotReportProblems; 587 } 588 } 589 } 590 591 return fDoNotReportProblems; 592 } 593 594 597 private void checkCircularDependencies(AntElementNode node) { 598 Target target= ((AntTargetNode)node).getTarget(); 599 String name= target.getName(); 600 if (name == null) { 601 return; 602 } 603 try { 604 target.getProject().topoSort(name, target.getProject().getTargets()); 605 } catch (BuildException be) { 606 String message = be.getMessage(); 608 if (message.startsWith("Circular")) { IProblem problem= createProblem(message, node.getProjectNode().getOffset(), node.getProjectNode().getSelectionLength(), AntModelProblem.SEVERITY_ERROR); 610 acceptProblem(problem); 611 markHierarchy(node.getProjectNode(), AntModelProblem.SEVERITY_ERROR, message); 612 } 613 } 614 } 615 616 619 public void handleBuildException(BuildException e, AntElementNode node, int severity) { 620 try { 621 if (node != null) { 622 markHierarchy(node, severity, e.getMessage()); 623 } 624 Location location= e.getLocation(); 625 int line= 0; 626 int originalOffset= 0; 627 int nonWhitespaceOffset= 0; 628 int length= 0; 629 if (location == Location.UNKNOWN_LOCATION && node != null) { 630 if (node.getImportNode() != null) { 631 node= node.getImportNode(); 632 } 633 nonWhitespaceOffset= node.getOffset(); 634 length= node.getLength(); 635 } else { 636 line= location.getLineNumber(); 637 if (line == 0) { 638 AntProjectNode projectNode= getProjectNode(); 639 if (projectNode != null) { 640 length= projectNode.getSelectionLength(); 641 nonWhitespaceOffset= projectNode.getOffset(); 642 if (severity == AntModelProblem.SEVERITY_ERROR) { 643 projectNode.setProblemSeverity(AntModelProblem.NO_PROBLEM); 644 projectNode.setProblemMessage(null); 645 } 646 } else { 647 return; 648 } 649 } else { 650 if (node == null) { 651 originalOffset= getOffset(line, 1); 652 nonWhitespaceOffset= originalOffset; 653 try { 654 nonWhitespaceOffset= getNonWhitespaceOffset(line, 1); 655 } catch (BadLocationException be) { 656 } 657 length= getLastCharColumn(line) - (nonWhitespaceOffset - originalOffset); 658 } else { 659 if (node.getImportNode() != null) { 660 node= node.getImportNode(); 661 } 662 nonWhitespaceOffset= node.getOffset(); 663 length= node.getLength(); 664 } 665 } 666 } 667 notifyProblemRequestor(e, nonWhitespaceOffset, length, severity); 668 } catch (BadLocationException e1) { 669 } 670 } 671 672 public void handleBuildException(BuildException e, AntElementNode node) { 673 handleBuildException(e, node, AntModelProblem.SEVERITY_ERROR); 674 } 675 676 679 public File getEditedFile() { 680 if (fLocationProvider != null && fEditedFile == null) { 681 fEditedFile= fLocationProvider.getLocation().toFile(); 682 } 683 return fEditedFile; 684 } 685 686 private void markHierarchy(AntElementNode openElement, int severity, String message) { 687 if (doNotReportProblems()) { 688 return; 689 } 690 while (openElement != null) { 691 openElement.setProblemSeverity(severity); 692 openElement.setProblemMessage(message); 693 openElement= openElement.getParentNode(); 694 } 695 } 696 697 700 public LocationProvider getLocationProvider() { 701 return fLocationProvider; 702 } 703 704 707 public void addTarget(Target newTarget, int line, int column) { 708 AntTargetNode targetNode= new AntTargetNode(newTarget); 709 fProjectNode.addChildNode(targetNode); 710 fCurrentTargetNode= targetNode; 711 fStillOpenElements.push(targetNode); 712 if (fNodeBeingResolved instanceof AntImportNode) { 713 targetNode.setImportNode(fNodeBeingResolved); 714 targetNode.setExternal(true); 715 targetNode.setFilePath(newTarget.getLocation().getFileName()); 716 } else { 717 String targetFileName= newTarget.getLocation().getFileName(); 718 boolean external= isNodeExternal(targetFileName); 719 targetNode.setExternal(external); 720 if (external) { 721 targetNode.setFilePath(targetFileName); 722 } 723 } 724 computeOffset(targetNode, line, column); 725 } 726 727 730 public void addProject(Project project, int line, int column) { 731 fProjectNode= new AntProjectNode((AntModelProject)project, this); 732 fStillOpenElements.push(fProjectNode); 733 computeOffset(fProjectNode, line, column); 734 } 735 736 739 public void addDTD(String name, int line, int column) { 740 AntDTDNode node= new AntDTDNode(name); 741 fStillOpenElements.push(node); 742 int offset= -1; 743 try { 744 if (column <= 0) { 745 offset= getOffset(line, 0); 746 int lastCharColumn= getLastCharColumn(line); 747 offset= computeOffsetUsingPrefix(line, offset, "<!DOCTYPE", lastCharColumn); } else { 749 offset= getOffset(line, column); 750 } 751 } catch (BadLocationException e) { 752 AntUIPlugin.log(e); 753 } 754 node.setOffset(offset); 755 fNonStructuralNodes.add(node); 756 } 757 758 761 public void addTask(Task newTask, Task parentTask, Attributes attributes, int line, int column) { 762 if (!canGetTaskInfo()) { 763 Target owningTarget= newTask.getOwningTarget(); 766 String name= owningTarget.getName(); 767 if (name == null || name.length() != 0 ) { 768 return; 770 } 771 } 772 AntTaskNode taskNode= null; 773 if (parentTask == null) { 774 taskNode= newTaskNode(newTask, attributes); 775 if (fCurrentTargetNode == null) { 776 fProjectNode.addChildNode(taskNode); 777 } else { 778 fCurrentTargetNode.addChildNode(taskNode); 779 } 780 } else { 781 taskNode= newNotWellKnownTaskNode(newTask, attributes); 782 AntTaskNode parentNode= (AntTaskNode)fTaskToNode.get(parentTask); 783 parentNode.addChildNode(taskNode); 784 } 785 fTaskToNode.put(newTask, taskNode); 786 787 fStillOpenElements.push(taskNode); 788 computeOffset(taskNode, line, column); 789 if (fNodeBeingResolved instanceof AntImportNode) { 790 taskNode.setImportNode(fNodeBeingResolved); 791 if (fNodeBeingResolvedIndex == -1) { 793 fNodeBeingResolvedIndex= fTaskNodes.indexOf(fNodeBeingResolved); 794 } 795 fNodeBeingResolvedIndex++; 796 fTaskNodes.add(fNodeBeingResolvedIndex, taskNode); 797 } else { 798 fTaskNodes.add(taskNode); 799 } 800 } 801 802 805 public void addEntity(String entityName, String entityPath) { 806 if (fEntityNameToPath == null) { 807 fEntityNameToPath= new HashMap (); 808 } 809 fEntityNameToPath.put(entityName, entityPath); 810 } 811 812 private AntTaskNode newTaskNode(Task newTask, Attributes attributes) { 813 AntTaskNode newNode= null; 814 String taskName= newTask.getTaskName(); 815 if (newTask instanceof UnknownElement) { 816 taskName= ((UnknownElement) newTask).getTag(); 818 } 819 820 if (isPropertySettingTask(taskName)) { 821 newNode= new AntPropertyNode(newTask, attributes); 822 } else if (taskName.equalsIgnoreCase("import")) { newNode= new AntImportNode(newTask, attributes); 824 } else if (taskName.equalsIgnoreCase("macrodef") || taskName.equalsIgnoreCase("presetdef") || taskName.equalsIgnoreCase("typedef") || taskName.equalsIgnoreCase("taskdef")) { newNode= new AntDefiningTaskNode(newTask, attributes); 829 } else if(taskName.equalsIgnoreCase("antcall")) { newNode= new AntTaskNode(newTask, generateLabel(taskName, attributes, IAntModelConstants.ATTR_TARGET)); 831 } else if(taskName.equalsIgnoreCase("mkdir")) { newNode= new AntTaskNode(newTask, generateLabel(taskName, attributes, IAntModelConstants.ATTR_DIR)); 833 } else if(taskName.equalsIgnoreCase("copy")) { newNode= new AntTaskNode(newTask, generateLabel(taskName, attributes, IAntModelConstants.ATTR_DESTFILE)); 835 } else if(taskName.equalsIgnoreCase("tar") || taskName.equalsIgnoreCase("jar") || taskName.equalsIgnoreCase("war") || taskName.equalsIgnoreCase("zip")) { newNode= new AntTaskNode(newTask, generateLabel(newTask.getTaskName(), attributes, IAntModelConstants.ATTR_DESTFILE)); 840 } else if(taskName.equalsIgnoreCase("untar") || taskName.equalsIgnoreCase("unjar") || taskName.equalsIgnoreCase("unwar") || taskName.equalsIgnoreCase("gunzip") || taskName.equalsIgnoreCase("bunzip2") || taskName.equalsIgnoreCase("unzip")) { newNode= new AntTaskNode(newTask, generateLabel(newTask.getTaskName(), attributes, IAntModelConstants.ATTR_SRC)); 847 } else if(taskName.equalsIgnoreCase("gzip") || taskName.equalsIgnoreCase("bzip2")) { newNode= new AntTaskNode(newTask, generateLabel(newTask.getTaskName(), attributes, IAntModelConstants.ATTR_ZIPFILE)); 850 } else if(taskName.equalsIgnoreCase("exec")) { String label = "exec "; String command = attributes.getValue(IAntModelConstants.ATTR_COMMAND); 853 if(command != null) { 854 label += command; 855 } 856 command = attributes.getValue(IAntModelConstants.ATTR_EXECUTABLE); 857 if(command != null) { 858 label += command; 859 } 860 newNode= new AntTaskNode(newTask, label); 861 } else if(taskName.equalsIgnoreCase("ant")) { newNode= new AntAntNode(newTask, attributes); 863 } else if(taskName.equalsIgnoreCase("delete")) { String label = "delete "; String file = attributes.getValue(IAntModelConstants.ATTR_FILE); 866 if(file != null) { 867 label+= file; 868 } else { 869 file = attributes.getValue(IAntModelConstants.ATTR_DIR); 870 if(file != null) { 871 label+= file; 872 } 873 } 874 newNode= new AntTaskNode(newTask, label); 875 } else { 876 newNode = newNotWellKnownTaskNode(newTask, attributes); 877 } 878 setExternalInformation(newTask, newNode); 879 return newNode; 880 } 881 882 886 private boolean isPropertySettingTask(String taskName) { 887 return taskName.equalsIgnoreCase("property") || taskName.equalsIgnoreCase("available") || taskName.equalsIgnoreCase("basename") || taskName.equalsIgnoreCase("condition") || taskName.equalsIgnoreCase("dirname") || taskName.equalsIgnoreCase("loadfile") || taskName.equalsIgnoreCase("pathconvert") || taskName.equalsIgnoreCase("uptodate") || taskName.equalsIgnoreCase("xmlproperty") || taskName.equalsIgnoreCase("loadproperties"); } 898 899 private boolean isNodeExternal(String fileName) { 900 File taskFile= new File (fileName); 901 return !taskFile.equals(getEditedFile()); 902 } 903 904 private AntTaskNode newNotWellKnownTaskNode(Task newTask, Attributes attributes) { 905 AntTaskNode newNode= new AntTaskNode(newTask); 906 String id= attributes.getValue("id"); if (id != null) { 908 newNode.setId(id); 909 } 910 String taskName= newTask.getTaskName(); 911 if ("attribute".equals(taskName) || "element".equals(taskName)) { String name= attributes.getValue("name"); if (name != null) { 914 newNode.setBaseLabel(name); 915 } 916 } 917 918 setExternalInformation(newTask, newNode); 919 return newNode; 920 } 921 922 private void setExternalInformation(Task newTask, AntTaskNode newNode) { 923 String taskFileName= newTask.getLocation().getFileName(); 924 boolean external= isNodeExternal(taskFileName); 925 newNode.setExternal(external); 926 if (external) { 927 newNode.setFilePath(taskFileName); 928 } 929 } 930 931 private String generateLabel(String taskName, Attributes attributes, String attributeName) { 932 StringBuffer label = new StringBuffer (taskName); 933 String srcFile = attributes.getValue(attributeName); 934 if(srcFile != null) { 935 label.append(' '); 936 label.append(srcFile); 937 } 938 return label.toString(); 939 } 940 941 private void computeLength(AntElementNode element, int line, int column) { 942 if (element.isExternal()) { 943 element.setExternalInfo(line, column); 944 return; 945 } 946 try { 947 int length; 948 int offset; 949 if (column <= 0) { 950 column= getLastCharColumn(line); 951 String lineText= fDocument.get(fDocument.getLineOffset(line - 1), column); 952 StringBuffer searchString= new StringBuffer ("</"); searchString.append(element.getName()); 954 searchString.append('>'); 955 int index= lineText.indexOf(searchString.toString()); 956 if (index == -1) { 957 index= lineText.indexOf("/>"); if (index == -1 ) { 959 index= column; } else { 961 index= index + 3; 962 } 963 } else { 964 index= index + searchString.length() + 1; 965 } 966 offset= getOffset(line, index); 967 } else { 968 offset= getOffset(line, column); 969 } 970 971 length= offset - element.getOffset(); 972 element.setLength(length); 973 } catch (BadLocationException e) { 974 } 976 } 977 978 private void computeOffset(AntElementNode element, int line, int column) { 979 if (!canGetPositionInfo()) { 980 return; 981 } 982 if (element.isExternal()) { 983 element.setExternalInfo(line - 1, column); 984 return; 985 } 986 try { 987 String prefix= "<" + element.getName(); int offset = computeOffset(line, column, prefix); 989 element.setOffset(offset + 1); 990 element.setSelectionLength(element.getName().length()); 991 } catch (BadLocationException e) { 992 } 994 } 995 996 private int computeOffset(int line, int column, String prefix) throws BadLocationException { 997 int offset; 998 if (column <= 0) { 999 offset= getOffset(line, 0); 1000 int lastCharColumn= getLastCharColumn(line); 1001 offset= computeOffsetUsingPrefix(line, offset, prefix, lastCharColumn); 1002 } else { 1003 column= column -1; 1004 offset= getOffset(line, column); 1005 offset= computeOffsetUsingPrefix(line, offset, prefix, column); 1006 } 1007 return offset; 1008 } 1009 1010 private int computeOffsetUsingPrefix(int line, int offset, String prefix, int column) throws BadLocationException { 1011 String lineText= fDocument.get(fDocument.getLineOffset(line - 1), column); 1012 int lastIndex= lineText.indexOf(prefix); 1013 if (lastIndex > -1) { 1014 offset= getOffset(line, lastIndex + 1); 1015 } else { 1016 return computeOffsetUsingPrefix(line - 1, offset, prefix, getLastCharColumn(line - 1)); 1017 } 1018 return offset; 1019 } 1020 1021 1024 public int getOffset(int line, int column) throws BadLocationException { 1025 return fDocument.getLineOffset(line - 1) + column - 1; 1026 } 1027 1028 private int getNonWhitespaceOffset(int line, int column) throws BadLocationException { 1029 int offset= fDocument.getLineOffset(line - 1) + column - 1; 1030 while(Character.isWhitespace(fDocument.getChar(offset))) { 1031 offset++; 1032 } 1033 return offset; 1034 } 1035 1036 public int getLine(int offset) { 1037 try { 1038 return fDocument.getLineOfOffset(offset) + 1; 1039 } catch (BadLocationException be) { 1040 return -1; 1041 } 1042 } 1043 1044 private int getLastCharColumn(int line) throws BadLocationException { 1045 String lineDelimiter= fDocument.getLineDelimiter(line - 1); 1046 int lineDelimiterLength= lineDelimiter != null ? lineDelimiter.length() : 0; 1047 return fDocument.getLineLength(line - 1) - lineDelimiterLength; 1048 } 1049 1050 1053 public void setCurrentElementLength(int lineNumber, int column) { 1054 fLastNode= (AntElementNode)fStillOpenElements.pop(); 1055 if (fLastNode == fCurrentTargetNode) { 1056 fCurrentTargetNode= null; } 1058 if (canGetPositionInfo()) { 1059 computeLength(fLastNode, lineNumber, column); 1060 } 1061 } 1062 1063 private void acceptProblem(IProblem problem) { 1064 if (fProblemRequestor != null) { 1065 fProblemRequestor.acceptProblem(problem); 1066 } 1067 if (fMarkerUpdater != null) { 1068 fMarkerUpdater.acceptProblem(problem); 1069 } 1070 } 1071 1072 1075 public IFile getFile() { 1076 IPath location= fLocationProvider.getLocation(); 1077 if (location == null) { 1078 return null; 1079 } 1080 IFile[] files= ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(location); 1081 if (files.length > 0) { 1082 return files[0]; 1083 } 1084 return null; 1085 } 1086 1087 private void beginReporting() { 1088 if (fProblemRequestor != null) { 1089 fProblemRequestor.beginReporting(); 1090 } 1091 if (fMarkerUpdater != null) { 1092 fMarkerUpdater.beginReporting(); 1093 } 1094 } 1095 1096 private void endReporting() { 1097 if (fProblemRequestor != null) { 1098 fProblemRequestor.endReporting(); 1099 } 1100 } 1101 1102 private IProblem createProblem(Exception exception, int offset, int length, int severity) { 1103 return createProblem(exception.getMessage(), offset, length, severity); 1104 } 1105 1106 private IProblem createProblem(String message, int offset, int length, int severity) { 1107 return new AntModelProblem(message, severity, offset, length, getLine(offset)); 1108 } 1109 1110 private void notifyProblemRequestor(Exception exception, AntElementNode element, int severity) { 1111 if (doNotReportProblems()) { 1112 return; 1113 } 1114 AntElementNode importNode= element.getImportNode(); 1115 if (importNode != null) { 1116 element= importNode; 1117 } 1118 IProblem problem= createProblem(exception, element.getOffset(), element.getLength(), severity); 1119 acceptProblem(problem); 1120 element.associatedProblem(problem); 1121 } 1122 1123 private void notifyProblemRequestor(Exception exception, int offset, int length, int severity) { 1124 if (doNotReportProblems()) { 1125 return; 1126 } 1127 if (fProblemRequestor != null) { 1128 IProblem problem= createProblem(exception, offset, length, severity); 1129 acceptProblem(problem); 1130 } 1131 } 1132 1133 1136 public void warning(Exception exception) { 1137 handleError(exception, AntModelProblem.SEVERITY_WARNING); 1138 } 1139 1140 1143 public void error(Exception exception) { 1144 handleError(exception, AntModelProblem.SEVERITY_ERROR); 1145 } 1146 1147 1150 public void errorFromElementText(Exception exception, int start, int count) { 1151 AntElementNode node= fLastNode; 1152 if (node == null) { 1153 if (!fStillOpenElements.empty()) { 1154 node= (AntElementNode)fStillOpenElements.peek(); 1155 } 1156 } 1157 if (node == null) { 1158 return; 1159 } 1160 computeEndLocationForErrorNode(node, start, count); 1161 notifyProblemRequestor(exception, start, count, AntModelProblem.SEVERITY_ERROR); 1162 markHierarchy(fLastNode, AntModelProblem.SEVERITY_ERROR, exception.getMessage()); 1163 } 1164 1165 1168 public void errorFromElement(Exception exception, AntElementNode node, int lineNumber, int column) { 1169 if (node == null) { 1170 if (!fStillOpenElements.empty()) { 1171 node= (AntElementNode)fStillOpenElements.peek(); 1172 } else { 1173 node= fLastNode; 1174 } 1175 } 1176 computeEndLocationForErrorNode(node, lineNumber, column); 1177 notifyProblemRequestor(exception, node, AntModelProblem.SEVERITY_ERROR); 1178 markHierarchy(node, AntModelProblem.SEVERITY_ERROR, exception.getMessage()); 1179 } 1180 1181 private AntElementNode createProblemElement(SAXParseException exception) { 1182 int lineNumber= exception.getLineNumber(); 1183 StringBuffer message= new StringBuffer (exception.getMessage()); 1184 if (lineNumber != -1){ 1185 message.append(AntModelMessages.AntModel_1 + lineNumber); 1186 } 1187 1188 AntElementNode errorNode= new AntElementNode(message.toString()); 1189 errorNode.setFilePath(exception.getSystemId()); 1190 errorNode.setProblemSeverity(AntModelProblem.SEVERITY_ERROR); 1191 errorNode.setProblemMessage(exception.getMessage()); 1192 computeErrorLocation(errorNode, exception); 1193 return errorNode; 1194 } 1195 1196 private void computeErrorLocation(AntElementNode element, SAXParseException exception) { 1197 if (element.isExternal()) { 1198 return; 1199 } 1200 1201 int line= exception.getLineNumber(); 1202 int startColumn= exception.getColumnNumber(); 1203 computeEndLocationForErrorNode(element, line, startColumn); 1204 } 1205 1206 private void computeEndLocationForErrorNode(AntElementNode element, int line, int startColumn) { 1207 try { 1208 if (line <= 0) { 1209 line= 1; 1210 } 1211 int endColumn; 1212 if (startColumn <= 0) { 1213 if (element.getOffset() > -1) { 1214 startColumn= element.getOffset() + 1; 1215 } else { 1216 startColumn= 1; 1217 } 1218 endColumn= getLastCharColumn(line) + 1; 1219 } else { 1220 if (startColumn > 1) { 1221 --startColumn; 1222 } 1223 1224 endColumn= startColumn; 1225 if (startColumn <= getLastCharColumn(line)) { 1226 ++endColumn; 1227 } 1228 } 1229 1230 int correction= 0; 1231 if (element.getOffset() == -1) { 1232 int originalOffset= getOffset(line, startColumn); 1233 int nonWhitespaceOffset= originalOffset; 1234 try { 1235 nonWhitespaceOffset= getNonWhitespaceOffset(line, startColumn); 1236 } catch (BadLocationException be) { 1237 } 1238 element.setOffset(nonWhitespaceOffset); 1239 correction= nonWhitespaceOffset - originalOffset; 1240 } 1241 if (endColumn - startColumn == 0) { 1242 int offset= getOffset(line, startColumn); 1243 element.setLength(offset - element.getOffset() - correction); 1244 } else { 1245 element.setLength(endColumn - startColumn - correction); 1246 } 1247 } catch (BadLocationException e) { 1248 } 1250 } 1251 1252 private void handleError(Exception exception, int severity) { 1253 AntElementNode node= null; 1254 if (fStillOpenElements.isEmpty()) { 1255 if (exception instanceof SAXParseException ) { 1256 node= createProblemElement((SAXParseException )exception); 1257 } 1258 } else { 1259 node= (AntElementNode)fStillOpenElements.peek(); 1260 } 1261 if (node == null) { 1262 return; 1263 } 1264 markHierarchy(node, severity, exception.getMessage()); 1265 1266 if (exception instanceof SAXParseException ) { 1267 SAXParseException parseException= (SAXParseException )exception; 1268 if (node.getOffset() == -1) { 1269 computeEndLocationForErrorNode(node, parseException.getLineNumber() - 1, parseException.getColumnNumber()); 1270 } else { 1271 int lineNumber= parseException.getLineNumber(); 1272 int columnNumber= parseException.getColumnNumber(); 1273 if (columnNumber == -1) { 1274 columnNumber= 1; 1275 } 1276 try { 1277 AntElementNode childNode= node.getNode(getNonWhitespaceOffset(lineNumber, columnNumber) + 1); 1278 if (childNode != null && childNode != node) { 1279 node= childNode; 1280 node.setProblemSeverity(severity); 1281 node.setProblemMessage(exception.getMessage()); 1282 } else { 1283 node= createProblemElement(parseException); 1284 } 1285 } catch (BadLocationException be) { 1286 node= createProblemElement(parseException); 1287 } 1288 } 1289 } 1290 1291 notifyProblemRequestor(exception, node, severity); 1292 1293 if (node != null) { 1294 while (node.getParentNode() != null) { 1295 AntElementNode parentNode= node.getParentNode(); 1296 if (parentNode.getLength() == -1) { 1297 parentNode.setLength(node.getOffset() - parentNode.getOffset() + node.getLength()); 1298 } 1299 node= parentNode; 1300 } 1301 } 1302 } 1303 1304 1307 public void fatalError(Exception exception) { 1308 handleError(exception, AntModelProblem.SEVERITY_FATAL_ERROR); 1309 } 1310 1311 public AntElementNode getOpenElement() { 1312 if (fStillOpenElements.isEmpty()) { 1313 return null; 1314 } 1315 return (AntElementNode)fStillOpenElements.peek(); 1316 } 1317 1318 1321 public String getEntityName(String path) { 1322 if (fEntityNameToPath != null) { 1323 Iterator itr= fEntityNameToPath.keySet().iterator(); 1324 String entityPath; 1325 String name; 1326 while (itr.hasNext()) { 1327 name= (String ) itr.next(); 1328 entityPath= (String ) fEntityNameToPath.get(name); 1329 if (entityPath.equals(path)) { 1330 return name; 1331 } 1332 } 1333 } 1334 return null; 1335 } 1336 1337 private ClassLoader getClassLoader(ClassLoader contextClassLoader) { 1338 if (fLocalClassLoader != null) { 1339 ((AntClassLoader) fLocalClassLoader).setPluginContextClassloader(contextClassLoader); 1340 return fLocalClassLoader; 1341 } 1342 if (fgClassLoader == null) { 1343 fgClassLoader= AntCorePlugin.getPlugin().getNewClassLoader(true); 1344 } 1345 if (fgClassLoader instanceof AntClassLoader) { 1346 ((AntClassLoader) fgClassLoader).setPluginContextClassloader(contextClassLoader); 1347 } 1348 return fgClassLoader; 1349 } 1350 1351 public String getTargetDescription(String targetName) { 1352 AntTargetNode target= getTargetNode(targetName); 1353 if (target != null) { 1354 return target.getTarget().getDescription(); 1355 } 1356 return null; 1357 } 1358 1359 public AntTargetNode getTargetNode(String targetName ) { 1360 AntProjectNode projectNode= getProjectNode(); 1361 if (projectNode == null) { 1362 return null; 1363 } 1364 if (projectNode.hasChildren()) { 1365 List possibleTargets= projectNode.getChildNodes(); 1366 Iterator iter= possibleTargets.iterator(); 1367 while (iter.hasNext()) { 1368 AntElementNode node = (AntElementNode) iter.next(); 1369 if (node instanceof AntTargetNode) { 1370 AntTargetNode targetNode = (AntTargetNode) node; 1371 if (targetName.equalsIgnoreCase(targetNode.getTarget().getName())) { 1372 return targetNode; 1373 } 1374 } 1375 } 1376 } 1377 return null; 1378 } 1379 1380 1383 public AntProjectNode getProjectNode(boolean doReconcile) { 1384 if (doReconcile) { 1385 synchronized (getLockObject()) { reconcile(); 1387 } 1388 } 1389 return fProjectNode; 1390 } 1391 1392 1395 public AntProjectNode getProjectNode() { 1396 return getProjectNode(true); 1397 } 1398 1399 public AntElementNode getNode(int offset, boolean waitForReconcile) { 1400 if (getProjectNode(waitForReconcile) != null) { 1401 return getProjectNode(waitForReconcile).getNode(offset); 1402 } 1403 return null; 1404 } 1405 1406 1409 private void reconcileTaskAndTypes() { 1410 if (fCurrentNodeIdentifiers == null || fDefinerNodeIdentifierToDefinedTasks == null) { 1411 return; 1412 } 1413 Iterator iter= fDefinerNodeIdentifierToDefinedTasks.keySet().iterator(); 1414 ComponentHelper helper= ComponentHelper.getComponentHelper(fProjectNode.getProject()); 1415 while (iter.hasNext()) { 1416 String key = (String ) iter.next(); 1417 if (fCurrentNodeIdentifiers.get(key) == null) { 1418 removeDefinerTasks(key, helper.getAntTypeTable()); 1419 } 1420 } 1421 } 1422 1423 protected void removeDefinerTasks(String definerIdentifier, Hashtable typeTable) { 1424 if (fDefinerNodeIdentifierToDefinedTasks == null) { 1425 return; 1426 } 1427 List tasks= (List ) fDefinerNodeIdentifierToDefinedTasks.get(definerIdentifier); 1428 if (tasks == null) { 1429 return; 1430 } 1431 Iterator iterator= tasks.iterator(); 1432 while (iterator.hasNext()) { 1433 typeTable.remove(iterator.next()); 1434 } 1435 } 1436 1437 1440 public void addComment(int lineNumber, int columnNumber, int length) { 1441 AntCommentNode commentNode= new AntCommentNode(); 1442 int offset= -1; 1443 try { 1444 offset= computeOffset(lineNumber, columnNumber, "-->"); } catch (BadLocationException e) { 1446 commentNode.setExternal(true); 1447 commentNode.setExternalInfo(lineNumber, columnNumber); 1448 offset= length-1; 1449 } 1450 commentNode.setOffset(offset - length); 1451 commentNode.setLength(length); 1452 fNonStructuralNodes.add(commentNode); 1453 } 1454 1455 1458 public boolean canGetTaskInfo() { 1459 return fHasTaskInfo; 1460 } 1461 1462 1465 public boolean canGetLexicalInfo() { 1466 return fHasLexicalInfo; 1467 } 1468 1469 1472 public void setClassLoader(URLClassLoader loader) { 1473 AntDefiningTaskNode.setJavaClassPath(loader.getURLs()); 1474 fLocalClassLoader= loader; 1475 } 1476 1477 1480 public boolean canGetPositionInfo() { 1481 return fHasPositionInfo; 1482 } 1483 1484 public String getPath(String text, int offset) { 1485 if (fEntityNameToPath != null) { 1486 String path= (String )fEntityNameToPath.get(text); 1487 if (path != null) { 1488 return path; 1489 } 1490 } 1491 AntElementNode node= getNode(offset, true); 1492 if (node != null) { 1493 return node.getReferencedElement(offset); 1494 } 1495 return null; 1496 } 1497 1498 1501 public String getText(int offset, int length) { 1502 try { 1503 return fDocument.get(offset, length); 1504 } catch (BadLocationException e) { 1505 } 1506 return null; 1507 } 1508 1509 private AntElementNode findPropertyNode(String text, List children) { 1510 Iterator iter= children.iterator(); 1511 while (iter.hasNext()) { 1512 AntElementNode element = (AntElementNode) iter.next(); 1513 if (element instanceof AntPropertyNode) { 1514 if (((AntPropertyNode)element).getProperty(text) != null){ 1515 return element; 1516 } 1517 } else if (element.hasChildren()) { 1518 AntElementNode found= findPropertyNode(text, element.getChildNodes()); 1519 if (found != null) { 1520 return found; 1521 } 1522 } 1523 } 1524 return null; 1525 } 1526 1527 public AntElementNode getPropertyNode(String text) { 1528 AntProjectNode node= getProjectNode(); 1529 if (node == null || !node.hasChildren()) { 1530 return null; 1531 } 1532 1533 return findPropertyNode(text, node.getChildNodes()); 1534 } 1535 1536 public List getNonStructuralNodes() { 1537 return fNonStructuralNodes; 1538 } 1539 1540 public void updateForInitialReconcile() { 1541 fMarkerUpdater.updateMarkers(); 1542 fShouldReconcile= AntUIPlugin.getDefault().getPreferenceStore().getBoolean(AntEditorPreferenceConstants.EDITOR_RECONCILE); 1543 } 1544 1545 public void updateMarkers() { 1546 boolean temp= fShouldReconcile; 1547 try { 1548 fShouldReconcile= true; 1549 reconcile(); 1550 fMarkerUpdater.updateMarkers(); 1551 } finally { 1552 fShouldReconcile= temp; 1553 } 1554 } 1555 1556 public AntElementNode getReferenceNode(String text) { 1557 Object reference = getReferenceObject(text); 1558 if (reference == null) { 1559 return null; 1560 } 1561 1562 Collection nodes = fTaskToNode.keySet(); 1563 Iterator iter = nodes.iterator(); 1564 while (iter.hasNext()) { 1565 Object original = iter.next(); 1566 Object object = original; 1567 if (object instanceof UnknownElement) { 1568 UnknownElement element = (UnknownElement) object; 1569 RuntimeConfigurable wrapper = element.getWrapper(); 1570 Map attributes = wrapper.getAttributeMap(); 1571 String id = (String ) attributes.get("id"); if (text.equals(id)) { 1573 return (AntElementNode)fTaskToNode.get(original); 1574 } 1575 } 1576 } 1577 return null; 1578 } 1579 1580 public Object getReferenceObject(String refId) { 1581 AntProjectNode projectNode= getProjectNode(); 1582 if (projectNode == null) { 1583 return null; 1584 } 1585 try { 1586 Project project= projectNode.getProject(); 1587 Object ref= project.getReference(refId); 1588 return ref; 1589 1590 } catch (BuildException be) { 1591 handleBuildException(be, null); 1592 } 1593 return null; 1594 } 1595 1596 public String getPropertyValue(String propertyName) { 1597 AntProjectNode projectNode= getProjectNode(); 1598 if (projectNode == null) { 1599 return null; 1600 } 1601 return projectNode.getProject().getProperty(propertyName); 1602 } 1603 1604 1607 public void install() { 1608 fListener= new IDocumentListener() { 1609 public void documentAboutToBeChanged(DocumentEvent event) { 1610 synchronized (fDirtyLock) { 1611 fIsDirty= true; 1612 } 1613 } 1614 public void documentChanged(DocumentEvent event) {} 1615 }; 1616 fDocument.addDocumentListener(fListener); 1617 } 1618 1619 private void reconcileForPropertyChange(boolean classpathChanged) { 1620 if (classpathChanged) { 1621 fProjectNode= null; fgClassLoader= null; 1623 AntDefiningTaskNode.setJavaClassPath(); 1624 ProjectHelper.reset(); 1625 } 1626 fIsDirty= true; 1627 reconcile(); 1628 AntModelCore.getDefault().notifyAntModelListeners(new AntModelChangeEvent(this, true)); 1629 fMarkerUpdater.updateMarkers(); 1630 } 1631 1632 1635 public void setProperties(Map properties) { 1636 fProperties= properties; 1637 } 1638 1639 1642 public void setPropertyFiles(String [] propertyFiles) { 1643 if (propertyFiles != null) { 1644 fPropertyFiles= Arrays.asList(propertyFiles); 1645 } 1646 } 1647 1648 public void setDefiningTaskNodeText(AntDefiningTaskNode node) { 1649 if (fDefinersToText == null) { 1650 fDefinersToText= new HashMap (); 1651 fCurrentNodeIdentifiers= new HashMap (); 1652 } 1653 1654 Object nodeIdentifier= node.getIdentifier(); 1655 String nodeText= null; 1656 if (fPreviousDefinersToText != null) { 1657 nodeText= (String ) fPreviousDefinersToText.get(nodeIdentifier); 1658 } 1659 String newNodeText= getText(node.getOffset(), node.getLength()); 1660 if (nodeText != null) { 1661 if (nodeText.equals(newNodeText)) { 1662 node.setNeedsToBeConfigured(false); 1663 List tasks= (List ) fDefinerNodeIdentifierToDefinedTasks.get(nodeIdentifier); 1665 if (tasks != null) { 1666 for (Iterator iter = tasks.iterator(); iter.hasNext(); ) { 1667 String taskName = (String ) iter.next(); 1668 fTaskNameToDefiningNode.put(taskName, node); 1669 } 1670 } 1671 } 1672 } 1673 1674 if (newNodeText != null) { 1675 fDefinersToText.put(nodeIdentifier, newNodeText); 1676 } 1677 fCurrentNodeIdentifiers.put(nodeIdentifier, nodeIdentifier); 1678 } 1679 1680 protected void removeDefiningTaskNodeInfo(AntDefiningTaskNode node) { 1681 Object identifier= node.getIdentifier(); 1682 if (identifier != null && fCurrentNodeIdentifiers != null) { 1683 fCurrentNodeIdentifiers.remove(identifier); 1684 fDefinersToText.remove(identifier); 1685 } 1686 } 1687 1688 protected void addDefinedTasks(List newTasks, AntDefiningTaskNode node) { 1689 if (fTaskNameToDefiningNode == null) { 1690 fTaskNameToDefiningNode= new HashMap (); 1691 fDefinerNodeIdentifierToDefinedTasks= new HashMap (); 1692 } 1693 1694 Object identifier= node.getIdentifier(); 1695 if (identifier == null) { 1696 return; 1697 } 1698 if (newTasks.isEmpty() && fCurrentNodeIdentifiers != null) { 1699 fCurrentNodeIdentifiers.remove(identifier); 1700 } 1701 fDefinerNodeIdentifierToDefinedTasks.put(identifier, newTasks); 1702 Iterator iter= newTasks.iterator(); 1703 while (iter.hasNext()) { 1704 String name = (String ) iter.next(); 1705 fTaskNameToDefiningNode.put(name, node); 1706 } 1707 } 1708 1709 public AntDefiningTaskNode getDefininingTaskNode(String nodeName) { 1710 if (fTaskNameToDefiningNode != null) { 1711 AntDefiningTaskNode node= (AntDefiningTaskNode)fTaskNameToDefiningNode.get(nodeName); 1712 if (node == null) { 1713 nodeName = getNamespaceCorrectName(nodeName); 1714 node= (AntDefiningTaskNode)fTaskNameToDefiningNode.get(nodeName); 1715 } 1716 return node; 1717 } 1718 return null; 1719 } 1720 1721 public String getNamespaceCorrectName(String nodeName) { 1722 String prefix= org.apache.tools.ant.ProjectHelper.extractUriFromComponentName(nodeName); 1723 String uri= getPrefixMapping(prefix); 1724 nodeName= org.apache.tools.ant.ProjectHelper.genComponentName(uri, org.apache.tools.ant.ProjectHelper.extractNameFromComponentName(nodeName)); 1725 return nodeName; 1726 } 1727 1728 public String getUserNamespaceCorrectName(String nodeName) { 1729 String prefix= org.apache.tools.ant.ProjectHelper.extractUriFromComponentName(nodeName); 1730 if (prefix.length() > 0) { 1731 String uri= getUserPrefixMapping(prefix); 1732 nodeName= org.apache.tools.ant.ProjectHelper.genComponentName(uri, org.apache.tools.ant.ProjectHelper.extractNameFromComponentName(nodeName)); 1733 } 1734 return nodeName; 1735 } 1736 1737 public AntTaskNode getMacroDefAttributeNode(String macroDefAttributeName) { 1738 if (fTaskNameToDefiningNode == null) { 1739 return null; 1740 } 1741 Iterator iter = fTaskNameToDefiningNode.values().iterator(); 1742 while(iter.hasNext()) { 1743 AntDefiningTaskNode definingNode = (AntDefiningTaskNode) iter.next(); 1744 List attributes= definingNode.getChildNodes(); 1745 if (attributes != null) { 1746 Iterator attributeItr= attributes.iterator(); 1747 while (attributeItr.hasNext()) { 1748 AntTaskNode attributeNode= (AntTaskNode) attributeItr.next(); 1749 if (macroDefAttributeName.equals(attributeNode.getLabel())) { 1750 return attributeNode; 1751 } 1752 } 1753 } 1754 } 1755 return null; 1756 } 1757 1758 1763 public void setShouldReconcile(boolean shouldReconcile) { 1764 fShouldReconcile= shouldReconcile; 1765 if (fShouldReconcile) { 1766 reconcile(); 1767 } 1768 } 1769 1770 1773 public void addPrefixMapping(String prefix, String uri) { 1774 if (fNamespacePrefixMappings == null) { 1775 fNamespacePrefixMappings= new HashMap (); 1776 } 1777 fNamespacePrefixMappings.put(prefix, uri); 1778 } 1779 1780 private String getPrefixMapping(String prefix) { 1781 if (fNamespacePrefixMappings != null) { 1782 return (String ) fNamespacePrefixMappings.get(prefix); 1783 } 1784 return null; 1785 } 1786 1787 private String getUserPrefixMapping(String prefix) { 1788 if (fNamespacePrefixMappings != null) { 1789 Set entrySet= fNamespacePrefixMappings.entrySet(); 1790 Iterator entries= entrySet.iterator(); 1791 while (entries.hasNext()) { 1792 Map.Entry entry = (Map.Entry ) entries.next(); 1793 if (entry.getValue().equals(prefix)) { 1794 return (String ) entry.getKey(); 1795 } 1796 } 1797 } 1798 return null; 1799 } 1800 1801 1804 class Small { 1805 } 1806} | Popular Tags |