org.apache.commons.vfs2.FileSelector Java Examples

The following examples show how to use org.apache.commons.vfs2.FileSelector. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractFileObject.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Traverses the descendants of this file, and builds a list of selected files.
 *
 * @param selector The FileSelector.
 * @param depthwise if true files are added after their descendants, before otherwise.
 * @param selected A List of the located FileObjects.
 * @throws FileSystemException if an error occurs.
 */
@Override
public void findFiles(final FileSelector selector, final boolean depthwise, final List<FileObject> selected)
        throws FileSystemException {
    try {
        if (exists()) {
            // Traverse starting at this file
            final DefaultFileSelectorInfo info = new DefaultFileSelectorInfo();
            info.setBaseFolder(this);
            info.setDepth(0);
            info.setFile(this);
            traverse(info, selector, depthwise, selected);
        }
    } catch (final Exception e) {
        throw new FileSystemException("vfs.provider/find-files.error", fileName, e);
    }
}
 
Example #2
Source File: BuildInput.java    From spoofax with Apache License 2.0 6 votes vote down vote up
public BuildInput(BuildState state, IProject project, Iterable<ResourceChange> resourceChanges,
    Multimap<ILanguageImpl, FileObject> includePaths, BuildOrder buildOrder, @Nullable FileSelector parseSelector,
    boolean analyze, @Nullable FileSelector analyzeSelector, boolean transform,
    @Nullable FileSelector transformSelector, Iterable<ITransformGoal> transformGoals,
    @Nullable IMessagePrinter messagePrinter, boolean throwOnErrors, Set<ILanguageImpl> pardonedLanguages) {
    this.state = state;
    this.project = project;
    this.sourceChanges = resourceChanges;
    this.includePaths = includePaths;
    this.buildOrder = buildOrder;
    this.selector = parseSelector;
    this.analyze = analyze;
    this.analyzeSelector = analyzeSelector;
    this.transform = transform;
    this.transformSelector = transformSelector;
    this.transformGoals = transformGoals;
    this.messagePrinter = messagePrinter;
    this.throwOnErrors = throwOnErrors;
    this.pardonedLanguages = pardonedLanguages;
}
 
Example #3
Source File: AbstractFileSystem.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a temporary local copy of a file and its descendants.
 *
 * @param file The FileObject to replicate.
 * @param selector The FileSelector.
 * @return The replicated File.
 * @throws FileSystemException if an error occurs.
 */
@Override
public File replicateFile(final FileObject file, final FileSelector selector) throws FileSystemException {
    if (!FileObjectUtils.exists(file)) {
        throw new FileSystemException("vfs.provider/replicate-missing-file.error", file.getName());
    }

    try {
        return doReplicateFile(file, selector);
    } catch (final Exception e) {
        throw new FileSystemException("vfs.provider/replicate-file.error", file.getName(), e);
    }
}
 
Example #4
Source File: PrivilegedFileReplicator.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a local copy of the file, and all its descendants.
 *
 * @param srcFile The source FileObject.
 * @param selector The file selector.
 * @return The replicated file.
 * @throws FileSystemException if an error occurs.
 */
@Override
public File replicateFile(final FileObject srcFile, final FileSelector selector) throws FileSystemException {
    try {
        final ReplicateAction action = new ReplicateAction(srcFile, selector);
        return AccessController.doPrivileged(action);
    } catch (final PrivilegedActionException e) {
        throw new FileSystemException("vfs.impl/replicate-file.error", e, srcFile.getName());
    }
}
 
Example #5
Source File: SynchronizedFileObject.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
@Override
public void findFiles(final FileSelector selector, final boolean depthwise, final List<FileObject> selected)
        throws FileSystemException {
    synchronized (this) {
        super.findFiles(selector, depthwise, selected);
    }
}
 
Example #6
Source File: DefaultFileReplicator.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a local copy of the file, and all its descendants.
 *
 * @param srcFile The file to copy.
 * @param selector The FileSelector.
 * @return the created File.
 * @throws FileSystemException if an error occurs copying the file.
 */
@Override
public File replicateFile(final FileObject srcFile, final FileSelector selector) throws FileSystemException {
    final String basename = srcFile.getName().getBaseName();
    final File file = allocateFile(basename);

    // Copy from the source file
    final FileObject destFile = getContext().toFileObject(file);
    destFile.copyFrom(srcFile, selector);

    return file;
}
 
Example #7
Source File: AbstractFileObject.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Lists the set of matching descendants of this file, in depthwise order.
 *
 * @param selector The FileSelector.
 * @return list of files or null if the base file (this object) do not exist or the {@code selector} is null
 * @throws FileSystemException if an error occurs.
 */
public List<FileObject> listFiles(final FileSelector selector) throws FileSystemException {
    if (!exists() || selector == null) {
        return null;
    }

    final ArrayList<FileObject> list = new ArrayList<>();
    this.findFiles(selector, true, list);
    return list;
}
 
Example #8
Source File: AbstractFileObject.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes this file, and all children matching the {@code selector}.
 *
 * @param selector The FileSelector.
 * @return the number of deleted files.
 * @throws FileSystemException if an error occurs.
 */
@Override
public int delete(final FileSelector selector) throws FileSystemException {
    int nuofDeleted = 0;

    /*
     * VFS-210 if (getType() == FileType.IMAGINARY) { // File does not exist return nuofDeleted; }
     */

    // Locate all the files to delete
    final ArrayList<FileObject> files = new ArrayList<>();
    findFiles(selector, true, files);

    // Delete 'em
    final int count = files.size();
    for (int i = 0; i < count; i++) {
        final AbstractFileObject file = FileObjectUtils.getAbstractFileObject(files.get(i));
        // file.attach();

        // VFS-210: It seems impossible to me that findFiles will return a list with hidden files/directories
        // in it, else it would not be hidden. Checking for the file-type seems ok in this case
        // If the file is a folder, make sure all its children have been deleted
        if (file.getType().hasChildren() && file.getChildren().length != 0) {
            // Skip - as the selector forced us not to delete all files
            continue;
        }

        // Delete the file
        if (file.deleteSelf()) {
            nuofDeleted++;
        }
    }

    return nuofDeleted;
}
 
Example #9
Source File: AbstractFileObject.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Copies another file to this file.
 *
 * @param file The FileObject to copy.
 * @param selector The FileSelector.
 * @throws FileSystemException if an error occurs.
 */
@Override
public void copyFrom(final FileObject file, final FileSelector selector) throws FileSystemException {
    if (!FileObjectUtils.exists(file)) {
        throw new FileSystemException("vfs.provider/copy-missing-file.error", file);
    }

    // Locate the files to copy across
    final ArrayList<FileObject> files = new ArrayList<>();
    file.findFiles(selector, false, files);

    // Copy everything across
    for (final FileObject srcFile : files) {
        // Determine the destination file
        final String relPath = file.getName().getRelativeName(srcFile.getName());
        final FileObject destFile = resolveFile(relPath, NameScope.DESCENDENT_OR_SELF);

        // Clean up the destination file, if necessary
        if (FileObjectUtils.exists(destFile) && destFile.getType() != srcFile.getType()) {
            // The destination file exists, and is not of the same type,
            // so delete it
            // TODO - add a pluggable policy for deleting and overwriting existing files
            destFile.deleteAll();
        }

        // Copy across
        try {
            if (srcFile.getType().hasContent()) {
                FileObjectUtils.writeContent(srcFile, destFile);
            } else if (srcFile.getType().hasChildren()) {
                destFile.createFolder();
            }
        } catch (final IOException e) {
            throw new FileSystemException("vfs.provider/copy-file.error", e, srcFile, destFile);
        }
    }
}
 
Example #10
Source File: AbstractFileObject.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Traverses a file.
 */
private static void traverse(final DefaultFileSelectorInfo fileInfo, final FileSelector selector,
        final boolean depthwise, final List<FileObject> selected) throws Exception {
    // Check the file itself
    final FileObject file = fileInfo.getFile();
    final int index = selected.size();

    // If the file is a folder, traverse it
    if (file.getType().hasChildren() && selector.traverseDescendents(fileInfo)) {
        final int curDepth = fileInfo.getDepth();
        fileInfo.setDepth(curDepth + 1);

        // Traverse the children
        final FileObject[] children = file.getChildren();
        for (final FileObject child : children) {
            fileInfo.setFile(child);
            traverse(fileInfo, selector, depthwise, selected);
        }

        fileInfo.setFile(file);
        fileInfo.setDepth(curDepth);
    }

    // Add the file if doing depthwise traversal
    if (selector.includeFile(fileInfo)) {
        if (depthwise) {
            // Add this file after its descendants
            selected.add(file);
        } else {
            // Add this file before its descendants
            selected.add(index, file);
        }
    }
}
 
Example #11
Source File: LocalFileSystem.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a temporary local copy of a file and its descendants.
 */
@Override
protected File doReplicateFile(final FileObject fileObject, final FileSelector selector) throws Exception {
    final LocalFile localFile = (LocalFile) FileObjectUtils.getAbstractFileObject(fileObject);
    final File file = localFile.getLocalFile();
    final SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        final FilePermission requiredPerm = new FilePermission(file.getAbsolutePath(), "read");
        sm.checkPermission(requiredPerm);
    }
    return file;
}
 
Example #12
Source File: ResourceUtils.java    From spoofax with Apache License 2.0 5 votes vote down vote up
public static Iterable<FileObject> find(FileObject base, FileSelector selector) throws FileSystemException {
    final FileObject[] files = base.findFiles(selector);
    if(files == null) {
        return Iterables2.empty();
    }
    return Iterables2.from(files);
}
 
Example #13
Source File: Builder.java    From spoofax with Apache License 2.0 5 votes vote down vote up
private void identifyResources(Iterable<ResourceChange> changes, BuildInput input,
    Multimap<ILanguageImpl, IdentifiedResourceChange> identifiedChanges, ICancel cancel)
    throws InterruptedException {
    final Iterable<ILanguageImpl> languages = input.buildOrder.languages();
    final FileSelector selector = input.selector;
    final FileObject location = input.project.location();

    for(ResourceChange change : changes) {
        cancel.throwIfCancelled();
        final FileObject resource = change.resource;
        if(selector != null) {
            try {
                if(!FileSelectorUtils.include(selector, resource, location)) {
                    continue;
                }
            } catch(FileSystemException e) {
                logger.error("Error determining if {} should be ignored from the build, including it", e, resource);
            }
        }

        final IdentifiedResource identifiedResource = languageIdentifier.identifyToResource(resource, languages);
        if(identifiedResource != null) {
            final IdentifiedResourceChange identifiedChange =
                new IdentifiedResourceChange(change, identifiedResource);
            identifiedChanges.put(identifiedChange.language, identifiedChange);
        }
    }
}
 
Example #14
Source File: FileObject.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Override
public void copyFrom(org.apache.commons.vfs2.FileObject srcFile, FileSelector selector) {
    try {
        this.fileObject.copyFrom(srcFile, selector);
    } catch (FileSystemException e) {
        throw new VFSFileSystemException(e);
    }
}
 
Example #15
Source File: FileObject.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Override
public int delete(FileSelector selector) {
    try {
        return this.fileObject.delete(selector);
    } catch (FileSystemException e) {
        throw new VFSFileSystemException(e);
    }
}
 
Example #16
Source File: FileObject.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Override
public void findFiles(FileSelector selector, boolean depthwise, List<org.apache.commons.vfs2.FileObject> selected) {
    try {
        this.fileObject.findFiles(selector, depthwise, selected);
    } catch (FileSystemException e) {
        throw new VFSFileSystemException(e);
    }
}
 
Example #17
Source File: FileObject.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Override
public FileObject[] findFiles(FileSelector selector) {
    try {
        return Lists.mutable.with(this.fileObject.findFiles(selector)).collect(new Function<org.apache.commons.vfs2.FileObject, FileObject>() {
            @Override
            public FileObject valueOf(org.apache.commons.vfs2.FileObject fileObject1) {
                return toDaFileObject(fileObject1);
            }
        })
                .toArray(new FileObject[0]);
    } catch (FileSystemException e) {
        throw new VFSFileSystemException(e);
    }
}
 
Example #18
Source File: BuildInputBuilder.java    From spoofax with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the transformation file selector to given selector.
 */
public BuildInputBuilder withTransformSelector(FileSelector transformSelector) {
    this.transformSelector = transformSelector;
    return this;
}
 
Example #19
Source File: CleanInput.java    From spoofax with Apache License 2.0 4 votes vote down vote up
public CleanInput(IProject project, Iterable<ILanguageImpl> languages, @Nullable FileSelector selector) {
    this.project = project;
    this.languages = languages;
    this.selector = selector;
}
 
Example #20
Source File: PrivilegedFileReplicator.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
public ReplicateAction(final FileObject srcFile, final FileSelector selector) {
    this.srcFile = srcFile;
    this.selector = selector;
}
 
Example #21
Source File: BuildInputBuilder.java    From spoofax with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the analysis file selector to given selector.
 */
public BuildInputBuilder withAnalyzeSelector(FileSelector analyzeSelector) {
    this.analyzeSelector = analyzeSelector;
    return this;
}
 
Example #22
Source File: KettleFileRepositoryIT.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
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 );
}
 
Example #23
Source File: ConnectionFileObject.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override public void copyFrom( FileObject file, FileSelector selector ) throws FileSystemException {
  resolvedFileObject.copyFrom( file, selector );
}
 
Example #24
Source File: ConnectionFileObject.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override public int delete( FileSelector selector ) throws FileSystemException {
  return resolvedFileObject.delete( selector );
}
 
Example #25
Source File: NonAccessibleFileObject.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override
public void copyFrom( FileObject arg0, FileSelector arg1 ) throws FileSystemException {
  throw new NotImplementedException();
}
 
Example #26
Source File: NonAccessibleFileObject.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override
public int delete( FileSelector arg0 ) throws FileSystemException {
  return 0;
}
 
Example #27
Source File: NonAccessibleFileObject.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override
public FileObject[] findFiles( FileSelector arg0 ) throws FileSystemException {
  throw new NotImplementedException();
}
 
Example #28
Source File: BuildInputBuilder.java    From spoofax with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the file selector to given selector.
 */
public BuildInputBuilder withSelector(FileSelector selector) {
    this.selector = selector;
    return this;
}
 
Example #29
Source File: NonAccessibleFileObject.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override
public void findFiles( FileSelector arg0, boolean arg1, @SuppressWarnings( "rawtypes" ) List arg2 ) throws FileSystemException {
  throw new NotImplementedException();
}
 
Example #30
Source File: CleanInputBuilder.java    From spoofax with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the file selector to given selector.
 */
public CleanInputBuilder withSelector(FileSelector selector) {
    this.selector = selector;
    return this;
}