KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > build > ClasspathHelper


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.build;
12
13 import java.io.*;
14 import java.net.*;
15 import java.util.*;
16
17 import org.eclipse.core.resources.*;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.core.runtime.preferences.*;
20 import org.eclipse.jdt.core.*;
21 import org.eclipse.pde.core.plugin.*;
22 import org.eclipse.pde.internal.core.*;
23
24 public class ClasspathHelper {
25
26     public static String JavaDoc getDevEntriesProperties(String JavaDoc fileName, boolean checkExcluded) {
27         File file = new File(fileName);
28         if (!file.exists()) {
29             File directory = file.getParentFile();
30             if (directory != null && (!directory.exists() || directory.isFile())) {
31                 directory.mkdirs();
32             }
33         }
34         Properties properties = new Properties();
35         WorkspaceModelManager manager = PDECore.getDefault().getWorkspaceModelManager();
36         IPluginModelBase[] models = manager.getAllModels();
37         for (int i = 0; i < models.length; i++) {
38             String JavaDoc id = models[i].getPluginBase().getId();
39             if (id == null)
40                 continue;
41             String JavaDoc entry = writeEntry(getOutputFolders(models[i], checkExcluded));
42             if (entry.length() > 0)
43                 properties.put(id, entry);
44         }
45         
46         try {
47             FileOutputStream stream = new FileOutputStream(fileName);
48             properties.store(stream, ""); //$NON-NLS-1$
49
stream.flush();
50             stream.close();
51             return new URL("file:" + fileName).toString(); //$NON-NLS-1$
52
} catch (IOException e) {
53             PDECore.logException(e);
54         }
55         return getDevEntries(checkExcluded);
56     }
57     
58     public static String JavaDoc getDevEntries(boolean checkExcluded) {
59         WorkspaceModelManager manager = PDECore.getDefault().getWorkspaceModelManager();
60         IPluginModelBase[] models = manager.getAllModels();
61         ArrayList list = new ArrayList();
62         for (int i = 0; i < models.length; i++) {
63             String JavaDoc id = models[i].getPluginBase().getId();
64             if (id == null || id.trim().length() == 0)
65                 continue;
66             IPath[] paths = getOutputFolders(models[i], checkExcluded);
67             for (int j = 0; j < paths.length; j++) {
68                 list.add(paths[j]);
69             }
70         }
71         String JavaDoc entry = writeEntry((IPath[])list.toArray(new IPath[list.size()]));
72         return entry.length() > 0 ? entry : "bin"; //$NON-NLS-1$
73
}
74     
75     private static String JavaDoc writeEntry(IPath[] paths) {
76         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
77         for (int i = 0; i < paths.length; i++) {
78             buffer.append(paths[i].toString());
79             if (i < paths.length - 1)
80                 buffer.append(","); //$NON-NLS-1$
81
}
82         return buffer.toString();
83     }
84
85     private static IPath[] getOutputFolders(IPluginModelBase model, boolean checkExcluded) {
86         ArrayList result = new ArrayList();
87         IProject project = model.getUnderlyingResource().getProject();
88         try {
89             if (project.hasNature(JavaCore.NATURE_ID)) {
90                 IJavaProject jProject = JavaCore.create(project);
91                 
92                 List excluded = getFoldersToExclude(project, checkExcluded);
93                 IPath path = jProject.getOutputLocation();
94                 if (path != null && !excluded.contains(path))
95                     addPath(result, project, path);
96                 
97                 IClasspathEntry[] entries = jProject.getRawClasspath();
98                 for (int i = 0; i < entries.length; i++) {
99                     if (entries[i].getContentKind() == IPackageFragmentRoot.K_SOURCE
100                             && entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
101                         path = entries[i].getOutputLocation();
102                         if (path != null && !excluded.contains(path))
103                             addPath(result, project, path);
104                     }
105                 }
106             }
107         } catch (JavaModelException e) {
108         } catch (CoreException e) {
109         }
110         return (IPath[])result.toArray(new IPath[result.size()]);
111     }
112
113     private static void addPath(ArrayList result, IProject project, IPath path) {
114         if (path.getDevice() == null) {
115             if (path.segmentCount() >= 1) {
116                 if (path.segment(0).equals(project.getName())) {
117                     path = path.removeFirstSegments(1);
118                     if (path.segmentCount() == 0)
119                         path = new Path("."); //$NON-NLS-1$
120
}
121             }
122         }
123         
124         if (!result.contains(path))
125             result.add(path);
126     }
127     
128     private static List getFoldersToExclude(IProject project, boolean checkExcluded) {
129         ArrayList list = new ArrayList();
130         if (checkExcluded) {
131             IEclipsePreferences pref = new ProjectScope(project).getNode(PDECore.PLUGIN_ID);
132             if (pref != null) {
133                 String JavaDoc binExcludes = pref.get(PDECore.SELFHOSTING_BIN_EXLCUDES, ""); //$NON-NLS-1$
134
StringTokenizer tokenizer = new StringTokenizer(binExcludes, ","); //$NON-NLS-1$
135
while (tokenizer.hasMoreTokens()) {
136                     list.add(new Path(tokenizer.nextToken().trim()));
137                 }
138             }
139         }
140         return list;
141     }
142
143 }
144
Popular Tags