Search in sources :

Example 6 with FileSelectInfo

use of org.apache.commons.vfs2.FileSelectInfo 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;
        }
        // 
        if (!Utils.isEmpty(onemask) || !Utils.isEmpty(excludeonemask)) {
            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 {
                        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.
                    }
                }
            } catch (Exception e) {
                if (onerequired) {
                    fileInputList.addNonAccessibleFile(new NonAccessibleFileObject(onefile));
                }
                log.logError(Const.getStackTracker(e));
            }
        } else {
            try {
                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 : IOException(java.io.IOException) FileSelectInfo(org.apache.commons.vfs2.FileSelectInfo) IOException(java.io.IOException) AllFileSelector(org.apache.commons.vfs2.AllFileSelector) FileObject(org.apache.commons.vfs2.FileObject)

Example 7 with FileSelectInfo

use of org.apache.commons.vfs2.FileSelectInfo in project pentaho-kettle by pentaho.

the class FileInputList method createFolderList.

public static FileInputList createFolderList(VariableSpace space, String[] folderName, String[] folderRequired) {
    FileInputList fileInputList = new FileInputList();
    // Replace possible environment variables...
    final String[] realfolder = space.environmentSubstitute(folderName);
    for (int i = 0; i < realfolder.length; i++) {
        final String onefile = realfolder[i];
        final boolean onerequired = YES.equalsIgnoreCase(folderRequired[i]);
        final boolean subdirs = true;
        final FileTypeFilter filter = FileTypeFilter.ONLY_FOLDERS;
        if (Utils.isEmpty(onefile)) {
            continue;
        }
        FileObject directoryFileObject = null;
        try {
            // Find all folder names in this directory
            // 
            directoryFileObject = KettleVFS.getFileObject(onefile, space);
            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())) {
                                return true;
                            }
                            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++) {
                        if (fileObjects[j].exists()) {
                            fileInputList.addFile(fileObjects[j]);
                        }
                    }
                }
                if (Utils.isEmpty(fileObjects)) {
                    if (onerequired) {
                        fileInputList.addNonAccessibleFile(directoryFileObject);
                    }
                }
                // Sort the list: quicksort, only for regular files
                fileInputList.sortFiles();
            } else {
                if (onerequired && !directoryFileObject.exists()) {
                    fileInputList.addNonExistantFile(directoryFileObject);
                }
            }
        } catch (Exception e) {
            log.logError(Const.getStackTracker(e));
        } finally {
            try {
                if (directoryFileObject != null) {
                    directoryFileObject.close();
                }
                directoryFileObject = null;
            } catch (Exception e) {
            // Ignore
            }
        }
    }
    return fileInputList;
}
Also used : IOException(java.io.IOException) FileSelectInfo(org.apache.commons.vfs2.FileSelectInfo) IOException(java.io.IOException) AllFileSelector(org.apache.commons.vfs2.AllFileSelector) FileObject(org.apache.commons.vfs2.FileObject)

Example 8 with FileSelectInfo

use of org.apache.commons.vfs2.FileSelectInfo in project pentaho-kettle by pentaho.

the class KettleFileRepositoryIT method verifyJobSamples.

private void verifyJobSamples(RepositoryDirectoryInterface samplesDirectory) throws Exception {
    FileObject jobSamplesFolder = KettleVFS.getFileObject("samples/jobs/");
    FileObject[] files = jobSamplesFolder.findFiles(new FileSelector() {

        @Override
        public boolean traverseDescendents(FileSelectInfo arg0) throws Exception {
            return true;
        }

        @Override
        public boolean includeFile(FileSelectInfo info) throws Exception {
            return info.getFile().getName().getExtension().equalsIgnoreCase("kjb");
        }
    });
    List<FileObject> filesList = Arrays.asList(files);
    Collections.sort(filesList, new Comparator<FileObject>() {

        @Override
        public int compare(FileObject o1, FileObject o2) {
            return o1.getName().getPath().compareTo(o2.getName().getPath());
        }
    });
    for (FileObject file : filesList) {
        String jobFilename = file.getName().getPath();
        System.out.println("Storing/Loading/validating job '" + jobFilename + "'");
        // Load the JobMeta object...
        // 
        JobMeta jobMeta = new JobMeta(jobFilename, repository);
        jobMeta.setFilename(null);
        // The name is sometimes empty in the file, duplicates are present too...
        // Replaces slashes and the like as well...
        // 
        jobMeta.setName(Const.createName(file.getName().getBaseName()));
        jobMeta.setName(jobMeta.getName().replace('/', '-'));
        if (Utils.isEmpty(jobMeta.getName())) {
            jobMeta.setName(Const.createName(file.getName().getBaseName()));
        }
        if (jobMeta.getName().contains("/")) {
            jobMeta.setName(jobMeta.getName().replace('/', '-'));
        }
        // Save it in the repository in the samples folder
        // 
        jobMeta.setRepositoryDirectory(samplesDirectory);
        repository.save(jobMeta, "unit testing");
        assertNotNull(jobMeta.getObjectId());
        // Load it back up again...
        // 
        JobMeta repJobMeta = repository.loadJob(jobMeta.getObjectId(), null);
        String oneXml = repJobMeta.getXML();
        // Save & load it again
        // 
        repository.save(jobMeta, "unit testing");
        repJobMeta = repository.loadJob(jobMeta.getObjectId(), null);
        String twoXml = repJobMeta.getXML();
        // The XML needs to be identical after loading
        // 
        // storeFile(oneXml, "/tmp/one.ktr");
        // storeFile(twoXml, "/tmp/two.ktr");
        // 
        assertEquals(oneXml, twoXml);
    }
    // Verify the number of stored files, see if we can find them all again.
    // 
    System.out.println("Stored " + files.length + " job samples in folder " + samplesDirectory.getPath());
    String[] jobNames = repository.getJobNames(samplesDirectory.getObjectId(), false);
    assertEquals(files.length, jobNames.length);
}
Also used : JobMeta(org.pentaho.di.job.JobMeta) FileSelector(org.apache.commons.vfs2.FileSelector) FileObject(org.apache.commons.vfs2.FileObject) FileSelectInfo(org.apache.commons.vfs2.FileSelectInfo) KettleException(org.pentaho.di.core.exception.KettleException)

Example 9 with FileSelectInfo

use of org.apache.commons.vfs2.FileSelectInfo in project pentaho-metaverse by pentaho.

the class VfsDateRangeFilter method includeFile.

@Override
public boolean includeFile(FileSelectInfo fileInfo) {
    boolean result = super.includeFile(fileInfo);
    try {
        if (fileInfo.getFile().getType() == FileType.FOLDER) {
            Date folderDate = format.parse(fileInfo.getFile().getName().getBaseName());
            // assume a match on start & end dates
            int startCompare = 0;
            int endCompare = 0;
            // it is a valid date, now, is it greater than or equal to the requested date?
            if (startingDate != null) {
                startCompare = folderDate.compareTo(startingDate);
            }
            if (endingDate != null) {
                endCompare = folderDate.compareTo(endingDate);
            }
            return startCompare >= 0 && endCompare <= 0 && result;
        } else {
            return false;
        }
    } catch (ParseException | FileSystemException e) {
        // folder name is not a valid date string, reject it
        return false;
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) ParseException(java.text.ParseException) Date(java.util.Date)

Example 10 with FileSelectInfo

use of org.apache.commons.vfs2.FileSelectInfo in project pentaho-metaverse by pentaho.

the class VfsDateRangeFilterTest method testAccept_startDateSet.

@Test
public void testAccept_startDateSet() throws Exception {
    filter = new VfsDateRangeFilter(format, start);
    FileSelectInfo fsi = mock(FileSelectInfo.class);
    FileObject fo = mock(FileObject.class);
    FileName fn = mock(FileName.class);
    when(fn.getBaseName()).thenReturn(end);
    when(fo.getType()).thenReturn(FileType.FOLDER);
    when(fo.getName()).thenReturn(fn);
    when(fsi.getFile()).thenReturn(fo);
    when(fsi.getDepth()).thenReturn(1);
    assertTrue(filter.includeFile(fsi));
    when(fn.getBaseName()).thenReturn(start);
    assertTrue("Start date is not inclusive", filter.includeFile(fsi));
    when(fn.getBaseName()).thenReturn("20000101");
    assertFalse("Before start date was accepted", filter.includeFile(fsi));
}
Also used : FileName(org.apache.commons.vfs2.FileName) FileObject(org.apache.commons.vfs2.FileObject) FileSelectInfo(org.apache.commons.vfs2.FileSelectInfo) Test(org.junit.Test)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)20 FileSelectInfo (org.apache.commons.vfs2.FileSelectInfo)19 IOException (java.io.IOException)10 AllFileSelector (org.apache.commons.vfs2.AllFileSelector)9 KettleException (org.pentaho.di.core.exception.KettleException)9 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)9 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)7 FileName (org.apache.commons.vfs2.FileName)5 FileSelector (org.apache.commons.vfs2.FileSelector)5 Test (org.junit.Test)5 FileSystemException (org.apache.commons.vfs2.FileSystemException)3 JobMeta (org.pentaho.di.job.JobMeta)2 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1