KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > search > PluginJavaSearchUtil


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.search;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashSet JavaDoc;
16
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.Path;
20 import org.eclipse.jdt.core.IJavaElement;
21 import org.eclipse.jdt.core.IJavaProject;
22 import org.eclipse.jdt.core.IPackageFragment;
23 import org.eclipse.jdt.core.IPackageFragmentRoot;
24 import org.eclipse.jdt.core.JavaCore;
25 import org.eclipse.jdt.core.JavaModelException;
26 import org.eclipse.pde.core.plugin.IFragment;
27 import org.eclipse.pde.core.plugin.IPlugin;
28 import org.eclipse.pde.core.plugin.IPluginBase;
29 import org.eclipse.pde.core.plugin.IPluginImport;
30 import org.eclipse.pde.core.plugin.IPluginLibrary;
31 import org.eclipse.pde.internal.core.ClasspathUtilCore;
32 import org.eclipse.pde.internal.core.PDECore;
33 import org.eclipse.pde.internal.core.plugin.WorkspacePluginModelBase;
34
35 public class PluginJavaSearchUtil {
36
37     public static void collectAllPrerequisites(IPlugin plugin, HashSet JavaDoc set) {
38         if (!set.add(plugin))
39             return;
40
41         if (plugin.getModel() instanceof WorkspacePluginModelBase) {
42             IFragment[] fragments =
43                 PDECore.getDefault().getWorkspaceModelManager().getFragmentsFor(
44                     plugin.getId(),
45                     plugin.getVersion());
46             for (int i = 0; i < fragments.length; i++) {
47                 set.add(fragments[i]);
48             }
49         }
50
51         IPluginImport[] imports = plugin.getImports();
52         for (int i = 0; i < imports.length; i++) {
53             if (imports[i].isReexported()) {
54                 IPlugin child = PDECore.getDefault().findPlugin(imports[i].getId());
55                 if (child != null)
56                     collectAllPrerequisites(child, set);
57             }
58         }
59     }
60
61     public static IPackageFragment[] collectPackageFragments(
62         IPluginBase[] models,
63         IProject parentProject)
64         throws JavaModelException {
65         ArrayList JavaDoc result = new ArrayList JavaDoc();
66         IPackageFragmentRoot[] roots =
67             JavaCore.create(parentProject).getAllPackageFragmentRoots();
68
69         for (int i = 0; i < models.length; i++) {
70             IPluginBase preReq = models[i];
71             IResource resource = preReq.getModel().getUnderlyingResource();
72             if (resource == null) {
73                 ArrayList JavaDoc libraryPaths = getLibraryPaths(preReq);
74                 for (int j = 0; j < roots.length; j++) {
75                     if (libraryPaths.contains(roots[j].getPath())) {
76                         extractFragments(roots[j], result);
77                     }
78                 }
79             } else {
80                 IProject project = resource.getProject();
81                 for (int j = 0; j < roots.length; j++) {
82                     IJavaProject jProject = (IJavaProject) roots[j].getParent();
83                     if (jProject.getProject().equals(project)) {
84                         extractFragments(roots[j], result);
85                     }
86                 }
87             }
88         }
89         return (IPackageFragment[]) result.toArray(new IPackageFragment[result.size()]);
90
91     }
92
93     private static void extractFragments(IPackageFragmentRoot root, ArrayList JavaDoc result) {
94         try {
95             IJavaElement[] children = root.getChildren();
96             for (int i = 0; i < children.length; i++) {
97                 IPackageFragment fragment = (IPackageFragment) children[i];
98                 if (fragment.getChildren().length > 0)
99                     result.add(fragment);
100             }
101         } catch (JavaModelException e) {
102         }
103     }
104
105     private static ArrayList JavaDoc getLibraryPaths(IPluginBase plugin) {
106         ArrayList JavaDoc libraryPaths = new ArrayList JavaDoc();
107         IFragment[] fragments =
108             PDECore.getDefault().findFragmentsFor(plugin.getId(), plugin.getVersion());
109
110         IPluginLibrary[] libraries = plugin.getLibraries();
111         for (int i = 0; i < libraries.length; i++) {
112             String JavaDoc libraryName =
113                 ClasspathUtilCore.expandLibraryName(libraries[i].getName());
114             String JavaDoc path =
115                 plugin.getModel().getInstallLocation() + Path.SEPARATOR + libraryName;
116             if (new File JavaDoc(path).exists()) {
117                 libraryPaths.add(new Path(path));
118             } else {
119                 findLibraryInFragments(fragments, libraryName, libraryPaths);
120             }
121         }
122         return libraryPaths;
123     }
124
125     private static void findLibraryInFragments(
126         IFragment[] fragments,
127         String JavaDoc libraryName,
128         ArrayList JavaDoc libraryPaths) {
129         for (int i = 0; i < fragments.length; i++) {
130             String JavaDoc path =
131                 fragments[i].getModel().getInstallLocation()
132                     + Path.SEPARATOR
133                     + libraryName;
134             if (new File JavaDoc(path).exists()) {
135                 libraryPaths.add(new Path(path));
136                 break;
137             }
138         }
139     }
140
141 }
142
Popular Tags