Example usage for org.apache.commons.vfs FileObject resolveFile

List of usage examples for org.apache.commons.vfs FileObject resolveFile

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileObject resolveFile.

Prototype

public FileObject resolveFile(String path) throws FileSystemException;

Source Link

Document

Finds a file, relative to this file.

Usage

From source file:org.ofbiz.commons.vfs.ofbiz.OfbizHomeProvider.java

public FileObject findFile(FileObject base, String name, FileSystemOptions properties)
        throws FileSystemException {
    //new Exception("findFile(" + base + ", " + name + ")").printStackTrace();
    try {// w w w . j  a va2  s .c  om
        URL location = FlexibleLocation.resolveLocation("ofbizhome://.");
        FileObject ofbizBase = getContext().resolveFile(location.toString(), properties);
        return VFSUtil.toFileObject(ofbizBase.getFileSystem().getFileSystemManager(),
                ofbizBase.resolveFile(name.substring(13)).getURL().toString(), properties);
    } catch (Exception e) {
        FileSystemException fse = new FileSystemException(e.getMessage(), null, e);
        fse.initCause(e);
        throw fse;
    }
}

From source file:org.sonatype.gshell.commands.vfs.CopyCommand.java

public Object execute(final CommandContext context) throws Exception {
    assert context != null;
    IO io = context.getIo();//  w  w  w . j a  va2 s.  com

    FileObject source = resolveFile(context, sourcePath);
    FileObject target = resolveFile(context, targetPath);

    new FileObjectAssert(source).exists();

    // TODO: Validate more

    if (target.exists() && target.getType().hasChildren()) {
        target = target.resolveFile(source.getName().getBaseName());
    }

    log.info("Copying {} -> {}", source, target);

    target.copyFrom(source, Selectors.SELECT_ALL);

    FileObjects.close(source, target);

    return Result.SUCCESS;
}

From source file:pt.webdetails.cpf.repository.vfs.VfsRepositoryAccess.java

protected FileObject resolveFile(FileObject folder, String file) throws Exception {
    if (file == null || file.startsWith("/") || file.startsWith(".") || file.contains("/../")) {
        throw new IllegalArgumentException(
                "Path cannot be null or start with \"/\" or \".\" - Illegal Path: " + file);
    }// w  w w  .  j a  v a  2  s  .  co m
    FileObject repoFile = folder.resolveFile(file);
    return repoFile;
}

From source file:r.base.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   ww w  . java2s.c  om
 * @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
 */
@Primitive("list.files")
public static StringVector listFiles(@Current final Context context, final StringVector paths,
        final String pattern, final boolean allFiles, final boolean fullNames, boolean recursive,
        final boolean ignoreCase) 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) {
                        add(folder, ".");
                        add(folder, "..");
                    }
                    for (FileObject child : folder.getChildren()) {
                        if (filter(child)) {
                            add(child);
                        }
                    }
                }
            }
            return result.build();
        }

        void add(FileObject file) {
            if (fullNames) {
                result.add(file.getName().getURI());
            } else {
                result.add(file.getName().getBaseName());
            }
        }

        void add(FileObject folder, String name) throws FileSystemException {
            if (fullNames) {
                result.add(folder.resolveFile(name).getName().getURI());
            } else {
                result.add(name);
            }
        }

        boolean filter(FileObject child) throws FileSystemException {
            if (!allFiles && isHidden(child)) {
                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:r.base.Files.java

/**
 * Helper function to extract a zip entry to the given folder.
 *///  w  w  w.  ja v a2 s. c  o m
private static void unzipExtract(ZipInputStream zin, ZipEntry entry, FileObject exdir, boolean junkpaths,
        boolean overwrite) throws IOException {
    if (junkpaths) {
        throw new EvalException("unzip(junpaths=false) not yet implemented");
    }

    FileObject exfile = exdir.resolveFile(entry.getName());
    if (exfile.exists() && !overwrite) {
        throw new EvalException("file to be extracted '%s' already exists", exfile.getName().getURI());
    }
    OutputStream out = exfile.getContent().getOutputStream();
    try {

        byte buffer[] = new byte[64 * 1024];
        int bytesRead;
        while ((bytesRead = zin.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
    } finally {
        out.close();
    }
}

From source file:unitTests.dataspaces.VFSFileObjectAdapterTest.java

@Before
public void setUp() throws IOException, MalformedURIException {

    testDir = new File(System.getProperty("java.io.tmpdir"), "ProActive-VFSFileObjectAdapterTest");
    final File differentDir = new File(testDir, "different");
    final File rootDir = new File(testDir, "root space");
    final File aoDir = new File(rootDir, "ao1");
    final File someDir = new File(aoDir, "dir");
    final File someFile = new File(someDir, "file.txt");
    assertTrue(someDir.mkdirs());/* ww  w  .  j a v  a 2  s.  co m*/
    assertTrue(differentDir.mkdir());
    assertTrue(someFile.createNewFile());

    rootDirPath = rootDir.getCanonicalPath();
    rootFileUri = rootDir.toURI().toURL().toExternalForm();
    differentDirPath = differentDir.getCanonicalPath();

    final FileObject rootFileObject = fileSystemManager.resolveFile(rootFileUri);
    if (server == null) {
        server = new FileSystemServerDeployer(rootDirPath, false);
    }
    vfsServerUrl = server.getVFSRootURL();

    final FileName mountintPointFileName = rootFileObject.getName();
    adaptee = rootFileObject.resolveFile(fileURI.getRelativeToSpace());

    rootUris = new ArrayList<String>();
    rootUris.add(rootFileUri);
    rootUris.add(vfsServerUrl);

    dsFileObject = new VFSFileObjectAdapter(adaptee, spaceURI, mountintPointFileName, rootUris, rootFileUri);
}

From source file:unitTests.dataspaces.VFSMountManagerHelperTest.java

/**
 * Tests closing all FileSystems/*from  w  ww.ja  va2 s.  c  o m*/
 * @throws Exception
 */
@Ignore("vfs close file system doesn't seem to work properly")
@Test
public void testCloseFileSystems() throws Exception {
    logger.info("*************** testCloseFileSystems");
    String[] validUrls = server.getVFSRootURLs();
    ArrayList<FileObject> fos = new ArrayList<FileObject>();
    for (String validUrl : validUrls) {
        FileObject mounted = VFSMountManagerHelper.mount(validUrl);
        Assert.assertTrue(mounted.exists());
        fos.add(mounted);
    }

    VFSMountManagerHelper.closeFileSystems(Arrays.asList(validUrls));

    boolean onlyExceptions = true;
    for (FileObject closedFo : fos) {
        try {
            FileObject toto = closedFo.resolveFile("toto");
            toto.createFile();
            onlyExceptions = false;
            logger.error(toto.getURL() + " exists : " + toto.exists());
        } catch (FileSystemException e) {
            // this should occur
        }
    }
    Assert.assertTrue("Only Exceptions received", onlyExceptions);
}