Example usage for org.eclipse.jdt.core IJarEntryResource isFile

List of usage examples for org.eclipse.jdt.core IJarEntryResource isFile

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJarEntryResource isFile.

Prototype

boolean isFile();

Source Link

Document

Returns true if this jar entry represents a file.

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./*from   ww  w  .ja v  a 2  s .  co 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:ca.ecliptical.pde.ds.search.DescriptorQueryParticipant.java

License:Open Source License

private IJarEntryResource[] findMatchingJarEntries(IJarEntryResource resource, Filter filter) {
    ArrayList<IJarEntryResource> results = new ArrayList<IJarEntryResource>();
    for (IJarEntryResource child : resource.getChildren()) {
        if (child.isFile() && filter.matches(Collections.singletonMap("filename", child.getName()))) { //$NON-NLS-1$
            results.add(child);// w  ww.j a v  a2  s .  co m
        }
    }

    return results.toArray(new IJarEntryResource[results.size()]);
}

From source file:ca.ecliptical.pde.ds.search.DescriptorQueryParticipant.java

License:Open Source License

private IJarEntryResource findJarEntry(IJarEntryResource resource, IPath path) {
    if (!resource.getName().equals(path.segment(0)))
        return null;

    if (path.segmentCount() == 1)
        return resource;

    if (resource.isFile())
        return null;

    for (IJarEntryResource child : resource.getChildren()) {
        IJarEntryResource result = findJarEntry(child, path.removeFirstSegments(1));
        if (result != null)
            return result;
    }//from  ww  w .jav  a 2 s. c  o m

    return null;
}

From source file:com.amashchenko.eclipse.strutsclipse.ProjectUtil.java

License:Apache License

public static List<JarEntryStorage> findJarEntryStrutsResources(final IDocument document) {
    List<JarEntryStorage> results = new ArrayList<JarEntryStorage>();
    try {/*from   w  w  w .  ja v  a  2  s . co m*/
        IJavaProject javaProject = getCurrentJavaProject(document);

        if (javaProject != null && javaProject.exists()) {
            IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
            for (IPackageFragmentRoot root : roots) {
                if (root.isArchive()) {
                    Object[] nonJavaResources = root.getNonJavaResources();
                    for (Object nonJavaRes : nonJavaResources) {
                        if (nonJavaRes instanceof IJarEntryResource) {
                            IJarEntryResource jarEntry = (IJarEntryResource) nonJavaRes;
                            if (jarEntry.isFile() && (StrutsXmlConstants.STRUTS_DEFAULT_FILE_NAME
                                    .equals(jarEntry.getName())
                                    || StrutsXmlConstants.STRUTS_PLUGIN_FILE_NAME.equals(jarEntry.getName()))) {
                                results.add(new JarEntryStorage(root.getPath(), jarEntry));
                            }
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
    return results;
}

From source file:com.codenvy.ide.ext.java.server.JavaNavigation.java

License:Open Source License

private JarEntryFile findJarFile(JarEntryDirectory directory, String path) {
    for (IJarEntryResource children : directory.getChildren()) {
        if (children.isFile() && children.getFullPath().toOSString().equals(path)) {
            return (JarEntryFile) children;
        }// w  ww  .j  a  v a  2s .  co m
        if (!children.isFile()) {
            JarEntryFile file = findJarFile((JarEntryDirectory) children, path);
            if (file != null) {
                return file;
            }
        }
    }
    return null;
}

From source file:com.google.gdt.eclipse.core.java.ClasspathResourceUtilities.java

License:Open Source License

/**
 * Returns the given file or JAR entry if it is on the given package fragment.
 * The file can be a Java file, class file, or a non-Java resource.
 * <p>/*from  www  . j a va2s  .c  om*/
 * This method returns null for .java files or .class files inside JARs.
 * 
 * @param fileName the file name
 * @param pckgFragment the package fragment to search
 * @return the file as an IResource or IJarEntryResource, or null
 * @throws JavaModelException
 */
public static IStorage resolveFileOnPackageFragment(String fileName, IPackageFragment pckgFragment)
        throws JavaModelException {

    boolean isJavaFile = JavaCore.isJavaLikeFileName(fileName);
    boolean isClassFile = ResourceUtils.endsWith(fileName, ".class");

    // Check the non-Java resources first
    Object[] nonJavaResources = pckgFragment.getNonJavaResources();
    for (Object nonJavaResource : nonJavaResources) {
        if (nonJavaResource instanceof IFile) {
            IFile file = (IFile) nonJavaResource;
            String resFileName = file.getName();

            if (ResourceUtils.areFilenamesEqual(resFileName, fileName)) {
                // Java source files that have been excluded from the build path
                // show up as non-Java resources, but we'll ignore them since
                // they're not available on the classpath.
                if (!JavaCore.isJavaLikeFileName(resFileName)) {
                    return file;
                } else {
                    return null;
                }
            }
        }

        // JAR resources are not IResource's, so we need to handle them
        // differently
        if (nonJavaResource instanceof IJarEntryResource) {
            IJarEntryResource jarEntry = (IJarEntryResource) nonJavaResource;
            if (jarEntry.isFile() && ResourceUtils.areFilenamesEqual(jarEntry.getName(), fileName)) {
                return jarEntry;
            }
        }
    }

    // If we're looking for a .java or .class file, we can use the regular
    // Java Model methods.

    if (isJavaFile) {
        ICompilationUnit cu = pckgFragment.getCompilationUnit(fileName);
        if (cu.exists()) {
            return (IFile) cu.getCorrespondingResource();
        }
    }

    if (isClassFile) {
        IClassFile cf = pckgFragment.getClassFile(fileName);
        if (cf.exists()) {
            return (IFile) cf.getCorrespondingResource();
        }
    }

    return null;
}

From source file:com.google.gwt.eclipse.core.modules.ModuleUtils.java

License:Open Source License

/**
 * Returns whether a JAR resource is a module XML.
 *
 * @param jarResource the JAR resource to check
 * @return <code>true</code> if the resource is a module XML, and <code>false</code> otherwise
 *///from  w  w  w  .  ja  v a 2s  .c  o m
public static boolean isModuleXml(IJarEntryResource jarResource) {
    return (jarResource.isFile() && jarResource.getName().endsWith(FILE_EXTENSION));
}

From source file:it.unibz.instasearch.indexing.WorkspaceIndexerJDT.java

License:Open Source License

/**
 * @param indexWriter/*from w  w  w .  j ava  2s  .  c o  m*/
 * @param jar 
 * @param resources
 * @param monitor
 * @throws IOException 
 * @throws CoreException 
 */
private void indexNonJavaResources(IndexWriter indexWriter, IPackageFragmentRoot jar,
        IJarEntryResource[] resources, IProgressMonitor monitor) throws Exception {

    String jarName = getJarName(jar);
    String projectPath = getProjectPath(jar);

    for (IJarEntryResource resource : resources) {
        if (monitor.isCanceled())
            return;

        if (resource.isFile()) {
            if (isIndexable(resource))
                indexStorageWithRetry(indexWriter, resource, projectPath, IResource.NULL_STAMP, jarName);
        } else {
            indexNonJavaResources(indexWriter, jar, resource.getChildren(), monitor);
        }
    }

}

From source file:nz.net.ultraq.eclipse.thymeleaf.ThymeleafPlugin.java

License:Apache License

/**
 * <p>/*  w  w  w .  ja  v a  2 s .c o m*/
 * Scan the current project's dependencies looking for XML file (ending with
 * "Dialect.xml").
 * <p>
 * The return list contains JarEntryResource that will be read by the
 * ProcessorCache.
 * 
 * @return List<IJarEntryResource> A list of XML files corresponding to the
 *         definitions of Thymeleaf dialects that exist in the current's
 *         project dependencies.
 * @throws JavaModelException
 * @throws CoreException
 */
private List<IJarEntryResource> getDialectDefinitions() throws JavaModelException, CoreException {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

    List<IJarEntryResource> resources = new ArrayList<IJarEntryResource>();

    IProject[] projects = root.getProjects();

    // Each project of the workspace is processed
    for (IProject project : projects) {

        System.out.println("project name: " + project.getName());

        // Only Java project are scanned
        // TODO Is that filter really necessary ?
        if (project.isNatureEnabled("org.eclipse.jdt.core.javanature")) {

            IJavaProject javaProject = JavaCore.create(project);
            IPackageFragment[] packages = javaProject.getPackageFragments();

            for (IPackageFragment packageFragment : packages) {

                // No need to iterated over java classes, just resources
                for (Object ob : packageFragment.getNonJavaResources()) {

                    IJarEntryResource jarEntry = (IJarEntryResource) ob;

                    // Resources are filtered by their suffix
                    if (jarEntry.isFile() && jarEntry.getName().endsWith("Dialect.xml")) {
                        resources.add(jarEntry);
                    }
                }
            }
        }
    }

    return resources;
}

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.ja  va 2s .c o  m*/
 *
 * @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;
}