Example usage for org.apache.commons.vfs2 FileObject getChildren

List of usage examples for org.apache.commons.vfs2 FileObject getChildren

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileObject getChildren.

Prototype

FileObject[] getChildren() throws FileSystemException;

Source Link

Document

Lists the children of this file.

Usage

From source file:org.pentaho.metaverse.impl.VfsLineageCollector.java

@Override
public List<String> listArtifactsForFile(String pathToArtifact, String startingDate, String endingDate)
        throws IllegalArgumentException {
    List<String> paths = new ArrayList<>();

    try {/*w ww .ja  v  a  2 s. co m*/
        FileSystemOptions opts = new FileSystemOptions();
        FileObject lineageRootFolder = KettleVFS.getFileObject(getOutputFolder(), opts);

        FileSelector dateRangeFilter = new VfsDateRangeFilter(format, startingDate, endingDate);
        FileSelector depthFilter = new FileDepthSelector(1, 256);

        if (lineageRootFolder.exists() && lineageRootFolder.getType() == FileType.FOLDER) {

            // get all of the date folders of lineage we have
            FileObject[] dayFolders = lineageRootFolder.findFiles(dateRangeFilter);
            for (FileObject dayFolder : dayFolders) {
                FileObject[] listThisFolder = dayFolder.findFiles(depthFilter);
                for (FileObject currentFile : listThisFolder) {
                    FileObject requested = currentFile.resolveFile(pathToArtifact);
                    if (requested.exists() && requested.getType() == FileType.FOLDER) {
                        FileObject[] requestedChildren = requested.getChildren();
                        for (FileObject requestedChild : requestedChildren) {
                            if (requestedChild.getType() == FileType.FILE) {
                                paths.add(requestedChild.getName().getPath());
                            }
                        }
                    }
                }
            }
        }
        return paths;
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:org.pentaho.reporting.designer.extensions.pentaho.repository.model.RepositoryTreeModel.java

/**
 * 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//from   w  w  w . j a va2  s .co  m
 *          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;
    }
}

From source file:org.pentaho.reporting.designer.extensions.pentaho.repository.model.RepositoryTreeModel.java

/**
 * 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//  w  w  w .  j  a v  a  2s. co m
 *          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;
    }
}

From source file:org.pentaho.reporting.designer.extensions.pentaho.repository.model.RepositoryTreeModel.java

/**
 * Returns the index of child in parent. If either <code>parent</code> or <code>child</code> is <code>null</code>,
 * returns -1. If either <code>parent</code> or <code>child</code> don't belong to this tree model, returns -1.
 *
 * @param parent/*from  w ww . j a  v a 2s.c om*/
 *          a note in the tree, obtained from this data source
 * @param childNode
 *          the node we are interested in
 * @return the index of the child in the parent, or -1 if either <code>child</code> or <code>parent</code> are
 *         <code>null</code> or don't belong to this tree model
 */
public int getIndexOfChild(Object parent, final Object childNode) {
    if (parent instanceof RepositoryTreeRoot) {
        final RepositoryTreeRoot root1 = (RepositoryTreeRoot) parent;
        parent = root1.getRoot();
        if (parent == null) {
            return -1;
        }
    }

    try {
        final FileObject parChild = (FileObject) childNode;
        final FileObject parElement = (FileObject) parent;
        final FileObject[] childs = parElement.getChildren();
        int count = 0;
        for (int i = 0; i < childs.length; i++) {
            final FileObject child = childs[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 (child.getName().equals(parChild.getName())) {
                return count;
            }

            count += 1;
        }

        return -1;
    } catch (FileSystemException fse) {
        logger.debug("Failed", fse);
        return -1;
    }
}

From source file:org.pentaho.vfs.ui.VfsBrowser.java

public boolean moveItem(TreeItem source, TreeItem destination) throws FileSystemException {
    FileObject file = (FileObject) source.getData();
    FileObject destFile = (FileObject) destination.getData();
    if (!file.exists() && !destFile.exists()) {
        return false;
    }/*from   www. ja  va 2s.  c  om*/
    try {
        if (destFile.getChildren() != null) {
            destFile = destFile.resolveFile(source.getText());
        }
    } catch (Exception e) {
        destFile = destFile.getParent().resolveFile(source.getText());
        destination = destination.getParentItem();
    }
    if (!file.getParent().equals(destFile.getParent())) {
        file.moveTo(destFile);
        TreeItem destTreeItem = new TreeItem(destination, SWT.NONE);
        destTreeItem.setImage(getFileImage(source.getDisplay()));
        destTreeItem.setData(destFile);
        destTreeItem.setData("isLoaded", Boolean.FALSE); //$NON-NLS-1$
        populateTreeItemText(destTreeItem, destFile);
        source.dispose();
    }
    return true;
}

From source file:org.pentaho.vfs.ui.VfsBrowser.java

public void populateFileSystemTree(final FileObject inputFile, final Tree tree, TreeItem item) {
    if (inputFile == null) {
        return;//from  w  w  w  .  j av  a 2  s . c om
    }
    if (item == null) {
        item = new TreeItem(tree, SWT.NONE);
        String rootName = inputFile.getName().getBaseName();
        if (rootName == null || "".equals(rootName)) {
            rootName = "/";
        }
        item.setText(rootName);
        item.setData(inputFile);
        item.setExpanded(true);
        tree.setSelection(item);
    } else {
        item.setData(inputFile);
    }
    final TreeItem myItem = item;
    Runnable r = new Runnable() {
        public void run() {
            FileObject[] children = null;
            try {
                children = (FileObject[]) fileObjectChildrenMap.get(inputFile.getName().getFriendlyURI());
                if (children == null && inputFile.getType().equals(FileType.FOLDER)) {
                    children = inputFile.getChildren();
                    if (children == null) {
                        children = new FileObject[0];
                    }
                    Arrays.sort(children, new Comparator<FileObject>() {
                        public int compare(FileObject o1, FileObject o2) {
                            try {
                                if (o1.getType().equals(o2.getType())) {
                                    return o1.getName().getBaseName().compareTo(o2.getName().getBaseName());
                                }
                                if (o1.getType().equals(FileType.FOLDER)) {
                                    return -1;
                                }
                                if (o1.getType().equals(FileType.FILE)) {
                                    return 1;
                                }
                            } catch (Exception e) {
                            }
                            return 0;
                        }

                        public boolean equals(Object obj) {
                            return super.equals(obj);
                        }
                    });
                    fileObjectChildrenMap.put(inputFile.getName().getFriendlyURI(), children);
                }
            } catch (FileSystemException e) {
                e.printStackTrace();
            }
            myItem.setData("isLoaded", Boolean.TRUE); //$NON-NLS-1$
            if (children != null) {
                myItem.setImage(getFolderImage(tree.getDisplay()));
            } else if (showFoldersOnly) {
                myItem.removeAll();
                myItem.dispose();
                return;
            }
            for (int i = 0; children != null && i < children.length; i++) {
                FileObject fileObj = children[i];
                try {
                    if (fileObj.getType().hasChildren()) {
                        TreeItem childTreeItem = new TreeItem(myItem, SWT.NONE);
                        populateTreeItemText(childTreeItem, fileObj);
                        childTreeItem.setImage(getFileImage(tree.getDisplay()));
                        childTreeItem.setData(fileObj);
                        childTreeItem.setData("isLoaded", Boolean.FALSE); //$NON-NLS-1$
                        childTreeItem.setImage(getFolderImage(tree.getDisplay()));
                        TreeItem tmpItem = new TreeItem(childTreeItem, SWT.NONE);
                        populateTreeItemText(tmpItem, fileObj);
                    } else if (fileObj.getType().equals(FileType.FOLDER)) {
                        TreeItem childTreeItem = new TreeItem(myItem, SWT.NONE);
                        populateTreeItemText(childTreeItem, fileObj);
                        childTreeItem.setImage(getFileImage(tree.getDisplay()));
                        childTreeItem.setData(fileObj);
                        childTreeItem.setData("isLoaded", Boolean.FALSE); //$NON-NLS-1$
                        childTreeItem.setImage(getFolderImage(tree.getDisplay()));
                        TreeItem tmpItem = new TreeItem(childTreeItem, SWT.NONE);
                        populateTreeItemText(tmpItem, fileObj);
                    } else if (!fileObj.getType().equals(FileType.FOLDER) && !showFoldersOnly) {
                        if (isAcceptedByFilter(fileObj.getName())) {
                            TreeItem childTreeItem = new TreeItem(myItem, SWT.NONE);
                            populateTreeItemText(childTreeItem, fileObj);
                            childTreeItem.setImage(getFileImage(tree.getDisplay()));
                            childTreeItem.setData(fileObj);
                            childTreeItem.setData("isLoaded", Boolean.FALSE); //$NON-NLS-1$
                        }
                    }
                } catch (FileSystemException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (fileSystemTree.getItemCount() > 0 && fileSystemTree.getSelection().length == 0) {
                fileSystemTree.setSelection(fileSystemTree.getItem(0));
                fileSystemTree.getItem(0).setExpanded(true);
            }
        }
    };
    BusyIndicator.showWhile(tree.getDisplay(), r);
}

From source file:org.renjin.appengine.AppEngineLocalFilesSystemProviderTest.java

@Test
public void test() throws FileSystemException {

    File basePath = new File(getClass().getResource("/jarfiletest.jar").getFile()).getParentFile();

    FileSystemManager dfsm = AppEngineContextFactory
            .createFileSystemManager(new AppEngineLocalFilesSystemProvider(basePath));

    FileObject jarFile = dfsm.resolveFile("/jarfiletest.jar");
    assertThat(jarFile.getName().getURI(), equalTo("file:///jarfiletest.jar"));
    assertThat(jarFile.exists(), equalTo(true));

    FileObject jarRoot = dfsm.resolveFile("jar:file:///jarfiletest.jar!/r/library");
    assertThat(jarRoot.exists(), equalTo(true));
    assertThat(jarRoot.getType(), equalTo(FileType.FOLDER));
    assertThat(jarRoot.getChildren().length, equalTo(1));
}

From source file:org.renjin.primitives.files.Files.java

/**
 * {@code list.files} produce a character vector of the names of files in the named directory.
 *
 * @param paths  a character vector of full path names; the default corresponds to the working
 *  directory getwd(). Missing values will be ignored.
 * @param pattern an optional regular expression. Only file names which match the regular
 * expression will be returned./*from w  ww . ja v  a 2  s .c  o  m*/
 * @param allFiles  If FALSE, only the names of visible files are returned. If TRUE, all
 * file names will be returned.
 * @param fullNames If TRUE, the directory path is prepended to the file names. If FALSE,
 * only the file names are returned.
 * @param recursive Should the listing recurse into directories?
 * @param ignoreCase Should pattern-matching be case-insensitive?
 *
 * If a path does not exist or is not a directory or is unreadable it is skipped, with a warning.
 * The files are sorted in alphabetical order, on the full path if full.names = TRUE. Directories are included only if recursive = FALSE.
 *
 * @return
 */
@Internal("list.files")
public static StringVector listFiles(@Current final Context context, final StringVector paths,
        final String pattern, final boolean allFiles, final boolean fullNames, final boolean recursive,
        final boolean ignoreCase, final boolean includeDirs) throws IOException {

    return new Object() {

        private final StringVector.Builder result = new StringVector.Builder();
        private final RE filter = pattern == null ? null : new ExtendedRE(pattern).ignoreCase(ignoreCase);

        public StringVector list() throws IOException {
            for (String path : paths) {
                FileObject folder = context.resolveFile(path);
                if (folder.getType() == FileType.FOLDER) {
                    if (allFiles & !recursive) {
                        add(path, ".");
                        add(path, "..");
                    }
                    for (FileObject child : folder.getChildren()) {
                        if (filter(child)) {
                            add(path, child);
                        }
                    }
                }
            }
            return result.build();
        }

        void add(String path, FileObject file) {
            if (fullNames) {
                result.add(path + "/" + file.getName().getBaseName());
            } else {
                result.add(file.getName().getBaseName());
            }
        }

        void add(String path, String name) throws FileSystemException {
            if (fullNames) {
                result.add(path + "/" + name);
            } else {
                result.add(name);
            }
        }

        boolean filter(FileObject child) throws FileSystemException {
            if (!allFiles && isHidden(child)) {
                return false;
            }
            if (recursive && !includeDirs && child.getType() == FileType.FOLDER) {
                return false;
            }
            if (filter != null && !filter.match(child.getName().getBaseName())) {
                return false;
            }
            return true;
        }

        private boolean isHidden(FileObject file) throws FileSystemException {
            return file.isHidden() || file.getName().getBaseName().startsWith(".");
        }
    }.list();
}

From source file:org.renjin.primitives.files.Files.java

private static void delete(FileObject file, boolean recursive) throws FileSystemException {
    if (file.exists()) {
        if (file.getType() == FileType.FILE) {
            file.delete();/*w  ww .j av a  2  s . co  m*/
        } else if (file.getType() == FileType.FOLDER) {
            if (file.getChildren().length == 0) {
                file.delete();
            } else if (recursive) {
                file.delete();
            }
        }
    }
}

From source file:org.renjin.primitives.packaging.ClasspathPackageTest.java

@Test
public void resolvePackageRootTest() throws FileSystemException {

    FileSystemManager fileSystemManager = FileSystemUtils.getMinimalFileSystemManager();
    ClasspathPackage classpathPackage = new ClasspathPackage(new FqPackageName("org.renjin", "base"));

    FileObject fileObject = classpathPackage.resolvePackageRoot(fileSystemManager);
    for (FileObject object : fileObject.getChildren()) {
        System.out.println(object);
    }//from   ww  w .j a v  a  2s .  co  m
}