blob: cbcf60e6ca89f37404a6d3c9ed2b297b0d4d903f (
plain) (
blame)
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
|
/*******************************************************************************
* Copyright (c) 2008-2010 Sonatype, Inc.
* 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:
* Sonatype, Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.m2e.core.project;
import java.io.File;
import java.util.LinkedHashSet;
import java.util.List;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.apache.maven.model.Resource;
/**
* Collection of helper methods to map between MavenProject and IResource.
*
* @author igor
*/
public class MavenProjectUtils {
private MavenProjectUtils() {
}
/**
* Returns project resource for given filesystem location or null the location is outside of project.
*
* @param resourceLocation absolute filesystem location
* @return IPath the full, absolute workspace path resourceLocation
*/
public static IPath getProjectRelativePath(IProject project, String resourceLocation) {
if(resourceLocation == null) {
return null;
}
IPath projectLocation = project.getLocation();
IPath directory = Path.fromOSString(resourceLocation); // this is an absolute path!
if(projectLocation == null || !projectLocation.isPrefixOf(directory)) {
return null;
}
return directory.removeFirstSegments(projectLocation.segmentCount()).makeRelative().setDevice(null);
}
public static IPath[] getResourceLocations(IProject project, List<Resource> resources) {
LinkedHashSet<IPath> locations = new LinkedHashSet<IPath>();
for(Resource resource : resources) {
locations.add(getProjectRelativePath(project, resource.getDirectory()));
}
return locations.toArray(new IPath[locations.size()]);
}
public static IPath[] getSourceLocations(IProject project, List<String> roots) {
LinkedHashSet<IPath> locations = new LinkedHashSet<IPath>();
for(String root : roots) {
IPath path = getProjectRelativePath(project, root);
if(path != null) {
locations.add(path);
}
}
return locations.toArray(new IPath[locations.size()]);
}
/**
* Returns the full, absolute path of the given file relative to the workspace. Returns null if the file does not
* exist or is not a member of this project.
*/
public static IPath getFullPath(IProject project, File file) {
if (project == null || file == null) {
return null;
}
IPath projectPath = project.getLocation();
if(projectPath == null) {
return null;
}
IPath filePath = new Path(file.getAbsolutePath());
if (!projectPath.isPrefixOf(filePath)) {
return null;
}
IResource resource = project.findMember(filePath.removeFirstSegments(projectPath.segmentCount()));
if (resource == null) {
return null;
}
return resource.getFullPath();
}
}
|