Example usage for org.apache.commons.vfs.provider.local LocalFile close

List of usage examples for org.apache.commons.vfs.provider.local LocalFile close

Introduction

In this page you can find the example usage for org.apache.commons.vfs.provider.local LocalFile close.

Prototype

public void close() throws FileSystemException 

Source Link

Document

Closes this file, and its content.

Usage

From source file:org.docx4j.extras.vfs.LoadFromVFSZipFile.java

public OpcPackage getPackageFromFileObject(FileObject fo) throws Docx4JException {
    OpcPackage thePackage = null;//  ww w  .  j  ava  2s .  c  om

    if (!(fo instanceof LocalFile)) {
        //Non-Local file such as webdav file.
        //We make a temporary local copy of the file.
        //TODO: 28/03/08 - This is a second approach of dealing with non-local file.
        //The first approach which is more preferable and done in SVN version 233
        //does not create a temporary local copy of the file. 
        //It uses Commons-VFS "zip://" uri scheme to directly access the non-local file.
        //This second approach is being carried out because the writing operation cannot
        //use Commons-VFS "zip://" scheme as the scheme does not support writing to 
        //zip file yet.
        //TODO: 28/03/08 - DO NOT use "tmp://" scheme to make a temporary file. 
        //as it is going to fail. The current Commons-VFS library is still buggy. 
        StringBuffer sb = new StringBuffer("file:///");
        sb.append(System.getProperty("user.home"));
        sb.append("/.docx4j/tmp/");
        sb.append(fo.getName().getBaseName());
        String tmpPath = sb.toString().replace('\\', '/');

        LocalFile localCopy = null;
        try {
            localCopy = (LocalFile) VFS.getManager().resolveFile(tmpPath);
            localCopy.copyFrom(fo, new FileTypeSelector(FileType.FILE));
            localCopy.close();

            thePackage = getPackageFromLocalFile(localCopy);
        } catch (FileSystemException exc) {
            exc.printStackTrace();
            throw new Docx4JException("Could not create a temporary local copy", exc);
        } finally {
            if (localCopy != null) {
                try {
                    localCopy.delete();
                } catch (FileSystemException exc) {
                    exc.printStackTrace();
                    log.warn("Couldn't delete temporary file " + tmpPath);
                }
            }
        }
    } else {
        thePackage = getPackageFromLocalFile((LocalFile) fo);
    }

    return thePackage;
}

From source file:org.docx4j.extras.vfs.VFSDoc.java

/**
 * @param in//from  ww w  . ja  v a2 s  . c o m
 * @return
 * @throws Exception
 */
public static WordprocessingMLPackage convert(org.apache.commons.vfs.FileObject in) throws Exception {

    LocalFile localCopy = null;
    if (!(in instanceof LocalFile)) {

        StringBuffer sb = new StringBuffer("file:///");
        sb.append(System.getProperty("user.home"));
        sb.append("/.docx4j/tmp/");
        sb.append(in.getName().getBaseName());
        String tmpPath = sb.toString().replace('\\', '/');

        try {
            localCopy = (LocalFile) VFS.getManager().resolveFile(tmpPath);
            localCopy.copyFrom(in, new FileTypeSelector(FileType.FILE));
            localCopy.close();
        } catch (FileSystemException exc) {
            exc.printStackTrace();
            throw new Docx4JException("Could not create a temporary local copy", exc);
        } finally {
            if (localCopy != null) {
                try {
                    localCopy.delete();
                } catch (FileSystemException exc) {
                    exc.printStackTrace();
                    log.warn("Couldn't delete temporary file " + tmpPath);
                }
            }
        }
    } else {
        localCopy = (LocalFile) in;
    }

    String localPath = VFSUtils.getLocalFilePath(in);
    if (localPath == null) {
        throw new Docx4JException("Couldn't get local path");
    }

    return Doc.convert(new FileInputStream(localPath));
}