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

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

Introduction

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

Prototype

FileType getType() throws FileSystemException;

Source Link

Document

Returns this file's type.

Usage

From source file:com.sshtools.appframework.ui.IconStore.java

public MIMEEntry getMIMEEntryForFile(FileObject file, boolean useMagic) {
    try {//  ww  w . j a v a2  s  .  co  m
        if (file.getType().equals(FileType.FILE) || file.getType().equals(FileType.FOLDER)) {
            MIMEEntry mime = mimeCache.get(file);
            if (mime == null) {
                mime = mimeService.getMimeTypeForFile(file, useMagic);
            }

            if (mime != null) {
                mimeCache.cache(file, mime);
            }

            return mime;
        } else {
            return null;
        }
    } catch (Exception fse) {
        LOG.debug("Failed to load MIME.", fse);
        return null;
    }
}

From source file:com.stratuscom.harvester.classloading.VFSClassLoaderTest.java

/**
Just to make sure that we have the base setup correct, ensure that we
can read the 'start.properties' file inside the reggie-module jar.
@throws Exception//w w w . j a  v a  2 s.c o  m
 */
@Test
public void testCanReadStartDotProperties() throws Exception {
    FileObject startProperties = reggieModuleRoot.resolveFile("start.properties");
    assertNotNull(startProperties);
    assertTrue(
            "Properties file unreadable:" + startProperties.toString() + " type=" + startProperties.getType(),
            startProperties.isReadable());
}

From source file:com.sshtools.appframework.ui.IconStore.java

public Icon getIconForFile(FileObject file, int size, boolean useMagic) {
    try {// www  .j av a 2  s. co  m
        if (file.getType().equals(FileType.FILE)) {
            MIMEEntry mime = mimeCache.get(file);
            if (mime == null) {
                mime = mimeService.getMimeTypeForFile(file, useMagic);
            }

            if (mime != null) {
                mimeCache.cache(file, mime);
            }

            if (mime != null && mime.getIcon() != null) {
                Icon icon = getIcon(mime.getIcon(), size);
                if (icon != null) {
                    return icon;
                }
            }

            if (mime != null && mime.getGenericIcon() != null) {
                Icon icon = getIcon(mime.getGenericIcon(), size);
                if (icon != null) {
                    return icon;
                }
            }

            if (mime != null && mime.getSubclasses() != null) {
                for (String subclass : mime.getSubclasses()) {
                    Icon icon = getIcon(subclass, size);
                    if (icon != null) {
                        return icon;
                    }
                }
            }

            return getIcon("text-x-generic", size);
        } else if (file.getType().equals(FileType.FOLDER)) {
            return getIcon("folder", size);
        } else if (file.getType().equals(FileType.IMAGINARY)) {
            return getIcon("emblem-unreadable", size);
        } else {
            return getIcon("text-x-generic", size);
        }
    } catch (Exception fse) {
        LOG.debug("Failed to load icon.", fse);
        return getIcon("dialog-error", size);
    }
}

From source file:net.sourceforge.fullsync.ui.FileObjectChooser.java

private void tableItemDoubleClicked(Event event) {
    TableItem[] items = tableItems.getSelection();
    if (items.length > 0) {
        try {/*from  www .  j  a  va2  s .c  o m*/
            TableItem item = items[0];
            FileObject file = (FileObject) item.getData();
            if (file.getType().hasChildren()) {
                setActiveFileObject(file);
            }
        } catch (FileSystemException fse) {
            fse.printStackTrace();
        }
    }
}

From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignProvider.java

private static boolean performChange(ChangedDocument changedDocument,
        FileSystemDesignProvider originalDesignProvider, OverlayStatus status, String targetEncoding,
        FileObject baseFolder, Logger log, DesignFileValidator validator) throws FileSystemException,
        NoSuchAlgorithmException, UnsupportedEncodingException, IOException, WGDesignSyncException {

    boolean conflictFileCreated = false;
    log.info("Updating overlay resource " + changedDocument.getDocumentKey());

    // Find files which represent the document in source and target
    FileObject sourceDocFile = originalDesignProvider.getBaseFolder()
            .resolveFile(changedDocument.getSourceFilePath());
    FileObject targetDocFile = baseFolder.resolveFile(changedDocument.getTargetFilePath());

    // Collect files to copy and delete
    Map<FileObject, FileObject> filesToCopy = new HashMap<FileObject, FileObject>();
    List<FileObject> filesToDelete = new ArrayList<FileObject>();

    // Collect for file containers: Must traverse container content
    if (changedDocument.getDocumentKey().getDocType() == WGDocument.TYPE_FILECONTAINER) {

        if (changedDocument.getChangeType() == ChangeType.NEW) {
            targetDocFile.createFolder();
        }/*w  w  w  . j  a  va  2s .c  o m*/

        // Copy all files in container from the source to the target 
        for (FileObject sourceFile : sourceDocFile.getChildren()) {
            if (sourceFile.getType().equals(FileType.FILE)) {
                if (!isValidDesignFile(sourceFile, validator)) {
                    continue;
                }
                filesToCopy.put(sourceFile, targetDocFile.resolveFile(sourceFile.getName().getBaseName()));
            }
        }

        // Delete all files in target that were deployed with previous base version but are deleted in current base version 
        for (FileObject targetFile : targetDocFile.getChildren()) {
            if (targetFile.getType().equals(FileType.FILE)) {
                if (!isValidDesignFile(targetFile, validator)) {
                    continue;
                }

                FileObject sourceFile = sourceDocFile.resolveFile(targetFile.getName().getBaseName());
                if (sourceFile.exists()) {
                    continue;
                }

                // Only delete those that were deployed with previous base version and have unaltered content
                String resourcePath = baseFolder.getName().getRelativeName(targetFile.getName());
                ResourceData resourceData = status.getOverlayData().getOverlayResources().get(resourcePath);
                if (resourceData != null) {
                    String hash = MD5HashingInputStream.getStreamHash(targetFile.getContent().getInputStream());
                    if (resourceData.getMd5Hash().equals(hash)) {
                        filesToDelete.add(targetFile);
                    }
                }
            }
        }

    }

    // Collect for anything else
    else {
        filesToCopy.put(sourceDocFile, targetDocFile);
    }

    // Copy files
    for (Map.Entry<FileObject, FileObject> files : filesToCopy.entrySet()) {

        FileObject sourceFile = files.getKey();
        FileObject targetFile = files.getValue();
        String resourcePath = baseFolder.getName().getRelativeName(targetFile.getName());

        if (changedDocument.getChangeType() == ChangeType.CONFLICT) {
            // Do a test if the current file is conflicting. If so we write to a conflict file instead
            InputStream in = new BufferedInputStream(sourceFile.getContent().getInputStream());
            String currentHash = MD5HashingInputStream.getStreamHash(in);
            ResourceData deployedHash = status.getOverlayData().getOverlayResources().get(resourcePath);
            boolean skipConflict = false;

            // Conflict on file container: A single file might just be missing in the target. We can safely copy that to the target without treating as conflict (#00002440)
            if (deployedHash == null
                    && changedDocument.getDocumentKey().getDocType() == WGDocument.TYPE_FILECONTAINER
                    && !targetFile.exists()) {
                skipConflict = true;
            }

            if (!skipConflict && (deployedHash == null || !deployedHash.getMd5Hash().equals(currentHash))) {
                targetFile = createConflictFile(targetFile);
                conflictFileCreated = true;
                log.warn("Modified overlay resource " + resourcePath
                        + " is updated in base design. We write the updated base version to conflict file for manual resolution: "
                        + baseFolder.getName().getRelativeName(targetFile.getName()));
            }
        }

        // Write file
        InputStream in = new BufferedInputStream(sourceFile.getContent().getInputStream());
        MD5HashingOutputStream out = new MD5HashingOutputStream(
                new BufferedOutputStream(targetFile.getContent().getOutputStream(false)));

        // Update resource data
        resourceInToOut(in, originalDesignProvider.getFileEncoding(), out, targetEncoding);
        OverlayData.ResourceData resourceData = new OverlayData.ResourceData();
        resourceData.setMd5Hash(out.getHash());
        status.getOverlayData().setOverlayResource(resourcePath, resourceData);

    }

    // Delete files
    for (FileObject fileToDelete : filesToDelete) {
        String resourcePath = baseFolder.getName().getRelativeName(fileToDelete.getName());
        fileToDelete.delete();
        status.getOverlayData().removeOverlayResource(resourcePath);
    }

    return conflictFileCreated;

}

From source file:de.innovationgate.utils.DirComparer.java

private void addFileHash(Map<String, String> hashes, FileObject file, FileObject root)
        throws NoSuchAlgorithmException, IOException, FileSystemException {

    String path = root.getName().getRelativeName(file.getName());
    if (file.getType().equals(FileType.FOLDER)) {
        hashes.put(path, "");
    } else {/*from   w  w w  .  j  a v  a  2  s. c  om*/
        String hash = MD5HashingInputStream.getStreamHash(file.getContent().getInputStream());
        hashes.put(path, hash);
    }
}

From source file:net.sf.jabb.web.action.VfsTreeAction.java

/**
 * Populate a node.//from w  w  w  . ja v  a 2  s . com
 * @param root      Relative root directory.
 * @param file      The file object.
 * @return   The node data structure which presents the file.
 * @throws FileSystemException
 */
protected JsTreeNodeData populateTreeNodeData(FileObject root, FileObject file) throws FileSystemException {
    boolean noChild = true;
    FileType type = file.getType();
    if (type.equals(FileType.FOLDER) || type.equals(FileType.FILE_OR_FOLDER)) {
        noChild = file.getChildren().length == 0;
    }
    String relativePath = root.getName().getRelativeName(file.getName());
    return populateTreeNodeData(file, noChild, relativePath);
}

From source file:de.innovationgate.wgpublisher.design.fs.AbstractDesignFile.java

protected List<FileObject> getFileContainerFiles() throws FileSystemException, WGDesignSyncException {
    FileObject[] filesArray = getCodeFile().getChildren();
    if (filesArray == null) {
        throw new WGDesignSyncException("Cannot collect files from directory '"
                + getCodeFile().getName().getPathDecoded() + "'. Please verify directory existence.");
    }//from w ww . ja va 2s . c o  m

    List<FileObject> files = new ArrayList<FileObject>();
    for (FileObject fileobj : filesArray) {
        if (fileobj.getType().equals(FileType.FILE) && !isExcludedFileContainerFile(fileobj)) {
            files.add(fileobj);
        }
    }
    return files;
}

From source file:com.flicklib.folderscanner.AdvancedFolderScanner.java

/**
 * add the compressed files to the file group, which are in the specified directory.
 * @param sm// ww  w . j av  a  2 s  .com
 * @param fg
 * @param fileList
 * @param folderName
 * @throws FileSystemException 
 */
private void addCompressedFiles(FileGroup fg, FileObject[] fileList, String folderName)
        throws FileSystemException {
    for (FileObject f : fileList) {
        if (f.getType().hasChildren() && folderName.equals(f.getName().getBaseName().toLowerCase())) {
            addCompressedFiles(fg, f.getChildren());
        }
    }
}

From source file:com.flicklib.folderscanner.AdvancedFolderScanner.java

/**
 * add the files, which has similar names, to the movie object
 * /*  w w  w  .j av a  2 s  .  c  o m*/
 * @param sm
 * @param files
 * @param next
 * @throws FileSystemException 
 */
private void addFiles(FileGroup fg, FileObject[] files, String plainFileName) throws FileSystemException {
    for (FileObject f : files) {
        if (!f.getType().hasChildren()) {
            String baseName = getNameWithoutExt(f);
            String ext = getExtension(f);
            if (MovieFileFilter.VIDEO_EXT_EXTENSIONS.contains(ext)) {
                if (LevenshteinDistance.distance(plainFileName, baseName) <= 3) {
                    fg.getFiles().add(createFileMeta(f, MovieFileType.getTypeByExtension(ext)));
                }
            }
        }
    }
}