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

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

Introduction

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

Prototype

public FileName getName();

Source Link

Document

Returns the name of this file.

Usage

From source file:com.thinkberg.webdav.Util.java

public static String getETag(FileObject object) {
    String fileName = object.getName().getPath();
    String lastModified = "";
    try {//from  w  w  w .j  a v  a 2s.c  om
        lastModified = String.valueOf(object.getContent().getLastModifiedTime());
    } catch (FileSystemException e) {
        // ignore error here
    }

    return DigestUtils.shaHex(fileName + lastModified);
}

From source file:com.newatlanta.appengine.junit.vfs.gae.GaeVfsTestCase.java

public static void assertEqualPaths(File file, FileObject fileObject) throws Exception {
    String fileObjectPath = new File(fileObject.getName().getPath()).getAbsolutePath();
    assertEquals(file.getAbsolutePath(), fileObjectPath);
    assertEquals(file.getName(), fileObject.getName().getBaseName());
}

From source file:cc.aileron.commons.util.ClassPattrnFinderUtils.java

/**
 * ??????/*from w w w.  j  a  v a 2s  . co m*/
 * 
 * @param targetPackage
 * @param pattern
 * @return
 * @throws IOException
 * @throws URISyntaxException
 * @throws ResourceNotFoundException
 */
private static final List<Class<?>> tryGetClassNameList(final String targetPackage, final Pattern pattern)
        throws IOException, URISyntaxException, ResourceNotFoundException {
    final List<Class<?>> result = new ArrayList<Class<?>>();

    final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    final String path = targetPackage.replace('.', '/') + "/";
    final Enumeration<URL> urls = classLoader.getResources(path);
    while (urls.hasMoreElements()) {
        final URL url = urls.nextElement();
        final Resource resource = ResourceConvertUtils.convertUrl(url);
        for (final FileObject file : resource.toFileObject().getChildren()) {
            final String name = file.getName().getBaseName().split("\\.")[0];
            if (!file.getType().equals(FileType.FILE) || !file.getName().getExtension().equals("class")) {
                continue;
            }
            if (pattern != null && !pattern.matcher(name).matches()) {
                continue;
            }

            final Class<?> classObject = getClass(targetPackage + "." + name);
            if (classObject != null) {
                result.add(classObject);
            }
        }
    }
    return result;
}

From source file:com.newatlanta.appengine.junit.vfs.gae.GaeFolderTestCase.java

private static void assertSubFolders(FileObject testFolder, String[] subFolderNames) throws Exception {
    FileName rootName = testFolder.getFileSystem().getRootName();
    FileObject[] subFolders = testFolder.findFiles(Selectors.EXCLUDE_SELF);
    assertEquals(subFolders.length, subFolderNames.length);
    for (int i = 0; i < subFolders.length; i++) {
        FileObject subObject = subFolders[i];
        assertTrue(subObject.getName().getPath().endsWith(subFolderNames[i]));
        assertFolder(subObject);/*  ww w . j av  a 2  s  .  co m*/
        assertEquals(subObject.getParent(), i == subFolders.length - 1 ? testFolder : subFolders[i + 1]);
        assertTrue(rootName.isDescendent(subObject.getName()));
        assertTrue(subObject.getName().isAncestor(rootName));
    }
}

From source file:com.panet.imeta.core.vfs.KettleVFS.java

public static String getFilename(FileObject fileObject) {
    FileName fileName = fileObject.getName();
    String root = fileName.getRootURI();
    if (!root.startsWith("file:"))
        return fileName.getURI(); // nothing we can do about non-normal files.
    if (root.endsWith(":/")) // Windows
    {//from w  w  w.  j  a  va2 s. c o  m
        root = root.substring(8, 10);
    } else // *nix & OSX
    {
        root = "";
    }
    String fileString = root + fileName.getPath();
    if (!"/".equals(Const.FILE_SEPARATOR)) {
        fileString = Const.replace(fileString, "/", Const.FILE_SEPARATOR);
    }
    return fileString;
}

From source file:com.panet.imeta.core.fileinput.FileInputList.java

public static String getRequiredFilesDescription(List<FileObject> nonExistantFiles) {
    StringBuffer buffer = new StringBuffer();
    for (Iterator<FileObject> iter = nonExistantFiles.iterator(); iter.hasNext();) {
        FileObject file = iter.next();
        buffer.append(file.getName().getURI());
        buffer.append(Const.CR);/*from   w  w  w  . ja va2 s.  c  o  m*/
    }
    return buffer.toString();
}

From source file:net.sf.vfsjfilechooser.utils.VFSUtils.java

/**
 * Tells whether a file is hidden/*w  w  w .jav a 2  s . c  om*/
 * @param fileObject a file representation
 * @return whether a file is hidden
 */
public static boolean isHiddenFile(FileObject fileObject) {
    try {
        return fileObject.getName().getBaseName().charAt(0) == '.';
    } catch (Exception ex) {
        return false;
    }
}

From source file:com.threecrickets.sincerity.util.IoUtil.java

/**
 * Copies a file or a complete subdirectory tree using Apache Commons VFS.
 * /*from ww w  .  j a v  a 2s  .c  o  m*/
 * @param manager
 *        The file system manager
 * @param file
 *        The source file or directory
 * @param folder
 *        The target folder
 * @throws IOException
 *         In case of an I/O error
 */
public static void copyRecursive(FileSystemManager manager, FileObject file, FileObject folder)
        throws IOException {
    folder.createFolder();
    FileType type = file.getType();
    FileObject target = manager.resolveFile(folder + "/" + file.getName().getBaseName());
    if (type == FileType.FILE)
        org.apache.commons.vfs.FileUtil.copyContent(file, target);
    else if (type == FileType.FOLDER) {
        for (FileObject child : file.getChildren())
            copyRecursive(manager, child, target);
    }
}

From source file:com.panet.imeta.core.fileinput.FileInputList.java

public static FileInputList createFileList(VariableSpace space, String[] fileName, String[] fileMask,
        String[] fileRequired, boolean[] includeSubdirs, FileTypeFilter[] fileTypeFilters) {
    FileInputList fileInputList = new FileInputList();

    // Replace possible environment variables...
    final String realfile[] = space.environmentSubstitute(fileName);
    final String realmask[] = space.environmentSubstitute(fileMask);

    for (int i = 0; i < realfile.length; i++) {
        final String onefile = realfile[i];
        final String onemask = realmask[i];

        final boolean onerequired = YES.equalsIgnoreCase(fileRequired[i]);
        final boolean subdirs = includeSubdirs[i];
        final FileTypeFilter filter = ((fileTypeFilters == null || fileTypeFilters[i] == null)
                ? FileTypeFilter.ONLY_FILES
                : fileTypeFilters[i]);/*from  www. ja  v  a 2 s . c  o m*/

        if (Const.isEmpty(onefile))
            continue;

        // 
        // If a wildcard is set we search for files
        //
        if (!Const.isEmpty(onemask)) {
            try {
                // Find all file names that match the wildcard in this directory
                //
                FileObject directoryFileObject = KettleVFS.getFileObject(onefile);
                if (directoryFileObject != null && directoryFileObject.getType() == FileType.FOLDER) // it's a directory
                {
                    FileObject[] fileObjects = directoryFileObject.findFiles(new AllFileSelector() {
                        public boolean traverseDescendents(FileSelectInfo info) {
                            return info.getDepth() == 0 || subdirs;
                        }

                        public boolean includeFile(FileSelectInfo info) {
                            // Never return the parent directory of a file list.
                            if (info.getDepth() == 0) {
                                return false;
                            }

                            FileObject fileObject = info.getFile();
                            try {
                                if (fileObject != null && filter.isFileTypeAllowed(fileObject.getType())) {
                                    String name = fileObject.getName().getBaseName();
                                    boolean matches = Pattern.matches(onemask, name);
                                    /*
                                    if (matches)
                                    {
                                        System.out.println("File match: URI: "+info.getFile()+", name="+name+", depth="+info.getDepth());
                                    }
                                    */
                                    return matches;
                                }
                                return false;
                            } catch (FileSystemException ex) {
                                // Upon error don't process the file.
                                return false;
                            }
                        }
                    });
                    if (fileObjects != null) {
                        for (int j = 0; j < fileObjects.length; j++) {
                            if (fileObjects[j].exists())
                                fileInputList.addFile(fileObjects[j]);
                        }
                    }
                    if (Const.isEmpty(fileObjects)) {
                        if (onerequired)
                            fileInputList.addNonAccessibleFile(directoryFileObject);
                    }

                    // Sort the list: quicksort, only for regular files
                    fileInputList.sortFiles();
                } else {
                    FileObject[] children = directoryFileObject.getChildren();
                    for (int j = 0; j < children.length; j++) {
                        // See if the wildcard (regexp) matches...
                        String name = children[j].getName().getBaseName();
                        if (Pattern.matches(onemask, name))
                            fileInputList.addFile(children[j]);
                    }
                    // We don't sort here, keep the order of the files in the archive.
                }
            } catch (Exception e) {
                LogWriter.getInstance().logError("FileInputList", Const.getStackTracker(e));
            }
        } else
        // A normal file...
        {
            try {
                FileObject fileObject = KettleVFS.getFileObject(onefile);
                if (fileObject.exists()) {
                    if (fileObject.isReadable()) {
                        fileInputList.addFile(fileObject);
                    } else {
                        if (onerequired)
                            fileInputList.addNonAccessibleFile(fileObject);
                    }
                } else {
                    if (onerequired)
                        fileInputList.addNonExistantFile(fileObject);
                }
            } catch (Exception e) {
                LogWriter.getInstance().logError("FileInputList", Const.getStackTracker(e));
            }
        }
    }

    return fileInputList;
}

From source file:com.threecrickets.sincerity.util.IoUtil.java

/**
 * Unpacks all files in an archive using Apache Commons VFS.
 * <p>/*from w w w.jav  a  2 s.c om*/
 * Supported formats: zip, tar.gz/tgz, tar.bz2.
 * 
 * @param archiveFile
 *        The archive file
 * @param destinationDir
 *        The destination directory
 * @param workDir
 *        The work directory
 * @throws IOException
 *         In case of an I/O error
 */
public static void unpack(File archiveFile, File destinationDir, File workDir) throws IOException {
    String scheme = null;
    String name = archiveFile.getName();
    DefaultFileSystemManager manager = new DefaultFileSystemManager();
    try {
        boolean untar = false;
        if (name.endsWith(".zip")) {
            scheme = "zip:";
            manager.addProvider("zip", new ZipFileProvider());
        } else if (name.endsWith(".tar.gz") || name.endsWith(".tgz")) {
            scheme = "gz:";
            untar = true;
            manager.addProvider("tar", new TarFileProvider());
            manager.addProvider("gz", new GzipFileProvider());
            manager.addProvider("tgz", new TgzFileProvider());
        } else if (name.endsWith(".tar.bz2")) {
            scheme = "bz2:";
            untar = true;
            manager.addProvider("tar", new TarFileProvider());
            manager.addProvider("bz2", new Bzip2FileProvider());
            manager.addProvider("tbz2", new Tbz2FileProvider());
        }

        if (scheme != null) {
            DefaultFileReplicator replicator = new DefaultFileReplicator(workDir);
            replicator.init();
            manager.setReplicator(replicator);
            manager.setTemporaryFileStore(replicator);
            DefaultLocalFileProvider fileProvider = new DefaultLocalFileProvider();
            manager.addProvider("file", fileProvider);
            manager.setDefaultProvider(fileProvider);
            manager.setFilesCache(new DefaultFilesCache());
            manager.init();

            String path = scheme + archiveFile.toURI();
            FileObject fileObject = manager.resolveFile(path);
            FileObject[] children = fileObject.getChildren();
            if (untar && children.length > 0) {
                FileObject tar = manager
                        .resolveFile(new File(workDir, children[0].getName().getBaseName()).toURI().toString());
                org.apache.commons.vfs.FileUtil.copyContent(children[0], tar);
                tar = manager.resolveFile("tar:" + tar.getName());
                children = tar.getChildren();
            }

            for (FileObject child : children)
                copyRecursive(manager, child, manager.resolveFile(destinationDir.toURI().toString()));
        }
    } finally {
        manager.close();
    }
}