Example usage for org.eclipse.jdt.core ITypeRoot getParent

List of usage examples for org.eclipse.jdt.core ITypeRoot getParent

Introduction

In this page you can find the example usage for org.eclipse.jdt.core ITypeRoot getParent.

Prototype

IJavaElement getParent();

Source Link

Document

Returns the element directly containing this element, or null if this element has no parent.

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./*  w  ww  .  j  a  va 2s  . c  om*/
 *
 * @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: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 ww w  . j av a 2s  .com*/
 *
 * @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;
}

From source file:org.eclipse.recommenders.internal.apidocs.rcp.JavadocProvider.java

License:Open Source License

@JavaSelectionSubscriber
public void onCompilationUnitSelection(final ITypeRoot root, final JavaElementSelectionEvent selection,
        final Composite parent) throws CoreException {
    final IType type = root.findPrimaryType();
    if (type != null) {
        render(type, parent);//from  w  w w.ja v  a  2 s.  com
    } else if (root.getParent() instanceof IPackageFragment) {
        IPackageFragment packageFragment = (IPackageFragment) root.getParent();
        render(packageFragment, parent);
    }
}

From source file:org.eclipse.xtext.common.types.ui.trace.TraceForTypeRootProvider.java

License:Open Source License

protected SourceRelativeURI getPathInFragmentRoot(final ITypeRoot derivedResource, String simpleFileName) {
    if (simpleFileName == null)
        return null;
    return new SourceRelativeURI(URI
            .createURI(derivedResource.getParent().getElementName().replace('.', '/') + "/" + simpleFileName));
}

From source file:org.eclipse.xtext.common.types.ui.trace.TraceForTypeRootProvider.java

License:Open Source License

protected IPath getSourcePath(final ITypeRoot derivedJavaType) {
    IJavaElement current = derivedJavaType.getParent();
    while (current != null) {
        if (current instanceof IPackageFragmentRoot) {
            IPackageFragmentRoot fragmentRoot = (IPackageFragmentRoot) current;
            try {
                IPath attachmentPath = fragmentRoot.getSourceAttachmentPath();
                if (attachmentPath != null)
                    return attachmentPath;
            } catch (JavaModelException e) {
            }//from ww  w .j  a va2 s.c om
            if (current instanceof JarPackageFragmentRoot)
                return fragmentRoot.getPath();

        }
        current = current.getParent();
    }
    return null;
}

From source file:org.eclipse.xtext.common.types.ui.trace.TraceForTypeRootProvider.java

License:Open Source License

protected Charset getSourceEncoding(final ITypeRoot derivedJavaType) {
    // this should be symmetric to org.eclipse.jdt.internal.core.SourceMapper.findSource(String) 
    try {//from w ww  .j a v  a2  s  . c om
        IJavaElement current = derivedJavaType.getParent();
        while (current != null) {
            if (current instanceof IPackageFragmentRoot) {
                IPackageFragmentRoot root = (IPackageFragmentRoot) current;
                try {
                    // see org.eclipse.jdt.internal.core.ClasspathEntry.getSourceAttachmentEncoding()
                    IClasspathAttribute[] attributes = root.getResolvedClasspathEntry().getExtraAttributes();
                    for (int i = 0, length = attributes.length; i < length; i++) {
                        IClasspathAttribute attribute = attributes[i];
                        if (SOURCE_ATTACHMENT_ENCODING.equals(attribute.getName()))
                            return Charset.forName(attribute.getValue());
                    }
                } catch (JavaModelException e) {
                }
                return Charset.forName(ResourcesPlugin.getWorkspace().getRoot().getDefaultCharset());
            }
            current = current.getParent();
        }
        return Charset.forName(ResourcesPlugin.getWorkspace().getRoot().getDefaultCharset());
    } catch (CoreException e) {
        log.error("Error determining encoding for source file for " + derivedJavaType.getElementName(), e);
        return Charsets.UTF_8;
    }
}

From source file:org.summer.dsl.builder.trace.TraceForTypeRootProvider.java

License:Open Source License

protected String getPathInFragmentRoot(final ITypeRoot derivedResource) {
    return derivedResource.getParent().getElementName().replace('.', '/') + "/";
}