Example usage for org.eclipse.jdt.core IPackageFragment getAttachedJavadoc

List of usage examples for org.eclipse.jdt.core IPackageFragment getAttachedJavadoc

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IPackageFragment getAttachedJavadoc.

Prototype

String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException;

Source Link

Document

Returns the Javadoc as HTML source if this element has attached Javadoc, null otherwise.

This should be used only for binary elements.

Usage

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

License:Open Source License

/**
 * Returns the Javadoc for a package which could be present in package.html, package-info.java
 * or from an attached Javadoc./*  www  .j a va  2 s .c  o  m*/
 *
 * @param packageFragment the package which is requesting for the document
 * @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) 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 {
                    element = packageInfo.getParent(); // parent is the IPackageFragment
                }
                return new JavadocContentAccess2(element, packageJavadocNode, cuSource).toHTML();
            }
        }
    }

    // 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
    else {
        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);
            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;
}

From source file:fr.obeo.ariadne.ide.connector.java.internal.explorer.JavaExplorer.java

License:Open Source License

/**
 * Explores the given package located in the given classpath entry of the currently analyzed Java project
 * in order to extract Ariadne concept./*from w w w.  j  a v a  2 s.com*/
 * 
 * @param classpathEntry
 *            The classpath entry
 * @param iPackageFragment
 *            The package
 * @param monitor
 *            The progress monitor
 */
private void explorePackage(ClasspathEntry classpathEntry, IPackageFragment iPackageFragment,
        IProgressMonitor monitor) {
    try {
        // Find or create matching types container
        TypesContainer typesContainer = null;
        List<TypesContainer> typesContainers = classpathEntry.getTypesContainers();
        for (TypesContainer aTypesContainer : typesContainers) {
            if (aTypesContainer.getName().equals(iPackageFragment.getElementName())) {
                typesContainer = aTypesContainer;
                break;
            }
        }

        if (typesContainer == null) {
            typesContainer = CodeFactory.eINSTANCE.createTypesContainer();
            typesContainer.setName(iPackageFragment.getElementName());
            classpathEntry.getTypesContainers().add(typesContainer);
        }

        String attachedJavadoc = iPackageFragment.getAttachedJavadoc(monitor);
        typesContainer.setDescription(attachedJavadoc);

        if (iPackageFragment.getKind() == IPackageFragmentRoot.K_BINARY) {
            IClassFile[] classFiles = iPackageFragment.getClassFiles();
            for (IClassFile iClassFile : classFiles) {
                this.exploreType(iClassFile.getType(), typesContainer, monitor);
            }
        } else if (iPackageFragment.getKind() == IPackageFragmentRoot.K_SOURCE) {
            ICompilationUnit[] compilationUnits = iPackageFragment.getCompilationUnits();
            for (ICompilationUnit iCompilationUnit : compilationUnits) {
                IType[] allTypes = iCompilationUnit.getAllTypes();
                for (IType iType : allTypes) {
                    Type type = this.exploreType(iType, typesContainer, monitor);

                    IResource resource = iType.getResource();
                    IProject project = resource.getProject();

                    if (RepositoryProvider.isShared(project)) {
                        RepositoryProvider provider = RepositoryProvider.getProvider(project);
                        IFileHistoryProvider fileHistoryProvider = provider.getFileHistoryProvider();
                        IFileHistory fileHistory = fileHistoryProvider.getFileHistoryFor(resource,
                                IFileHistoryProvider.NONE, monitor);
                        IFileRevision[] fileRevisions = fileHistory.getFileRevisions();
                        for (IFileRevision iFileRevision : fileRevisions) {
                            String identifier = iFileRevision.getContentIdentifier();

                            List<Repository> repositories = this.ariadneProject.getRepositories();
                            for (Repository repository : repositories) {
                                List<Commit> commits = repository.getCommits();
                                for (Commit commit : commits) {
                                    if (commit.getId().equals(identifier)) {
                                        // Link the commit to the type created
                                        List<ResourceChange> resourceChanges = commit.getResourceChanges();
                                        for (ResourceChange resourceChange : resourceChanges) {
                                            if (resourceChange.getPath()
                                                    .equals(iFileRevision.getURI().toString())
                                                    || resourceChange.getPath()
                                                            .endsWith(resource.getFullPath().toString())) {
                                                resourceChange.setVersionedElement(type);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
}

From source file:org.eclipse.che.jdt.javadoc.JavadocContentAccess2.java

License:Open Source License

/**
 * Returns the Javadoc for a package which could be present in package.html, package-info.java
 * or from an attached Javadoc./*from  w  w w  . j  av  a 2s.  c  om*/
 *
 * @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 {
                    element = packageInfo.getParent(); // parent is the IPackageFragment
                }
                return new JavadocContentAccess2(element, packageJavadocNode, cuSource, urlPrefix).toHTML();
            }
        }
    }

    // 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
    else {
        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;
}