All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softicar.platform.common.io.classpath.ClasspathFileFetcher Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.io.classpath;

import com.softicar.platform.common.io.classpath.file.IClasspathFile;
import com.softicar.platform.common.io.classpath.file.PlainClasspathFile;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * Retrieves {@link IClasspathFile} instances that reference files from the
 * given class paths (either directories or .jar archives), using the provided
 * filters.
 *
 * @author Alexander Schmidt
 */
public class ClasspathFileFetcher {

	private final Collection classpaths;
	private Collection extensionFilter;
	private Collection packageFilter;

	public ClasspathFileFetcher(Collection classpaths) {

		this.classpaths = classpaths;
		this.extensionFilter = Collections.emptyList();
		this.packageFilter = Collections.emptyList();
	}

	public ClasspathFileFetcher setExtensionFilter(Collection filter) {

		this.extensionFilter = ClasspathFileUtils.getNormalizedExtensionFilter(filter);
		return this;
	}

	public ClasspathFileFetcher setPackageFilter(Package filter) {

		return setPackageFilter(Collections.singleton(filter));
	}

	public ClasspathFileFetcher setPackageFilter(Collection filter) {

		this.packageFilter = Objects.requireNonNull(filter);
		return this;
	}

	public List getClasspathFiles() {

		List classpathFiles = new ArrayList<>();
		for (PlainClasspathFile file: new PlainClasspathFileFetcher().getClasspathFiles(classpaths)) {
			if (file.isJarFile()) {
				ZipClasspathFileFetcher fetcher = new ZipClasspathFileFetcher(file.getFile(), extensionFilter, packageFilter);
				classpathFiles.addAll(fetcher.getClasspathFiles());
			} else {
				if (isValidPackage(file) && isValidExtension(file)) {
					classpathFiles.add(file);
				}
			}
		}
		return classpathFiles;
	}

	private boolean isValidPackage(PlainClasspathFile classpathFile) {

		if (packageFilter.isEmpty()) {
			return true;
		} else {
			String filePathInsideClassPath = classpathFile.getFilePathInsideClassPath();
			if (filePathInsideClassPath != null) {
				for (String pathPrefix: ClasspathFileUtils.getRelativePaths(packageFilter)) {
					if (filePathInsideClassPath.startsWith(pathPrefix)) {
						return true;
					}
				}
			}
			return false;
		}
	}

	private boolean isValidExtension(PlainClasspathFile classpathFile) {

		return extensionFilter.isEmpty() || extensionFilter.contains(classpathFile.getExtension());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy