Example usage for org.eclipse.jdt.core IPackageFragmentRoot getSourceAttachmentPath

List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot getSourceAttachmentPath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IPackageFragmentRoot getSourceAttachmentPath.

Prototype

IPath getSourceAttachmentPath() throws JavaModelException;

Source Link

Document

Returns the absolute path to the source archive attached to this package fragment root's binary archive.

Usage

From source file:at.bestsolution.fxide.jdt.text.javadoc.JavadocContentAccess2.java

License:Open Source License

private static String getFileContentFromAttachedSource(IPackageFragmentRoot root, String filePath)
        throws CoreException {
    IPath sourceAttachmentPath = root.getSourceAttachmentPath();
    if (sourceAttachmentPath != null) {
        File file = null;//from  w  ww  .  ja v  a 2  s.  c  o  m
        String encoding = null;

        if (sourceAttachmentPath.getDevice() == null) {
            //the path could be a workspace relative path to a zip or to the source folder
            IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();
            IResource res = wsRoot.findMember(sourceAttachmentPath);

            if (res instanceof IFile) {
                // zip in the workspace
                IPath location = res.getLocation();
                if (location == null)
                    return null;
                file = location.toFile();
                encoding = ((IFile) res).getCharset(false);

            } else if (res instanceof IContainer) {
                // folder in the workspace
                res = ((IContainer) res).findMember(filePath);
                if (!(res instanceof IFile))
                    return null;
                encoding = ((IFile) res).getCharset(false);
                if (encoding == null)
                    encoding = getSourceAttachmentEncoding(root);
                return getContentsFromInputStream(((IFile) res).getContents(), encoding);
            }
        }

        if (file == null || !file.exists())
            file = sourceAttachmentPath.toFile();

        if (file.isDirectory()) {
            //the path is an absolute filesystem path to the source folder
            IPath packagedocPath = sourceAttachmentPath.append(filePath);
            if (packagedocPath.toFile().exists())
                return getFileContent(packagedocPath.toFile());

        } else if (file.exists()) {
            //the package documentation is in a Jar/Zip
            IPath sourceAttachmentRootPath = root.getSourceAttachmentRootPath();
            String packagedocPath;
            //consider the root path also in the search path if it exists
            if (sourceAttachmentRootPath != null) {
                packagedocPath = sourceAttachmentRootPath.append(filePath).toString();
            } else {
                packagedocPath = filePath;
            }
            ZipFile zipFile = null;
            InputStream in = null;
            try {
                zipFile = new ZipFile(file, ZipFile.OPEN_READ);
                ZipEntry packagedocFile = zipFile.getEntry(packagedocPath);
                if (packagedocFile != null) {
                    in = zipFile.getInputStream(packagedocFile);
                    if (encoding == null)
                        encoding = getSourceAttachmentEncoding(root);
                    return getContentsFromInputStream(in, encoding);
                }
            } catch (IOException e) {
                throw new CoreException(
                        new Status(IStatus.ERROR, "at.bestsolution.fxide.jdt.text", e.getMessage(), e));
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException e) {
                    //ignore
                }
                try {
                    if (zipFile != null) {
                        zipFile.close();//this will close the InputStream also
                    }
                } catch (IOException e) {
                    //ignore
                }
            }
        }
    }

    return null;
}

From source file:ccw.ClojureCore.java

License:Open Source License

private static IEditorInput findEditorInputInSourceAttachment(IPackageFragmentRoot packageFragmentRoot,
        String searchedPackage, String searchedFileName) throws JavaModelException {

    final IPath sourceAttachmentPath = packageFragmentRoot.getSourceAttachmentPath();

    if (sourceAttachmentPath == null) {
        return null;
    }/*from www . j a  va2 s  .  co m*/

    final String searchedPath = searchedPackage.replaceAll("\\.", "/");

    final IResource workspaceResource = ResourcesPlugin.getWorkspace().getRoot()
            .findMember(sourceAttachmentPath);

    // Find in workspace
    if (workspaceResource != null) {
        if (workspaceResource.getType() == IResource.FOLDER) {
            IFolder folder = (IFolder) workspaceResource;
            IFile r = (IFile) folder.findMember(searchedPath + "/" + searchedFileName);
            if (r != null && r.exists()) {
                return new FileEditorInput(r);
            }
        } else {
            // Don't know what to do here
        }
    }

    // Find outside workspace or in archive 
    final IPath sourceAbsolutePath = toOSAbsoluteIPath(sourceAttachmentPath);

    final File sourceFile = sourceAbsolutePath.toFile();
    if (!sourceFile.exists()) {
        CCWPlugin.logWarning("sourceFile " + sourceFile + " does not exist form sourceAttachmentPath "
                + sourceAttachmentPath);
        // Nothing can be done
    } else if (sourceFile.isDirectory()) {
        final File maybeSourceFile = sourceAbsolutePath.append(searchedPath + "/" + searchedFileName).toFile();
        if (maybeSourceFile.exists()) {
            return new LocalFileStorageEditorInput(new LocalFileStorage(maybeSourceFile));
        } else {
            // Nothing, alas
        }
    } else {
        ZipFile zipFile;
        try {
            zipFile = new JarFile(sourceFile, true, JarFile.OPEN_READ);
            ZipEntry zipEntry = zipFile.getEntry(searchedPath + "/" + searchedFileName);
            if (zipEntry != null) {
                return new ZipEntryStorageEditorInput(new ZipEntryStorage(zipFile, zipEntry));
            } else {
                // Nothing, alas
            }
        } catch (IOException e) {
            CCWPlugin.logError("Error trying to open " + sourceAbsolutePath, e);
        }
    }
    return null;
}

From source file:com.codenvy.ide.ext.java.server.javadoc.JavadocContentAccess2.java

License:Open Source License

private static String getFileContentFromAttachedSource(IPackageFragmentRoot root, String filePath)
        throws CoreException {
    IPath sourceAttachmentPath = root.getSourceAttachmentPath();
    if (sourceAttachmentPath != null) {
        File file = null;/*ww  w .  j ava 2s .c o m*/
        String encoding = null;

        if (sourceAttachmentPath.getDevice() == null) {
            //the path could be a workspace relative path to a zip or to the source folder
            IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();
            IResource res = wsRoot.findMember(sourceAttachmentPath);

            if (res instanceof IFile) {
                // zip in the workspace
                IPath location = res.getLocation();
                if (location == null)
                    return null;
                file = location.toFile();
                encoding = ((IFile) res).getCharset(false);

            } else if (res instanceof IContainer) {
                // folder in the workspace
                res = ((IContainer) res).findMember(filePath);
                if (!(res instanceof IFile))
                    return null;
                encoding = ((IFile) res).getCharset(false);
                if (encoding == null)
                    encoding = getSourceAttachmentEncoding(root);
                return getContentsFromInputStream(((IFile) res).getContents(), encoding);
            }
        }

        if (file == null || !file.exists())
            file = sourceAttachmentPath.toFile();

        if (file.isDirectory()) {
            //the path is an absolute filesystem path to the source folder
            IPath packagedocPath = sourceAttachmentPath.append(filePath);
            if (packagedocPath.toFile().exists())
                return getFileContent(packagedocPath.toFile());

        } else if (file.exists()) {
            //the package documentation is in a Jar/Zip
            IPath sourceAttachmentRootPath = root.getSourceAttachmentRootPath();
            String packagedocPath;
            //consider the root path also in the search path if it exists
            if (sourceAttachmentRootPath != null) {
                packagedocPath = sourceAttachmentRootPath.append(filePath).toString();
            } else {
                packagedocPath = filePath;
            }
            ZipFile zipFile = null;
            InputStream in = null;
            try {
                zipFile = new ZipFile(file, ZipFile.OPEN_READ);
                ZipEntry packagedocFile = zipFile.getEntry(packagedocPath);
                if (packagedocFile != null) {
                    in = zipFile.getInputStream(packagedocFile);
                    if (encoding == null)
                        encoding = getSourceAttachmentEncoding(root);
                    return getContentsFromInputStream(in, encoding);
                }
            } catch (IOException e) {
                throw new CoreException(
                        new Status(IStatus.ERROR, "JavaPlugin.getPluginId()", e.getMessage(), e));
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException e) {
                    //ignore
                }
                try {
                    if (zipFile != null) {
                        zipFile.close();//this will close the InputStream also
                    }
                } catch (IOException e) {
                    //ignore
                }
            }
        }
    }

    return null;
}

From source file:com.codenvy.ide.ext.java.server.javadoc.JavaDocLocations.java

License:Open Source License

/**
 * Returns the reason for why the Javadoc of the Java element could not be retrieved.
 *
 * @param element whose Javadoc could not be retrieved
 * @param root the root of the Java element
 * @return the String message for why the Javadoc could not be retrieved for the Java element or
 *         <code>null</code> if the Java element is from a source container
 * @since 3.9//from w  ww . j  av  a2 s .  c o m
 */
public static String getExplanationForMissingJavadoc(IJavaElement element, IPackageFragmentRoot root) {
    String message = null;
    try {
        boolean isBinary = (root.exists() && root.getKind() == IPackageFragmentRoot.K_BINARY);
        if (isBinary) {
            boolean hasAttachedJavadoc = JavaDocLocations.getJavadocBaseLocation(element) != null;
            boolean hasAttachedSource = root.getSourceAttachmentPath() != null;
            IOpenable openable = element.getOpenable();
            boolean hasSource = openable.getBuffer() != null;

            // Provide hint why there's no Java doc
            if (!hasAttachedSource && !hasAttachedJavadoc)
                message = CorextMessages.JavaDocLocations_noAttachments;
            else if (!hasAttachedJavadoc && !hasSource)
                message = CorextMessages.JavaDocLocations_noAttachedJavadoc;
            else if (!hasAttachedSource)
                message = CorextMessages.JavaDocLocations_noAttachedSource;
            else if (!hasSource)
                message = CorextMessages.JavaDocLocations_noInformation;

        }
    } catch (JavaModelException e) {
        message = CorextMessages.JavaDocLocations_error_gettingJavadoc;
        LOG.error(message, e);
    }
    return message;
}

From source file:com.ifedorenko.m2e.sourcelookup.internal.JavaProjectSources.java

License:Open Source License

private void addJavaProject(IJavaProject project) throws CoreException {
    if (project != null) {
        final Map<File, IPackageFragmentRoot> classpath = new LinkedHashMap<File, IPackageFragmentRoot>();
        for (IPackageFragmentRoot fragment : project.getPackageFragmentRoots()) {
            if (fragment.getKind() == IPackageFragmentRoot.K_BINARY
                    && fragment.getSourceAttachmentPath() != null) {
                File classpathLocation;
                if (fragment.isExternal()) {
                    classpathLocation = fragment.getPath().toFile();
                } else {
                    classpathLocation = toFile(fragment.getPath());
                }/*from w  w w.  ja  va  2 s .c om*/
                if (classpathLocation != null) {
                    classpath.put(classpathLocation, fragment);
                }
            }
        }

        final JavaProjectInfo projectInfo = new JavaProjectInfo(project, classpath);

        final Set<File> projectLocations = new HashSet<File>();

        final String jarLocation = project.getProject().getPersistentProperty(BinaryProjectPlugin.QNAME_JAR);
        if (jarLocation != null) {
            // maven binary project
            projectLocations.add(new File(jarLocation));
        } else {
            // regular project
            projectLocations.add(toFile(project.getOutputLocation()));
            for (IClasspathEntry cpe : project.getRawClasspath()) {
                if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    projectLocations.add(toFile(cpe.getOutputLocation()));
                }
            }
        }

        synchronized (lock) {
            projects.put(project, projectLocations);
            for (File projectLocation : projectLocations) {
                locations.put(projectLocation, projectInfo);
            }
        }
    }
}

From source file:com.mountainminds.eclemma.internal.core.instr.SourceLocation.java

License:Open Source License

public static ISourceLocation findLocation(IPackageFragmentRoot root) throws JavaModelException {
    if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
        IPath path = EclEmmaCorePlugin.getAbsolutePath(root.getPath());
        return new SourceLocation(path, new Path(IPackageFragmentRoot.DEFAULT_PACKAGEROOT_PATH));
    } else {/*from w w  w  . j  a  v  a2 s  .  c om*/
        IPath path = root.getSourceAttachmentPath();
        if (path != null) {
            path = EclEmmaCorePlugin.getAbsolutePath(path);
            return new SourceLocation(path, root.getSourceAttachmentRootPath());
        } else {
            return null;
        }
    }
}

From source file:com.redhat.ceylon.eclipse.code.explorer.JavaElementImageProvider.java

License:Open Source License

/**
 * Returns an image descriptor for a java element. This is the base image, no overlays.
 * @param element the element//from  w  w  w . j a  v  a  2 s. co  m
 * @param renderFlags the image flags
 * @return returns the image descriptor
 */
public ImageDescriptor getBaseImageDescriptor(IJavaElement element, int renderFlags) {

    try {
        switch (element.getElementType()) {
        case IJavaElement.INITIALIZER:
            return JavaPluginImages.DESC_MISC_PRIVATE; // 23479
        case IJavaElement.METHOD: {
            IMethod method = (IMethod) element;
            IType declType = method.getDeclaringType();
            int flags = method.getFlags();
            if (declType.isEnum() && isDefaultFlag(flags) && method.isConstructor())
                return JavaPluginImages.DESC_MISC_PRIVATE;
            return getMethodImageDescriptor(JavaModelUtil.isInterfaceOrAnnotation(declType), flags);
        }
        case IJavaElement.FIELD: {
            IMember member = (IMember) element;
            IType declType = member.getDeclaringType();
            return getFieldImageDescriptor(JavaModelUtil.isInterfaceOrAnnotation(declType), member.getFlags());
        }
        case IJavaElement.LOCAL_VARIABLE:
            return JavaPluginImages.DESC_OBJS_LOCAL_VARIABLE;

        case IJavaElement.PACKAGE_DECLARATION:
            return JavaPluginImages.DESC_OBJS_PACKDECL;

        case IJavaElement.IMPORT_DECLARATION:
            return JavaPluginImages.DESC_OBJS_IMPDECL;

        case IJavaElement.IMPORT_CONTAINER:
            return JavaPluginImages.DESC_OBJS_IMPCONT;

        case IJavaElement.TYPE: {
            IType type = (IType) element;

            IType declType = type.getDeclaringType();
            boolean isInner = declType != null;
            boolean isInInterfaceOrAnnotation = isInner && JavaModelUtil.isInterfaceOrAnnotation(declType);
            return getTypeImageDescriptor(isInner, isInInterfaceOrAnnotation, type.getFlags(),
                    useLightIcons(renderFlags));
        }

        case IJavaElement.PACKAGE_FRAGMENT_ROOT: {
            IPackageFragmentRoot root = (IPackageFragmentRoot) element;
            IPath attach = root.getSourceAttachmentPath();
            if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
                if (root.isArchive()) {
                    if (root.isExternal()) {
                        if (attach == null) {
                            return JavaPluginImages.DESC_OBJS_EXTJAR;
                        } else {
                            return JavaPluginImages.DESC_OBJS_EXTJAR_WSRC;
                        }
                    } else {
                        if (attach == null) {
                            return JavaPluginImages.DESC_OBJS_JAR;
                        } else {
                            return JavaPluginImages.DESC_OBJS_JAR_WSRC;
                        }
                    }
                } else {
                    if (attach == null) {
                        return JavaPluginImages.DESC_OBJS_CLASSFOLDER;
                    } else {
                        return JavaPluginImages.DESC_OBJS_CLASSFOLDER_WSRC;
                    }
                }
            } else {
                return JavaPluginImages.DESC_OBJS_PACKFRAG_ROOT;
            }
        }

        case IJavaElement.PACKAGE_FRAGMENT:
            return getPackageFragmentIcon(element);

        case IJavaElement.COMPILATION_UNIT:
            return JavaPluginImages.DESC_OBJS_CUNIT;

        case IJavaElement.CLASS_FILE:
            /* this is too expensive for large packages
            try {
               IClassFile cfile= (IClassFile)element;
               if (cfile.isClass())
             return JavaPluginImages.IMG_OBJS_CFILECLASS;
               return JavaPluginImages.IMG_OBJS_CFILEINT;
            } catch(JavaModelException e) {
               // fall through;
            }*/
            return JavaPluginImages.DESC_OBJS_CFILE;

        case IJavaElement.JAVA_PROJECT:
            IJavaProject jp = (IJavaProject) element;
            if (jp.getProject().isOpen()) {
                IProject project = jp.getProject();
                IWorkbenchAdapter adapter = (IWorkbenchAdapter) project.getAdapter(IWorkbenchAdapter.class);
                if (adapter != null) {
                    ImageDescriptor result = adapter.getImageDescriptor(project);
                    if (result != null)
                        return result;
                }
                return DESC_OBJ_PROJECT;
            }
            return DESC_OBJ_PROJECT_CLOSED;

        case IJavaElement.JAVA_MODEL:
            return JavaPluginImages.DESC_OBJS_JAVA_MODEL;

        case IJavaElement.TYPE_PARAMETER:
            return JavaPluginImages.DESC_OBJS_TYPEVARIABLE;

        case IJavaElement.ANNOTATION:
            return JavaPluginImages.DESC_OBJS_ANNOTATION;

        default:
            // ignore. Must be a new, yet unknown Java element
            // give an advanced IWorkbenchAdapter the chance
            IWorkbenchAdapter wbAdapter = (IWorkbenchAdapter) element.getAdapter(IWorkbenchAdapter.class);
            if (wbAdapter != null && !(wbAdapter instanceof JavaWorkbenchAdapter)) { // avoid recursion
                ImageDescriptor imageDescriptor = wbAdapter.getImageDescriptor(element);
                if (imageDescriptor != null) {
                    return imageDescriptor;
                }
            }
            return JavaPluginImages.DESC_OBJS_GHOST;
        }

    } catch (JavaModelException e) {
        if (e.isDoesNotExist())
            return JavaPluginImages.DESC_OBJS_UNKNOWN;
        JavaPlugin.log(e);
        return JavaPluginImages.DESC_OBJS_GHOST;
    }
}

From source file:com.siteview.mde.internal.launching.sourcelookup.PDESourceLookupDirector.java

License:Open Source License

private ISourceContainer getArchiveSourceContainer(String location) throws JavaModelException {
    IWorkspaceRoot root = PDELaunchingPlugin.getWorkspace().getRoot();
    IFile[] containers = root.findFilesForLocationURI(URIUtil.toURI(location));
    for (int i = 0; i < containers.length; i++) {
        IJavaElement element = JavaCore.create(containers[i]);
        if (element instanceof IPackageFragmentRoot) {
            IPackageFragmentRoot archive = (IPackageFragmentRoot) element;
            IPath path = archive.getSourceAttachmentPath();
            if (path == null || path.segmentCount() == 0)
                continue;

            IPath rootPath = archive.getSourceAttachmentRootPath();
            boolean detectRootPath = rootPath != null && rootPath.segmentCount() > 0;

            IFile archiveFile = root.getFile(path);
            if (archiveFile.exists())
                return new ArchiveSourceContainer(archiveFile, detectRootPath);

            File file = path.toFile();
            if (file.exists())
                return new ExternalArchiveSourceContainer(file.getAbsolutePath(), detectRootPath);
        }//  www  .j a  v a 2s. c om
    }
    return null;
}

From source file:com.siteview.mde.internal.ui.views.plugins.PluginsLabelProvider.java

License:Open Source License

public Image getImage(Object obj) {
    if (obj instanceof IMonitorModelBase) {
        return getImage((IMonitorModelBase) obj);
    }/*from   w  w  w.  j  ava 2s. c o m*/

    if (obj instanceof FileAdapter) {
        return getImage((FileAdapter) obj);
    }

    if (obj instanceof IPackageFragmentRoot) {
        IPackageFragmentRoot root = (IPackageFragmentRoot) obj;
        boolean hasSource = false;

        try {
            hasSource = root.getSourceAttachmentPath() != null;
        } catch (JavaModelException e) {
        }
        return JavaUI.getSharedImages()
                .getImage(hasSource ? org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_EXTERNAL_ARCHIVE_WITH_SOURCE
                        : org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_EXTERNAL_ARCHIVE);
    }

    if (obj instanceof IPackageFragment) {
        return JavaUI.getSharedImages().getImage(org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_PACKAGE);
    }

    if (obj instanceof ICompilationUnit) {
        return JavaUI.getSharedImages().getImage(org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_CUNIT);
    }

    if (obj instanceof IClassFile) {
        return JavaUI.getSharedImages().getImage(org.eclipse.jdt.ui.ISharedImages.IMG_OBJS_CFILE);
    }

    if (obj instanceof IStorage) {
        if (obj instanceof IJarEntryResource && !((IJarEntryResource) obj).isFile())
            return folderImage;
        return getFileImage(((IStorage) obj).getName());
    }

    return null;
}

From source file:copied.org.eclipse.pde.internal.launching.sourcelookup.PDESourceLookupParticipant.java

License:Open Source License

private ISourceContainer getArchiveSourceContainer(String location) throws JavaModelException {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IFile[] containers = root.findFilesForLocationURI(URIUtil.toURI(location));
    for (int i = 0; i < containers.length; i++) {
        IJavaElement element = JavaCore.create(containers[i]);
        if (element instanceof IPackageFragmentRoot) {
            IPackageFragmentRoot archive = (IPackageFragmentRoot) element;
            IPath path = archive.getSourceAttachmentPath();
            if (path == null || path.segmentCount() == 0)
                continue;

            IPath rootPath = archive.getSourceAttachmentRootPath();
            boolean detectRootPath = rootPath != null && rootPath.segmentCount() > 0;

            IFile archiveFile = root.getFile(path);
            if (archiveFile.exists())
                return new ArchiveSourceContainer(archiveFile, detectRootPath);

            File file = path.toFile();
            if (file.exists())
                return new ExternalArchiveSourceContainer(file.getAbsolutePath(), detectRootPath);
        }// www  .  java2  s  . co  m
    }
    return null;
}