Search in sources :

Example 11 with IJarEntryResource

use of org.eclipse.jdt.core.IJarEntryResource in project xtext-eclipse by eclipse.

the class SourceAttachmentPackageFragmentRootWalker method getLogicalURI.

/**
 * Converts the physical URI to a logic URI based on the bundle symbolic name.
 */
protected URI getLogicalURI(URI uri, IStorage storage, TraversalState state) {
    if (bundleSymbolicName != null) {
        URI logicalURI = URI.createPlatformResourceURI(bundleSymbolicName, false);
        List<?> parents = state.getParents();
        for (int i = 1; i < parents.size(); i++) {
            Object obj = parents.get(i);
            if (obj instanceof IPackageFragment) {
                logicalURI = logicalURI.appendSegments(((IPackageFragment) obj).getElementName().split("\\."));
            } else if (obj instanceof IJarEntryResource) {
                logicalURI = logicalURI.appendSegment(((IJarEntryResource) obj).getName());
            } else if (obj instanceof IFolder) {
                logicalURI = logicalURI.appendSegment(((IFolder) obj).getName());
            }
        }
        return logicalURI.appendSegment(uri.lastSegment());
    }
    return uri;
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource) URI(org.eclipse.emf.common.util.URI) IFolder(org.eclipse.core.resources.IFolder)

Example 12 with IJarEntryResource

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());
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IPath(org.eclipse.core.runtime.IPath) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource) ExternalPackageFragmentRoot(org.eclipse.jdt.internal.core.ExternalPackageFragmentRoot) URI(org.eclipse.emf.common.util.URI) IResource(org.eclipse.core.resources.IResource)

Example 13 with IJarEntryResource

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;
}
Also used : IPath(org.eclipse.core.runtime.IPath) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource) IStorage(org.eclipse.core.resources.IStorage) URI(org.eclipse.emf.common.util.URI) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ForwardingMap(com.google.common.collect.ForwardingMap) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 14 with IJarEntryResource

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;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource) IContainer(org.eclipse.core.resources.IContainer) URI(org.eclipse.emf.common.util.URI) IResource(org.eclipse.core.resources.IResource) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Aggregations

IJarEntryResource (org.eclipse.jdt.core.IJarEntryResource)14 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)8 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)7 IFile (org.eclipse.core.resources.IFile)5 URI (org.eclipse.emf.common.util.URI)5 IJavaElement (org.eclipse.jdt.core.IJavaElement)5 IResource (org.eclipse.core.resources.IResource)4 IStorage (org.eclipse.core.resources.IStorage)4 IPath (org.eclipse.core.runtime.IPath)4 Path (org.eclipse.core.runtime.Path)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 ITypeRoot (org.eclipse.jdt.core.ITypeRoot)2 Javadoc (org.eclipse.jdt.core.dom.Javadoc)2 ForwardingMap (com.google.common.collect.ForwardingMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 IContainer (org.eclipse.core.resources.IContainer)1 IFolder (org.eclipse.core.resources.IFolder)1 IProject (org.eclipse.core.resources.IProject)1