Java Code Examples for org.apache.commons.vfs2.FileObject#isFolder()

The following examples show how to use org.apache.commons.vfs2.FileObject#isFolder() . 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: HopVfsFileDialog.java    From hop with Apache License 2.0 6 votes vote down vote up
/**
 * Double clicked on a file or folder
 *
 * @param event
 */
private void fileDefaultSelected( Event event ) {
  FileObject fileObject = getSelectedFileObject();
  if ( fileObject == null ) {
    return;
  }

  try {
    navigateTo( HopVfs.getFilename( fileObject ), true );

    if ( fileObject.isFolder() ) {
      // Browse into the selected folder...
      //
      refreshBrowser();
    } else {
      // Take this file as the user choice for this dialog
      //
      okButton();
    }
  } catch ( Throwable e ) {
    showError( "Error handling default selection on file " + fileObject.toString(), e );
  }

}
 
Example 2
Source File: StorageObject.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/** 删除内容,同时判断上一级目录(只判断一级)是否为空,为空则删除上一级目录 */
public void deleteContent(StorageMapping mapping) throws Exception {
	FileSystemManager manager = this.getFileSystemManager();
	String prefix = this.getPrefix(mapping);
	String path = this.path();
	FileSystemOptions options = this.getOptions(mapping);
	try (FileObject fo = manager.resolveFile(prefix + PATHSEPARATOR + path, options)) {
		if (fo.exists() && fo.isFile()) {
			fo.delete();
			if ((!StringUtils.startsWith(path, PATHSEPARATOR)) && (StringUtils.contains(path, PATHSEPARATOR))) {
				FileObject parent = fo.getParent();
				if ((null != parent) && parent.exists() && parent.isFolder()) {
					if (parent.getChildren().length == 0) {
						parent.delete();
					}
				}
			}
		}
		manager.closeFileSystem(fo.getFileSystem());
	}
}
 
Example 3
Source File: VFSNotebookRepo.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
private Map<String, NoteInfo> listFolder(FileObject fileObject) throws IOException {
  Map<String, NoteInfo> noteInfos = new HashMap<>();
  if (fileObject.isFolder()) {
    if (fileObject.getName().getBaseName().startsWith(".")) {
      LOGGER.warn("Skip hidden folder: " + fileObject.getName().getPath());
      return noteInfos;
    }
    for (FileObject child : fileObject.getChildren()) {
      noteInfos.putAll(listFolder(child));
    }
  } else {
    // getPath() method returns a string without root directory in windows, so we use getURI() instead
    // windows does not support paths with "file:///" prepended. so we replace it by "/"
    String noteFileName = fileObject.getName().getURI().replace("file:///", "/");
    if (noteFileName.endsWith(".zpln")) {
      try {
        String noteId = getNoteId(noteFileName);
        String notePath = getNotePath(rootNotebookFolder, noteFileName);
        noteInfos.put(noteId, new NoteInfo(noteId, notePath));
      } catch (IOException e) {
        LOGGER.warn(e.getMessage());
      }
    }
  }
  return noteInfos;
}
 
Example 4
Source File: FileObjectContentLocation.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Lists all content entities stored in this content-location. This method filters out all files that have an invalid
 * name (according to the repository rules).
 *
 * @return the content entities for this location.
 * @throws ContentIOException if an repository error occured.
 */
public ContentEntity[] listContents() throws ContentIOException {
  try {
    final FileObject file = getBackend();
    final FileObject[] files = file.getChildren();
    final ContentEntity[] entities = new ContentEntity[files.length];
    for ( int i = 0; i < files.length; i++ ) {
      final FileObject child = files[i];
      if ( RepositoryUtilities.isInvalidPathName( child.getPublicURIString() ) ) {
        continue;
      }

      if ( child.isFolder() ) {
        entities[i] = new FileObjectContentLocation( this, child );
      } else if ( child.isFile() ) {
        entities[i] = new FileObjectContentLocation( this, child );
      }
    }
    return entities;
  } catch ( FileSystemException e ) {
    throw new RuntimeException( e );
  }
}
 
Example 5
Source File: FileObjectContentLocation.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the content entity with the given name. If the entity does not exist, an Exception will be raised.
 *
 * @param name the name of the entity to be retrieved.
 * @return the content entity for this name, never null.
 * @throws ContentIOException if an repository error occured.
 */
public ContentEntity getEntry( final String name ) throws ContentIOException {
  try {
    if ( RepositoryUtilities.isInvalidPathName( name ) ) {
      throw new IllegalArgumentException( "The name given is not valid." );
    }

    final FileObject file = getBackend();
    final FileObject child = file.resolveFile( name );
    if ( child.exists() == false ) {
      throw new ContentIOException( "Not found:" + child );
    }

    if ( child.isFolder() ) {
      return new FileObjectContentLocation( this, child );
    } else if ( child.isFile() ) {
      return new FileObjectContentItem( this, child );
    } else {
      throw new ContentIOException( "Not File nor directory." );
    }
  } catch ( FileSystemException e ) {
    throw new RuntimeException( e );
  }
}
 
Example 6
Source File: LanguageComponentFactory.java    From spoofax with Apache License 2.0 6 votes vote down vote up
@Override public IComponentCreationConfigRequest requestFromDirectory(FileObject directory) throws MetaborgException {
    try {
        if(!directory.exists()) {
            throw new MetaborgException(
                logger.format("Cannot request component creation from directory {}, it does not exist", directory));
        }
        if(!directory.isFolder()) {
            throw new MetaborgException(
                logger.format("Cannot request component creation from {}, it is not a directory", directory));
        }

        if(!componentConfigService.available(directory)) {
            throw new MetaborgException(logger.format(
                "Cannot request component creation from directory {}, there is no component config file inside the directory",
                directory));
        }

        return request(directory);
    } catch(IOException e) {
        throw new MetaborgException(
            logger.format("Cannot request component creation from directory {}, unexpected I/O error", directory),
            e);
    }
}
 
Example 7
Source File: LanguageComponentFactory.java    From spoofax with Apache License 2.0 6 votes vote down vote up
@Override public Collection<IComponentCreationConfigRequest> requestAllInDirectory(FileObject directory)
    throws MetaborgException {
    final Set<IComponentCreationConfigRequest> requests = Sets.newHashSet();
    try {
        if(!directory.exists()) {
            throw new MetaborgException("Cannot scan directory " + directory + ", it does not exist");
        }
        if(!directory.isFolder()) {
            throw new MetaborgException("Cannot scan " + directory + ", it is not a directory");
        }

        final Iterable<FileObject> files = ResourceUtils.find(directory, new LanguageFileScanSelector());
        for(FileObject file : files) {
            final IComponentCreationConfigRequest request;
            if(file.isFolder()) {
                request = requestFromDirectory(file);
            } else {
                request = requestFromArchive(file);
            }
            requests.add(request);
        }
    } catch(FileSystemException e) {
        throw new MetaborgException("Cannot scan " + directory + ", unexpected I/O error", e);
    }
    return requests;
}
 
Example 8
Source File: HopVfsFileDialog.java    From hop with Apache License 2.0 5 votes vote down vote up
private void showFilename( FileObject fileObject ) {
  try {
    wFilename.setText( HopVfs.getFilename( fileObject ) );

    FileContent content = fileObject.getContent();

    String details = "";

    if ( fileObject.isFolder() ) {
      details += "Folder: " + HopVfs.getFilename( fileObject ) + Const.CR;
    } else {
      details += "Name: " + fileObject.getName().getBaseName() + "   ";
      details += "Folder: " + HopVfs.getFilename( fileObject.getParent() ) + "   ";
      details += "Size: " + content.getSize();
      if ( content.getSize() >= 1024 ) {
        details += " (" + getFileSize( fileObject ) + ")";
      }
      details += Const.CR;
    }
    details += "Last modified: " + getFileDate( fileObject ) + Const.CR;
    details += "Readable: " + (fileObject.isReadable()?"Yes":"No") + "  ";
    details += "Writable: " + (fileObject.isWriteable()?"Yes":"No") + "  ";
    details += "Executable: " + (fileObject.isExecutable()?"Yes":"No") + Const.CR;
    if ( fileObject.isSymbolicLink() ) {
      details += "This is a symbolic link" + Const.CR;
    }
    Map<String, Object> attributes = content.getAttributes();
    if ( attributes != null && !attributes.isEmpty() ) {
      details += "Attributes: " + Const.CR;
      for ( String key : attributes.keySet() ) {
        Object value = attributes.get( key );
        details += "   " + key + " : " + ( value == null ? "" : value.toString() ) + Const.CR;
      }
    }
    showDetails( details );
  } catch ( Throwable e ) {
    showError( "Error getting information on file " + fileObject.toString(), e );
  }
}
 
Example 9
Source File: FileObjectContentLocation.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new location for the given parent and directory.
 *
 * @param parent  the parent location.
 * @param backend the backend.
 * @throws ContentIOException if an error occured or the file did not point to a directory.
 */
public FileObjectContentLocation( final ContentLocation parent, final FileObject backend ) throws ContentIOException {
  super( parent, backend );
  boolean error;
  try {
    error = backend.exists() == false || backend.isFolder() == false;
  } catch ( FileSystemException e ) {
    throw new RuntimeException( e );
  }
  if ( error ) {
    throw new ContentIOException( "The given backend-file is not a directory." );
  }
}
 
Example 10
Source File: FileObjectContentLocation.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new root-location for the given repository and directory.
 *
 * @param repository the repository for which a location should be created.
 * @param backend    the backend.
 * @throws ContentIOException if an error occured or the file did not point to a directory.
 */
public FileObjectContentLocation( final Repository repository, final FileObject backend ) throws ContentIOException {
  super( repository, backend );
  boolean error;
  try {
    error = backend.exists() == false || backend.isFolder() == false;
  } catch ( FileSystemException e ) {
    throw new RuntimeException( e );
  }
  if ( error ) {
    throw new ContentIOException( "The given backend-file is not a directory." );
  }
}
 
Example 11
Source File: ConfigBasedProjectService.java    From spoofax with Apache License 2.0 5 votes vote down vote up
private IProject findProject(FileObject resource) {
    try {
        FileObject dir = (resource.isFolder() ? resource : resource.getParent());
        while(dir != null) {
            FileName name = dir.getName();
            if(projectConfigService.available(dir)) {
                final ConfigRequest<? extends IProjectConfig> configRequest = projectConfigService.get(dir);
                if(!configRequest.valid()) {
                    logger.error("Errors occurred when retrieving project configuration from project directory {}", dir);
                    configRequest.reportErrors(new StreamMessagePrinter(sourceTextService, false, false, logger));
                }

                final IProjectConfig config = configRequest.config();
                if(config == null) {
                    logger.error("Could not retrieve project configuration from project directory {}", dir);
                    return null;
                }

                final IProject project = new Project(dir, config);
                IProject prevProject;
                if((prevProject = projects.putIfAbsent(name, project)) != null) {
                    logger.warn("Project with location {} already exists", name);
                    return prevProject;
                }
                return project;
            }
            dir = dir.getParent();
        }
    } catch(FileSystemException e) {
        logger.error("Error while searching for project configuration.",e);
    }
    logger.warn("No project configuration file was found for {}.",resource);
    return null;
}
 
Example 12
Source File: CLIUtils.java    From spoofax with Apache License 2.0 5 votes vote down vote up
/** Load a language from the directory or archive indicated by location */
public ILanguageImpl loadLanguage(FileObject location) throws MetaborgException {
    try {
        if(location.isFolder()) {
            return spoofax.languageDiscoveryService.languageFromDirectory(location);
        } else if(location.isFile()) {
            return spoofax.languageDiscoveryService.languageFromArchive(location);
        } else {
            throw new MetaborgException("Cannot load language from location with type " + location.getType());
        }
    } catch(FileSystemException ex) {
        throw new MetaborgException(ex);
    }
}
 
Example 13
Source File: LanguageComponentFactory.java    From spoofax with Apache License 2.0 5 votes vote down vote up
@Override public IComponentCreationConfigRequest requestFromArchive(FileObject archiveFile) throws MetaborgException {
    try {
        if(!archiveFile.exists()) {
            throw new MetaborgException(logger
                .format("Cannot request component creation from archive file {}, it does not exist", archiveFile));
        }
        if(!archiveFile.isFile()) {
            throw new MetaborgException(logger
                .format("Cannot request component creation from archive file {}, it is not a file", archiveFile));
        }

        final String archiveFileUri = archiveFile.getName().getURI();
        final FileObject archiveContents = resourceService.resolve("zip:" + archiveFileUri + "!/");
        if(!archiveContents.exists() || !archiveContents.isFolder()) {
            throw new MetaborgException(logger.format(
                "Cannot request component creation from archive file {}, it is not a zip archive", archiveFile));
        }

        if(!componentConfigService.available(archiveContents)) {
            throw new MetaborgException(logger.format(
                "Cannot request component creation from archive file {}, there is no component config file inside the archive",
                archiveFile));
        }

        return request(archiveContents);
    } catch(IOException e) {
        throw new MetaborgException(logger.format(
            "Cannot request component creation from archive file {}, unexpected I/O error", archiveFile), e);
    }
}