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

The following examples show how to use org.apache.commons.vfs2.FileObject#isFile() . 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
@GuiToolbarElement(
  root = BROWSER_TOOLBAR_PARENT_ID,
  id = BROWSER_ITEM_ID_NAVIGATE_UP,
  toolTip = "Navigate to the parent folder",
  image = "ui/images/9x9_arrow_up.svg"
)
public void navigateUp() {
  try {
    FileObject fileObject = HopVfs.getFileObject( wFilename.getText() );
    if ( fileObject.isFile() ) {
      fileObject = fileObject.getParent();
    }
    FileObject parent = fileObject.getParent();
    if ( parent != null ) {
      navigateTo( HopVfs.getFilename( parent ), true );
    }
  } catch ( Throwable e ) {
    showError( "Error navigating up: '" + activeFileObject.toString(), e );
  }
}
 
Example 2
Source File: PentahoAvroInputFormat.java    From pentaho-hadoop-shims with Apache License 2.0 6 votes vote down vote up
private DataFileStream<GenericRecord> createDataFileStream() throws Exception {
  DatumReader<GenericRecord> datumReader;
  if ( useFieldAsInputStream ) {
    datumReader = new GenericDatumReader<GenericRecord>();
    inputStream.reset();
    return new DataFileStream<GenericRecord>( inputStream, datumReader );
  }
  if ( schemaFileName != null && schemaFileName.length() > 0 ) {
    Schema schema = new Schema.Parser().parse( KettleVFS.getInputStream( schemaFileName, variableSpace ) );
    datumReader = new GenericDatumReader<GenericRecord>( schema );
  } else {
    datumReader = new GenericDatumReader<GenericRecord>();
  }
  FileObject fileObject = KettleVFS.getFileObject( fileName, variableSpace );
  if ( fileObject.isFile() ) {
    this.inputStream = fileObject.getContent().getInputStream();
    return new DataFileStream<>( inputStream, datumReader );
  } else {
    FileObject[] avroFiles = fileObject.findFiles( new FileExtensionSelector( "avro" ) );
    if ( !Utils.isEmpty( avroFiles ) ) {
      this.inputStream = avroFiles[ 0 ].getContent().getInputStream();
      return new DataFileStream<>( inputStream, datumReader );
    }
    return null;
  }
}
 
Example 3
Source File: PentahoAvroInputFormat.java    From pentaho-hadoop-shims with Apache License 2.0 6 votes vote down vote up
private DataFileStream<Object> createNestedDataFileStream() throws Exception {
  DatumReader<Object> datumReader;
  if ( useFieldAsInputStream ) {
    datumReader = new GenericDatumReader<Object>();
    inputStream.reset();
    return new DataFileStream<Object>( inputStream, datumReader );
  }
  if ( schemaFileName != null && schemaFileName.length() > 0 ) {
    Schema schema = new Schema.Parser().parse( KettleVFS.getInputStream( schemaFileName, variableSpace ) );
    datumReader = new GenericDatumReader<Object>( schema );
  } else {
    datumReader = new GenericDatumReader<Object>();
  }
  FileObject fileObject = KettleVFS.getFileObject( fileName, variableSpace );
  if ( fileObject.isFile() ) {
    this.inputStream = fileObject.getContent().getInputStream();
    return new DataFileStream<>( inputStream, datumReader );
  } else {
    FileObject[] avroFiles = fileObject.findFiles( new FileExtensionSelector( "avro" ) );
    if ( !Utils.isEmpty( avroFiles ) ) {
      this.inputStream = avroFiles[ 0 ].getContent().getInputStream();
      return new DataFileStream<>( inputStream, datumReader );
    }
    return null;
  }
}
 
Example 4
Source File: FileObjectResourceData.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public FileObjectResourceData( final ResourceKey key ) throws ResourceLoadingException {
  if ( key == null ) {
    throw new NullPointerException();
  }
  final FileObject fileObject = (FileObject) key.getIdentifier();
  try {
    if ( fileObject.exists() == false ) {
      throw new ResourceLoadingException( "File-handle given does not point to an existing file." );
    }
    if ( fileObject.isFile() == false ) {
      throw new ResourceLoadingException( "File-handle given does not point to a regular file." );
    }
    if ( fileObject.isReadable() == false ) {
      throw new ResourceLoadingException( "File '" + fileObject + "' is not readable." );
    }
  } catch ( FileSystemException fse ) {
    throw new ResourceLoadingException( "Unable to create FileObjectResourceData : ", fse );
  }

  this.key = key;
  this.fileObject = fileObject;
}
 
Example 5
Source File: FileTypeMap.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Find the scheme for the provider of a layered file system.
 * <p>
 * This will check the FileContentInfo or file extension.
 * </p>
 *
 * @return Scheme supporting the file type or null (if unknonw).
 */
public String getScheme(final FileObject file) throws FileSystemException {
    // Check the file's mime type for a match
    final FileContent content = file.getContent();
    final String mimeType = content.getContentInfo().getContentType();
    if (mimeType != null) {
        return mimeTypeMap.get(mimeType);
    }

    // no specific mime-type - if it is a file also check the extension
    if (!file.isFile()) {
        return null; // VFS-490 folders don't use extensions for mime-type
    }
    final String extension = file.getName().getExtension();
    return extensionMap.get(extension);
}
 
Example 6
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 7
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 8
Source File: HopVfsFileDialog.java    From hop with Apache License 2.0 5 votes vote down vote up
private void refreshStates() {
  // Navigation icons...
  //
  browserToolbarWidgets.enableToolbarItem( BROWSER_ITEM_ID_NAVIGATE_PREVIOUS, navigationIndex > 0 );
  browserToolbarWidgets.enableToolbarItem( BROWSER_ITEM_ID_NAVIGATE_NEXT, navigationIndex + 1 < navigationHistory.size() );

  // Up...
  //
  boolean canGoUp;
  try {
    FileObject fileObject = HopVfs.getFileObject( wFilename.getText() );
    if ( fileObject.isFile() ) {
      canGoUp = fileObject.getParent().getParent() != null;
    } else {
      canGoUp = fileObject.getParent() != null;
    }
  } catch ( Exception e ) {
    canGoUp = false;
  }
  browserToolbarWidgets.enableToolbarItem( BROWSER_ITEM_ID_NAVIGATE_UP, canGoUp );

  // Bookmarks...
  //
  boolean bookmarkSelected = getSelectedBookmark() != null;
  bookmarksToolbarWidgets.enableToolbarItem( BOOKMARKS_ITEM_ID_BOOKMARK_GOTO, bookmarkSelected );
  bookmarksToolbarWidgets.enableToolbarItem( BOOKMARKS_ITEM_ID_BOOKMARK_REMOVE, bookmarkSelected );

  wOk.setEnabled( StringUtils.isNotEmpty( wFilename.getText() ) );
}
 
Example 9
Source File: RamFileSystem.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Import the given file with the name relative to the given root
 *
 * @param fo
 * @param root
 * @throws FileSystemException
 */
void toRamFileObject(final FileObject fo, final FileObject root) throws FileSystemException {
    final RamFileObject memFo = (RamFileObject) this
            .resolveFile(fo.getName().getPath().substring(root.getName().getPath().length()));
    if (fo.getType().hasChildren()) {
        // Create Folder
        memFo.createFolder();
        // Import recursively
        final FileObject[] fos = fo.getChildren();
        for (final FileObject child : fos) {
            this.toRamFileObject(child, root);
        }
    } else if (fo.isFile()) {
        // Read bytes
        try {
            final InputStream is = fo.getContent().getInputStream();
            try {
                final OutputStream os = new BufferedOutputStream(memFo.getOutputStream(), BUFFER_SIZE);
                int i;
                while ((i = is.read()) != -1) {
                    os.write(i);
                }
                os.close();
            } finally {
                try {
                    is.close();
                } catch (final IOException ignored) {
                    /* ignore on close exception. */
                }
                // TODO: close os
            }
        } catch (final IOException e) {
            throw new FileSystemException(e.getClass().getName() + " " + e.getMessage());
        }
    } else {
        throw new FileSystemException("File is not a folder nor a file " + memFo);
    }
}
 
Example 10
Source File: DefaultURLStreamHandler.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
@Override
protected void parseURL(final URL u, final String spec, final int start, final int limit) {
    try {
        final FileObject old = context.resolveFile(u.toExternalForm(), fileSystemOptions);

        FileObject newURL;
        if (start > 0 && spec.charAt(start - 1) == ':') {
            newURL = context.resolveFile(old, spec, fileSystemOptions);
        } else {
            if (old.isFile() && old.getParent() != null) {
                // for files we have to resolve relative
                newURL = old.getParent().resolveFile(spec);
            } else {
                newURL = old.resolveFile(spec);
            }
        }

        final String url = newURL.getName().getURI();
        final StringBuilder filePart = new StringBuilder();
        final String protocolPart = UriParser.extractScheme(context.getFileSystemManager().getSchemes(), url, filePart);

        setURL(u, protocolPart, "", -1, null, null, filePart.toString(), null, null);
    } catch (final FileSystemException fse) {
        // This is rethrown to MalformedURLException in URL anyway
        throw new RuntimeException(fse.getMessage());
    }
}
 
Example 11
Source File: SingleFileProjectService.java    From spoofax with Apache License 2.0 5 votes vote down vote up
@Override public IProject get(FileObject resource) {
    FileObject rootFolder = resource;
    try {
        // project location should be a directory (otherwise building gave errors), so take parent dir (if possible)
        if(resource.isFile()) {
            rootFolder = resource.getParent();
        }
    } catch(FileSystemException e) {
        // ignore
    }
    final IProjectConfig config = projectConfigService.defaultConfig(rootFolder);
    return new Project(rootFolder, config);
}
 
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);
    }
}