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

The following examples show how to use org.apache.commons.vfs2.FileObject#getChildren() . 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: ProviderCacheStrategyTests.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Test the on_call strategy
 */
public void testOnCallCache() throws Exception {
    final FileObject scratchFolder = getWriteFolder();
    if (FileObjectUtils.isInstanceOf(getBaseFolder(), RamFileObject.class)
            || scratchFolder.getFileSystem() instanceof VirtualFileSystem) {
        // cant check ram filesystem as every manager holds its own ram filesystem data
        return;
    }

    scratchFolder.delete(Selectors.EXCLUDE_SELF);

    final DefaultFileSystemManager fs = createManager();
    fs.setCacheStrategy(CacheStrategy.ON_CALL);
    fs.init();
    final FileObject foBase2 = getBaseTestFolder(fs);

    final FileObject cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath());

    FileObject[] fos = cachedFolder.getChildren();
    assertContainsNot(fos, "file1.txt");

    scratchFolder.resolveFile("file1.txt").createFile();

    fos = cachedFolder.getChildren();
    assertContains(fos, "file1.txt");
}
 
Example 2
Source File: JobEntrySSH2PUT.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private List<FileObject> getFiles( String localfolder ) throws KettleFileException {
  try {
    List<FileObject> myFileList = new ArrayList<FileObject>();

    // Get all the files in the local directory...

    FileObject localFiles = KettleVFS.getFileObject( localfolder, this );
    FileObject[] children = localFiles.getChildren();
    if ( children != null ) {
      for ( int i = 0; i < children.length; i++ ) {
        // Get filename of file or directory
        if ( children[i].getType().equals( FileType.FILE ) ) {
          myFileList.add( children[i] );

        }
      } // end for
    }

    return myFileList;
  } catch ( IOException e ) {
    throw new KettleFileException( e );
  }

}
 
Example 3
Source File: ProviderReadTests.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Tests can perform operations on a folder while reading from a different files.
 */
public void testConcurrentReadFolder() throws Exception {
    final FileObject file = resolveFile1Txt();
    assertTrue(file.exists());
    final FileObject folder = getReadFolderDir1();
    assertTrue(folder.exists());

    // Start reading from the file
    final InputStream instr = file.getContent().getInputStream();
    try {
        // Do some operations
        folder.exists();
        folder.getType();
        folder.getChildren();
    } finally {
        instr.close();
    }
}
 
Example 4
Source File: ResourceAgent.java    From spoofax with Apache License 2.0 6 votes vote down vote up
@Override public String[] readdir(String fn) {
    try {
        final FileObject resource = resourceService.resolve(workingDir, fn);
        if(!resource.exists() || resource.getType() == FileType.FILE) {
            return new String[0];
        }
        final FileName name = resource.getName();
        final FileObject[] children = resource.getChildren();
        final String[] strings = new String[children.length];
        for(int i = 0; i < children.length; ++i) {
            final FileName absName = children[i].getName();
            strings[i] = name.getRelativeName(absName);
        }
        return strings;
    } catch(FileSystemException e) {
        throw new RuntimeException("Could not list contents of directory " + fn, e);
    }
}
 
Example 5
Source File: KettleFileRepository.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public RepositoryDirectoryInterface loadRepositoryDirectoryTree( RepositoryDirectoryInterface dir ) throws KettleException {
  try {
    String folderName = calcDirectoryName( dir );
    FileObject folder = KettleVFS.getFileObject( folderName );

    for ( FileObject child : folder.getChildren() ) {
      if ( child.getType().equals( FileType.FOLDER ) ) {
        if ( !child.isHidden() || !repositoryMeta.isHidingHiddenFiles() ) {
          if ( !".meta".equals( child.getName().getBaseName() ) ) {
            RepositoryDirectory subDir = new RepositoryDirectory( dir, child.getName().getBaseName() );
            subDir.setObjectId( new StringObjectId( calcObjectId( subDir ) ) );
            dir.addSubdirectory( subDir );

            loadRepositoryDirectoryTree( subDir );
          }
        }
      }
    }

    return dir;
  } catch ( Exception e ) {
    throw new KettleException( "Unable to load the directory tree from this file repository", e );
  }
}
 
Example 6
Source File: Shell.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Lists the children of a folder.
 */
private void listChildren(final FileObject dir, final boolean recursive, final String prefix)
        throws FileSystemException {
    final FileObject[] children = dir.getChildren();
    for (final FileObject child : children) {
        System.out.print(prefix);
        System.out.print(child.getName().getBaseName());
        if (child.getType() == FileType.FOLDER) {
            System.out.println("/");
            if (recursive) {
                listChildren(child, recursive, prefix + "    ");
            }
        } else {
            System.out.println();
        }
    }
}
 
Example 7
Source File: KettleFileRepository.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Override
public String[] getJobNames( ObjectId id_directory, boolean includeDeleted ) throws KettleException {
  try {
    List<String> list = new ArrayList<String>();

    RepositoryDirectoryInterface tree = loadRepositoryDirectoryTree();
    RepositoryDirectoryInterface directory = tree.findDirectory( id_directory );

    String folderName = calcDirectoryName( directory );
    FileObject folder = KettleVFS.getFileObject( folderName );

    for ( FileObject child : folder.getChildren() ) {
      if ( child.getType().equals( FileType.FILE ) ) {
        if ( !child.isHidden() || !repositoryMeta.isHidingHiddenFiles() ) {
          String name = child.getName().getBaseName();

          if ( name.endsWith( EXT_JOB ) ) {

            String jobName = name.substring( 0, name.length() - 4 );
            list.add( jobName );
          }
        }
      }
    }

    return list.toArray( new String[list.size()] );
  } catch ( Exception e ) {
    throw new KettleException(
      "Unable to get list of transformations names in folder with id : " + id_directory, e );
  }
}
 
Example 8
Source File: RepositoryTreeModel.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the child of <code>parent</code> at index <code>index</code> in the parent's child array.
 * <code>parent</code> must be a node previously obtained from this data source. This should not return
 * <code>null</code> if <code>index</code> is a valid index for <code>parent</code> (that is <code>index >= 0 && index
 * < getChildCount(parent</code>)).
 *
 * @param parent
 *          a node in the tree, obtained from this data source
 * @return the child of <code>parent</code> at index <code>index</code>
 */
public Object getChild( Object parent, final int index ) {
  if ( parent instanceof RepositoryTreeRoot ) {
    final RepositoryTreeRoot root1 = (RepositoryTreeRoot) parent;
    parent = root1.getRoot();
    if ( parent == null ) {
      return null;
    }
  }

  try {
    final FileObject parElement = (FileObject) parent;
    final FileObject[] children = parElement.getChildren();
    int count = 0;
    for ( int i = 0; i < children.length; i++ ) {
      final FileObject child = children[i];
      if ( isShowFoldersOnly() && child.getType() != FileType.FOLDER ) {
        continue;
      }
      if ( isShowHiddenFiles() == false && child.isHidden() ) {
        continue;
      }
      if ( child.getType() != FileType.FOLDER
          && PublishUtil.acceptFilter( filters, child.getName().getBaseName() ) == false ) {
        continue;
      }

      if ( count == index ) {
        return child;
      }

      count += 1;
    }
    return children[index];
  } catch ( FileSystemException fse ) {
    logger.debug( "Failed", fse );
    return null;
  }
}
 
Example 9
Source File: RepositoryTreeModel.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the number of children of <code>parent</code>. Returns 0 if the node is a leaf or if it has no children.
 * <code>parent</code> must be a node previously obtained from this data source.
 *
 * @param parent
 *          a node in the tree, obtained from this data source
 * @return the number of children of the node <code>parent</code>
 */
public int getChildCount( Object parent ) {
  if ( parent instanceof RepositoryTreeRoot ) {
    final RepositoryTreeRoot root1 = (RepositoryTreeRoot) parent;
    parent = root1.getRoot();
    if ( parent == null ) {
      return 0;
    }
  }
  try {
    final FileObject parElement = (FileObject) parent;
    if ( parElement.getType() != FileType.FOLDER ) {
      return 0;
    }

    final FileObject[] children = parElement.getChildren();
    int count = 0;
    for ( int i = 0; i < children.length; i++ ) {
      final FileObject child = children[i];
      if ( isShowFoldersOnly() && child.getType() != FileType.FOLDER ) {
        continue;
      }
      if ( isShowHiddenFiles() == false && child.isHidden() ) {
        continue;
      }
      if ( child.getType() != FileType.FOLDER
          && PublishUtil.acceptFilter( filters, child.getName().getBaseName() ) == false ) {
        continue;
      }

      count += 1;
    }
    return count;
  } catch ( FileSystemException fse ) {
    logger.debug( "Failed", fse );
    return 0;
  }
}
 
Example 10
Source File: KettleFileRepository.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Override
public String[] getTransformationNames( ObjectId id_directory, boolean includeDeleted ) throws KettleException {
  try {
    List<String> list = new ArrayList<String>();

    RepositoryDirectoryInterface tree = loadRepositoryDirectoryTree();
    RepositoryDirectoryInterface directory = tree.findDirectory( id_directory );

    String folderName = calcDirectoryName( directory );
    FileObject folder = KettleVFS.getFileObject( folderName );

    for ( FileObject child : folder.getChildren() ) {
      if ( child.getType().equals( FileType.FILE ) ) {
        if ( !child.isHidden() || !repositoryMeta.isHidingHiddenFiles() ) {
          String name = child.getName().getBaseName();

          if ( name.endsWith( EXT_TRANSFORMATION ) ) {

            String transName = name.substring( 0, name.length() - 4 );
            list.add( transName );
          }
        }
      }
    }

    return list.toArray( new String[list.size()] );
  } catch ( Exception e ) {
    throw new KettleException(
      "Unable to get list of transformations names in folder with id : " + id_directory, e );
  }
}
 
Example 11
Source File: ShowFileTask.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Logs the details of a file.
 */
private void showFile(final FileObject file, final String prefix) throws Exception {
    // Write details
    final StringBuilder msg = new StringBuilder(prefix);
    msg.append(file.getName().getBaseName());
    if (file.exists()) {
        msg.append(" (");
        msg.append(file.getType().getName());
        msg.append(")");
    } else {
        msg.append(" (unknown)");
    }
    log(msg.toString());

    if (file.exists()) {
        final String newPrefix = prefix + INDENT;
        if (file.getType().hasContent()) {
            try (final FileContent content = file.getContent()) {
                log(newPrefix + "Content-Length: " + content.getSize());
                log(newPrefix + "Last-Modified" + new Date(content.getLastModifiedTime()));
            }
            if (showContent) {
                log(newPrefix + "Content:");
                logContent(file, newPrefix);
            }
        }
        if (file.getType().hasChildren()) {
            final FileObject[] children = file.getChildren();
            for (final FileObject child : children) {
                if (recursive) {
                    showFile(child, newPrefix);
                } else {
                    log(newPrefix + child.getName().getBaseName());
                }
            }
        }
    }
}
 
Example 12
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 13
Source File: FtpFileObject.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
@Override
protected FileObject[] doListChildrenResolved() throws Exception {
    synchronized (getFileSystem()) {
        if (this.fileInfo != null && this.fileInfo.isSymbolicLink()) {
            final FileObject linkDest = getLinkDestination();
            // VFS-437: Try to avoid a recursion loop.
            if (this.isCircular(linkDest)) {
                return null;
            }
            return linkDest.getChildren();
        }
    }
    return null;
}
 
Example 14
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 15
Source File: ProviderCacheStrategyTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Test the on_resolve strategy
 */
public void testOnResolveCache() throws Exception {
    final FileObject scratchFolder = getWriteFolder();
    if (FileObjectUtils.isInstanceOf(getBaseFolder(), RamFileObject.class)
            || scratchFolder.getFileSystem() instanceof VirtualFileSystem) {
        // cant check ram filesystem as every manager holds its own ram filesystem data
        return;
    }

    scratchFolder.delete(Selectors.EXCLUDE_SELF);

    final DefaultFileSystemManager fs = createManager();
    fs.setCacheStrategy(CacheStrategy.ON_RESOLVE);
    fs.init();
    final FileObject foBase2 = getBaseTestFolder(fs);

    FileObject cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath());

    FileObject[] fos = cachedFolder.getChildren();
    assertContainsNot(fos, "file1.txt");

    scratchFolder.resolveFile("file1.txt").createFile();

    fos = cachedFolder.getChildren();
    assertContainsNot(fos, "file1.txt");

    cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath());
    fos = cachedFolder.getChildren();
    assertContains(fos, "file1.txt");
}
 
Example 16
Source File: DefaultFileMonitor.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively fires create events for all children if recursive descent is enabled. Otherwise the create event
 * is only fired for the initial FileObject.
 *
 * @param child The child to add.
 */
private void fireAllCreate(final FileObject child) {
    // Add listener so that it can be triggered
    if (this.fm.getFileListener() != null) {
        child.getFileSystem().addListener(child, this.fm.getFileListener());
    }

    ((AbstractFileSystem) child.getFileSystem()).fireFileCreated(child);

    // Remove it because a listener is added in the queueAddFile
    if (this.fm.getFileListener() != null) {
        child.getFileSystem().removeListener(child, this.fm.getFileListener());
    }

    this.fm.queueAddFile(child); // Add

    try {
        if (this.fm.isRecursive() && child.getType().hasChildren()) {
            final FileObject[] newChildren = child.getChildren();
            for (final FileObject element : newChildren) {
                fireAllCreate(element);
            }
        }
    } catch (final FileSystemException fse) {
        LOG.error(fse.getLocalizedMessage(), fse);
    }
}
 
Example 17
Source File: OldVFSNotebookRepo.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Override
public List<OldNoteInfo> list(AuthenticationInfo subject) throws IOException {
  FileObject rootDir = getRootDir();

  FileObject[] children = rootDir.getChildren();

  List<OldNoteInfo> infos = new LinkedList<>();
  for (FileObject f : children) {
    String fileName = f.getName().getBaseName();
    if (f.isHidden()
        || fileName.startsWith(".")
        || fileName.startsWith("#")
        || fileName.startsWith("~")) {
      // skip hidden, temporary files
      continue;
    }

    if (!isDirectory(f)) {
      // currently single note is saved like, [NOTE_ID]/note.json.
      // so it must be a directory
      continue;
    }

    OldNoteInfo info = null;

    try {
      info = getNoteInfo(f);
      if (info != null) {
        infos.add(info);
      }
    } catch (Exception e) {
      LOG.error("Can't read note " + f.getName().toString());
    }
  }

  return infos;
}
 
Example 18
Source File: KettleFileRepository.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Override
public List<RepositoryElementMetaInterface> getTransformationObjects( ObjectId idDirectory,
  boolean includeDeleted ) throws KettleException {

  try {
    List<RepositoryElementMetaInterface> list = new ArrayList<RepositoryElementMetaInterface>();

    RepositoryDirectoryInterface tree = loadRepositoryDirectoryTree();
    RepositoryDirectoryInterface directory = tree.findDirectory( idDirectory );

    String folderName = calcDirectoryName( directory );
    FileObject folder = KettleVFS.getFileObject( folderName );

    for ( FileObject child : folder.getChildren() ) {
      if ( child.getType().equals( FileType.FILE ) ) {
        if ( !child.isHidden() || !repositoryMeta.isHidingHiddenFiles() ) {

          String name = child.getName().getBaseName();

          if ( name.endsWith( EXT_TRANSFORMATION ) ) {

            String transName = name.substring( 0, name.length() - 4 );

            ObjectId id = new StringObjectId( calcObjectId( directory, transName, EXT_TRANSFORMATION ) );
            Date date = new Date( child.getContent().getLastModifiedTime() );
            list.add( new RepositoryObject(
              id, transName, directory, "-", date, RepositoryObjectType.TRANSFORMATION, "", false ) );
          }
        }
      }
    }

    return list;
  } catch ( Exception e ) {
    throw new KettleException( "Unable to get list of transformations in folder with id : " + idDirectory, e );
  }
}
 
Example 19
Source File: CustomRamProviderTest.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Test if listing files with known scheme prefix works.
 * <p>
 * This test is not RamProvider specific but it uses it as a simple test-bed.
 * Verifies VFS-741.
 */
@Test
public void testSchemePrefix() throws FileSystemException
{
    // use a :-prefix with a known scheme (unknown scheme works since VFS-398)
    final String KNOWN_SCHEME = manager.getSchemes()[0]; // typically "ram"

    // we test with this file name
    final String testDir = "/prefixtest/";
    final String testFileName = KNOWN_SCHEME + ":test:txt";
    final String expectedName = testDir + testFileName;

    final FileObject dir = prepareSpecialFile(testDir, testFileName);


    // verify we can list dir

    // if not it throws:
    // Caused by: org.apache.commons.vfs2.FileSystemException: Invalid descendent file name "ram:data:test.txt".
    //   at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveName
    //   at org.apache.commons.vfs2.provider.AbstractFileObject.getChildren
    //   at org.apache.commons.vfs2.provider.AbstractFileObject.traverse
    //   at org.apache.commons.vfs2.provider.AbstractFileObject.findFiles

    // test methods to get the child:
    final FileObject[] findFilesResult = dir.findFiles(new AllFileSelector()); // includes dir
    final FileObject[] getChildrenResult = dir.getChildren();
    final FileObject getChildResult = dir.getChild(testFileName);

    // validate findFiles returns expected result
    assertEquals("Unexpected result findFiles: " + Arrays.toString(findFilesResult), 2, findFilesResult.length);
    String resultName = findFilesResult[0].getName().getPathDecoded();
    assertEquals("findFiles Child name does not match", expectedName, resultName);
    assertEquals("Did findFiles but child was no file", FileType.FILE, findFilesResult[0].getType());

    // validate getChildren returns expected result
    assertEquals("Unexpected result getChildren: " + Arrays.toString(getChildrenResult), 1, getChildrenResult.length);
    resultName = getChildrenResult[0].getName().getPathDecoded();
    assertEquals("getChildren Child name does not match", expectedName, resultName);
    assertEquals("Did getChildren but child was no file", FileType.FILE, getChildrenResult[0].getType());

    // validate getChild returns expected child
    assertNotNull("Did not find direct child", getChildResult);
    resultName = getChildResult.getName().getPathDecoded();
    assertEquals("getChild name does not match", expectedName, resultName);
    assertEquals("getChild was no file", FileType.FILE, getChildResult.getType());
}
 
Example 20
Source File: ProviderReadTests.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Walks a folder structure, asserting it contains exactly the expected files and folders.
 */
protected void assertSameStructure(final FileObject folder, final FileInfo expected) throws Exception {
    // Setup the structure
    final List<FileInfo> queueExpected = new ArrayList<>();
    queueExpected.add(expected);

    final List<FileObject> queueActual = new ArrayList<>();
    queueActual.add(folder);

    while (queueActual.size() > 0) {
        final FileObject file = queueActual.remove(0);
        final FileInfo info = queueExpected.remove(0);

        // Check the type is correct
        assertSame(info.type, file.getType());

        if (info.type == FileType.FILE) {
            continue;
        }

        // Check children
        final FileObject[] children = file.getChildren();

        // Make sure all children were found
        assertNotNull(children);
        int length = children.length;
        if (info.children.size() != children.length) {
            for (final FileObject element : children) {
                if (element.getName().getBaseName().startsWith(".")) {
                    --length;
                    continue;
                }
                System.out.println(element.getName());
            }
        }

        assertEquals("count children of \"" + file.getName() + "\"", info.children.size(), length);

        // Recursively check each child
        for (final FileObject child : children) {
            final String childName = child.getName().getBaseName();
            if (childName.startsWith(".")) {
                continue;
            }
            final FileInfo childInfo = info.children.get(childName);

            // Make sure the child is expected
            assertNotNull(childInfo);

            // Add to the queue of files to check
            queueExpected.add(childInfo);
            queueActual.add(child);
        }
    }
}