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

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

Introduction

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

Prototype

Object[] getNonJavaResources() throws JavaModelException;

Source Link

Document

Returns an array of non-Java resources contained in this package fragment root.

Usage

From source file:org.springframework.ide.eclipse.beans.ui.properties.NonJavaResourceContentProvider.java

License:Open Source License

private Object[] getNonJavaResources(IPackageFragmentRoot root) throws JavaModelException {
    Object[] nonJavaResources = root.getNonJavaResources();

    // Replace JAR entries with our own wrapper
    if (root.getKind() == IPackageFragmentRoot.K_BINARY && root.getResource() instanceof IFile) {
        for (int i = 0; i < nonJavaResources.length; i++) {
            Object resource = nonJavaResources[i];
            if (resource instanceof IStorage) {
                IStorage storage = (IStorage) resource;
                nonJavaResources[i] = new ZipEntryStorage((IFile) root.getResource(),
                        storage.getFullPath().toString());
            }/* w w  w  . java2  s. c o m*/
        }
    }
    return nonJavaResources;
}

From source file:org.springframework.ide.eclipse.core.io.EclipsePathMatchingResourcePatternResolver.java

License:Open Source License

private Resource processRawResource(IPackageFragmentRoot[] roots, Resource resource)
        throws IOException, JavaModelException {
    if (resource instanceof FileSystemResource) {
        // This can only be something in the Eclipse workspace

        // first check the location in the project that this pattern resolver is associated with (most likely path)
        Path path = new Path(((FileSystemResource) resource).getPath());
        IPath projectLocation = this.project.getLocation();
        if (projectLocation.isPrefixOf(path)) {
            int segmentsToRemove = projectLocation.segmentCount();
            IPath projectRelativePath = path.removeFirstSegments(segmentsToRemove);
            IFile file = this.project.getFile(projectRelativePath);
            if (file != null && file.exists()) {
                return new FileResource(file);
            }/*from w w  w  .  jav  a 2  s  . c  o  m*/
        }

        // then check the simple getFileForLocation (faster in case it is not a linked resource)
        IFile fileForLocation = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
        if (fileForLocation != null) {
            return new FileResource(fileForLocation);
        }

        // fall back to full resolution via findFilesForLocationURI
        IResource[] allResourcesFor = ResourcesPlugin.getWorkspace().getRoot()
                .findFilesForLocationURI(resource.getURI());
        for (IResource res : allResourcesFor) {
            return new FileResource((IFile) res);
        }
    } else if (resource instanceof UrlResource) {
        URL url = resource.getURL();
        String path = url.getPath();
        int ix = path.indexOf('!');
        if (ix > 0) {
            String entryName = path.substring(ix + 1);
            path = path.substring(0, ix);

            try {
                return new ExternalFile(new File(new URI(path)), entryName, project);
            } catch (URISyntaxException e) {
            }
        } else {
            IResource[] allResourcesFor = ResourcesPlugin.getWorkspace().getRoot()
                    .findFilesForLocationURI(resource.getURI());
            for (IResource res : allResourcesFor) {
                return new FileResource((IFile) res);
            }
        }
    } else if (resource instanceof ClassPathResource) {
        String path = ((ClassPathResource) resource).getPath();
        String fileName = path;
        String packageName = "";
        int ix = path.lastIndexOf('/');
        if (ix > 0) {
            fileName = path.substring(ix + 1);
            packageName = path.substring(0, ix).replace('/', '.');
        }
        if (fileName.endsWith(ClassUtils.CLASS_FILE_SUFFIX)) {
            String typeName = packageName + "." + fileName.substring(0, fileName.length() - 6);
            for (IPackageFragmentRoot root : roots) {
                Resource storage = null;

                if ("".equals(packageName) && root.exists()) {
                    storage = processClassResource(path, fileName, typeName, root, root.getChildren());
                }

                IPackageFragment packageFragment = root.getPackageFragment(packageName);
                if (storage == null && packageFragment != null && packageFragment.exists()) {
                    storage = processClassResource(path, fileName, typeName, root,
                            packageFragment.getChildren());
                }

                if (storage != null) {
                    return storage;
                }
            }
        }

        else {
            for (IPackageFragmentRoot root : roots) {
                IStorage storage = null;

                // Look in the root of the package fragment root
                if ("".equals(packageName) && root.exists()) {
                    storage = contains(root.getNonJavaResources(), fileName);
                }

                // Check the package
                IPackageFragment packageFragment = root.getPackageFragment(packageName);
                if (storage == null && packageFragment != null && packageFragment.exists()) {
                    storage = contains(packageFragment.getNonJavaResources(), fileName);
                }

                // Check the root folder in case no package exists 
                if (storage == null) {
                    IResource rootResource = root.getUnderlyingResource();
                    if (rootResource instanceof IFolder) {
                        IFile file = ((IFolder) rootResource).getFile(path);
                        if (file.exists()) {
                            storage = file;
                        }
                    }
                }

                // Found the resource in the package fragment root? -> construct usable Resource
                if (storage != null) {
                    if (storage instanceof IFile) {
                        return new FileResource((IFile) storage);
                    } else if (storage instanceof IJarEntryResource) {

                        // Workspace jar or resource
                        if (root.getResource() != null) {
                            return new StorageResource(new ZipEntryStorage((IFile) root.getResource(),
                                    ((IJarEntryResource) storage).getFullPath().toString()), project);
                        }
                        // Workspace external jar
                        else {
                            File jarFile = root.getPath().toFile();
                            return new ExternalFile(jarFile,
                                    ((IJarEntryResource) storage).getFullPath().toString(), project);
                        }
                    }
                }
            }
        }
    }
    return null;
}

From source file:org.springframework.ide.eclipse.internal.uaa.monitor.LibraryUsageMonitor.java

License:Open Source License

private JarEntryDirectory getJarEntryDirectory(IPackageFragmentRoot entry) {
    try {//w  ww  .  j  av a 2 s .  c  o  m
        for (Object nonJavaResource : entry.getNonJavaResources()) {
            if (nonJavaResource instanceof JarEntryDirectory) {
                JarEntryDirectory directory = ((JarEntryDirectory) nonJavaResource);
                if (directory != null && META_INF_DIRECTORY_NAME.equals(directory.getName())) {
                    return directory;
                }
            }
        }
    } catch (JavaModelException e) {
    }
    return null;
}

From source file:org.springframework.ide.eclipse.ui.viewers.JavaFileSuffixFilter.java

License:Open Source License

@Override
public boolean select(Viewer viewer, Object parent, Object element) {
    if (element instanceof IPackageFragmentRoot) {
        IPackageFragmentRoot root = (IPackageFragmentRoot) element;
        try {/*from  w ww.j  a  v a2  s  .  co  m*/
            for (IJavaElement child : root.getChildren()) {
                // recursive! Only show containers that contain a configs
                if (select(viewer, parent, child)) {
                    return true;
                }
            }
            for (Object nonJavaResource : root.getNonJavaResources()) {
                // recursive! Only show containers that contain a configs
                if (select(viewer, parent, nonJavaResource)) {
                    return true;
                }
            }
        } catch (JavaModelException e) {
            SpringUIPlugin.log(e.getStatus());
        }
        return false;
    } else if (element instanceof IPackageFragment) {
        IPackageFragment fragment = (IPackageFragment) element;
        try {
            for (IJavaElement child : fragment.getChildren()) {
                // recursive! Only show containers that contain a configs
                if (select(viewer, parent, child)) {
                    return true;
                }
            }
            for (Object nonJavaResource : fragment.getNonJavaResources()) {
                // recursive! Only show containers that contain a configs
                if (select(viewer, parent, nonJavaResource)) {
                    return true;
                }
            }
        } catch (JavaModelException e) {
            SpringUIPlugin.log(e.getStatus());
        }
        return false;
    } else if (element instanceof JarEntryDirectory) {
        for (IJarEntryResource child : ((JarEntryDirectory) element).getChildren()) {
            // recursive! Only show containers that contain a configs
            if (select(viewer, parent, child)) {
                return true;
            }
        }
    } else if (element instanceof IStorage && !(element instanceof IFile)) {
        return hasAllowedFileSuffix(((IStorage) element).getFullPath());
    }
    return super.select(viewer, parent, element);
}