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.eclipse.che.plugin.java.server.JavaNavigation.java

License:Open Source License

public JarEntry getEntry(IJavaProject project, int rootId, String path) throws CoreException {
    IPackageFragmentRoot root = getPackageFragmentRoot(project, rootId);
    if (root == null) {
        return null;
    }/*from w  w  w  . j ava 2s.c  o  m*/
    if (path.startsWith("/")) {

        JarPackageFragmentRoot jarPackageFragmentRoot = (JarPackageFragmentRoot) root;
        ZipFile jar = null;
        try {
            jar = jarPackageFragmentRoot.getJar();
            ZipEntry entry = jar.getEntry(path.substring(1));
            if (entry != null) {
                JarEntry result = DtoFactory.getInstance().createDto(JarEntry.class);
                result.setType(JarEntryType.FILE);
                result.setPath(path);
                result.setName(entry.getName().substring(entry.getName().lastIndexOf("/") + 1));
                return result;
            }
        } finally {
            if (jar != null) {
                JavaModelManager.getJavaModelManager().closeZipFile(jar);
            }
        }

        Object[] resources = root.getNonJavaResources();

        for (Object resource : resources) {
            if (resource instanceof JarEntryFile) {
                JarEntryFile file = (JarEntryFile) resource;
                if (file.getFullPath().toOSString().equals(path)) {
                    return getJarEntryResource(file);
                }
            }
            if (resource instanceof JarEntryDirectory) {
                JarEntryDirectory directory = (JarEntryDirectory) resource;
                JarEntryFile file = findJarFile(directory, path);
                if (file != null) {
                    return getJarEntryResource(file);
                }
            }
        }

    } else {
        //java class or file
        IType type = project.findType(path);
        if (type != null && type.isBinary()) {
            IClassFile classFile = type.getClassFile();
            return getJarClass(classFile);
        }
    }

    return null;
}

From source file:org.eclipse.jpt.jaxb.core.internal.gen.SchemaGenerator.java

License:Open Source License

private IFile getJaxbPropertiesFileFromPackageRoots(Iterable<IPackageFragmentRoot> packageFragmentRoots) {
    IJavaElement[] javaElements;//w w w.  jav a 2  s.c o m
    IFile jaxbPropertiesFile = null;
    try {
        for (IPackageFragmentRoot pfr : packageFragmentRoots) {
            jaxbPropertiesFile = this.findJaxbPropertiesFile(pfr.getNonJavaResources());
            if (jaxbPropertiesFile != null) {
                return jaxbPropertiesFile;
            }
            javaElements = pfr.getChildren();
            for (IJavaElement je : javaElements) {
                jaxbPropertiesFile = this.findJaxbPropertiesFile(((IPackageFragment) je).getNonJavaResources());
                if (jaxbPropertiesFile != null) {
                    return jaxbPropertiesFile;
                }
            }
        }
    } catch (JavaModelException jme) {
        throw new RuntimeException(jme);
    }
    return null;
}

From source file:org.eclipse.oomph.manifests.OpenManifestHandler.java

License:Open Source License

@SuppressWarnings("restriction")
private IEditorInput getManifestEditorInputStorage(IPackageFragmentRoot root) throws JavaModelException {
    for (Object object : root.getNonJavaResources()) {
        if (object instanceof IJarEntryResource) {
            IJarEntryResource jarEntryResource = (IJarEntryResource) object;
            if ("META-INF".equalsIgnoreCase(jarEntryResource.getName())) {
                for (IJarEntryResource child : jarEntryResource.getChildren()) {
                    if ("MANIFEST.MF".equalsIgnoreCase(child.getName())) {
                        return new org.eclipse.jdt.internal.ui.javaeditor.JarEntryEditorInput(child);
                    }//  w  w w .j  av  a  2 s .  co  m
                }
            }
        } else if (object instanceof IContainer) {
            IContainer container = (IContainer) object;
            if ("META-INF".equalsIgnoreCase(container.getName())) {
                IFile file = container.getFile(new Path("MANIFEST.MF"));
                if (file.exists()) {
                    return new FileEditorInput(file);
                }
            }
        }
    }

    return null;
}

From source file:org.eclipse.wst.xml.search.editor.util.JdtUtils.java

License:Open Source License

public static IJarEntryResource getJavaResourceFileFromBinary(IProject project, String jarNamePattern,
        String packageName, String fileName) {
    IJavaProject javaProject = getJavaProject(project);
    if (javaProject != null) {
        try {/*from   w w w . j a v a  2  s  . c  om*/
            IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
            IPackageFragmentRoot root = null;
            for (int i = 0; i < roots.length; i++) {
                root = roots[i];
                if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
                    if (StringUtils.isEmpty(jarNamePattern)
                            || root.getElementName().startsWith(jarNamePattern)) {
                        Object[] nonJavaResources = null;
                        if (packageName != null) {
                            IPackageFragment fragment = root.getPackageFragment(packageName);
                            if (fragment != null) {
                                nonJavaResources = fragment.getNonJavaResources();
                            }
                        } else {
                            nonJavaResources = root.getNonJavaResources();
                        }
                        if (nonJavaResources != null) {
                            Object nonJavaResource = null;
                            for (int j = 0; j < nonJavaResources.length; j++) {
                                nonJavaResource = nonJavaResources[j];
                                if (nonJavaResource instanceof IJarEntryResource) {
                                    IJarEntryResource r = (IJarEntryResource) nonJavaResource;
                                    if (r.isFile() && fileName.equals(r.getName())) {
                                        return r;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (JavaModelException e) {
            Trace.trace(Trace.SEVERE, (new StringBuilder("Error getting Java project src for project '"))
                    .append(project.getName()).append("'").toString(), e);
        }
    }
    return null;
}

From source file:org.eclipse.wst.xml.search.editor.util.JdtUtils.java

License:Open Source License

public static IJarEntryResource[] getJavaResourcesFileFromBinary(IProject project, String jarNamePattern,
        String packageName, String fileName) {
    Collection<IJarEntryResource> files = new ArrayList<IJarEntryResource>();
    IJavaProject javaProject = getJavaProject(project);
    if (javaProject != null) {
        try {//from w w  w .  j  a  v a 2  s  .  c  om
            IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
            IPackageFragmentRoot root = null;
            for (int i = 0; i < roots.length; i++) {
                root = roots[i];
                if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
                    if (StringUtils.isEmpty(jarNamePattern) || root.getElementName().contains(jarNamePattern)) {
                        Object[] nonJavaResources = null;
                        if (packageName != null) {
                            IPackageFragment fragment = root.getPackageFragment(packageName);
                            if (fragment != null) {
                                nonJavaResources = fragment.getNonJavaResources();
                            }
                        } else {
                            nonJavaResources = root.getNonJavaResources();
                        }
                        if (nonJavaResources != null) {
                            Object nonJavaResource = null;
                            for (int j = 0; j < nonJavaResources.length; j++) {
                                nonJavaResource = nonJavaResources[j];
                                if (nonJavaResource instanceof IJarEntryResource) {
                                    IJarEntryResource r = (IJarEntryResource) nonJavaResource;
                                    if (r.isFile() && r.getName().contains(fileName)) {
                                        files.add(r);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (JavaModelException e) {
            Trace.trace(Trace.SEVERE, (new StringBuilder("Error getting Java project src for project '"))
                    .append(project.getName()).append("'").toString(), e);
        }
    }
    return files.toArray(new IJarEntryResource[files.size()]);
}

From source file:org.eclipse.xtext.ui.resource.PackageFragmentRootWalker.java

License:Open Source License

public T traverse(IPackageFragmentRoot root, boolean stopOnFirstResult) throws JavaModelException {
    T result = null;/*  w w w  . j a v  a2  s.c  o  m*/
    if (root.exists() && existsPhysically(root)) {
        Object[] resources = root.getNonJavaResources();
        TraversalState state = new TraversalState(root);
        for (Object object : resources) {
            if (object instanceof IJarEntryResource) {
                result = traverse((IJarEntryResource) object, stopOnFirstResult, state);
                if (stopOnFirstResult && result != null)
                    return result;
            }
        }

        IJavaElement[] children = root.getChildren();
        for (IJavaElement javaElement : children) {
            if (javaElement instanceof IPackageFragment) {
                result = traverse((IPackageFragment) javaElement, stopOnFirstResult, state);
                if (stopOnFirstResult && result != null)
                    return result;
            }
        }
    }
    return result;
}

From source file:org.eclipse.xtext.ui.util.JdtClasspathUriResolver.java

License:Open Source License

private Object[] getNonJavaResources(IPackageFragmentRoot packageFragmentRoot, IPackageFragment packageFragment)
        throws JavaModelException {
    Object[] nonJavaResources = null;
    if (packageFragment.isDefaultPackage()) {
        nonJavaResources = packageFragmentRoot.getNonJavaResources();
    } else {/*from  w ww  . j a va 2 s. c  om*/
        nonJavaResources = packageFragment.getNonJavaResources();
    }
    return nonJavaResources;
}

From source file:org.jboss.tools.arquillian.ui.internal.dialogs.ArquillianResourcesSelectionDialog.java

License:Open Source License

private void getResources() {
    allResources = new ArrayList<IPath>();
    if (javaProject != null && javaProject.isOpen()) {
        IPath testSourcePath = null;/*w ww  .ja v  a  2s .  c om*/
        try {
            IProject project = javaProject.getProject();
            if (project.hasNature(IMavenConstants.NATURE_ID)) {
                IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().create(project,
                        new NullProgressMonitor());
                MavenProject mavenProject = facade.getMavenProject(new NullProgressMonitor());
                Build build = mavenProject.getBuild();
                String testSourceDirectory = build.getTestSourceDirectory();
                testSourcePath = Path.fromOSString(testSourceDirectory);
                IPath workspacePath = ResourcesPlugin.getWorkspace().getRoot().getRawLocation();
                testSourcePath = testSourcePath.makeRelativeTo(workspacePath).makeAbsolute();

            }
            IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
            for (IClasspathEntry entry : rawClasspath) {
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
                    if (roots == null) {
                        continue;
                    }
                    for (IPackageFragmentRoot root : roots) {
                        IPath path = root.getPath();
                        String projectLocation = project.getLocation().toOSString();
                        IPath projectPath = Path.fromOSString(projectLocation);
                        IPath workspacePath = ResourcesPlugin.getWorkspace().getRoot().getRawLocation();
                        projectPath = projectPath.makeRelativeTo(workspacePath).makeAbsolute();
                        projectPath = projectPath.removeLastSegments(1);
                        path = projectPath.append(path);
                        if (path != null && path.equals(testSourcePath)) {
                            continue;
                        }

                        Object[] resources = root.getNonJavaResources();
                        for (Object resource : resources) {
                            addResource(allResources, resource, root.getPath());
                        }
                    }
                }
            }
        } catch (Exception e1) {
            ArquillianUIActivator.log(e1);
        }
    }
}

From source file:org.jboss.tools.arquillian.ui.internal.utils.ArquillianUIUtil.java

License:Open Source License

private static IFile getFile(IJavaProject javaProject, String fileName) throws JavaModelException {
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    for (IClasspathEntry entry : rawClasspath) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
            if (roots == null) {
                continue;
            }//  w  w  w  .ja  v a 2s. c o  m
            for (IPackageFragmentRoot root : roots) {
                Object[] resources = root.getNonJavaResources();
                int segments = root.getPath().segmentCount();
                for (Object resource : resources) {
                    if (resource instanceof IFile) {
                        IFile file = (IFile) resource;
                        IPath filePath = file.getProjectRelativePath();
                        IPath relativePath = filePath.removeFirstSegments(segments - 1);
                        if (fileName.equals(relativePath.toString())) {
                            return file;
                        }
                    }
                }

            }
        }
    }
    return null;
}

From source file:org.nuxeo.ide.sdk.deploy.Deployment.java

License:Open Source License

protected void copyPackageRoot(Set<String> commands, IPackageFragmentRoot root) throws Exception {
    for (IJavaElement child : root.getChildren()) {
        switch (child.getElementType()) {
        case IJavaElement.PACKAGE_FRAGMENT:
            copyPackageFragment(commands, root, (IPackageFragment) child);
            break;
        case IJavaElement.COMPILATION_UNIT:
            copyCompilationUnit(commands, root, (ICompilationUnit) child);
            break;
        }/*from   w  w w.ja  v a 2  s . c  o m*/
    }
    root.getNonJavaResources();
}