KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > util > ManifestUtils


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.util;
12
13 import java.util.LinkedList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.jdt.core.IJavaProject;
20 import org.eclipse.jdt.core.IPackageFragmentRoot;
21 import org.eclipse.jdt.core.JavaCore;
22 import org.eclipse.jdt.core.JavaModelException;
23 import org.eclipse.pde.core.build.IBuild;
24 import org.eclipse.pde.core.build.IBuildEntry;
25 import org.eclipse.pde.internal.core.build.WorkspaceBuildModel;
26 import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
27
28 public class ManifestUtils {
29
30     private ManifestUtils() {}
31
32     public static IPackageFragmentRoot[] findPackageFragmentRoots(IManifestHeader header, IProject project) {
33         IJavaProject javaProject = JavaCore.create(project);
34
35         String JavaDoc[] libs;
36         if (header == null || header.getValue() == null)
37             libs = new String JavaDoc[] {"."}; //$NON-NLS-1$
38
else
39             libs = header.getValue().split(","); //$NON-NLS-1$
40

41         IBuild build = getBuild(project);
42         if (build == null) {
43             try {
44                 return javaProject.getPackageFragmentRoots();
45             } catch (JavaModelException e) {
46                 return new IPackageFragmentRoot[0];
47             }
48         }
49         List JavaDoc pkgFragRoots = new LinkedList JavaDoc();
50         for (int j = 0; j < libs.length; j++) {
51             String JavaDoc lib = libs[j];
52             IPackageFragmentRoot root = null;
53             if (!lib.equals(".")) //$NON-NLS-1$
54
root = javaProject.getPackageFragmentRoot(project.getFile(lib));
55             if (root != null && root.exists()) {
56                 pkgFragRoots.add(root);
57             } else {
58                 IBuildEntry entry = build.getEntry("source." + lib); //$NON-NLS-1$
59
if (entry == null)
60                     continue;
61                 String JavaDoc[] tokens = entry.getTokens();
62                 for (int i = 0; i < tokens.length; i++) {
63                     IResource resource = project.findMember(tokens[i]);
64                     if (resource == null) continue;
65                     root = javaProject.getPackageFragmentRoot(resource);
66                     if (root != null && root.exists())
67                         pkgFragRoots.add(root);
68                 }
69             }
70         }
71         return (IPackageFragmentRoot[]) pkgFragRoots.toArray(new IPackageFragmentRoot[pkgFragRoots.size()]);
72     }
73
74     public final static IBuild getBuild(IProject project){
75         IFile buildProps = project.getFile("build.properties"); //$NON-NLS-1$
76
if (buildProps.exists()) {
77             WorkspaceBuildModel model = new WorkspaceBuildModel(buildProps);
78             if (model != null)
79                 return model.getBuild();
80         }
81         return null;
82     }
83
84     public static boolean isImmediateRoot(IPackageFragmentRoot root) throws JavaModelException {
85         int kind = root.getKind();
86         return kind == IPackageFragmentRoot.K_SOURCE
87         || (kind == IPackageFragmentRoot.K_BINARY && !root.isExternal());
88     }
89
90 }
91
Popular Tags