Search in sources :

Example 6 with AllFileSelector

use of org.apache.commons.vfs2.AllFileSelector 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 AllFileSelector

use of org.apache.commons.vfs2.AllFileSelector 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 AllFileSelector

use of org.apache.commons.vfs2.AllFileSelector in project spoofax by metaborg.

the class LanguageSpecBuilder method generateSourcesBuilderInput.

private GenerateSourcesBuilder.Input generateSourcesBuilderInput(LanguageSpecBuildInput input) throws FileSystemException, MetaborgException {
    final ISpoofaxLanguageSpec languageSpec = input.languageSpec();
    final ISpoofaxLanguageSpecConfig config = languageSpec.config();
    final FileObject baseLoc = input.languageSpec().location();
    final SpoofaxLangSpecCommonPaths paths = new SpoofaxLangSpecCommonPaths(baseLoc);
    final FileObject buildInfoLoc = paths.plutoBuildInfoDir();
    final SpoofaxContext context = new SpoofaxContext(baseLoc, buildInfoLoc);
    // SDF
    final Boolean sdfEnabled = config.sdfEnabled();
    final String sdfModule = config.sdfName();
    final FileObject sdfFileCandidate;
    final SdfVersion sdfVersion = config.sdfVersion();
    final Sdf2tableVersion sdf2tableVersion = config.sdf2tableVersion();
    switch(sdfVersion) {
        case sdf2:
            final Iterable<FileObject> sdfRoots = languagePathService.sourcePaths(input.project(), SpoofaxConstants.LANG_SDF_NAME);
            sdfFileCandidate = paths.findSyntaxMainFile(sdfRoots, sdfModule);
            break;
        case sdf3:
            sdfFileCandidate = paths.syntaxSrcGenMainFile(sdfModule);
            break;
        default:
            throw new MetaborgException("Unknown SDF version: " + sdfVersion);
    }
    @Nullable final File sdfFile;
    if (sdfFileCandidate != null && sdfFileCandidate.exists()) {
        sdfFile = resourceService.localPath(sdfFileCandidate);
    } else {
        sdfFile = null;
    }
    @Nullable final File sdfExternalDef;
    final String sdfExternalDefStr = config.sdfExternalDef();
    if (sdfExternalDefStr != null) {
        final FileObject sdfExternalDefLoc = resourceService.resolve(sdfExternalDefStr);
        if (!sdfExternalDefLoc.exists()) {
            throw new MetaborgException("External SDF definition at " + sdfExternalDefLoc + " does not exist");
        }
        sdfExternalDef = resourceService.localFile(sdfExternalDefLoc);
    } else {
        sdfExternalDef = null;
    }
    final Iterable<FileObject> sdfIncludePaths = languagePathService.sourceAndIncludePaths(languageSpec, SpoofaxConstants.LANG_SDF_NAME);
    final FileObject packSdfIncludesReplicateDir = paths.replicateDir().resolveFile("pack-sdf-includes");
    packSdfIncludesReplicateDir.delete(new AllFileSelector());
    final List<File> packSdfIncludePaths = Lists.newArrayList();
    for (FileObject path : sdfIncludePaths) {
        if (!path.exists()) {
            continue;
        }
        packSdfIncludePaths.add(resourceService.localFile(path, packSdfIncludesReplicateDir));
    }
    final Arguments packSdfArgs = config.sdfArgs();
    // SDF completions
    final String sdfCompletionModule = config.sdfName() + "-completion-insertions";
    @Nullable final File sdfCompletionFile;
    FileObject sdfCompletionFileCandidate = null;
    if (sdf2tableVersion == Sdf2tableVersion.c) {
        sdfCompletionFileCandidate = paths.syntaxCompletionMainFile(sdfCompletionModule);
    } else if (sdf2tableVersion == Sdf2tableVersion.java || sdf2tableVersion == Sdf2tableVersion.dynamic) {
        sdfCompletionFileCandidate = paths.syntaxCompletionMainFileNormalized(sdfCompletionModule);
    }
    boolean dataDependent = config.dataDependent();
    if (sdfCompletionFileCandidate != null && sdfCompletionFileCandidate.exists()) {
        sdfCompletionFile = resourceService.localPath(sdfCompletionFileCandidate);
    } else {
        sdfCompletionFile = null;
    }
    // Meta-SDF
    final Iterable<FileObject> sdfRoots = languagePathService.sourcePaths(input.project(), SpoofaxConstants.LANG_SDF_NAME);
    final String sdfMetaModule = config.metaSdfName();
    final FileObject sdfMetaFileCandidate = paths.findSyntaxMainFile(sdfRoots, sdfMetaModule);
    @Nullable final File sdfMetaFile;
    if (sdfMetaFileCandidate != null && sdfMetaFileCandidate.exists()) {
        sdfMetaFile = resourceService.localPath(sdfMetaFileCandidate);
    } else {
        sdfMetaFile = null;
    }
    // Stratego
    final String strModule = config.strategoName();
    final Iterable<FileObject> strRoots = languagePathService.sourcePaths(input.project(), SpoofaxConstants.LANG_STRATEGO_NAME);
    final FileObject strFileCandidate = paths.findStrMainFile(strRoots, strModule);
    @Nullable final File strFile;
    if (strFileCandidate != null && strFileCandidate.exists()) {
        strFile = resourceService.localPath(strFileCandidate);
    } else {
        strFile = null;
    }
    final String strStratPkg = paths.strJavaTransPkg(config.identifier().id);
    final String strJavaStratPkg = paths.strJavaStratPkg(config.identifier().id);
    final FileObject strJavaStratFileCandidate = paths.strMainJavaStratFile(config.identifier().id);
    @Nullable final File strJavaStratFile;
    if (strJavaStratFileCandidate.exists()) {
        strJavaStratFile = resourceService.localPath(strJavaStratFileCandidate);
    } else {
        strJavaStratFile = null;
    }
    final StrategoFormat strFormat = config.strFormat();
    @Nullable final File strExternalJar;
    final String strExternalJarStr = config.strExternalJar();
    if (strExternalJarStr != null) {
        final FileObject strExternalJarLoc = resourceService.resolve(strExternalJarStr);
        if (!strExternalJarLoc.exists()) {
            throw new MetaborgException("External Stratego JAR at " + strExternalJarLoc + " does not exist");
        }
        strExternalJar = resourceService.localFile(strExternalJarLoc);
    } else {
        strExternalJar = null;
    }
    final String strExternalJarFlags = config.strExternalJarFlags();
    final Iterable<FileObject> strIncludePaths = languagePathService.sourceAndIncludePaths(languageSpec, SpoofaxConstants.LANG_STRATEGO_NAME);
    final FileObject strjIncludesReplicateDir = paths.replicateDir().resolveFile("strj-includes");
    strjIncludesReplicateDir.delete(new AllFileSelector());
    final List<File> strjIncludeDirs = Lists.newArrayList();
    final List<File> strjIncludeFiles = Lists.newArrayList();
    for (FileObject path : strIncludePaths) {
        if (!path.exists()) {
            continue;
        }
        if (path.isFolder()) {
            strjIncludeDirs.add(resourceService.localFile(path, strjIncludesReplicateDir));
        }
        if (path.isFile()) {
            strjIncludeFiles.add(resourceService.localFile(path, strjIncludesReplicateDir));
        }
    }
    final Arguments strjArgs = config.strArgs();
    return new GenerateSourcesBuilder.Input(context, config.identifier().id, config.sourceDeps(), sdfEnabled, sdfModule, sdfFile, sdfVersion, sdf2tableVersion, dataDependent, sdfExternalDef, packSdfIncludePaths, packSdfArgs, sdfCompletionModule, sdfCompletionFile, sdfMetaModule, sdfMetaFile, strFile, strStratPkg, strJavaStratPkg, strJavaStratFile, strFormat, strExternalJar, strExternalJarFlags, strjIncludeDirs, strjIncludeFiles, strjArgs);
}
Also used : StrategoFormat(org.metaborg.spoofax.meta.core.config.StrategoFormat) ISpoofaxLanguageSpecConfig(org.metaborg.spoofax.meta.core.config.ISpoofaxLanguageSpecConfig) MetaborgException(org.metaborg.core.MetaborgException) Arguments(org.metaborg.util.cmd.Arguments) BuildInput(org.metaborg.core.build.BuildInput) ISpoofaxLanguageSpec(org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpec) SdfVersion(org.metaborg.spoofax.meta.core.config.SdfVersion) SpoofaxContext(org.metaborg.spoofax.meta.core.pluto.SpoofaxContext) AllFileSelector(org.apache.commons.vfs2.AllFileSelector) Sdf2tableVersion(org.metaborg.core.config.Sdf2tableVersion) FileObject(org.apache.commons.vfs2.FileObject) File(java.io.File) Nullable(javax.annotation.Nullable)

Example 9 with AllFileSelector

use of org.apache.commons.vfs2.AllFileSelector in project javautils by jiadongpo.

the class ZipfileUnpacker method unpack.

// @Override
public void unpack(final File outputDir) throws IOException {
    outputDir.mkdirs();
    final FileObject packFileObject = fileSystemManager.resolveFile(packLocation.toString());
    try {
        final FileObject zipFileSystem = fileSystemManager.createFileSystem(packFileObject);
        try {
            fileSystemManager.toFileObject(outputDir).copyFrom(zipFileSystem, new AllFileSelector());
        } finally {
            zipFileSystem.close();
        }
    } finally {
        packFileObject.close();
    }
}
Also used : AllFileSelector(org.apache.commons.vfs2.AllFileSelector) FileObject(org.apache.commons.vfs2.FileObject)

Example 10 with AllFileSelector

use of org.apache.commons.vfs2.AllFileSelector in project javautils by jiadongpo.

the class ZipfileUnpacker method unpack.

// @Override
public void unpack(final File outputDir) throws IOException {
    outputDir.mkdirs();
    final FileObject packFileObject = fileSystemManager.resolveFile(packLocation.toString());
    try {
        final FileObject zipFileSystem = fileSystemManager.createFileSystem(packFileObject);
        try {
            fileSystemManager.toFileObject(outputDir).copyFrom(zipFileSystem, new AllFileSelector());
        } finally {
            zipFileSystem.close();
        }
    } finally {
        packFileObject.close();
    }
}
Also used : AllFileSelector(org.apache.commons.vfs2.AllFileSelector) FileObject(org.apache.commons.vfs2.FileObject)

Aggregations

AllFileSelector (org.apache.commons.vfs2.AllFileSelector)18 FileObject (org.apache.commons.vfs2.FileObject)18 IOException (java.io.IOException)9 FileSelectInfo (org.apache.commons.vfs2.FileSelectInfo)9 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)7 KettleException (org.pentaho.di.core.exception.KettleException)7 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)7 FileSystemException (org.apache.commons.vfs2.FileSystemException)4 File (java.io.File)3 JSchException (com.jcraft.jsch.JSchException)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Nullable (javax.annotation.Nullable)1 URIx (maspack.fileutil.uri.URIx)1 FileSystemManager (org.apache.commons.vfs2.FileSystemManager)1