Search in sources :

Example 1 with IJarEntryResource

use of org.eclipse.jdt.core.IJarEntryResource in project che by eclipse.

the class JavadocContentAccess2 method getHTMLContent.

/**
     * Returns the Javadoc for a package which could be present in package.html, package-info.java
     * or from an attached Javadoc.
     *
     * @param packageFragment the package which is requesting for the document
     * @param urlPrefix
     * @return the document content in HTML format or <code>null</code> if there is no associated
     *         Javadoc
     * @throws CoreException if the Java element does not exists or an exception occurs while
     *             accessing the file containing the package Javadoc
     * @since 3.9
     */
public static String getHTMLContent(IPackageFragment packageFragment, String urlPrefix) throws CoreException {
    IPackageFragmentRoot root = (IPackageFragmentRoot) packageFragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    //1==> Handle the case when the documentation is present in package-info.java or package-info.class file
    ITypeRoot packageInfo;
    boolean isBinary = root.getKind() == IPackageFragmentRoot.K_BINARY;
    if (isBinary) {
        packageInfo = packageFragment.getClassFile(JavaModelUtil.PACKAGE_INFO_CLASS);
    } else {
        packageInfo = packageFragment.getCompilationUnit(JavaModelUtil.PACKAGE_INFO_JAVA);
    }
    if (packageInfo != null && packageInfo.exists()) {
        String cuSource = packageInfo.getSource();
        //the source can be null for some of the class files
        if (cuSource != null) {
            Javadoc packageJavadocNode = getPackageJavadocNode(packageFragment, cuSource);
            if (packageJavadocNode != null) {
                IJavaElement element;
                if (isBinary) {
                    element = ((IClassFile) packageInfo).getType();
                } else {
                    // parent is the IPackageFragment
                    element = packageInfo.getParent();
                }
                return new JavadocContentAccess2(element, packageJavadocNode, cuSource, urlPrefix).toHTML();
            }
        }
    } else // 2==> Handle the case when the documentation is done in package.html file. The file can be either in normal source folder or
    // coming from a jar file
    {
        Object[] nonJavaResources = packageFragment.getNonJavaResources();
        // 2.1 ==>If the package.html file is present in the source or directly in the binary jar
        for (Object nonJavaResource : nonJavaResources) {
            if (nonJavaResource instanceof IFile) {
                IFile iFile = (IFile) nonJavaResource;
                if (iFile.exists() && JavaModelUtil.PACKAGE_HTML.equals(iFile.getName())) {
                    return getIFileContent(iFile);
                }
            }
        }
        // 2.2==>The file is present in a binary container
        if (isBinary) {
            for (Object nonJavaResource : nonJavaResources) {
                // The content is from an external binary class folder
                if (nonJavaResource instanceof IJarEntryResource) {
                    IJarEntryResource jarEntryResource = (IJarEntryResource) nonJavaResource;
                    String encoding = getSourceAttachmentEncoding(root);
                    if (JavaModelUtil.PACKAGE_HTML.equals(jarEntryResource.getName()) && jarEntryResource.isFile()) {
                        return getHTMLContent(jarEntryResource, encoding);
                    }
                }
            }
            //2.3 ==>The file is present in the source attachment path.
            String contents = getHTMLContentFromAttachedSource(root, packageFragment, urlPrefix);
            if (contents != null)
                return contents;
        }
    }
    //3==> Handle the case when the documentation is coming from the attached Javadoc
    if ((root.isArchive() || root.isExternal())) {
        return packageFragment.getAttachedJavadoc(null);
    }
    return null;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IFile(org.eclipse.core.resources.IFile) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) Javadoc(org.eclipse.jdt.core.dom.Javadoc) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 2 with IJarEntryResource

use of org.eclipse.jdt.core.IJarEntryResource in project che by eclipse.

the class StandardJavaElementContentProvider method internalGetParent.

/**
     * Note: This method is for internal use only. Clients should not call this method.
     *
     * @param element the element
     * @return the parent of the element
     *
     * @noreference This method is not intended to be referenced by clients.
     */
protected Object internalGetParent(Object element) {
    // try to map resources to the containing package fragment
    if (element instanceof IResource) {
        IResource parent = ((IResource) element).getParent();
        IJavaElement jParent = JavaCore.create(parent);
        // http://bugs.eclipse.org/bugs/show_bug.cgi?id=31374
        if (jParent != null && jParent.exists())
            return jParent;
        return parent;
    } else if (element instanceof IJavaElement) {
        IJavaElement parent = ((IJavaElement) element).getParent();
        // we have to skip the package fragment root as the parent.
        if (element instanceof IPackageFragment) {
            return skipProjectPackageFragmentRoot((IPackageFragmentRoot) parent);
        }
        return parent;
    } else if (element instanceof IJarEntryResource) {
        return ((IJarEntryResource) element).getParent();
    }
    return null;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource) IResource(org.eclipse.core.resources.IResource) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 3 with IJarEntryResource

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

the class XbaseBreakpointUtil method getBreakpointURI.

// this URI is only used for breakpoints on JARed files
public SourceRelativeURI getBreakpointURI(IEditorInput input) {
    Object adapter = input.getAdapter(IResource.class);
    if (adapter != null)
        return null;
    if (input instanceof IStorageEditorInput) {
        IStorage storage;
        try {
            storage = ((IStorageEditorInput) input).getStorage();
            if (storage instanceof IResource)
                return null;
            if (storage instanceof IJarEntryResource) {
                IJarEntryResource jarEntryResource = (IJarEntryResource) storage;
                if (!jarEntryResource.getPackageFragmentRoot().isArchive())
                    return null;
                Object parent = jarEntryResource.getParent();
                if (parent instanceof IPackageFragment) {
                    String path = ((IPackageFragment) parent).getElementName().replace('.', '/');
                    return new SourceRelativeURI(path + "/" + storage.getName());
                } else if (parent instanceof IPackageFragmentRoot) {
                    return new SourceRelativeURI(storage.getName());
                }
            }
        } catch (CoreException e) {
            logger.error("Error finding breakpoint URI", e);
            return null;
        }
    } else if (input.getAdapter(IClassFile.class) != null) {
        IClassFile classFile = (IClassFile) input.getAdapter(IClassFile.class);
        ITrace traceToSource = traceForTypeRootProvider.getTraceToSource(classFile);
        if (traceToSource == null)
            return null;
        for (ILocationInResource loc : traceToSource.getAllAssociatedLocations()) return loc.getSrcRelativeResourceURI();
        return null;
    }
    return null;
}
Also used : IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IClassFile(org.eclipse.jdt.core.IClassFile) IStorage(org.eclipse.core.resources.IStorage) ILocationInResource(org.eclipse.xtext.generator.trace.ILocationInResource) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) CoreException(org.eclipse.core.runtime.CoreException) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource) ITrace(org.eclipse.xtext.generator.trace.ITrace) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) IResource(org.eclipse.core.resources.IResource)

Example 4 with IJarEntryResource

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

the class JavaProjectsStateHelper method getJarWithEntry.

protected IPackageFragmentRoot getJarWithEntry(URI uri) {
    Iterable<Pair<IStorage, IProject>> storages = getStorages(uri);
    IPackageFragmentRoot result = null;
    for (Pair<IStorage, IProject> storage2Project : storages) {
        IStorage storage = storage2Project.getFirst();
        if (storage instanceof IJarEntryResource) {
            IPackageFragmentRoot fragmentRoot = ((IJarEntryResource) storage).getPackageFragmentRoot();
            if (fragmentRoot != null) {
                // IPackageFragmentRoot has some unexpected caching - it may return a different project
                // thus we use the one that was used to record the IPackageFragmentRoot
                IProject actualProject = storage2Project.getSecond();
                IJavaProject javaProject = JavaCore.create(actualProject);
                if (!javaProject.exists()) {
                    javaProject = fragmentRoot.getJavaProject();
                }
                if (isAccessibleXtextProject(javaProject.getProject())) {
                    // if both projects are the same - fine
                    if (javaProject.equals(fragmentRoot.getJavaProject()))
                        return fragmentRoot;
                    // otherwise re-obtain the fragment root from the real project
                    if (fragmentRoot.isExternal()) {
                        IPackageFragmentRoot actualRoot = javaProject.getPackageFragmentRoot(fragmentRoot.getPath().toString());
                        if (actualProject.exists()) {
                            return actualRoot;
                        }
                    } else {
                        IPackageFragmentRoot actualRoot = javaProject.getPackageFragmentRoot(fragmentRoot.getResource());
                        if (actualRoot.exists()) {
                            return actualRoot;
                        }
                    }
                    result = fragmentRoot;
                }
                if (result == null)
                    result = fragmentRoot;
            }
        }
    }
    return result;
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource) IStorage(org.eclipse.core.resources.IStorage) IProject(org.eclipse.core.resources.IProject) Pair(org.eclipse.xtext.util.Pair) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 5 with IJarEntryResource

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

the class Storage2UriMapperJavaImpl method initializeData.

/**
 * @since 2.4
 */
protected PackageFragmentRootData initializeData(final IPackageFragmentRoot root) {
    final PackageFragmentRootData data = new PackageFragmentRootData(computeModificationStamp(root));
    data.addRoot(root);
    try {
        final SourceAttachmentPackageFragmentRootWalker<Void> walker = new SourceAttachmentPackageFragmentRootWalker<Void>() {

            @Override
            protected URI getURI(IFile file, org.eclipse.xtext.ui.resource.PackageFragmentRootWalker.TraversalState state) {
                if (!uriValidator.isPossiblyManaged(file))
                    return null;
                return super.getURI(file, state);
            }

            @Override
            protected URI getURI(IJarEntryResource jarEntry, org.eclipse.xtext.ui.resource.PackageFragmentRootWalker.TraversalState state) {
                if (!uriValidator.isPossiblyManaged(jarEntry))
                    return null;
                final URI uri = locator.getURI(root, jarEntry, state);
                if (!uriValidator.isValid(uri, jarEntry))
                    return null;
                return uri;
            }

            @Override
            protected Void handle(URI uri, IStorage storage, org.eclipse.xtext.ui.resource.PackageFragmentRootWalker.TraversalState state) {
                data.uri2Storage.put(uri, storage);
                return null;
            }
        };
        walker.traverse(root, false);
        if (walker.getBundleSymbolicName() != null)
            data.uriPrefix = URI.createPlatformResourceURI(walker.getBundleSymbolicName() + "/", true);
    } catch (RuntimeException e) {
        log.error(e.getMessage(), e);
    } catch (JavaModelException e) {
        log.debug(e.getMessage(), e);
    }
    return data;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IStorage(org.eclipse.core.resources.IStorage) URI(org.eclipse.emf.common.util.URI) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource)

Aggregations

IJarEntryResource (org.eclipse.jdt.core.IJarEntryResource)13 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)7 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)7 URI (org.eclipse.emf.common.util.URI)5 IFile (org.eclipse.core.resources.IFile)4 IResource (org.eclipse.core.resources.IResource)4 IStorage (org.eclipse.core.resources.IStorage)4 IPath (org.eclipse.core.runtime.IPath)4 IJavaElement (org.eclipse.jdt.core.IJavaElement)4 Path (org.eclipse.core.runtime.Path)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 ForwardingMap (com.google.common.collect.ForwardingMap)1 HashSet (java.util.HashSet)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 CoreException (org.eclipse.core.runtime.CoreException)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 IClassFile (org.eclipse.jdt.core.IClassFile)1