001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.search;
011:
012: import java.io.File;
013: import java.util.ArrayList;
014: import java.util.HashSet;
015:
016: import org.eclipse.core.resources.IProject;
017: import org.eclipse.core.resources.IResource;
018: import org.eclipse.core.runtime.IPath;
019: import org.eclipse.core.runtime.Path;
020: import org.eclipse.jdt.core.IJavaElement;
021: import org.eclipse.jdt.core.IJavaProject;
022: import org.eclipse.jdt.core.IPackageFragment;
023: import org.eclipse.jdt.core.IPackageFragmentRoot;
024: import org.eclipse.jdt.core.JavaModelException;
025: import org.eclipse.jdt.core.search.IJavaSearchScope;
026: import org.eclipse.jdt.core.search.SearchEngine;
027: import org.eclipse.pde.core.plugin.IFragmentModel;
028: import org.eclipse.pde.core.plugin.IPlugin;
029: import org.eclipse.pde.core.plugin.IPluginBase;
030: import org.eclipse.pde.core.plugin.IPluginImport;
031: import org.eclipse.pde.core.plugin.IPluginLibrary;
032: import org.eclipse.pde.core.plugin.IPluginModelBase;
033: import org.eclipse.pde.core.plugin.PluginRegistry;
034: import org.eclipse.pde.internal.core.ClasspathUtilCore;
035: import org.eclipse.pde.internal.core.PDEManager;
036:
037: public class PluginJavaSearchUtil {
038:
039: public static IPluginModelBase[] getPluginImports(IPluginImport dep) {
040: return getPluginImports(dep.getId());
041: }
042:
043: public static IPluginModelBase[] getPluginImports(
044: String pluginImportID) {
045: HashSet set = new HashSet();
046: collectAllPrerequisites(PluginRegistry
047: .findModel(pluginImportID), set);
048: return (IPluginModelBase[]) set
049: .toArray(new IPluginModelBase[set.size()]);
050: }
051:
052: public static void collectAllPrerequisites(IPluginModelBase model,
053: HashSet set) {
054: if (model == null || !set.add(model))
055: return;
056: IPluginImport[] imports = model.getPluginBase().getImports();
057: for (int i = 0; i < imports.length; i++) {
058: if (imports[i].isReexported()) {
059: IPluginModelBase child = PluginRegistry
060: .findModel(imports[i].getId());
061: if (child != null)
062: collectAllPrerequisites(child, set);
063: }
064: }
065: }
066:
067: public static IPackageFragment[] collectPackageFragments(
068: IPluginModelBase[] models, IJavaProject parentProject,
069: boolean filterEmptyPackages) throws JavaModelException {
070: ArrayList result = new ArrayList();
071: IPackageFragmentRoot[] roots = parentProject
072: .getAllPackageFragmentRoots();
073:
074: for (int i = 0; i < models.length; i++) {
075: IPluginModelBase model = models[i];
076: IResource resource = model.getUnderlyingResource();
077: if (resource == null) {
078: ArrayList libraryPaths = new ArrayList();
079: addLibraryPaths(model, libraryPaths);
080: for (int j = 0; j < roots.length; j++) {
081: if (libraryPaths.contains(roots[j].getPath())) {
082: extractPackageFragments(roots[j], result,
083: filterEmptyPackages);
084: }
085: }
086: } else {
087: IProject project = resource.getProject();
088: for (int j = 0; j < roots.length; j++) {
089: IJavaProject jProject = (IJavaProject) roots[j]
090: .getParent();
091: if (jProject.getProject().equals(project)) {
092: extractPackageFragments(roots[j], result,
093: filterEmptyPackages);
094: }
095: }
096: }
097: }
098: return (IPackageFragment[]) result
099: .toArray(new IPackageFragment[result.size()]);
100: }
101:
102: private static void extractPackageFragments(
103: IPackageFragmentRoot root, ArrayList result,
104: boolean filterEmpty) {
105: try {
106: IJavaElement[] children = root.getChildren();
107: for (int i = 0; i < children.length; i++) {
108: IPackageFragment fragment = (IPackageFragment) children[i];
109: if (!filterEmpty || fragment.hasChildren())
110: result.add(fragment);
111: }
112: } catch (JavaModelException e) {
113: }
114: }
115:
116: private static void addLibraryPaths(IPluginModelBase model,
117: ArrayList libraryPaths) {
118: IPluginBase plugin = model.getPluginBase();
119:
120: IFragmentModel[] fragments = new IFragmentModel[0];
121: if (plugin instanceof IPlugin)
122: fragments = PDEManager.findFragmentsFor(model);
123:
124: File file = new File(model.getInstallLocation());
125: if (file.isFile()) {
126: libraryPaths.add(new Path(file.getAbsolutePath()));
127: } else {
128: IPluginLibrary[] libraries = plugin.getLibraries();
129: for (int i = 0; i < libraries.length; i++) {
130: String libraryName = ClasspathUtilCore
131: .expandLibraryName(libraries[i].getName());
132: String path = plugin.getModel().getInstallLocation()
133: + IPath.SEPARATOR + libraryName;
134: if (new File(path).exists()) {
135: libraryPaths.add(new Path(path));
136: } else {
137: findLibraryInFragments(fragments, libraryName,
138: libraryPaths);
139: }
140: }
141: }
142: if (ClasspathUtilCore.hasExtensibleAPI(model)) {
143: for (int i = 0; i < fragments.length; i++) {
144: addLibraryPaths(fragments[i], libraryPaths);
145: }
146: }
147: }
148:
149: private static void findLibraryInFragments(
150: IFragmentModel[] fragments, String libraryName,
151: ArrayList libraryPaths) {
152: for (int i = 0; i < fragments.length; i++) {
153: String path = fragments[i].getInstallLocation()
154: + IPath.SEPARATOR + libraryName;
155: if (new File(path).exists()) {
156: libraryPaths.add(new Path(path));
157: break;
158: }
159: }
160: }
161:
162: public static IJavaSearchScope createSeachScope(
163: IJavaProject jProject) throws JavaModelException {
164: IPackageFragmentRoot[] roots = jProject
165: .getPackageFragmentRoots();
166: ArrayList filteredRoots = new ArrayList();
167: for (int i = 0; i < roots.length; i++) {
168: if (roots[i].getResource() != null
169: && roots[i].getResource().getProject().equals(
170: jProject.getProject())) {
171: filteredRoots.add(roots[i]);
172: }
173: }
174: return SearchEngine
175: .createJavaSearchScope((IJavaElement[]) filteredRoots
176: .toArray(new IJavaElement[filteredRoots.size()]));
177: }
178:
179: }
|