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

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

Introduction

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

Prototype

public boolean exists() throws FileSystemException;

Source Link

Document

Determines if this file exists.

Usage

From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java

public static boolean isFolder(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    try {//from ww  w . ja va2  s.  c  om
        if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
            if (ArgList[0].equals(null)) {
                return false;
            }
            FileObject file = null;

            try {
                // Source file
                file = KettleVFS.getFileObject((String) ArgList[0]);
                boolean isafolder = false;
                if (file.exists()) {
                    if (file.getType().equals(FileType.FOLDER)) {
                        isafolder = true;
                    } else {
                        new RuntimeException("[" + (String) ArgList[0] + "] is not a folder!");
                    }
                } else {
                    new RuntimeException("folder [" + (String) ArgList[0] + "] can not be found!");
                }
                return isafolder;
            } catch (IOException e) {
                throw new RuntimeException("The function call isFolder throw an error : " + e.toString());
            } finally {
                if (file != null) {
                    try {
                        file.close();
                    } catch (Exception e) {
                        // Ignore errors
                    }
                }
            }

        } else {
            throw new RuntimeException("The function call isFolder is not valid.");
        }
    } catch (Exception e) {
        throw new RuntimeException(e.toString());
    }
}

From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java

public static String getShortFilename(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    try {/*from   w ww  . j av  a  2  s  .  com*/
        if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
            if (ArgList[0].equals(null)) {
                return null;
            }
            FileObject file = null;

            try {
                // Source file
                file = KettleVFS.getFileObject((String) ArgList[0]);
                String Filename = null;
                if (file.exists()) {
                    Filename = file.getName().getBaseName().toString();

                } else {
                    new RuntimeException("file [" + (String) ArgList[0] + "] can not be found!");
                }

                return Filename;
            } catch (IOException e) {
                throw new RuntimeException(
                        "The function call getShortFilename throw an error : " + e.toString());
            } finally {
                if (file != null) {
                    try {
                        file.close();
                    } catch (Exception e) {
                        // Ignore errors
                    }
                }
            }

        } else {
            throw new RuntimeException("The function call getShortFilename is not valid.");
        }
    } catch (Exception e) {
        throw new RuntimeException(e.toString());
    }
}

From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java

public static String getFileExtension(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    try {/*  w  w w  .  j  a v  a  2s  .  c o  m*/
        if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
            if (ArgList[0].equals(null)) {
                return null;
            }
            FileObject file = null;

            try {
                // Source file
                file = KettleVFS.getFileObject((String) ArgList[0]);
                String Extension = null;
                if (file.exists()) {
                    Extension = file.getName().getExtension().toString();

                } else {
                    new RuntimeException("file [" + (String) ArgList[0] + "] can not be found!");
                }

                return Extension;
            } catch (IOException e) {
                throw new RuntimeException(
                        "The function call getFileExtension throw an error : " + e.toString());
            } finally {
                if (file != null) {
                    try {
                        file.close();
                    } catch (Exception e) {
                        // Ignore errors
                    }
                }
            }

        } else {
            throw new RuntimeException("The function call getFileExtension is not valid.");
        }
    } catch (Exception e) {
        throw new RuntimeException(e.toString());
    }
}

From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java

public static String getParentFoldername(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    try {/*from   w  w  w.  j  a  va 2 s. c om*/
        if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
            if (ArgList[0].equals(null)) {
                return null;
            }
            FileObject file = null;

            try {
                // Source file
                file = KettleVFS.getFileObject((String) ArgList[0]);
                String foldername = null;
                if (file.exists()) {
                    foldername = KettleVFS.getFilename(file.getParent());

                } else {
                    new RuntimeException("file [" + (String) ArgList[0] + "] can not be found!");
                }

                return foldername;
            } catch (IOException e) {
                throw new RuntimeException(
                        "The function call getParentFoldername throw an error : " + e.toString());
            } finally {
                if (file != null) {
                    try {
                        file.close();
                    } catch (Exception e) {
                        // Ignore errors
                    }
                }
            }

        } else {
            throw new RuntimeException("The function call getParentFoldername is not valid.");
        }
    } catch (Exception e) {
        throw new RuntimeException(e.toString());
    }
}

From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java

public static String getLastModifiedTime(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    try {/* ww  w.j a v a2  s.c  o m*/
        if (ArgList.length == 2 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
            if (ArgList[0].equals(null)) {
                return null;
            }
            FileObject file = null;

            try {
                // Source file
                file = KettleVFS.getFileObject((String) ArgList[0]);
                String dateformat = (String) ArgList[1];
                if (isNull(dateformat)) {
                    dateformat = "yyyy-MM-dd";
                }
                String lastmodifiedtime = null;
                if (file.exists()) {
                    java.util.Date lastmodifiedtimedate = new java.util.Date(
                            file.getContent().getLastModifiedTime());
                    java.text.DateFormat dateFormat = new SimpleDateFormat(dateformat);
                    lastmodifiedtime = dateFormat.format(lastmodifiedtimedate);

                } else {
                    new RuntimeException("file [" + (String) ArgList[0] + "] can not be found!");
                }

                return lastmodifiedtime;
            } catch (IOException e) {
                throw new RuntimeException(
                        "The function call getLastModifiedTime throw an error : " + e.toString());
            } finally {
                if (file != null) {
                    try {
                        file.close();
                    } catch (Exception e) {
                        // Ignore errors
                    }
                }
            }

        } else {
            throw new RuntimeException("The function call getLastModifiedTime is not valid.");
        }
    } catch (Exception e) {
        throw new RuntimeException(e.toString());
    }
}

From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java

public static void moveFile(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {

    try {// w ww .  j  av a2 s  .  c  o m
        if (ArgList.length == 3 && !isNull(ArgList[0]) && !isNull(ArgList[1]) && !isUndefined(ArgList[0])
                && !isUndefined(ArgList[1])) {
            FileObject fileSource = null, fileDestination = null;

            try {
                // Source file to move
                fileSource = KettleVFS.getFileObject((String) ArgList[0]);
                // Destination filename
                fileDestination = KettleVFS.getFileObject((String) ArgList[1]);
                if (fileSource.exists()) {
                    // Source file exists...
                    if (fileSource.getType() == FileType.FILE) {
                        // Great..source is a file ...
                        boolean overwrite = false;
                        if (!ArgList[1].equals(null)) {
                            overwrite = (Boolean) ArgList[2];
                        }
                        boolean destinationExists = fileDestination.exists();
                        // Let's move the file...
                        if ((destinationExists && overwrite) || !destinationExists) {
                            fileSource.moveTo(fileDestination);
                        }

                    }
                } else {
                    new RuntimeException("file to move [" + (String) ArgList[0] + "] can not be found!");
                }
            } catch (IOException e) {
                throw new RuntimeException("The function call moveFile throw an error : " + e.toString());
            } finally {
                if (fileSource != null) {
                    try {
                        fileSource.close();
                    } catch (Exception e) {
                        // Ignore errors
                    }
                }
                if (fileDestination != null) {
                    try {
                        fileDestination.close();
                    } catch (Exception e) {
                        // Ignore errors
                    }
                }
            }

        } else {
            throw new RuntimeException("The function call copyFile is not valid.");
        }
    } catch (Exception e) {
        throw new RuntimeException(e.toString());
    }
}

From source file:org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java

public static void deleteFile(Context actualContext, Scriptable actualObject, Object[] ArgList,
        Function FunctionContext) {

    try {/* w  w  w .j av a  2 s  . com*/
        if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
            // Object act = actualObject.get("_step_", actualObject);
            // ScriptValuesMod act = (ScriptValuesMod)Context.toType(scm_delete, ScriptValuesMod.class);

            FileObject fileObject = null;

            try {
                fileObject = KettleVFS.getFileObject(Context.toString(ArgList[0]));
                if (fileObject.exists()) {
                    if (fileObject.getType() == FileType.FILE) {
                        if (!fileObject.delete()) {
                            Context.reportRuntimeError(
                                    "We can not delete file [" + Context.toString(ArgList[0]) + "]!");
                        }
                    }

                } else {
                    Context.reportRuntimeError("file [" + Context.toString(ArgList[0]) + "] can not be found!");
                }
            } catch (IOException e) {
                throw Context.reportRuntimeError("The function call deleteFile is not valid.");
            } finally {
                if (fileObject != null) {
                    try {
                        fileObject.close();
                    } catch (Exception e) {
                        // Ignore errors
                    }
                }
            }

        } else {
            throw Context.reportRuntimeError("The function call deleteFile is not valid.");
        }
    } catch (Exception e) {
        throw Context.reportRuntimeError(e.toString());
    }
}

From source file:org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java

public static void createFolder(Context actualContext, Scriptable actualObject, Object[] ArgList,
        Function FunctionContext) {

    try {/* w w w  .ja v  a2 s.c om*/
        if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
            FileObject fileObject = null;

            try {
                fileObject = KettleVFS.getFileObject(Context.toString(ArgList[0]));
                if (!fileObject.exists()) {
                    fileObject.createFolder();
                } else {
                    Context.reportRuntimeError("folder [" + Context.toString(ArgList[0]) + "] already exist!");
                }
            } catch (IOException e) {
                throw Context.reportRuntimeError("The function call createFolder is not valid.");
            } finally {
                if (fileObject != null) {
                    try {
                        fileObject.close();
                    } catch (Exception e) {
                        // Ignore errors
                    }
                }
            }

        } else {
            throw Context.reportRuntimeError("The function call createFolder is not valid.");
        }
    } catch (Exception e) {
        throw Context.reportRuntimeError(e.toString());
    }
}

From source file:org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java

public static void copyFile(Context actualContext, Scriptable actualObject, Object[] ArgList,
        Function FunctionContext) {

    try {//  w  ww. j a v a2  s. c  om
        if (ArgList.length == 3 && !isNull(ArgList[0]) && !isNull(ArgList[1]) && !isUndefined(ArgList[0])
                && !isUndefined(ArgList[1])) {
            FileObject fileSource = null, fileDestination = null;

            try {
                // Source file to copy
                fileSource = KettleVFS.getFileObject(Context.toString(ArgList[0]));
                // Destination filename
                fileDestination = KettleVFS.getFileObject(Context.toString(ArgList[1]));
                if (fileSource.exists()) {
                    // Source file exists...
                    if (fileSource.getType() == FileType.FILE) {
                        // Great..source is a file ...
                        boolean overwrite = false;
                        if (!ArgList[1].equals(null)) {
                            overwrite = Context.toBoolean(ArgList[2]);
                        }
                        boolean destinationExists = fileDestination.exists();
                        // Let's copy the file...
                        if ((destinationExists && overwrite) || !destinationExists) {
                            FileUtil.copyContent(fileSource, fileDestination);
                        }

                    }
                } else {
                    Context.reportRuntimeError(
                            "file to copy [" + Context.toString(ArgList[0]) + "] can not be found!");
                }
            } catch (IOException e) {
                throw Context.reportRuntimeError("The function call copyFile throw an error : " + e.toString());
            } finally {
                if (fileSource != null) {
                    try {
                        fileSource.close();
                    } catch (Exception e) {
                        // Ignore errors
                    }
                }
                if (fileDestination != null) {
                    try {
                        fileDestination.close();
                    } catch (Exception e) {
                        // Ignore errors
                    }
                }
            }

        } else {
            throw Context.reportRuntimeError("The function call copyFileis not valid.");
        }
    } catch (Exception e) {
        throw Context.reportRuntimeError(e.toString());
    }
}

From source file:org.pentaho.di.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java

public static double getFileSize(Context actualContext, Scriptable actualObject, Object[] ArgList,
        Function FunctionContext) {
    try {//w  w  w . j av a2  s.  c o m
        if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
            if (ArgList[0].equals(null)) {
                return 0;
            }
            FileObject file = null;

            try {
                // Source file
                file = KettleVFS.getFileObject(Context.toString(ArgList[0]));
                long filesize = 0;
                if (file.exists()) {
                    if (file.getType().equals(FileType.FILE)) {
                        filesize = file.getContent().getSize();
                    } else {
                        Context.reportRuntimeError("[" + Context.toString(ArgList[0]) + "] is not a file!");
                    }
                } else {
                    Context.reportRuntimeError("file [" + Context.toString(ArgList[0]) + "] can not be found!");
                }
                return filesize;
            } catch (IOException e) {
                throw Context
                        .reportRuntimeError("The function call getFileSize throw an error : " + e.toString());
            } finally {
                if (file != null) {
                    try {
                        file.close();
                    } catch (Exception e) {
                        // Ignore close errors
                    }
                }
            }

        } else {
            throw Context.reportRuntimeError("The function call getFileSize is not valid.");
        }
    } catch (Exception e) {
        throw Context.reportRuntimeError(e.toString());
    }
}