/** @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; } }
@Override public Enumeration<URL> getResources(final String path) throws IOException { final ResourceList resourceList = scanResult.getResourcesWithPath(path); if (resourceList == null || resourceList.isEmpty()) { return super.getResources(path); } else { return new Enumeration<URL>() { int idx = 0; @Override public boolean hasMoreElements() { return idx < resourceList.size(); } @Override public URL nextElement() { return resourceList.get(idx++).getURL(); } }; } }
@Override public URL getResource(final String path) { final ResourceList resourceList = scanResult.getResourcesWithPath(path); if (resourceList == null || resourceList.isEmpty()) { return super.getResource(path); } else { return resourceList.get(0).getURL(); } }
/** * @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); } }
/** * @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; } }
@Override public InputStream getResourceAsStream(final String path) { final ResourceList resourceList = scanResult.getResourcesWithPath(path); if (resourceList == null || resourceList.isEmpty()) { return super.getResourceAsStream(path); } else { try { return resourceList.get(0).open(); } catch (final IOException e) { return null; } } } }
/** * @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; } }