public static ResourceList forResources(final Pattern pattern) { return new ResourceList(pattern); }
/** * Find a list of resources matching a given path on the classpath. for all * elements of java.class.path get a Collection of resources Pattern pattern * = Pattern.compile(".*"); gets all resources * * @return the resources in the order they are found */ public Collection<String> list() { final ArrayList<String> resources = new ArrayList<String>(); resources.addAll(systemPropertiesClasspathElements()); ClassLoader classLoader = getClass().getClassLoader(); if (classLoader instanceof URLClassLoader) { addResourcesFromUrlClassLoader(resources, (URLClassLoader) classLoader); } return resources; }
private Collection<String> systemPropertiesClasspathElements() { final ArrayList<String> resources = new ArrayList<>(); final String classPath = System.getProperty("java.class.path", "."); final String[] classPathElements = classPath.split(PATH_SEPARATOR); for (final String element : classPathElements) { resources.addAll(getResources(element, pattern)); } return resources; }
private Collection<String> getResources(final String element, final Pattern pattern) { final ArrayList<String> resources = new ArrayList<>(); final File file = new File(element); if (isAJarFile(file)) { resources.addAll(getResourcesFromJarFile(file, pattern)); } else { resources.addAll(getResourcesFromDirectory(file, pattern)); } return removeUnnecessaryFilesFrom(resources); }
private void addResourcesFromUrlClassLoader(ArrayList<String> resources, URLClassLoader classLoader) { URL[] classPathElements = classLoader.getURLs(); for(URL classPathElement : classPathElements) { if (isASerenityResourceJarFile(classPathElement.getFile())) { resources.addAll(getResources(classPathElement.getFile(), pattern)); } } }
/** * Resources (stylesheets, images) etc are all stored in the * src/main/resources/reports directory. When the jar is deployed, they will * end up on the classpath. */ public void copyHTMLResourcesTo(final File targetDirectory) throws IOException { Pattern resourcePattern = allFilesInDirectory(resourceDirectory); FileResources fileResource = FileResources.from(resourceDirectory); Collection<String> reportResources = ResourceList.forResources(resourcePattern).list(); for (String resourcePath : reportResources) { if (fileResourceFromAJar(resourcePath)) { fileResource.copyResourceTo(resourcePath, targetDirectory); } else if (fileResourceFromPath(resourcePath)) { fileResource.copyResourceTo(resourcePath, targetDirectory); } } }
private Collection<String> getResourcesFromDirectory(final File directory, final Pattern pattern) { final ArrayList<String> retval = new ArrayList<>(); final File[] fileList = directory.listFiles(); if (fileList != null) { for (final File file : fileList) { if (file.isDirectory() && (file.exists())) { retval.addAll(getResourcesFromDirectory(file, pattern)); } else { String fileName = ""; if (file.exists()) { try { fileName = file.getCanonicalPath(); final boolean accept = pattern.matcher(fileName).matches(); if (accept) { retval.add(fileName); } } catch (final IOException e) { throw new ResourceCopyingError("Could not read from the JAR file " + fileName , e); } } } } } return retval; }
private Collection<String> getResourcesFromJarFile(final File file, final Pattern pattern) { final ArrayList<String> matchingResources = new ArrayList<>(); try{ if (file.exists()) { try (ZipFile zf = zipFileFor(file)) { @SuppressWarnings("rawtypes") final Enumeration e = zf.entries(); while (e.hasMoreElements()) { final ZipEntry entry = (ZipEntry) e.nextElement(); final String fileName = entry.getName(); if (resourceDirectory.isEmpty() || fileName.contains(resourceDirectory)) { if (pattern.matcher(fileName).matches()) { matchingResources.add(fileName); } } } } } } catch (IOException e) { throw new ResourceCopyingError("Couldn't close the zip file " + file, e); } return matchingResources; }
private Collection<String> getResources(final String element, final Pattern pattern) { final ArrayList<String> resources = new ArrayList<String>(); final File file = new File(element); if (isAJarFile(file)) { resources.addAll(getResourcesFromJarFile(file, pattern)); } else { resources.addAll(getResourcesFromDirectory(file, pattern)); } return removeUnnecessaryFilesFrom(resources); }
/** * Resources (stylesheets, images) etc are all stored in the * src/main/resources/reports directory. When the jar is deployed, they will * end up on the classpath. */ public void copyHTMLResourcesTo(final File targetDirectory) throws IOException { Pattern resourcePattern = allFilesInDirectory(resourceDirectory); FileResources fileResource = FileResources.from(resourceDirectory); Collection<String> reportResources = ResourceList.forResources(resourcePattern).list(); for (String resourcePath : reportResources) { if (fileResourceFromAJar(resourcePath)) { fileResource.copyResourceTo(resourcePath, targetDirectory); } else if (fileResourceFromPath(resourcePath)) { fileResource.copyResourceTo(resourcePath, targetDirectory); } } }
private Collection<String> getResourcesFromDirectory(final File directory, final Pattern pattern) { final ArrayList<String> retval = new ArrayList<String>(); final File[] fileList = directory.listFiles(); if (fileList != null) { for (final File file : fileList) { if (file.isDirectory() && (file.exists())) { retval.addAll(getResourcesFromDirectory(file, pattern)); } else { if (file.exists()) { try { final String fileName = file.getCanonicalPath(); final boolean accept = pattern.matcher(fileName).matches(); if (accept) { retval.add(fileName); } } catch (final IOException e) { throw new ResourceCopyingError("Could not read from the JAR file", e); } } } } } return retval; }
private Collection<String> getResourcesFromJarFile(final File file, final Pattern pattern) { final ArrayList<String> retval = new ArrayList<String>(); if (file.exists()) { ZipFile zf; try { zf = zipFileFor(file); } catch (final IOException e) { throw new ResourceCopyingError("Could not read from the JAR file", e); } @SuppressWarnings("rawtypes") final Enumeration e = zf.entries(); while (e.hasMoreElements()) { final ZipEntry ze = (ZipEntry) e.nextElement(); final String fileName = ze.getName(); final boolean accept = pattern.matcher(fileName).matches(); if (accept) { retval.add(fileName); } } try { zf.close(); } catch (final IOException e1) { throw new ResourceCopyingError("Couldn't close the zip file", e1); } } return retval; }
private Collection<String> getResources(final String element, final Pattern pattern) { final ArrayList<String> resources = new ArrayList<String>(); final File file = new File(element); if (isAJarFile(file)) { resources.addAll(getResourcesFromJarFile(file, pattern)); } else { resources.addAll(getResourcesFromDirectory(file, pattern)); } return removeUnnecessaryFilesFrom(resources); }
/** * Find a list of resources matching a given path on the classpath. for all * elements of java.class.path get a Collection of resources Pattern pattern * = Pattern.compile(".*"); gets all resources * * @return the resources in the order they are found */ public Collection<String> list() { final ArrayList<String> resources = new ArrayList<>(); resources.addAll(systemPropertiesClasspathElements()); ClassLoader classLoader = getClass().getClassLoader(); if (classLoader instanceof URLClassLoader) { addResourcesFromUrlClassLoader(resources, (URLClassLoader) classLoader); } return resources; }
public Collection<String> systemPropertiesClasspathElements() { final ArrayList<String> resources = new ArrayList<String>(); final String classPath = System.getProperty("java.class.path", "."); final String[] classPathElements = classPath.split(PATH_SEPARATOR); for (final String element : classPathElements) { resources.addAll(getResources(element, pattern)); } return resources; }
public static ResourceList forResources(final String resourceDirectory, final Pattern pattern) { return new ResourceList(resourceDirectory, pattern); }
private Collection<String> getResourcesFromDirectory(final File directory, final Pattern pattern) { final ArrayList<String> retval = new ArrayList<String>(); final File[] fileList = directory.listFiles(); if (fileList != null) { for (final File file : fileList) { if (file.isDirectory() && (file.exists())) { retval.addAll(getResourcesFromDirectory(file, pattern)); } else { if (file.exists()) { try { final String fileName = file.getCanonicalPath(); final boolean accept = pattern.matcher(fileName).matches(); if (accept) { retval.add(fileName); } } catch (final IOException e) { throw new ResourceCopyingError("Could not read from the JAR file", e); } } } } } return retval; }
private Collection<String> getResourcesFromJarFile(final File file, final Pattern pattern) { final ArrayList<String> retval = new ArrayList<String>(); if (file.exists()) { ZipFile zf; try { zf = zipFileFor(file); } catch (final IOException e) { throw new ResourceCopyingError("Could not read from the JAR file", e); } @SuppressWarnings("rawtypes") final Enumeration e = zf.entries(); while (e.hasMoreElements()) { final ZipEntry ze = (ZipEntry) e.nextElement(); final String fileName = ze.getName(); final boolean accept = pattern.matcher(fileName).matches(); if (accept) { retval.add(fileName); } } try { zf.close(); } catch (final IOException e1) { throw new ResourceCopyingError("Couldn't close the zip file", e1); } } return retval; }
/** * Find a list of resources matching a given path on the classpath. for all * elements of java.class.path get a Collection of resources Pattern pattern * = Pattern.compile(".*"); gets all resources * * @return the resources in the order they are found */ public Collection<String> list() { final ArrayList<String> resources = new ArrayList<String>(); resources.addAll(systemPropertiesClasspathElements()); ClassLoader classLoader = getClass().getClassLoader(); if (classLoader instanceof URLClassLoader) { addResourcesFromUrlClassLoader(resources, (URLClassLoader) classLoader); } return resources; }
private void addResourcesFromUrlClassLoader(ArrayList<String> resources, URLClassLoader classLoader) { URL[] classPathElements = classLoader.getURLs(); for(URL classPathElement : classPathElements) { resources.addAll(getResources(classPathElement.getFile(), pattern)); } }