use of org.apache.commons.vfs2.provider.compressed.CompressedFileFileObject in project pentaho-kettle by pentaho.
the class FileInputList method createFileList.
public static FileInputList createFileList(VariableSpace space, String[] fileName, String[] fileMask, String[] excludeFileMask, String[] fileRequired, boolean[] includeSubdirs, FileTypeFilter[] fileTypeFilters) {
FileInputList fileInputList = new FileInputList();
// Replace possible environment variables...
final String[] realfile = space.environmentSubstitute(fileName);
final String[] realmask = space.environmentSubstitute(fileMask);
final String[] realExcludeMask = space.environmentSubstitute(excludeFileMask);
for (int i = 0; i < realfile.length; i++) {
final String onefile = realfile[i];
final String onemask = realmask[i];
final String excludeonemask = realExcludeMask[i];
final boolean onerequired = YES.equalsIgnoreCase(fileRequired[i]);
final boolean subdirs = includeSubdirs[i];
final FileTypeFilter filter = ((fileTypeFilters == null || fileTypeFilters[i] == null) ? FileTypeFilter.ONLY_FILES : fileTypeFilters[i]);
if (Utils.isEmpty(onefile)) {
continue;
}
try {
FileObject directoryFileObject = KettleVFS.getFileObject(onefile, space);
boolean processFolder = true;
if (onerequired) {
if (!directoryFileObject.exists()) {
// if we don't find folder..no need to continue
fileInputList.addNonExistantFile(directoryFileObject);
processFolder = false;
} else {
if (!directoryFileObject.isReadable()) {
fileInputList.addNonAccessibleFile(directoryFileObject);
processFolder = false;
}
}
}
//
if (processFolder) {
if (directoryFileObject != null && directoryFileObject.getType() == FileType.FOLDER) {
// it's a directory
FileObject[] fileObjects = directoryFileObject.findFiles(new AllFileSelector() {
@Override
public boolean traverseDescendents(FileSelectInfo info) {
return info.getDepth() == 0 || subdirs;
}
@Override
public boolean includeFile(FileSelectInfo info) {
// Never return the parent directory of a file list.
if (info.getDepth() == 0) {
return false;
}
FileObject fileObject = info.getFile();
try {
if (fileObject != null && filter.isFileTypeAllowed(fileObject.getType())) {
String name = info.getFile().getName().getBaseName();
boolean matches = true;
if (!Utils.isEmpty(onemask)) {
matches = Pattern.matches(onemask, name);
}
boolean excludematches = false;
if (!Utils.isEmpty(excludeonemask)) {
excludematches = Pattern.matches(excludeonemask, name);
}
return (matches && !excludematches);
}
return false;
} catch (IOException ex) {
// Upon error don't process the file.
return false;
}
}
});
if (fileObjects != null) {
for (int j = 0; j < fileObjects.length; j++) {
FileObject fileObject = fileObjects[j];
if (fileObject.exists()) {
fileInputList.addFile(fileObject);
}
}
}
if (Utils.isEmpty(fileObjects)) {
if (onerequired) {
fileInputList.addNonAccessibleFile(directoryFileObject);
}
}
// Sort the list: quicksort, only for regular files
fileInputList.sortFiles();
} else if (directoryFileObject instanceof CompressedFileFileObject) {
FileObject[] children = directoryFileObject.getChildren();
for (int j = 0; j < children.length; j++) {
// See if the wildcard (regexp) matches...
String name = children[j].getName().getBaseName();
boolean matches = true;
if (!Utils.isEmpty(onemask)) {
matches = Pattern.matches(onemask, name);
}
boolean excludematches = false;
if (!Utils.isEmpty(excludeonemask)) {
excludematches = Pattern.matches(excludeonemask, name);
}
if (matches && !excludematches) {
fileInputList.addFile(children[j]);
}
}
// We don't sort here, keep the order of the files in the archive.
} else {
FileObject fileObject = KettleVFS.getFileObject(onefile, space);
if (fileObject.exists()) {
if (fileObject.isReadable()) {
fileInputList.addFile(fileObject);
} else {
if (onerequired) {
fileInputList.addNonAccessibleFile(fileObject);
}
}
} else {
if (onerequired) {
fileInputList.addNonExistantFile(fileObject);
}
}
}
}
} catch (Exception e) {
if (onerequired) {
fileInputList.addNonAccessibleFile(new NonAccessibleFileObject(onefile));
}
log.logError(Const.getStackTracker(e));
}
}
return fileInputList;
}
Aggregations