/** @return A list of all resources (including classfiles and non-classfiles) found in whitelisted packages. */ public ResourceList getAllResources() { if (allWhitelistedResources == null || allWhitelistedResources.isEmpty()) { return new ResourceList(1); } else { return allWhitelistedResources; } }
/** * @param resourcePath * A complete resource path, relative to the classpath entry package root. * @return A list of all resources found in whitelisted packages that have the given path, relative to the * package root of the classpath element. May match several resources, up to one per classpath element. */ public ResourceList getResourcesWithPath(final String resourcePath) { if (closed.get()) { throw new IllegalArgumentException("Cannot use a ScanResult after it has been closed"); } if (allWhitelistedResources == null || allWhitelistedResources.isEmpty()) { return new ResourceList(1); } else { final String path = FileUtils.sanitizeEntryPath(resourcePath, /* removeInitialSlash = */ true); final ResourceList resourceList = pathToWhitelistedResourceList.get(path); return (resourceList == null ? new ResourceList(1) : resourceList); } }
/** * @return This {@link ResourceList} as a map from path (obtained from {@link Resource#getPath()}), to a * {@link ResourceList} of {@link Resource} objects that have that path. */ public Map<String, ResourceList> asMap() { final Map<String, ResourceList> pathToResourceList = new HashMap<>(); for (final Resource resource : this) { final String path = resource.getPath(); ResourceList resourceList = pathToResourceList.get(path); if (resourceList == null) { resourceList = new ResourceList(1); pathToResourceList.put(path, resourceList); } resourceList.add(resource); } return pathToResourceList; }
/** * @param leafName * A resource leaf filename. * @return A list of all resources found in whitelisted packages that have the requested leafname. */ public ResourceList getResourcesWithLeafName(final String leafName) { if (closed.get()) { throw new IllegalArgumentException("Cannot use a ScanResult after it has been closed"); } if (allWhitelistedResources == null || allWhitelistedResources.isEmpty()) { return new ResourceList(1); } else { final ResourceList filteredResources = new ResourceList(); for (final Resource classpathResource : allWhitelistedResources) { final String relativePath = classpathResource.getPath(); final int lastSlashIdx = relativePath.lastIndexOf('/'); if (relativePath.substring(lastSlashIdx + 1).equals(leafName)) { filteredResources.add(classpathResource); } } return filteredResources; } }
/** * @param pattern * A pattern to match {@link Resource} paths with. * @return A list of all resources found in whitelisted packages that have a path matching the requested * pattern. */ public ResourceList getResourcesMatchingPattern(final Pattern pattern) { if (closed.get()) { throw new IllegalArgumentException("Cannot use a ScanResult after it has been closed"); } if (allWhitelistedResources == null || allWhitelistedResources.isEmpty()) { return new ResourceList(1); } else { final ResourceList filteredResources = new ResourceList(); for (final Resource classpathResource : allWhitelistedResources) { final String relativePath = classpathResource.getPath(); if (pattern.matcher(relativePath).matches()) { filteredResources.add(classpathResource); } } return filteredResources; } }
/** * @param extension * A filename extension, e.g. "xml" to match all resources ending in ".xml". * @return A list of all resources found in whitelisted packages that have the requested filename extension. */ public ResourceList getResourcesWithExtension(final String extension) { if (closed.get()) { throw new IllegalArgumentException("Cannot use a ScanResult after it has been closed"); } if (allWhitelistedResources == null || allWhitelistedResources.isEmpty()) { return new ResourceList(1); } else { String bareExtension = extension; while (bareExtension.startsWith(".")) { bareExtension = bareExtension.substring(1); } final ResourceList filteredResources = new ResourceList(); for (final Resource classpathResource : allWhitelistedResources) { final String relativePath = classpathResource.getPath(); final int lastSlashIdx = relativePath.lastIndexOf('/'); final int lastDotIdx = relativePath.lastIndexOf('.'); if (lastDotIdx > lastSlashIdx) { if (relativePath.substring(lastDotIdx + 1).equalsIgnoreCase(bareExtension)) { filteredResources.add(classpathResource); } } } return filteredResources; } }
/** * Find the subset of the {@link Resource} objects in this list for which the given filter predicate is true. * * @param filter * The {@link ResourceFilter} to apply. * @return The subset of the {@link Resource} objects in this list for which the given filter predicate is true. */ public ResourceList filter(final ResourceFilter filter) { final ResourceList resourcesFiltered = new ResourceList(); for (final Resource resource : this) { if (filter.accept(resource)) { resourcesFiltered.add(resource); } } return resourcesFiltered; }
/** * @param resourcePath * A complete resource path, relative to the classpath entry package root. * @return A list of all resources found in any classpath element, <i>whether in whitelisted packages or not<i>, * that have the given path, relative to the package root of the classpath element. (Resources will not * be returned if their path is blacklisted.) May match several resources, up to one per classpath * element. */ public ResourceList getResourcesWithPathIgnoringWhitelist(final String resourcePath) { if (closed.get()) { throw new IllegalArgumentException("Cannot use a ScanResult after it has been closed"); } final String path = FileUtils.sanitizeEntryPath(resourcePath, /* removeInitialSlash = */ true); final ResourceList matchingResources = new ResourceList(); for (final ClasspathElement classpathElt : classpathOrder) { final Resource matchingResource = classpathElt.getResource(path); if (matchingResource != null) { matchingResources.add(matchingResource); } } return matchingResources; }
if (classpathElt.whitelistedResources != null) { if (allWhitelistedResources == null) { allWhitelistedResources = new ResourceList(); pathToWhitelistedResourceList = new HashMap<>(); ResourceList resourceList = pathToWhitelistedResourceList.get(path); if (resourceList == null) { pathToWhitelistedResourceList.put(path, resourceList = new ResourceList());