1 14 15 package org.eclipse.ant.internal.ui.datatransfer; 16 17 import java.io.BufferedReader ; 18 import java.io.ByteArrayInputStream ; 19 import java.io.File ; 20 import java.io.FileNotFoundException ; 21 import java.io.FileReader ; 22 import java.io.IOException ; 23 import java.io.StringWriter ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Comparator ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Set ; 33 import java.util.TreeSet ; 34 import java.util.regex.Matcher ; 35 import java.util.regex.Pattern ; 36 37 import javax.xml.parsers.DocumentBuilderFactory ; 38 import javax.xml.parsers.ParserConfigurationException ; 39 import javax.xml.transform.OutputKeys ; 40 import javax.xml.transform.Result ; 41 import javax.xml.transform.Source ; 42 import javax.xml.transform.Transformer ; 43 import javax.xml.transform.TransformerConfigurationException ; 44 import javax.xml.transform.TransformerException ; 45 import javax.xml.transform.TransformerFactory ; 46 import javax.xml.transform.TransformerFactoryConfigurationError ; 47 import javax.xml.transform.dom.DOMSource ; 48 import javax.xml.transform.stream.StreamResult ; 49 50 import org.eclipse.ant.internal.ui.AntUIPlugin; 51 import org.eclipse.core.resources.IFile; 52 import org.eclipse.core.resources.IMarker; 53 import org.eclipse.core.resources.IProject; 54 import org.eclipse.core.resources.IResource; 55 import org.eclipse.core.resources.IWorkspace; 56 import org.eclipse.core.resources.IWorkspaceRoot; 57 import org.eclipse.core.resources.ResourcesPlugin; 58 import org.eclipse.core.runtime.CoreException; 59 import org.eclipse.core.runtime.IAdaptable; 60 import org.eclipse.core.runtime.IPath; 61 import org.eclipse.core.runtime.IStatus; 62 import org.eclipse.core.runtime.Path; 63 import org.eclipse.core.runtime.Status; 64 import org.eclipse.core.variables.VariablesPlugin; 65 import org.eclipse.jdt.core.Flags; 66 import org.eclipse.jdt.core.IClassFile; 67 import org.eclipse.jdt.core.IClasspathEntry; 68 import org.eclipse.jdt.core.ICompilationUnit; 69 import org.eclipse.jdt.core.IJavaElement; 70 import org.eclipse.jdt.core.IJavaModel; 71 import org.eclipse.jdt.core.IJavaModelMarker; 72 import org.eclipse.jdt.core.IJavaProject; 73 import org.eclipse.jdt.core.IMethod; 74 import org.eclipse.jdt.core.IPackageFragmentRoot; 75 import org.eclipse.jdt.core.IRegion; 76 import org.eclipse.jdt.core.ISourceReference; 77 import org.eclipse.jdt.core.IType; 78 import org.eclipse.jdt.core.JavaCore; 79 import org.eclipse.jdt.core.JavaModelException; 80 import org.eclipse.jdt.core.Signature; 81 import org.eclipse.jdt.core.search.IJavaSearchConstants; 82 import org.eclipse.jdt.core.search.IJavaSearchScope; 83 import org.eclipse.jdt.core.search.SearchEngine; 84 import org.eclipse.jdt.core.search.SearchMatch; 85 import org.eclipse.jdt.core.search.SearchParticipant; 86 import org.eclipse.jdt.core.search.SearchPattern; 87 import org.eclipse.jdt.core.search.SearchRequestor; 88 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 89 import org.eclipse.jface.viewers.ISelection; 90 import org.eclipse.jface.viewers.IStructuredSelection; 91 import org.eclipse.swt.widgets.Shell; 92 import org.eclipse.ui.IFileEditorInput; 93 import org.w3c.dom.Document ; 94 import org.xml.sax.SAXException ; 95 96 99 public class ExportUtil 100 { 101 private ExportUtil() 102 { 103 } 104 105 108 public static IResource getResource(ISelection selection) 109 { 110 if (selection instanceof IStructuredSelection) 111 { 112 for (Iterator iter = ((IStructuredSelection) selection).iterator(); iter.hasNext();) 113 { 114 IAdaptable adaptable = (IAdaptable) iter.next(); 115 return (IResource) adaptable.getAdapter(IResource.class); 116 } 117 } 118 return null; 119 } 120 121 124 public static IJavaProject getJavaProjectByName(String name) 125 { 126 IWorkspaceRoot rootWorkspace = ResourcesPlugin.getWorkspace().getRoot(); 127 IJavaModel javaModel = JavaCore.create(rootWorkspace); 128 IJavaProject[] javaProjects; 129 try 130 { 131 javaProjects = javaModel.getJavaProjects(); 132 } 133 catch (JavaModelException e) 134 { 135 return null; 136 } 137 for (int i = 0; i < javaProjects.length; i++) 138 { 139 IJavaProject javaProject = javaProjects[i]; 140 if (name.equals(javaProject.getProject().getName())) 141 { 142 return javaProject; 143 } 144 } 145 return null; 146 } 147 148 151 public static String getProjectRoot(IJavaProject project) 152 { 153 return project.getResource().getLocation().toString(); 154 } 155 156 162 public static String resolve(IPath file) 163 { 164 if (file == null) 165 { 166 return null; 167 } 168 try 169 { 170 IFile f = ResourcesPlugin.getWorkspace().getRoot().getFile(file); 171 IPath p = f.getLocation(); 172 return (p != null) ? p.toString() : f.toString(); 173 } 174 catch (IllegalArgumentException e) 175 { 176 String projectName = ExportUtil.removePrefix(file.toString(), "/"); IJavaProject project = ExportUtil.getJavaProjectByName(projectName); 179 return ExportUtil.getProjectRoot(project); 180 } 181 } 182 183 186 public static IJavaProject getJavaProject(String root) 187 { 188 IWorkspaceRoot rootWorkspace = ResourcesPlugin.getWorkspace().getRoot(); 189 IJavaModel javaModel = JavaCore.create(rootWorkspace); 190 IJavaProject[] javaProjects; 191 try 192 { 193 javaProjects = javaModel.getJavaProjects(); 194 } 195 catch (JavaModelException e) 196 { 197 return null; 198 } 199 for (int i = 0; i < javaProjects.length; i++) 200 { 201 IJavaProject javaProject = javaProjects[i]; 202 if (root.equals(javaProject.getPath().toString())) 203 { 204 return javaProject; 205 } 206 } 207 return null; 208 } 209 210 213 public static String removeProjectRoot(String file, IProject project) 214 { 215 String res = removePrefix(file, '/' + project.getName() + '/'); 216 if (res.equals('/' + project.getName())) 217 { 218 return "."; } 220 return res; 221 } 222 223 227 public static String replaceProjectRoot(String file, IProject project, String newProjectRoot) 228 { 229 String res = removeProjectRoot(file, project); 230 if (res.equals(".")) { 232 return newProjectRoot; 233 } 234 if (newProjectRoot == null) 235 { 236 return res; 237 } 238 if (!res.equals(file)) 239 { 240 return newProjectRoot + '/' + res; 241 } 242 return res; 243 } 244 245 250 public static List getClasspathProjects(IJavaProject project) throws JavaModelException 251 { 252 List projects = new ArrayList (); 253 IClasspathEntry entries[] = project.getRawClasspath(); 254 addClasspathProjects(projects, entries); 255 return sortProjectsUsingBuildOrder(projects); 256 } 257 258 private static void addClasspathProjects(List projects, IClasspathEntry[] entries) { 259 for (int i = 0; i < entries.length; i++) 260 { 261 IClasspathEntry classpathEntry = entries[i]; 262 if (classpathEntry.getContentKind() == IPackageFragmentRoot.K_SOURCE && 263 classpathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) { 264 String subProjectRoot = classpathEntry.getPath().toString(); 266 IJavaProject subProject = ExportUtil.getJavaProject(subProjectRoot); 267 if (subProject != null) 269 { 270 projects.add(subProject); 271 } 272 } 273 } 274 } 275 276 281 public static List getClasspathProjectsRecursive(IJavaProject project) throws JavaModelException 282 { 283 LinkedList result = new LinkedList (); 284 getClasspathProjectsRecursive(project, result); 285 return sortProjectsUsingBuildOrder(result); 286 } 287 288 private static void getClasspathProjectsRecursive(IJavaProject project, LinkedList result) throws JavaModelException 289 { 290 List projects = getClasspathProjects(project); 291 for (Iterator iter = projects.iterator(); iter.hasNext();) 292 { 293 IJavaProject javaProject = (IJavaProject) iter.next(); 294 if (!result.contains(javaProject)) { 295 result.addFirst(javaProject); 296 getClasspathProjectsRecursive(javaProject, result); } 298 } 299 } 300 301 306 private static List sortProjectsUsingBuildOrder(List javaProjects) { 307 if (javaProjects.isEmpty()) { 308 return javaProjects; 309 } 310 List result = new ArrayList (javaProjects.size()); 311 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 312 String [] buildOrder= workspace.getDescription().getBuildOrder(); 313 if (buildOrder == null) { IProject[] projects= new IProject[javaProjects.size()]; 315 int i= 0; 316 for (Iterator iter = javaProjects.iterator(); iter.hasNext(); i++) { 317 IJavaProject javaProject = (IJavaProject) iter.next(); 318 projects[i]= javaProject.getProject(); 319 } 320 IWorkspace.ProjectOrder po = ResourcesPlugin.getWorkspace().computeProjectOrder(projects); 321 projects=po.projects; 322 buildOrder= new String [projects.length]; 323 for (i = 0; i < projects.length; i++) { 324 buildOrder[i]= projects[i].getName(); 325 } 326 } 327 328 for (int i = 0; i < buildOrder.length && !javaProjects.isEmpty(); i++) { 329 String projectName = buildOrder[i]; 330 for (Iterator iter = javaProjects.iterator(); iter.hasNext();) { 331 IJavaProject javaProject = (IJavaProject) iter.next(); 332 if (javaProject.getProject().getName().equals(projectName)) { 333 result.add(javaProject); 334 iter.remove(); 335 } 336 } 337 } 338 result.addAll(javaProjects); 340 return result; 341 } 342 343 348 public static boolean hasCyclicDependency(IJavaProject javaProject) 349 throws CoreException 350 { 351 IMarker[] markers = javaProject.getProject().findMarkers( 352 IJavaModelMarker.BUILDPATH_PROBLEM_MARKER, false, 353 IResource.DEPTH_ONE); 354 for (int i = 0; i < markers.length; i++) 355 { 356 IMarker marker = markers[i]; 357 String cycleAttr = (String ) marker.getAttribute(IJavaModelMarker.CYCLE_DETECTED); 358 if (cycleAttr != null && cycleAttr.equals("true")) { 360 return true; 361 } 362 } 363 return false; 364 } 365 366 374 public static IType[] findTestsInContainer(String containerHandle) 375 { 376 IJavaElement container= JavaCore.create(containerHandle); 377 if (container == null) { 378 return new IType[0]; 379 } 380 final Object [] elements = new Object [] { container }; 381 final Set result= new HashSet (); 382 if (elements.length > 0) { 383 doFindTests(elements, result); 384 } 385 return (IType[]) result.toArray(new IType[result.size()]) ; 386 } 387 388 private static class JUnitSearchResultCollector extends SearchRequestor { 391 List fList; 392 Set fFailed = new HashSet (); 393 Set fMatches = new HashSet (); 394 395 public JUnitSearchResultCollector(List list) { 396 fList = list; 397 } 398 399 public void acceptSearchMatch(SearchMatch match) throws CoreException { 400 Object enclosingElement= match.getElement(); 401 if (!(enclosingElement instanceof IMethod)) 402 return; 403 404 IMethod method= (IMethod)enclosingElement; 405 406 IType declaringType= method.getDeclaringType(); 407 if (fMatches.contains(declaringType) || fFailed.contains(declaringType)) 408 return; 409 if (!hasSuiteMethod(declaringType) && !isTestType(declaringType)) { 410 fFailed.add(declaringType); 411 return; 412 } 413 fMatches.add(declaringType); 414 } 415 416 public void endReporting() { 417 fList.addAll(fMatches); 418 } 419 } 420 421 private static List searchMethod(final IJavaSearchScope scope) throws CoreException { 423 final List typesFound= new ArrayList (200); 424 searchMethod(typesFound, scope); 425 return typesFound; 426 } 427 428 private static List searchMethod(final List v, IJavaSearchScope scope) throws CoreException { 430 SearchRequestor requestor= new JUnitSearchResultCollector(v); 431 int matchRule = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE | SearchPattern.R_ERASURE_MATCH; 432 SearchPattern suitePattern= SearchPattern.createPattern("suite() Test", IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, matchRule); SearchParticipant[] participants= new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()}; 434 new SearchEngine().search(suitePattern, participants, scope, requestor, null); 435 return v; 436 } 437 438 private static void doFindTests(Object [] elements, Set result) { 440 int nElements= elements.length; 441 for (int i = 0; i < nElements; i++) { 442 try { 443 collectTypes(elements[i], result); 444 } catch (CoreException e) { 445 AntUIPlugin.log(e.getStatus()); 446 } 447 } 448 } 449 450 private static void collectTypes(Object element, Set result) throws CoreException { 452 element = computeScope(element); 453 while ((element instanceof ISourceReference) && !(element instanceof ICompilationUnit)) { 454 if (element instanceof IType) { 455 if (hasSuiteMethod((IType)element) || isTestType((IType)element)) { 456 result.add(element); 457 return; 458 } 459 } 460 element = ((IJavaElement)element).getParent(); 461 } 462 if (element instanceof ICompilationUnit) { 463 ICompilationUnit cu= (ICompilationUnit)element; 464 IType[] types= cu.getAllTypes(); 465 466 for (int i= 0; i < types.length; i++) { 467 if (hasSuiteMethod(types[i]) || isTestType(types[i])) 468 result.add(types[i]); 469 } 470 } 471 else if (element instanceof IJavaElement) { 472 List testCases= findTestCases((IJavaElement)element); 473 List suiteMethods= searchSuiteMethods((IJavaElement)element); 474 while (!suiteMethods.isEmpty()) { 475 if (!testCases.contains(suiteMethods.get(0))) { 476 testCases.add(suiteMethods.get(0)); 477 } 478 suiteMethods.remove(0); 479 } 480 result.addAll(testCases); 481 } 482 } 483 484 private static List findTestCases(IJavaElement element) throws JavaModelException { 486 List found = new ArrayList (); 487 IJavaProject javaProject= element.getJavaProject(); 488 489 if (javaProject == null) return found; 491 492 IType testCaseType = testCaseType(javaProject); 493 if (testCaseType == null) 494 return found; 495 496 IType[] subtypes= javaProject.newTypeHierarchy(testCaseType, getRegion(javaProject), null).getAllSubtypes(testCaseType); 497 498 if (subtypes == null) 499 throw new JavaModelException(new CoreException(new Status(IStatus.ERROR, AntUIPlugin.PI_ANTUI, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_MAIN_TYPE, null, null))); 500 501 for (int i = 0; i < subtypes.length; i++) { 502 try { 503 if (element.equals(subtypes[i].getAncestor(element.getElementType())) && hasValidModifiers(subtypes[i])) 504 found.add(subtypes[i]); 505 } catch (JavaModelException e) { 506 AntUIPlugin.log(e.getStatus()); 507 } 508 } 509 return found; 510 } 511 512 private static IType testCaseType(IJavaProject javaProject) { 514 try { 515 return javaProject.findType("junit.framework.TestCase"); } catch (JavaModelException e) { 517 AntUIPlugin.log(e.getStatus()); 518 return null; 519 } 520 } 521 522 private static IRegion getRegion(IJavaProject javaProject) throws JavaModelException{ 524 IRegion region = JavaCore.newRegion(); 525 IJavaElement[] elements= javaProject.getChildren(); 526 for(int i=0; i<elements.length; i++) { 527 if (((IPackageFragmentRoot)elements[i]).isArchive()) 528 continue; 529 region.add(elements[i]); 530 } 531 return region; 532 } 533 534 private static Object computeScope(Object element) { 536 if (element instanceof IFileEditorInput) 537 element= ((IFileEditorInput)element).getFile(); 538 if (element instanceof IResource) 539 element= JavaCore.create((IResource)element); 540 if (element instanceof IClassFile) { 541 IClassFile cf= (IClassFile)element; 542 element= cf.getType(); 543 } 544 return element; 545 } 546 547 private static List searchSuiteMethods(IJavaElement element) throws CoreException { 549 IJavaSearchScope scope= SearchEngine.createJavaSearchScope(new IJavaElement[] { element }, 551 IJavaSearchScope.SOURCES); 552 return searchMethod(scope); 553 } 554 555 private static boolean hasSuiteMethod(IType type) throws JavaModelException { 557 IMethod method= type.getMethod("suite", new String [0]); if (method == null || !method.exists()) 559 return false; 560 561 if (!Flags.isStatic(method.getFlags()) || 562 !Flags.isPublic(method.getFlags()) || 563 !Flags.isPublic(method.getDeclaringType().getFlags())) { 564 return false; 565 } 566 if (!Signature.getSimpleName(Signature.toString(method.getReturnType())).equals("Test" )) { return false; 568 } 569 return true; 570 } 571 572 private static boolean isTestType(IType type) throws JavaModelException { 574 if (!hasValidModifiers(type)) 575 return false; 576 577 IType[] interfaces= type.newSupertypeHierarchy(null).getAllSuperInterfaces(type); 578 for (int i= 0; i < interfaces.length; i++) 579 if(interfaces[i].getFullyQualifiedName('.').equals("junit.framework.Test" )) return true; 581 return false; 582 } 583 584 private static boolean hasValidModifiers(IType type) throws JavaModelException { 586 if (Flags.isAbstract(type.getFlags())) 587 return false; 588 if (!Flags.isPublic(type.getFlags())) 589 return false; 590 return true; 591 } 592 593 596 public static synchronized Comparator getJavaProjectComparator() 597 { 598 if (javaProjectComparator == null) 599 { 600 javaProjectComparator = new JavaProjectComparator(); 601 } 602 return javaProjectComparator; 603 } 604 605 private static Comparator javaProjectComparator; 606 607 private static class JavaProjectComparator implements Comparator 608 { 609 public int compare(Object o1, Object o2) 610 { 611 IJavaProject j1 = (IJavaProject) o1; 612 IJavaProject j2 = (IJavaProject) o2; 613 return j1.getProject().getName().compareTo(j2.getProject().getName()); 614 } 615 616 public boolean equals(Object obj) 617 { 618 if (obj == null) 619 { 620 return false; 621 } 622 return compare(this, obj) == 0; 623 } 624 } 625 626 629 public static synchronized Comparator getClassnameComparator() 630 { 631 if (classnameComparator == null) 632 { 633 classnameComparator = new ClassnameComparator(); 634 } 635 return classnameComparator; 636 } 637 638 private static Comparator classnameComparator; 639 640 private static class ClassnameComparator implements Comparator 641 { 642 public int compare(Object o1, Object o2) 643 { 644 return o1.getClass().getName().compareTo(o2.getClass().getName()); 645 } 646 647 public boolean equals(Object obj) 648 { 649 if (obj == null) 650 { 651 return false; 652 } 653 return compare(this, obj) == 0; 654 } 655 } 656 657 660 public static synchronized Comparator getIFileComparator() 661 { 662 if (fileComparator == null) 663 { 664 fileComparator = new IFileComparator(); 665 } 666 return fileComparator; 667 } 668 669 private static Comparator fileComparator; 670 671 private static class IFileComparator implements Comparator 672 { 673 public int compare(Object o1, Object o2) 674 { 675 IFile f1 = (IFile) o1; 676 IFile f2 = (IFile) o2; 677 return f1.toString().compareTo(f2.toString()); 678 } 679 680 public boolean equals(Object obj) 681 { 682 if (obj == null) 683 { 684 return false; 685 } 686 return compare(this, obj) == 0; 687 } 688 } 689 690 693 public static synchronized Comparator getITypeComparator() 694 { 695 if (typeComparator == null) 696 { 697 typeComparator = new TypeComparator(); 698 } 699 return typeComparator; 700 } 701 702 private static Comparator typeComparator; 703 704 private static class TypeComparator implements Comparator 705 { 706 public int compare(Object o1, Object o2) 707 { 708 IType t1 = (IType) o1; 709 IType t2 = (IType) o2; 710 return t1.getFullyQualifiedName().compareTo(t2.getFullyQualifiedName()); 711 } 712 713 public boolean equals(Object obj) 714 { 715 if (obj == null) 716 { 717 return false; 718 } 719 return compare(this, obj) == 0; 720 } 721 } 722 723 726 public static final String NEWLINE = System.getProperty("line.separator"); 728 public static String removePrefix(String s, String prefix) 729 { 730 if (s == null) 731 { 732 return null; 733 } 734 if (s.startsWith(prefix)) 735 { 736 return s.substring(prefix.length()); 737 } 738 return s; 739 } 740 741 744 public static String removeSuffix(String s, String suffix) 745 { 746 if (s == null) 747 { 748 return null; 749 } 750 if (s.endsWith(suffix)) 751 { 752 return s.substring(0, s.length() - suffix.length()); 753 } 754 return s; 755 } 756 757 760 public static String removePrefixAndSuffix(String s, String prefix, String suffix) 761 { 762 return removePrefix(removeSuffix(s, suffix), prefix); 763 } 764 765 768 public static String toString(Document doc) throws TransformerConfigurationException , TransformerFactoryConfigurationError , TransformerException 769 { 770 775 StringWriter writer = new StringWriter (); 776 Source source = new DOMSource (doc); 777 Result result = new StreamResult (writer); 778 TransformerFactory factory = TransformerFactory.newInstance(); 779 boolean indentFallback = false; 780 try 781 { 782 factory.setAttribute("indent-number", "4"); } 785 catch (IllegalArgumentException e) 786 { 787 indentFallback = true; 789 } 790 Transformer transformer = factory.newTransformer(); 791 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); if (indentFallback) 793 { 794 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); } 797 transformer.transform(source, result); 798 return writer.toString(); 799 } 800 801 804 public static Document parseXmlFile(File file) throws SAXException , IOException , ParserConfigurationException 805 { 806 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 807 factory.setValidating(false); 808 Document doc = factory.newDocumentBuilder().parse(file); 809 return doc; 810 } 811 812 815 public static Document parseXmlString(String s) throws SAXException , IOException , ParserConfigurationException 816 { 817 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 818 factory.setValidating(false); 819 Document doc = factory.newDocumentBuilder().parse(new ByteArrayInputStream (s.getBytes())); 820 return doc; 821 } 822 823 829 public static String toString(Collection c, String separator) 830 { 831 StringBuffer b = new StringBuffer (); 832 for (Iterator iter = c.iterator(); iter.hasNext();) 833 { 834 b.append((String ) iter.next()); 835 b.append(separator); 836 } 837 if (c.size() > 0) { 838 b.delete(b.length() - separator.length(), b.length()); 839 } 840 return b.toString(); 841 } 842 843 848 public static List removeDuplicates(List l) 849 { 850 List res = new ArrayList (); 851 for (Iterator iter = l.iterator(); iter.hasNext();) 852 { 853 Object element = iter.next(); 854 if (!res.contains(element)) 855 { 856 res.add(element); 857 } 858 } 859 return res; 860 } 861 862 865 public static boolean existsUserFile(String filename) 866 { 867 File buildFile = new File (filename); 868 if (buildFile.exists()) 869 { 870 BufferedReader in = null; 871 try 872 { 873 in = new BufferedReader (new FileReader (buildFile)); 874 int i = BuildFileCreator.WARNING.indexOf(NEWLINE); 875 String warning = BuildFileCreator.WARNING.substring(0, i); 876 String line; 877 while ((line = in.readLine()) != null) 878 { 879 if (line.indexOf(warning) != -1) 880 { 881 return false; 882 } 883 } 884 return true; 885 } catch (FileNotFoundException e) { 886 return false; 887 } catch (IOException e) { 888 return false; 889 } finally 890 { 891 try 892 { 893 if (in != null) 894 { 895 in.close(); 896 } 897 } 898 catch (IOException e) 899 { 900 } 902 } 903 } 904 return false; 905 } 906 907 915 public static boolean validateEdit(Shell shell, IFile file) { 916 return file.getWorkspace().validateEdit(new IFile[] {file}, shell).isOK(); 917 } 918 919 928 public static Set validateEdit(Shell shell, List files) throws CoreException 929 { 930 Set confirmedFiles = new TreeSet (getIFileComparator()); 931 IStatus status = ((IFile) files.get(0)).getWorkspace().validateEdit((IFile[]) files.toArray(new IFile[files.size()]), shell); 932 if (status.isMultiStatus() && status.getChildren().length > 0) 933 { 934 for (int i = 0; i < status.getChildren().length; i++) 935 { 936 IStatus statusChild = status.getChildren()[i]; 937 if (statusChild.isOK()) 938 { 939 confirmedFiles.add(files.get(i)); 940 } 941 } 942 } 943 else if (status.isOK()) 944 { 945 for (Iterator iterator = files.iterator(); iterator.hasNext();) 946 { 947 IFile file = (IFile) iterator.next(); 948 confirmedFiles.add(file); 949 } 950 } 951 if (status.getSeverity() == IStatus.ERROR) { 952 StringBuffer message = new StringBuffer (status.getPlugin() + ": " + status.getMessage() + NEWLINE); 957 if (status.isMultiStatus()) 958 { 959 for (int i = 0; i < status.getChildren().length; i++) 960 { 961 IStatus statusChild = status.getChildren()[i]; 962 message.append(statusChild.getMessage() + NEWLINE); 963 } 964 } 965 throw new CoreException(new Status(IStatus.ERROR, AntUIPlugin.PI_ANTUI, 0, 966 message.toString(), null)); 967 } 968 969 return confirmedFiles; 970 } 971 972 976 public static boolean isDefaultClasspath(IJavaProject project, EclipseClasspath classpath) 977 { 978 List list = removeDuplicates(classpath.rawClassPathEntries); 979 if (list.size() != 1) 980 { 981 return false; 982 } 983 String entry = (String ) list.iterator().next(); 984 if (EclipseClasspath.isProjectReference(entry)) 985 { 986 IJavaProject referencedProject = EclipseClasspath.resolveProjectReference(entry); 987 if (referencedProject == null) 988 { 989 return false; 991 } 992 else if (referencedProject.getProject().getName().equals(project.getProject().getName())) 993 { 994 return true; 995 } 996 } 997 998 return false; 999 } 1000 1001 1007 public static void addVariable(Map variable2valueMap, String s, String projectRoot) 1008 { 1009 if (s == null || s.equals("")) { 1011 return; 1012 } 1013 Pattern pattern = Pattern.compile("\\$\\{.*?\\}"); Matcher matcher = pattern.matcher(s); 1015 while (matcher.find()) 1016 { 1017 String variable = matcher.group(); 1018 String value; 1019 try 1020 { 1021 value = VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(variable); 1022 } 1023 catch (CoreException e) 1024 { 1025 value = variable; 1027 } 1028 variable = removePrefixAndSuffix(variable, "${", "}"); if (variable.startsWith("env_var:")) { 1032 value = "env." + variable.substring("env_var:".length()); } 1034 File file = new File (value); 1035 if (file.exists()) { 1036 value = ExportUtil.getRelativePath(file.getAbsolutePath(), projectRoot); 1037 } 1038 variable2valueMap.put(variable, value); 1039 } 1040 } 1041 1042 1046 public static String getRelativePath(String otherLocation, String basePath) { 1047 1048 IPath location= new Path(otherLocation); 1049 IPath base= new Path(basePath); 1050 if ((location.getDevice() != null && !location.getDevice().equalsIgnoreCase(base.getDevice())) || !location.isAbsolute()) { 1051 return otherLocation; 1052 } 1053 int baseCount = base.segmentCount(); 1054 int count = base.matchingFirstSegments(location); 1055 String temp = ""; for (int j = 0; j < baseCount - count; j++) { 1057 temp += "../"; } 1059 String relative= new Path(temp).append(location.removeFirstSegments(count)).toString(); 1060 if (relative.length() == 0) { 1061 relative= "."; } 1063 1064 return relative; 1065 } 1066} | Popular Tags |