use of org.eclipse.jdt.core.IJarEntryResource in project xtext-eclipse by eclipse.
the class JdtClasspathUriResolver method findResourceInWorkspace.
protected URI findResourceInWorkspace(IJavaProject javaProject, URI classpathUri) throws CoreException {
if (javaProject.exists()) {
String packagePath = classpathUri.trimSegments(1).path();
String fullPath = classpathUri.path();
String fileName = classpathUri.lastSegment();
IPath filePath = new Path(fileName);
String packageName = isEmpty(packagePath) ? "" : packagePath.substring(1).replace('/', '.');
for (IPackageFragmentRoot packageFragmentRoot : javaProject.getAllPackageFragmentRoots()) {
IPackageFragment packageFragment = packageFragmentRoot.getPackageFragment(packageName);
if (packageFragment.exists()) {
IResource packageFragmentResource = packageFragment.getResource();
if (packageFragmentResource instanceof IContainer) {
IFile file = ((IContainer) packageFragmentResource).getFile(filePath);
if (file.exists())
return createPlatformResourceURI(file);
} else {
// jar file or external class folder
if (packageFragmentRoot.isArchive()) {
// jar file
Object[] nonJavaResources = getNonJavaResources(packageFragmentRoot, packageFragment);
for (Object nonJavaResource : nonJavaResources) {
IJarEntryResource jarEntryResource = (IJarEntryResource) nonJavaResource;
if (fullPath.equals(jarEntryResource.getFullPath().toString())) {
IResource packageFragmentRootResource = packageFragmentRoot.getResource();
if (packageFragmentRootResource != null) {
// we have a resource - use nested platform/resource
URI packageFragmentRootURI = createPlatformResourceURI(packageFragmentRootResource);
URI result = createArchiveURI(packageFragmentRootURI, fullPath);
return result;
} else {
// no resource - use file uri
IPath packageFragmentRootPath = packageFragmentRoot.getPath();
URI packageFragmentRootURI = URI.createFileURI(packageFragmentRootPath.toString());
URI result = createArchiveURI(packageFragmentRootURI, fullPath);
return result;
}
}
}
} else if (packageFragmentRoot.isExternal()) {
// external class folder
Object[] nonJavaResources = getNonJavaResources(packageFragmentRoot, packageFragment);
for (Object nonJavaResource : nonJavaResources) {
IJarEntryResource jarEntryResource = (IJarEntryResource) nonJavaResource;
if (fileName.equals(jarEntryResource.getName())) {
IResource packageFragmentRootResource = ((ExternalPackageFragmentRoot) packageFragmentRoot).resource();
IPath absolutePath = packageFragmentRootResource.getFullPath();
absolutePath = absolutePath.append(fullPath);
return createPlatformResourceURI(absolutePath);
}
}
}
}
}
}
// not found in a source folder - look for a resource relative to project root
// of this project or one of its dependencies
URI result = findResourceInProjectRoot(javaProject, classpathUri.path(), Sets.<String>newHashSet());
if (result != null) {
return result;
}
}
return classpathUri;
}
use of org.eclipse.jdt.core.IJarEntryResource in project xtext-eclipse by eclipse.
the class Storage2UriMapperJavaImpl method getUri.
/**
* @since 2.5
*/
@Override
public URI getUri(/* @NonNull */
IStorage storage) {
if (storage instanceof IJarEntryResource) {
final IJarEntryResource casted = (IJarEntryResource) storage;
IPackageFragmentRoot packageFragmentRoot = casted.getPackageFragmentRoot();
Map<URI, IStorage> data = getAllEntries(packageFragmentRoot);
for (Map.Entry<URI, IStorage> entry : data.entrySet()) {
if (entry.getValue().equals(casted))
return entry.getKey();
}
if (packageFragmentRoot.exists() && packageFragmentRoot.isArchive()) {
IPath jarPath = packageFragmentRoot.getPath();
URI jarURI;
if (packageFragmentRoot.isExternal()) {
jarURI = URI.createFileURI(jarPath.toOSString());
} else {
jarURI = URI.createPlatformResourceURI(jarPath.toString(), true);
}
URI result = URI.createURI("archive:" + jarURI + "!" + storage.getFullPath());
return result;
}
}
return null;
}
use of org.eclipse.jdt.core.IJarEntryResource in project xtext-eclipse by eclipse.
the class JarEntryLocator method getURI.
/**
* @return a URI for the given jarEntry, can be <code>null</code>.
*/
public URI getURI(IPackageFragmentRoot root, IJarEntryResource jarEntry, TraversalState state) {
if (root.isArchive()) {
URI jarURI = JarEntryURIHelper.getUriForPackageFragmentRoot(root);
URI storageURI = URI.createURI(jarEntry.getFullPath().toString());
return createJarURI(root.isArchive(), jarURI, storageURI);
} else if (root instanceof ExternalPackageFragmentRoot) {
IResource resource = ((ExternalPackageFragmentRoot) root).resource();
IPath result = resource.getFullPath();
for (int i = 1; i < state.getParents().size(); i++) {
Object obj = state.getParents().get(i);
if (obj instanceof IPackageFragment) {
result = result.append(new Path(((IPackageFragment) obj).getElementName().replace('.', '/')));
} else if (obj instanceof IJarEntryResource) {
result = result.append(((IJarEntryResource) obj).getName());
}
}
result = result.append(jarEntry.getName());
return URI.createPlatformResourceURI(result.toString(), true);
} else {
throw new IllegalStateException("Unexpeced root type: " + root.getClass().getName());
}
}
use of org.eclipse.jdt.core.IJarEntryResource in project xtext-eclipse by eclipse.
the class PackageFragmentRootWalker method traverse.
protected T traverse(IJarEntryResource jarEntry, boolean stopOnFirstResult, TraversalState state) {
T result = null;
if (jarEntry.isFile()) {
result = handle(jarEntry, state);
} else {
state.push(jarEntry);
IJarEntryResource[] children = jarEntry.getChildren();
for (IJarEntryResource child : children) {
result = traverse(child, stopOnFirstResult, state);
if (stopOnFirstResult && result != null)
return result;
}
state.pop();
}
return result;
}
use of org.eclipse.jdt.core.IJarEntryResource in project xtext-eclipse by eclipse.
the class PackageFragmentRootWalker method traverse.
protected T traverse(IPackageFragment pack, boolean stopOnFirstResult, TraversalState state) throws JavaModelException {
T result = null;
state.push(pack);
IJavaElement[] children = pack.getChildren();
for (IJavaElement iJavaElement : children) {
if (iJavaElement instanceof IPackageFragment) {
result = traverse((IPackageFragment) iJavaElement, stopOnFirstResult, state);
if (stopOnFirstResult && result != null)
return result;
}
}
Object[] resources = pack.getNonJavaResources();
for (Object object : resources) {
if (object instanceof IJarEntryResource) {
result = traverse((IJarEntryResource) object, stopOnFirstResult, state);
if (stopOnFirstResult && result != null)
return result;
}
}
state.pop();
return result;
}
Aggregations