KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 package org.eclipse.pde.internal.core.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.IPath;
20 import org.eclipse.core.runtime.Path;
21 import org.eclipse.jdt.core.IJavaElement;
22 import org.eclipse.jdt.core.IJavaProject;
23 import org.eclipse.jdt.core.IPackageFragment;
24 import org.eclipse.jdt.core.IPackageFragmentRoot;
25 import org.eclipse.jdt.core.JavaModelException;
26 import org.eclipse.jdt.core.search.IJavaSearchScope;
27 import org.eclipse.jdt.core.search.SearchEngine;
28 import org.eclipse.pde.core.plugin.IFragmentModel;
29 import org.eclipse.pde.core.plugin.IPlugin;
30 import org.eclipse.pde.core.plugin.IPluginBase;
31 import org.eclipse.pde.core.plugin.IPluginImport;
32 import org.eclipse.pde.core.plugin.IPluginLibrary;
33 import org.eclipse.pde.core.plugin.IPluginModelBase;
34 import org.eclipse.pde.core.plugin.PluginRegistry;
35 import org.eclipse.pde.internal.core.ClasspathUtilCore;
36 import org.eclipse.pde.internal.core.PDEManager;
37
38 public class PluginJavaSearchUtil {
39     
40     public static IPluginModelBase[] getPluginImports(IPluginImport dep) {
41         return getPluginImports(dep.getId());
42     }
43
44     public static IPluginModelBase[] getPluginImports(String JavaDoc pluginImportID) {
45         HashSet JavaDoc set = new HashSet JavaDoc();
46         collectAllPrerequisites(PluginRegistry.findModel(pluginImportID), set);
47         return (IPluginModelBase[]) set.toArray(new IPluginModelBase[set.size()]);
48     }
49     
50     public static void collectAllPrerequisites(IPluginModelBase model, HashSet JavaDoc set) {
51         if (model == null || !set.add(model))
52             return;
53         IPluginImport[] imports = model.getPluginBase().getImports();
54         for (int i = 0; i < imports.length; i++) {
55             if (imports[i].isReexported()) {
56                 IPluginModelBase child = PluginRegistry.findModel(imports[i].getId());
57                 if (child != null)
58                     collectAllPrerequisites(child, set);
59             }
60         }
61     }
62     
63     public static IPackageFragment[] collectPackageFragments(
64         IPluginModelBase[] models,
65         IJavaProject parentProject,
66         boolean filterEmptyPackages)
67         throws JavaModelException {
68         ArrayList JavaDoc result = new ArrayList JavaDoc();
69         IPackageFragmentRoot[] roots = parentProject.getAllPackageFragmentRoots();
70
71         for (int i = 0; i < models.length; i++) {
72             IPluginModelBase model = models[i];
73             IResource resource = model.getUnderlyingResource();
74             if (resource == null) {
75                 ArrayList JavaDoc libraryPaths = new ArrayList JavaDoc();
76                 addLibraryPaths(model, libraryPaths);
77                 for (int j = 0; j < roots.length; j++) {
78                     if (libraryPaths.contains(roots[j].getPath())) {
79                         extractPackageFragments(roots[j], result, filterEmptyPackages);
80                     }
81                 }
82             } else {
83                 IProject project = resource.getProject();
84                 for (int j = 0; j < roots.length; j++) {
85                     IJavaProject jProject = (IJavaProject) roots[j].getParent();
86                     if (jProject.getProject().equals(project)) {
87                         extractPackageFragments(roots[j], result, filterEmptyPackages);
88                     }
89                 }
90             }
91         }
92         return (IPackageFragment[]) result.toArray(new IPackageFragment[result.size()]);
93     }
94
95     private static void extractPackageFragments(IPackageFragmentRoot root, ArrayList JavaDoc result, boolean filterEmpty) {
96         try {
97             IJavaElement[] children = root.getChildren();
98             for (int i = 0; i < children.length; i++) {
99                 IPackageFragment fragment = (IPackageFragment) children[i];
100                 if (!filterEmpty || fragment.hasChildren())
101                     result.add(fragment);
102             }
103         } catch (JavaModelException e) {
104         }
105     }
106
107     private static void addLibraryPaths(IPluginModelBase model, ArrayList JavaDoc libraryPaths) {
108         IPluginBase plugin = model.getPluginBase();
109         
110         IFragmentModel[] fragments = new IFragmentModel[0];
111         if (plugin instanceof IPlugin)
112             fragments = PDEManager.findFragmentsFor(model);
113
114         File JavaDoc file = new File JavaDoc(model.getInstallLocation());
115         if (file.isFile()) {
116             libraryPaths.add(new Path(file.getAbsolutePath()));
117         } else {
118             IPluginLibrary[] libraries = plugin.getLibraries();
119             for (int i = 0; i < libraries.length; i++) {
120                 String JavaDoc libraryName =
121                     ClasspathUtilCore.expandLibraryName(libraries[i].getName());
122                 String JavaDoc path =
123                     plugin.getModel().getInstallLocation() + IPath.SEPARATOR + libraryName;
124                 if (new File JavaDoc(path).exists()) {
125                     libraryPaths.add(new Path(path));
126                 } else {
127                     findLibraryInFragments(fragments, libraryName, libraryPaths);
128                 }
129             }
130         }
131         if (ClasspathUtilCore.hasExtensibleAPI(model)) {
132             for (int i = 0; i < fragments.length; i++) {
133                 addLibraryPaths(fragments[i], libraryPaths);
134             }
135         }
136     }
137
138     private static void findLibraryInFragments(
139         IFragmentModel[] fragments,
140         String JavaDoc libraryName,
141         ArrayList JavaDoc libraryPaths) {
142         for (int i = 0; i < fragments.length; i++) {
143             String JavaDoc path =
144                 fragments[i].getInstallLocation()
145                     + IPath.SEPARATOR
146                     + libraryName;
147             if (new File JavaDoc(path).exists()) {
148                 libraryPaths.add(new Path(path));
149                 break;
150             }
151         }
152     }
153     
154     public static IJavaSearchScope createSeachScope(IJavaProject jProject) throws JavaModelException {
155         IPackageFragmentRoot[] roots = jProject.getPackageFragmentRoots();
156         ArrayList JavaDoc filteredRoots = new ArrayList JavaDoc();
157         for (int i = 0; i < roots.length; i++) {
158             if (roots[i].getResource() != null
159                 && roots[i].getResource().getProject().equals(jProject.getProject())) {
160                 filteredRoots.add(roots[i]);
161             }
162         }
163         return SearchEngine.createJavaSearchScope(
164             (IJavaElement[]) filteredRoots.toArray(
165                 new IJavaElement[filteredRoots.size()]));
166     }
167
168
169 }
170
Popular Tags