01: /*******************************************************************************
02: * Copyright (c) 2006, 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.core.util;
11:
12: import java.util.LinkedList;
13: import java.util.List;
14:
15: import org.eclipse.core.resources.IFile;
16: import org.eclipse.core.resources.IProject;
17: import org.eclipse.core.resources.IResource;
18: import org.eclipse.jdt.core.IJavaProject;
19: import org.eclipse.jdt.core.IPackageFragmentRoot;
20: import org.eclipse.jdt.core.JavaCore;
21: import org.eclipse.jdt.core.JavaModelException;
22: import org.eclipse.pde.core.build.IBuild;
23: import org.eclipse.pde.core.build.IBuildEntry;
24: import org.eclipse.pde.internal.core.build.WorkspaceBuildModel;
25: import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
26:
27: public class ManifestUtils {
28:
29: private ManifestUtils() {
30: }
31:
32: public static IPackageFragmentRoot[] findPackageFragmentRoots(
33: IManifestHeader header, IProject project) {
34: IJavaProject javaProject = JavaCore.create(project);
35:
36: String[] libs;
37: if (header == null || header.getValue() == null)
38: libs = new String[] { "." }; //$NON-NLS-1$
39: else
40: libs = header.getValue().split(","); //$NON-NLS-1$
41:
42: IBuild build = getBuild(project);
43: if (build == null) {
44: try {
45: return javaProject.getPackageFragmentRoots();
46: } catch (JavaModelException e) {
47: return new IPackageFragmentRoot[0];
48: }
49: }
50: List pkgFragRoots = new LinkedList();
51: for (int j = 0; j < libs.length; j++) {
52: String lib = libs[j];
53: IPackageFragmentRoot root = null;
54: if (!lib.equals(".")) //$NON-NLS-1$
55: root = javaProject.getPackageFragmentRoot(project
56: .getFile(lib));
57: if (root != null && root.exists()) {
58: pkgFragRoots.add(root);
59: } else {
60: IBuildEntry entry = build.getEntry("source." + lib); //$NON-NLS-1$
61: if (entry == null)
62: continue;
63: String[] tokens = entry.getTokens();
64: for (int i = 0; i < tokens.length; i++) {
65: IResource resource = project.findMember(tokens[i]);
66: if (resource == null)
67: continue;
68: root = javaProject.getPackageFragmentRoot(resource);
69: if (root != null && root.exists())
70: pkgFragRoots.add(root);
71: }
72: }
73: }
74: return (IPackageFragmentRoot[]) pkgFragRoots
75: .toArray(new IPackageFragmentRoot[pkgFragRoots.size()]);
76: }
77:
78: public final static IBuild getBuild(IProject project) {
79: IFile buildProps = project.getFile("build.properties"); //$NON-NLS-1$
80: if (buildProps.exists()) {
81: WorkspaceBuildModel model = new WorkspaceBuildModel(
82: buildProps);
83: if (model != null)
84: return model.getBuild();
85: }
86: return null;
87: }
88:
89: public static boolean isImmediateRoot(IPackageFragmentRoot root)
90: throws JavaModelException {
91: int kind = root.getKind();
92: return kind == IPackageFragmentRoot.K_SOURCE
93: || (kind == IPackageFragmentRoot.K_BINARY && !root
94: .isExternal());
95: }
96:
97: }
|