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

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

Introduction

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

Prototype

IPath getSourceAttachmentRootPath() throws JavaModelException;

Source Link

Document

Returns the path within this package fragment root's source 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  ww w  . j  a  va 2  s  .  co 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: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;/*from   www .  ja v a  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.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  ww. java2s. co m
        IPath path = root.getSourceAttachmentPath();
        if (path != null) {
            path = EclEmmaCorePlugin.getAbsolutePath(path);
            return new SourceLocation(path, root.getSourceAttachmentRootPath());
        } else {
            return null;
        }
    }
}

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);
        }/*from w ww  . ja v  a 2s . c  o m*/
    }
    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);
        }/*  w w  w.  j a v a 2  s . co  m*/
    }
    return null;
}