Example usage for org.eclipse.jdt.internal.core.util Util isValidFolderNameForPackage

List of usage examples for org.eclipse.jdt.internal.core.util Util isValidFolderNameForPackage

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core.util Util isValidFolderNameForPackage.

Prototype

public static boolean isValidFolderNameForPackage(String folderName, String sourceLevel,
        String complianceLevel) 

Source Link

Document

Returns true if the given folder name is valid for a package, false if it is not.

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.DeltaProcessor.java

License:Open Source License

private int elementType(File res, int kind, int parentType, RootInfo rootInfo) {
    Path path = new Path(res.getAbsolutePath());
    switch (parentType) {
    case IJavaElement.JAVA_MODEL:
        // case of a movedTo or movedFrom project (other cases are handled in processResourceDelta(...)
        return IJavaElement.JAVA_PROJECT;

    case NON_JAVA_RESOURCE:
    case IJavaElement.JAVA_PROJECT:
        if (rootInfo == null) {
            rootInfo = enclosingRootInfo(path, kind);
        }/*from w w w .j  a v a 2 s  .co m*/
        if (rootInfo != null && rootInfo.isRootOfProject(path)) {
            return IJavaElement.PACKAGE_FRAGMENT_ROOT;
        }
        // not yet in a package fragment root or root of another project
        // or package fragment to be included (see below)
        // $FALL-THROUGH$

    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
    case IJavaElement.PACKAGE_FRAGMENT:
        if (rootInfo == null) {
            IPath rootPath = externalPath(res);
            rootInfo = enclosingRootInfo(rootPath, kind);
        }
        if (rootInfo == null) {
            return NON_JAVA_RESOURCE;
        }
        if (Util.isExcluded(path, rootInfo.inclusionPatterns, rootInfo.exclusionPatterns, false)) {
            return NON_JAVA_RESOURCE;
        }
        if (res.isDirectory()) {
            if (parentType == NON_JAVA_RESOURCE && !Util.isExcluded(new Path(res.getParent()),
                    rootInfo.inclusionPatterns, rootInfo.exclusionPatterns, false)) {
                // parent is a non-Java resource because it doesn't have a valid package name (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=130982)
                return NON_JAVA_RESOURCE;
            }
            String sourceLevel = rootInfo.project == null ? null
                    : rootInfo.project.getOption(JavaCore.COMPILER_SOURCE, true);
            String complianceLevel = rootInfo.project == null ? null
                    : rootInfo.project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
            if (Util.isValidFolderNameForPackage(res.getName(), sourceLevel, complianceLevel)) {
                return IJavaElement.PACKAGE_FRAGMENT;
            }
            return NON_JAVA_RESOURCE;
        }
        String fileName = res.getName();
        String sourceLevel = rootInfo.project == null ? null
                : rootInfo.project.getOption(JavaCore.COMPILER_SOURCE, true);
        String complianceLevel = rootInfo.project == null ? null
                : rootInfo.project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
        if (Util.isValidCompilationUnitName(fileName, sourceLevel, complianceLevel)) {
            return IJavaElement.COMPILATION_UNIT;
        } else if (Util.isValidClassFileName(fileName, sourceLevel, complianceLevel)) {
            return IJavaElement.CLASS_FILE;
        } else {
            IPath rootPath = externalPath(res);
            if ((rootInfo = rootInfo(rootPath, kind)) != null
                    && rootInfo.project.getProject().getFullPath().isPrefixOf(
                            rootPath) /*ensure root is a root of its project (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=185310) */) {
                // case of proj=src=bin and resource is a jar file on the classpath
                return IJavaElement.PACKAGE_FRAGMENT_ROOT;
            } else {
                return NON_JAVA_RESOURCE;
            }
        }

    default:
        return NON_JAVA_RESOURCE;
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.JarPackageFragmentRoot.java

License:Open Source License

private void initRawPackageInfo(HashtableOfArrayToObject rawPackageInfo, String entryName, boolean isDirectory,
        String compliance) {/*from w ww  .jav  a2 s  .  com*/
    int lastSeparator = isDirectory ? entryName.length() - 1 : entryName.lastIndexOf('/');
    String[] pkgName = Util.splitOn('/', entryName, 0, lastSeparator);
    String[] existing = null;
    int length = pkgName.length;
    int existingLength = length;
    while (existingLength >= 0) {
        existing = (String[]) rawPackageInfo.getKey(pkgName, existingLength);
        if (existing != null)
            break;
        existingLength--;
    }
    //        JavaModelManager manager = JavaModelManager.getJavaModelManager();
    for (int i = existingLength; i < length; i++) {
        // sourceLevel must be null because we know nothing about it based on a jar file
        if (Util.isValidFolderNameForPackage(pkgName[i], null, compliance)) {
            System.arraycopy(existing, 0, existing = new String[i + 1], 0, i);
            existing[i] = manager.intern(pkgName[i]);
            rawPackageInfo.put(existing, new ArrayList[] { EMPTY_LIST, EMPTY_LIST });
        } else {
            // non-Java resource folder
            if (!isDirectory) {
                ArrayList[] children = (ArrayList[]) rawPackageInfo.get(existing);
                if (children[1/*NON_JAVA*/] == EMPTY_LIST)
                    children[1/*NON_JAVA*/] = new ArrayList();
                children[1/*NON_JAVA*/].add(entryName);
            }
            return;
        }
    }
    if (isDirectory)
        return;

    // add classfile info amongst children
    ArrayList[] children = (ArrayList[]) rawPackageInfo.get(pkgName);
    if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(entryName)) {
        if (children[0/*JAVA*/] == EMPTY_LIST)
            children[0/*JAVA*/] = new ArrayList();
        String nameWithoutExtension = entryName.substring(lastSeparator + 1, entryName.length() - 6);
        children[0/*JAVA*/].add(nameWithoutExtension);
    } else {
        if (children[1/*NON_JAVA*/] == EMPTY_LIST)
            children[1/*NON_JAVA*/] = new ArrayList();
        children[1/*NON_JAVA*/].add(entryName);
    }

}

From source file:com.codenvy.ide.ext.java.server.internal.core.JavaProjectElementInfo.java

License:Open Source License

/**
 * Compute the non-java resources contained in this java project.
 */// w w w  .  j av a 2  s  .c o  m
private Object[] computeNonJavaResources(JavaProject project) {

    // determine if src == project and/or if bin == project
    IPath projectPath = project.getProject().getFullPath();
    boolean srcIsProject = false;
    boolean binIsProject = false;
    char[][] inclusionPatterns = null;
    char[][] exclusionPatterns = null;
    IPath projectOutput = null;
    boolean isClasspathResolved = true;
    try {
        IClasspathEntry entry = project.getClasspathEntryFor(projectPath);
        if (entry != null) {
            srcIsProject = true;
            inclusionPatterns = ((ClasspathEntry) entry).fullInclusionPatternChars();
            exclusionPatterns = ((ClasspathEntry) entry).fullExclusionPatternChars();
        }
        projectOutput = project.getOutputLocation();
        binIsProject = projectPath.equals(projectOutput);
    } catch (JavaModelException e) {
        isClasspathResolved = false;
    }

    Object[] resources = new IResource[5];
    int resourcesCounter = 0;
    try {
        IResource[] members = ((IContainer) project.getResource()).members();
        int length = members.length;
        if (length > 0) {
            String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true);
            String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
            IClasspathEntry[] classpath = project.getResolvedClasspath();
            for (int i = 0; i < length; i++) {
                IResource res = members[i];
                switch (res.getType()) {
                case IResource.FILE:
                    IPath resFullPath = res.getFullPath();
                    String resName = res.getName();

                    // ignore a jar file on the classpath
                    if (isClasspathResolved && isClasspathEntryOrOutputLocation(resFullPath,
                            res.getLocation()/* see https://bugs.eclipse
                                             .org/bugs/show_bug.cgi?id=244406 */, classpath, projectOutput)) {
                        break;
                    }
                    // ignore .java file if src == project
                    if (srcIsProject && Util.isValidCompilationUnitName(resName, sourceLevel, complianceLevel)
                            && !Util.isExcluded(res, inclusionPatterns, exclusionPatterns)) {
                        break;
                    }
                    // ignore .class file if bin == project
                    if (binIsProject && Util.isValidClassFileName(resName, sourceLevel, complianceLevel)) {
                        break;
                    }
                    // else add non java resource
                    if (resources.length == resourcesCounter) {
                        // resize
                        System.arraycopy(resources, 0, (resources = new IResource[resourcesCounter * 2]), 0,
                                resourcesCounter);
                    }
                    resources[resourcesCounter++] = res;
                    break;
                case IResource.FOLDER:
                    resFullPath = res.getFullPath();

                    // ignore non-excluded folders on the classpath or that correspond to an output location
                    if ((srcIsProject && !Util.isExcluded(res, inclusionPatterns, exclusionPatterns)
                            && Util.isValidFolderNameForPackage(res.getName(), sourceLevel, complianceLevel))
                            || (isClasspathResolved && isClasspathEntryOrOutputLocation(resFullPath,
                                    res.getLocation(), classpath, projectOutput))) {
                        break;
                    }
                    // else add non java resource
                    if (resources.length == resourcesCounter) {
                        // resize
                        System.arraycopy(resources, 0, (resources = new IResource[resourcesCounter * 2]), 0,
                                resourcesCounter);
                    }
                    resources[resourcesCounter++] = res;
                }
            }
        }
        if (resources.length != resourcesCounter) {
            System.arraycopy(resources, 0, (resources = new IResource[resourcesCounter]), 0, resourcesCounter);
        }
    } catch (CoreException e) {
        resources = NO_NON_JAVA_RESOURCES;
        resourcesCounter = 0;
    }
    return resources;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.PackageFragmentRootInfo.java

License:Open Source License

/**
 * Starting at this folder, create non-java resources for this package fragment root
 * and add them to the non-java resources collection.
 *
 * @exception org.eclipse.jdt.core.JavaModelException  The resource associated with this package fragment does not exist
 *///from   ww w.  j  a  va 2 s  .  c  om
static Object[] computeFolderNonJavaResources(IPackageFragmentRoot root, IContainer folder,
        char[][] inclusionPatterns, char[][] exclusionPatterns) throws JavaModelException {
    IResource[] nonJavaResources = new IResource[5];
    int nonJavaResourcesCounter = 0;
    try {
        IResource[] members = folder.members();
        int length = members.length;
        if (length > 0) {
            // if package fragment root refers to folder in another IProject, then
            // folder.getProject() is different than root.getJavaProject().getProject()
            // use the other java project's options to verify the name
            IJavaProject otherJavaProject = JavaCore.create(folder.getProject());
            String sourceLevel = otherJavaProject.getOption(JavaCore.COMPILER_SOURCE, true);
            String complianceLevel = otherJavaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
            JavaProject javaProject = (JavaProject) root.getJavaProject();
            IClasspathEntry[] classpath = javaProject.getResolvedClasspath();
            nextResource: for (int i = 0; i < length; i++) {
                IResource member = members[i];
                switch (member.getType()) {
                case IResource.FILE:
                    String fileName = member.getName();

                    // ignore .java files that are not excluded
                    if (Util.isValidCompilationUnitName(fileName, sourceLevel, complianceLevel)
                            && !Util.isExcluded(member, inclusionPatterns, exclusionPatterns))
                        continue nextResource;
                    // ignore .class files
                    if (Util.isValidClassFileName(fileName, sourceLevel, complianceLevel))
                        continue nextResource;
                    // ignore .zip or .jar file on classpath
                    if (isClasspathEntry(member.getFullPath(), classpath))
                        continue nextResource;
                    break;

                case IResource.FOLDER:
                    // ignore valid packages or excluded folders that correspond to a nested pkg fragment root
                    if (Util.isValidFolderNameForPackage(member.getName(), sourceLevel, complianceLevel)
                            && (!Util.isExcluded(member, inclusionPatterns, exclusionPatterns)
                                    || isClasspathEntry(member.getFullPath(), classpath)))
                        continue nextResource;
                    break;
                }
                if (nonJavaResources.length == nonJavaResourcesCounter) {
                    // resize
                    System.arraycopy(nonJavaResources, 0,
                            (nonJavaResources = new IResource[nonJavaResourcesCounter * 2]), 0,
                            nonJavaResourcesCounter);
                }
                nonJavaResources[nonJavaResourcesCounter++] = member;
            }
        }
        //      if (ExternalFoldersManager.isInternalPathForExternalFolder(folder.getFullPath())) {
        //         IJarEntryResource[] jarEntryResources = new IJarEntryResource[nonJavaResourcesCounter];
        //         for (int i = 0; i < nonJavaResourcesCounter; i++) {
        //            jarEntryResources[i] = new NonJavaResource(root, nonJavaResources[i]);
        //         }
        //         return jarEntryResources;
        //      } else if (nonJavaResources.length != nonJavaResourcesCounter) {
        //         System.arraycopy(nonJavaResources, 0, (nonJavaResources = new IResource[nonJavaResourcesCounter]), 0, nonJavaResourcesCounter);
        //      }
        return nonJavaResources;
    } catch (CoreException e) {
        throw new JavaModelException(e);
    }
}

From source file:org.eclipse.che.jdt.internal.core.DeltaProcessor.java

License:Open Source License

private int elementType(File res, int kind, int parentType, RootInfo rootInfo) {
    Path path = new Path(res.getAbsolutePath());
    switch (parentType) {
    case IJavaElement.JAVA_MODEL:
        // case of a movedTo or movedFrom project (other cases are handled in processResourceDelta(...)
        return IJavaElement.JAVA_PROJECT;

    case NON_JAVA_RESOURCE:
    case IJavaElement.JAVA_PROJECT:
        if (rootInfo == null) {
            rootInfo = enclosingRootInfo(path, kind);
        }//from w  w w . j  a  va  2 s.  c  o m
        if (rootInfo != null && rootInfo.isRootOfProject(path)) {
            return IJavaElement.PACKAGE_FRAGMENT_ROOT;
        }
        // not yet in a package fragment root or root of another project
        // or package fragment to be included (see below)
        // $FALL-THROUGH$

    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
    case IJavaElement.PACKAGE_FRAGMENT:
        if (rootInfo == null) {
            IPath rootPath = externalPath(res);
            rootInfo = enclosingRootInfo(rootPath, kind);
        }
        if (rootInfo == null) {
            return NON_JAVA_RESOURCE;
        }
        if (Util.isExcluded(path, rootInfo.inclusionPatterns, rootInfo.exclusionPatterns, false)) {
            return NON_JAVA_RESOURCE;
        }
        if (res.isDirectory()) {
            if (parentType == NON_JAVA_RESOURCE && !Util.isExcluded(new Path(res.getParent()),
                    rootInfo.inclusionPatterns, rootInfo.exclusionPatterns, false)) {
                // parent is a non-Java resource because it doesn't have a valid package name (see https://bugs.eclipse
                // .org/bugs/show_bug.cgi?id=130982)
                return NON_JAVA_RESOURCE;
            }
            String sourceLevel = rootInfo.project == null ? null
                    : rootInfo.project.getOption(JavaCore.COMPILER_SOURCE, true);
            String complianceLevel = rootInfo.project == null ? null
                    : rootInfo.project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
            if (Util.isValidFolderNameForPackage(res.getName(), sourceLevel, complianceLevel)) {
                return IJavaElement.PACKAGE_FRAGMENT;
            }
            return NON_JAVA_RESOURCE;
        }
        String fileName = res.getName();
        String sourceLevel = rootInfo.project == null ? null
                : rootInfo.project.getOption(JavaCore.COMPILER_SOURCE, true);
        String complianceLevel = rootInfo.project == null ? null
                : rootInfo.project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
        if (Util.isValidCompilationUnitName(fileName, sourceLevel, complianceLevel)) {
            return IJavaElement.COMPILATION_UNIT;
        } else if (Util.isValidClassFileName(fileName, sourceLevel, complianceLevel)) {
            return IJavaElement.CLASS_FILE;
        } else {
            IPath rootPath = externalPath(res);
            if ((rootInfo = rootInfo(rootPath, kind)) != null
                    && rootInfo.project.getProject().getFullPath().isPrefixOf(rootPath) /*ensure root is a root of its project (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=185310)
                                                                                        */) {
                // case of proj=src=bin and resource is a jar file on the classpath
                return IJavaElement.PACKAGE_FRAGMENT_ROOT;
            } else {
                return NON_JAVA_RESOURCE;
            }
        }

    default:
        return NON_JAVA_RESOURCE;
    }
}

From source file:org.eclipse.jdt.internal.core.PackageFragment.java

License:Open Source License

private boolean isValidPackageName() {
    JavaProject javaProject = (JavaProject) getJavaProject();
    String sourceLevel = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
    String complianceLevel = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
    for (int i = 0, length = this.names.length; i < length; i++) {
        if (!Util.isValidFolderNameForPackage(this.names[i], sourceLevel, complianceLevel))
            return false;
    }/*  w w  w . j  a  v a  2  s  .c  o m*/
    return true;
}

From source file:org.eclipse.jdt.internal.core.PackageFragmentRootInfo.java

License:Open Source License

/**
 * Starting at this folder, create non-java resources for this package fragment root
 * and add them to the non-java resources collection.
 *
 * @exception JavaModelException  The resource associated with this package fragment does not exist
 *///from  www . j  a v a 2s.c  o  m
static Object[] computeFolderNonJavaResources(IPackageFragmentRoot root, IContainer folder,
        char[][] inclusionPatterns, char[][] exclusionPatterns) throws JavaModelException {
    IResource[] nonJavaResources = new IResource[5];
    int nonJavaResourcesCounter = 0;
    JavaProject project = (JavaProject) root.getJavaProject();
    try {
        // GROOVY start
        // here, we only care about non-source package roots in Groovy projects
        boolean isInterestingPackageRoot = LanguageSupportFactory.isInterestingProject(project.getProject())
                && root.getRawClasspathEntry().getEntryKind() != IClasspathEntry.CPE_SOURCE;
        // GROOVY end
        IClasspathEntry[] classpath = project.getResolvedClasspath();
        IResource[] members = folder.members();
        int length = members.length;
        if (length > 0) {
            String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true);
            String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
            nextResource: for (int i = 0; i < length; i++) {
                IResource member = members[i];
                switch (member.getType()) {
                case IResource.FILE:
                    String fileName = member.getName();

                    // ignore .java files that are not excluded
                    // GROOVY start
                    /* old {
                     if (Util.isValidCompilationUnitName(fileName, sourceLevel, complianceLevel) && !Util.isExcluded(member, inclusionPatterns, exclusionPatterns))
                    } new */
                    if ((Util.isValidCompilationUnitName(fileName, sourceLevel, complianceLevel)
                            && !Util.isExcluded(member, inclusionPatterns, exclusionPatterns)) &&
                    // we want to show groovy scripts that are coming from class folders
                            !(isInterestingPackageRoot
                                    && LanguageSupportFactory.isInterestingSourceFile(fileName)))
                        // GROOVY end
                        continue nextResource;
                    // ignore .class files
                    if (Util.isValidClassFileName(fileName, sourceLevel, complianceLevel))
                        continue nextResource;
                    // ignore .zip or .jar file on classpath
                    if (isClasspathEntry(member.getFullPath(), classpath))
                        continue nextResource;
                    break;

                case IResource.FOLDER:
                    // ignore valid packages or excluded folders that correspond to a nested pkg fragment root
                    if (Util.isValidFolderNameForPackage(member.getName(), sourceLevel, complianceLevel)
                            && (!Util.isExcluded(member, inclusionPatterns, exclusionPatterns)
                                    || isClasspathEntry(member.getFullPath(), classpath)))
                        continue nextResource;
                    break;
                }
                if (nonJavaResources.length == nonJavaResourcesCounter) {
                    // resize
                    System.arraycopy(nonJavaResources, 0,
                            (nonJavaResources = new IResource[nonJavaResourcesCounter * 2]), 0,
                            nonJavaResourcesCounter);
                }
                nonJavaResources[nonJavaResourcesCounter++] = member;
            }
        }
        if (ExternalFoldersManager.isInternalPathForExternalFolder(folder.getFullPath())) {
            IJarEntryResource[] jarEntryResources = new IJarEntryResource[nonJavaResourcesCounter];
            for (int i = 0; i < nonJavaResourcesCounter; i++) {
                jarEntryResources[i] = new NonJavaResource(root, nonJavaResources[i]);
            }
            return jarEntryResources;
        } else if (nonJavaResources.length != nonJavaResourcesCounter) {
            System.arraycopy(nonJavaResources, 0, (nonJavaResources = new IResource[nonJavaResourcesCounter]),
                    0, nonJavaResourcesCounter);
        }
        return nonJavaResources;
    } catch (CoreException e) {
        throw new JavaModelException(e);
    }
}