/
WorkspaceClassPathFinder.java
226 lines (191 loc) · 6.37 KB
/
WorkspaceClassPathFinder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*******************************************************************************
* Copyright (c) 2005 Actuate Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Actuate Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.birt.report.debug.internal.ui.launcher.util;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.birt.report.designer.ui.IDatasetWorkspaceClasspathFinder;
import org.eclipse.birt.report.viewer.utilities.IWorkspaceClasspathFinder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import com.ibm.icu.util.StringTokenizer;
/**
* Class used to find a classpath based on projects or workspace
*
* @deprecated
*/
public class WorkspaceClassPathFinder implements IWorkspaceClasspathFinder, IDatasetWorkspaceClasspathFinder {
public WorkspaceClassPathFinder() {
}
public String getClassPath(String projects) {
if (projects == null || projects.length() == 0) {
return null;
}
StringBuffer wbuf = new StringBuffer();
StringTokenizer token = new StringTokenizer(projects, PROPERTYSEPARATOR);
boolean hasHeader = false;
while (token.hasMoreTokens()) {
String projectName = token.nextToken();
List paths = getProjectPaths(projectName);
for (int i = 0; i < paths.size(); i++) {
String url = (String) paths.get(i);
if (url != null && url.length() != 0)
if (i == 0 && !hasHeader) {
wbuf.append(url);
hasHeader = true;
} else {
wbuf.append(PROPERTYSEPARATOR + url);
}
}
}
return wbuf.toString();
}
public String getClassPath() {
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
String projectString = ""; //$NON-NLS-1$
for (int i = 0; i < projects.length; i++) {
IProject proj = projects[i];
projectString += proj.getName() + PROPERTYSEPARATOR;
}
return getClassPath(projectString);
}
/**
* @param projectName
* @return
*/
private List getProjectPaths(String projectName) {
List retValue = new ArrayList();
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
if (projectName == null) {
return retValue;
}
List paths = getProjectPath(project);
for (int j = 0; j < paths.size(); j++) {
URL url = (URL) paths.get(j);
if (url != null) {
retValue.add(url.getPath());
}
}
String url = getProjectOutClassPath(project);
if (url != null) {
retValue.add(url);
}
return retValue;
}
private String getProjectOutClassPath(IProject project) {
if (!hasJavaNature(project)) {
return null;
}
IJavaProject fCurrJProject = JavaCore.create(project);
IPath path = null;
boolean projectExists = (project.exists() && project.getFile(".classpath").exists()); //$NON-NLS-1$
if (projectExists) {
if (path == null) {
path = fCurrJProject.readOutputLocation();
// String curPath = path.toOSString( );
// String directPath = project.getLocation( ).toOSString( );
// int index = directPath.lastIndexOf( File.separator );
String absPath = getFullPath(path, project);
return absPath;
}
}
return null;
}
private List getProjectPath(IProject project) {
List retValue = new ArrayList();
if (!hasJavaNature(project)) {
return retValue;
}
IJavaProject fCurrJProject = JavaCore.create(project);
IClasspathEntry[] classpathEntries = null;
boolean projectExists = (project.exists() && project.getFile(".classpath").exists()); //$NON-NLS-1$
if (projectExists) {
if (classpathEntries == null) {
classpathEntries = fCurrJProject.readRawClasspath();
}
}
if (classpathEntries != null) {
retValue = getExistingEntries(classpathEntries, project);
}
return retValue;
}
private List getExistingEntries(IClasspathEntry[] classpathEntries, IProject project) {
ArrayList newClassPath = new ArrayList();
for (int i = 0; i < classpathEntries.length; i++) {
IClasspathEntry curr = classpathEntries[i];
if (curr.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
try {
boolean inWorkSpace = true;
IWorkspace space = ResourcesPlugin.getWorkspace();
if (space == null || space.getRoot() == null) {
inWorkSpace = false;
}
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IPath path = curr.getPath();
if (root.findMember(path) == null) {
inWorkSpace = false;
}
if (inWorkSpace) {
String absPath = getFullPath(path, root.findMember(path).getProject());
URL url = new URL("file:///" + absPath);//$NON-NLS-1$ //file:/
newClassPath.add(url);
} else {
newClassPath.add(curr.getPath().toFile().toURL());
}
} catch (MalformedURLException e) {
// DO nothing
}
}
}
return newClassPath;
}
private String getFullPath(IPath path, IProject project) {
// String curPath = path.toOSString( );
// String directPath = project.getLocation( ).toOSString( );
// int index = directPath.lastIndexOf( File.separator );
// String absPath = directPath.substring( 0, index ) + curPath;
// return absPath;
String directPath;
try {
directPath = project.getDescription().getLocationURI().toURL().getPath();
} catch (Exception e) {
directPath = project.getLocation().toOSString();
}
String curPath = path.toOSString();
int index = curPath.substring(1).indexOf(File.separator);
String absPath = directPath + curPath.substring(index + 1);
return absPath;
}
/**
* Returns true if the given project is accessible and it has a java nature,
* otherwise false.
*
* @param project IProject
* @return boolean
*/
private boolean hasJavaNature(IProject project) {
try {
return project.hasNature(JavaCore.NATURE_ID);
} catch (CoreException e) {
// project does not exist or is not open
}
return false;
}
}