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

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

Introduction

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

Prototype

String getElementName();

Source Link

Document

Returns the name of this element.

Usage

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 {/*w  w w.j a  v  a 2s. 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  ww  w  .ja v a2s .  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.wst.xml.ui.internal.contentassist.tapestry.TapestryRootComponentsProposalComputer.java

License:Open Source License

/**
 * Get custom component attribute templates
 * /*from w  w w .j  av a 2 s. com*/
 * @param project
 * @param contextTypeId
 * @param nodeName
 * @param tapestryClassLoader
 * @return
 */
public List<Template> getCustomComponentsAttributes(IProject project, String contextTypeId, String nodeName,
        TapestryClassLoader tapestryClassLoader) {
    components.clear();
    String prefix = nodeName.substring(0, nodeName.indexOf(':'));
    try {
        final IFile res = project.getFile(TapestryContants.CUSTOM_COMPONENTS_FILE);
        if (res == null || prefix == null)
            return components;
        List<ComponentPackage> packageList = loadCustomComponentsPackageListWithPrefix(res, prefix);
        IPackageFragmentRoot[] roots = JavaCore.create(project).getAllPackageFragmentRoots();
        for (ComponentPackage cp : packageList) {
            for (IPackageFragmentRoot root : roots) {
                if (cp.isArchive() == root.isArchive() && cp.getFragmentRoot().equals(root.getElementName())) {
                    IPackageFragment packInstance = root.getPackageFragment(cp.getPath());
                    if (!root.isArchive()) {
                        //If current custom component is in source directory
                        if (packInstance != null) {
                            IJavaElement[] elements = packInstance.getChildren();
                            for (IJavaElement ele : elements) {
                                if (ele.getElementType() == IJavaElement.COMPILATION_UNIT
                                        && ele.getElementName().endsWith(".java")) {
                                    String name = ele.getElementName().substring(0,
                                            ele.getElementName().indexOf('.'));
                                    if ((prefix + ":" + name).toLowerCase().equals(nodeName)) {
                                        goThroughClass((ICompilationUnit) ele, contextTypeId);
                                        return components;
                                    }
                                }
                            }
                        }
                    } else if (packInstance instanceof PackageFragment) {
                        //Current custom component is in jar files
                        for (Object packo : ((PackageFragment) packInstance)
                                .getChildrenOfType(IJavaElement.CLASS_FILE)) {
                            ClassFile packi = (ClassFile) packo;
                            String className = packi.getElementName().substring(0,
                                    packi.getElementName().indexOf('.'));
                            if (className.indexOf('$') < 0
                                    && (prefix + ":" + className.toLowerCase()).equals(nodeName)) {
                                TapestryCoreComponents component = null;
                                try {
                                    component = tapestryClassLoader.loadComponentAttributesFromClassFile(root,
                                            prefix, packi);
                                } catch (ClassFormatException e) {
                                    e.printStackTrace();
                                }
                                if (component != null) {
                                    for (String paramName : component.getPamameters()) {
                                        Template template = new Template(paramName,
                                                "add attribute " + paramName, contextTypeId,
                                                buildAttributeInsertCode(paramName), true);
                                        components.add(template);
                                    }
                                    return components;
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:org.eclipse.wst.xml.ui.internal.contentassist.tapestry.TapestryRootComponentsProposalComputer.java

License:Open Source License

private List<String> getCustomComponentsNameList(IPackageFragmentRoot[] roots, ComponentPackage cp) {
    List<String> componentNameList = new ArrayList<String>();
    try {//w  w w  .  j  a va2  s.c om
        for (IPackageFragmentRoot root : roots) {
            if (root instanceof JarPackageFragmentRoot == cp.isArchive()
                    && root.getElementName().equals(cp.getFragmentRoot())) {
                if (!root.isArchive()) {
                    // Load custom components from source directory
                    IPackageFragment packInstance = root.getPackageFragment(cp.getPath());
                    if (packInstance != null) {
                        IJavaElement[] elements = packInstance.getChildren();
                        for (IJavaElement ele : elements) {
                            if (ele.getElementType() == IJavaElement.COMPILATION_UNIT
                                    && ele.getElementName().endsWith(".java")) {
                                String name = ele.getElementName().substring(0,
                                        ele.getElementName().indexOf('.'));
                                componentNameList.add(name);
                            }
                        }
                    }
                } else {
                    // Load custom components from jar files
                    for (IJavaElement pack : root.getChildren()) {
                        if (pack != null && pack instanceof PackageFragment
                                && pack.getElementName().equals(cp.getPath())) {
                            for (Object packo : ((PackageFragment) pack)
                                    .getChildrenOfType(IJavaElement.CLASS_FILE)) {
                                ClassFile packi = (ClassFile) packo;
                                String itemName = packi.getElementName();
                                if (itemName.indexOf('$') < 0 && itemName.endsWith(".class"))
                                    componentNameList.add(itemName.substring(0, itemName.length() - 6));
                            }
                            break;
                        }
                    }
                }
                return componentNameList;
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
    return componentNameList;
}

From source file:org.eclipse.xtext.ui.tests.core.builder.impl.PackageFragmentRootWalkerTest.java

License:Open Source License

@Test
public void testTraversePackageFragmentRoot() throws Exception {
    IJavaProject project = createJavaProject("foo");
    String jarName = "JarWalkerTest.jar";
    IFile file = project.getProject().getFile(jarName);
    file.create(getClass().getResourceAsStream(jarName), true, new NullProgressMonitor());
    addJarToClasspath(project, file);//from   w w  w.j  a  v  a 2 s .co  m

    final Set<IPath> pathes = new HashSet<IPath>();
    PackageFragmentRootWalker<Void> walker = new PackageFragmentRootWalker<Void>() {
        @Override
        protected Void handle(IJarEntryResource jarEntry, TraversalState state) {
            pathes.add(jarEntry.getFullPath());
            return null;
        }
    };
    for (IPackageFragmentRoot root : project.getPackageFragmentRoots()) {
        if (root.getElementName().equals(jarName))
            walker.traverse(root, false);
    }
    assertEquals(3, pathes.size());
}

From source file:org.eclipselabs.spray.xtext.ui.commands.SprayJavaProjectUtil.java

License:Open Source License

protected IPackageFragmentRoot getPackageFragmentRoot(IJavaProject project, String name)
        throws JavaModelException {
    for (IPackageFragmentRoot pfr : project.getPackageFragmentRoots()) {
        if (pfr.getElementName().equals(name)) {
            return pfr;
        }/*from w  w  w.j  a v  a 2s  . c  om*/
    }
    return null;
}

From source file:org.fastcode.util.SourceUtil.java

License:Open Source License

/**
 *
 * @param project//from w  w  w.ja v a2 s .  co  m
 * @param paramType
 * @return
 * @throws Exception
 */
public static Object getElementFromProject(final IJavaProject project, final String paramType,
        final String filter) throws Exception {

    IType type = project.findType(paramType);
    if (type != null && type.exists()) {
        return type;
    }

    IFile file = project.getProject().getFile(paramType);
    if (file != null && file.exists()) {
        return file;
    }

    final IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots();
    int nClasses = 0;

    for (final IPackageFragmentRoot root : roots) {
        if (root.getElementName() != null && filter != null && root.getElementName().endsWith(filter)) {
            continue;
        }
        final IJavaElement[] elements = root.getChildren();
        for (int i = 0; i < elements.length; i++) {
            final IJavaElement element = elements[i];
            final IPackageFragment fragment = (IPackageFragment) element.getAdapter(IPackageFragment.class);
            if (fragment == null) {
                continue;
            }

            // TODO redo this logic later with JavaCore.create(file)
            String path = fragment.getPath().toString();
            if (path.length() > root.getPath().toString().length()) {
                path = path.substring(root.getPath().toString().length() + 1);
            }

            final String newpath = path.replaceAll(FORWARD_SLASH, "\\.");

            if (paramType.startsWith(newpath)) {
                String tmpName = paramType.replaceFirst(newpath, path);
                tmpName = tmpName.replaceFirst("\\.", FORWARD_SLASH);
                tmpName = root.getPath().toString() + FORWARD_SLASH + tmpName;
                tmpName = tmpName.substring(project.getProject().getName().length() + 1);
                final IResource resource = project.getProject().findMember(tmpName);
                // JavaCore.create(file);

                if (resource != null && resource.exists()) {
                    return resource;
                }
            }

            final IJavaElement fes[] = fragment.getChildren();

            for (int j = 0; j < fes.length; j++) {
                final String className = fes[j].getElementName();

                final int elementType = fes[j].getElementType();
                if (elementType == TYPE) {
                    type = (IType) fes[j];
                } else if (elementType == COMPILATION_UNIT) {
                    type = ((ICompilationUnit) fes[j]).findPrimaryType();
                } else if (elementType == CLASS_FILE) {
                    type = ((IClassFile) fes[j]).findPrimaryType();
                } else if (elementType == IResource.FILE) {
                    file = (IFile) fes[j];
                }

                if (type != null && type.getFullyQualifiedName().equals(paramType)) {
                    return type;
                } else if (file != null
                        && paramType.equals(fragment.getElementName() + DOT + fes[j].getElementName())) {
                    return file;
                }

                nClasses++;
            }
        }
    }
    final String projectName = project.getElementName();
    return null;
}

From source file:org.grails.ide.eclipse.editor.groovy.elements.GrailsProject.java

License:Open Source License

/**
 * Converts this {@link ICompilationUnit} into a {@link GrailsElementKind} if
 * it corresponds to a standard Grails artifact.  Otherwise returns {@link GrailsElementKind#OTHER}.
 * Returns {@link GrailsElementKind#INVALID} if compilation unit is not from this Project or compilation unit
 * is not from a grails project.//from   w w  w .j  a va  2  s  .  co  m
 * @param unit
 * @return
 */
public GrailsElementKind getElementKind(ICompilationUnit unit) {
    if (unit == null || (!unit.getJavaProject().equals(javaProject))) {
        return GrailsElementKind.INVALID;
    }

    if (!(unit instanceof GroovyCompilationUnit)) {
        return GrailsElementKind.OTHER;
    }

    IPackageFragmentRoot root = ((CompilationUnit) unit).getPackageFragmentRoot();
    String rootName = root.getElementName();
    if (GrailsElementKind.CONTROLLER_CLASS.getSourceFolder().equals(rootName)) {
        if (primaryTypeExists(unit)) {
            return GrailsElementKind.CONTROLLER_CLASS;
        } else {
            return GrailsElementKind.OTHER;
        }
    } else if (GrailsElementKind.DOMAIN_CLASS.getSourceFolder().equals(rootName)) {
        if (primaryTypeExists(unit)) {
            return GrailsElementKind.DOMAIN_CLASS;
        } else {
            return GrailsElementKind.OTHER;
        }
    } else if (GrailsElementKind.TAGLIB_CLASS.getSourceFolder().equals(rootName)) {
        if (primaryTypeExists(unit)) {
            return GrailsElementKind.TAGLIB_CLASS;
        } else {
            return GrailsElementKind.OTHER;
        }
    } else if (GrailsElementKind.SERVICE_CLASS.getSourceFolder().equals(rootName)) {
        if (primaryTypeExists(unit)) {
            return GrailsElementKind.SERVICE_CLASS;
        } else {
            return GrailsElementKind.OTHER;
        }
    } else if (GrailsElementKind.INTEGRATION_TEST.getSourceFolder().equals(rootName)) {
        if (primaryTypeExists(unit)) {
            return GrailsElementKind.INTEGRATION_TEST;
        } else {
            return GrailsElementKind.OTHER;
        }
    } else if (GrailsElementKind.UNIT_TEST.getSourceFolder().endsWith(rootName)) {
        if (primaryTypeExists(unit)) {
            return GrailsElementKind.UNIT_TEST;
        } else {
            return GrailsElementKind.OTHER;
        }
    } else if (GrailsElementKind.INTEGRATION_TEST.getSourceFolder().endsWith(rootName)) {
        if (primaryTypeExists(unit)) {
            return GrailsElementKind.INTEGRATION_TEST;
        } else {
            return GrailsElementKind.OTHER;
        }
    } else if (rootName.equals(GrailsElementKind.CONF_FOLDER)) {
        String elementName = unit.getElementName();
        if (GrailsElementKind.BOOT_STRAP.getNameSuffix().equals(elementName)) {
            return GrailsElementKind.BOOT_STRAP;
        } else if (GrailsElementKind.BUILD_CONFIG.getNameSuffix().equals(elementName)) {
            return GrailsElementKind.BUILD_CONFIG;
        } else if (GrailsElementKind.CONFIG.getNameSuffix().equals(elementName)) {
            return GrailsElementKind.CONFIG;
        } else if (GrailsElementKind.DATA_SOURCE.getNameSuffix().equals(elementName)) {
            return GrailsElementKind.DATA_SOURCE;
        } else if (GrailsElementKind.URL_MAPPINGS.getNameSuffix().equals(elementName)) {
            return GrailsElementKind.URL_MAPPINGS;
        }

        // only for testing
    } else if (cuHandleToElementKind.containsKey(unit.getHandleIdentifier())) {
        return cuHandleToElementKind.get(unit.getHandleIdentifier());
    }
    return GrailsElementKind.OTHER;
}

From source file:org.grails.ide.eclipse.explorer.providers.GrailsNavigatorLabelProvider.java

License:Open Source License

public String getText(Object element) {
    if (element instanceof ILogicalFolder) {
        ILogicalFolder logicalFolder = (ILogicalFolder) element;
        String name = logicalFolder.getName();
        if (name == null) {
            GrailsProjectStructureTypes type = logicalFolder.getType();
            if (type != null) {
                name = type.getDisplayName();
            }//www .  j  ava2 s .  co  m
        }
        return name;

    } else if (element instanceof IPackageFragmentRoot) {
        IPackageFragmentRoot root = (IPackageFragmentRoot) element;

        if (GrailsResourceUtil.isReimagedGrailsSourceFolder(root)) {
            try {
                IResource resource = root.getCorrespondingResource();
                if (resource instanceof IFolder) {
                    IFolder folder = (IFolder) resource;
                    GrailsProjectStructureTypes type = GrailsResourceUtil.getGrailsContainerType(folder);
                    if (type != null) {
                        return type.getDisplayName();
                    }
                }
            } catch (JavaModelException e) {
                GrailsCoreActivator.log(e);
            }
        } else if (GrailsResourceUtil.isGrailsDependencyPackageFragmentRoot(root)) {
            return GrailsResourceUtil.convertRootName(root.getElementName());
        }
    } else if (element instanceof IFolder) {
        IFolder folder = (IFolder) element;

        if (GrailsNature.isGrailsProject(folder.getProject())
                && GrailsResourceUtil.isReimagedGrailsProjectFileFolder(folder)) {
            GrailsProjectStructureTypes type = GrailsResourceUtil.getGrailsContainerType(folder);
            if (type != null) {
                return type.getDisplayName();
            }
        }
    }
    return null;
}

From source file:org.jboss.tools.common.text.ext.hyperlink.xml.FaceletSourceTagHyperlink.java

License:Open Source License

/**
 * /* w  w  w  . j  a v a 2  s  . c om*/
 * @param project project from which we are going to open File
 * @param classPathResource resource relativy to class path
 * @param xmodelPath xmodel path to file
 * @return
 */
private static JarEntryEditorInput seachResourceInClassPath(IProject project, IPath classPathResource,
        IPath xmodelPath) {
    IJavaProject javaProject = JavaCore.create(project);
    try {
        for (IPackageFragmentRoot fragmentRoot : javaProject.getAllPackageFragmentRoots()) {
            if (fragmentRoot instanceof JarPackageFragmentRoot) {
                JarPackageFragmentRoot jarPackageFragmentRoot = (JarPackageFragmentRoot) fragmentRoot;
                ZipEntry zipEntry = jarPackageFragmentRoot.getJar().getEntry(classPathResource.toString());
                //the name of jar file shoul be in xmodel path
                //for now if model have following
                if (zipEntry != null && xmodelPath.toString().contains(fragmentRoot.getElementName())) {
                    JarEntryFile fileInJar = new JarEntryFile(classPathResource.lastSegment());
                    Object parent = null;
                    String parentName = classPathResource.removeLastSegments(1).toString();
                    if (parentName.length() == 0) {
                        parent = jarPackageFragmentRoot;
                    } else {
                        parent = jarPackageFragmentRoot
                                .getPackageFragment(classPathResource.removeLastSegments(1).toString());
                    }
                    fileInJar.setParent(parent);
                    JarEntryEditorInput jarEditorInput = new JarEntryEditorInput(fileInJar);
                    return jarEditorInput;
                }
            }
        }
    } catch (JavaModelException e) {
        ExtensionsPlugin.getDefault().logError(e);
    } catch (CoreException e) {
        ExtensionsPlugin.getDefault().logError(e);
    }
    return null;
}