Search in sources :

Example 1 with CompressedFileFileObject

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;
}
Also used : CompressedFileFileObject(org.apache.commons.vfs2.provider.compressed.CompressedFileFileObject) IOException(java.io.IOException) FileSelectInfo(org.apache.commons.vfs2.FileSelectInfo) FileSystemException(org.apache.commons.vfs2.FileSystemException) IOException(java.io.IOException) AllFileSelector(org.apache.commons.vfs2.AllFileSelector) CompressedFileFileObject(org.apache.commons.vfs2.provider.compressed.CompressedFileFileObject) FileObject(org.apache.commons.vfs2.FileObject)

Aggregations

IOException (java.io.IOException)1 AllFileSelector (org.apache.commons.vfs2.AllFileSelector)1 FileObject (org.apache.commons.vfs2.FileObject)1 FileSelectInfo (org.apache.commons.vfs2.FileSelectInfo)1 FileSystemException (org.apache.commons.vfs2.FileSystemException)1 CompressedFileFileObject (org.apache.commons.vfs2.provider.compressed.CompressedFileFileObject)1