Search in sources :

Example 56 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class ScriptAddedFunctions method isFile.

public static boolean isFile(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) {
    try {
        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 isafile = false;
                if (file.exists()) {
                    if (file.getType().equals(FileType.FILE)) {
                        isafile = true;
                    } else {
                        throw new RuntimeException("[" + ArgList[0] + "] is not a file!");
                    }
                } else {
                    throw new RuntimeException("file [" + ArgList[0] + "] can not be found!");
                }
                return isafile;
            } catch (IOException e) {
                throw new RuntimeException("The function call is File throw an error : " + e.toString());
            } finally {
                if (file != null) {
                    try {
                        file.close();
                    } catch (Exception e) {
                    // Ignore errors
                    }
                }
            }
        } else {
            throw new RuntimeException("The function call isFile is not valid.");
        }
    } catch (Exception e) {
        throw new RuntimeException(e.toString());
    }
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) ScriptException(javax.script.ScriptException) IOException(java.io.IOException)

Example 57 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class ScriptAddedFunctions method getLastModifiedTime.

public static String getLastModifiedTime(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) {
    try {
        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 {
                    throw new RuntimeException("file [" + 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());
    }
}
Also used : Date(java.util.Date) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) DateFormat(java.text.DateFormat) KettleFileException(org.pentaho.di.core.exception.KettleFileException) ScriptException(javax.script.ScriptException) IOException(java.io.IOException)

Example 58 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class ScriptValuesAddedFunctions method copyFile.

public static void copyFile(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) {
    try {
        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());
    }
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) FileNotFoundException(java.io.FileNotFoundException) WrappedException(org.mozilla.javascript.WrappedException) EvaluatorException(org.mozilla.javascript.EvaluatorException) JavaScriptException(org.mozilla.javascript.JavaScriptException) IOException(java.io.IOException)

Example 59 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class ScriptValuesAddedFunctions method getLastModifiedTime.

public static String getLastModifiedTime(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) {
    try {
        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(Context.toString(ArgList[0]));
                String dateformat = Context.toString(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 {
                    Context.reportRuntimeError("file [" + Context.toString(ArgList[0]) + "] can not be found!");
                }
                return lastmodifiedtime;
            } catch (IOException e) {
                throw Context.reportRuntimeError("The function call getLastModifiedTime throw an error : " + e.toString());
            } finally {
                if (file != null) {
                    try {
                        file.close();
                    } catch (Exception e) {
                    // Ignore errors
                    }
                }
            }
        } else {
            throw Context.reportRuntimeError("The function call getLastModifiedTime is not valid.");
        }
    } catch (Exception e) {
        throw Context.reportRuntimeError(e.toString());
    }
}
Also used : Date(java.util.Date) FileObject(org.apache.commons.vfs2.FileObject) Context(org.mozilla.javascript.Context) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) DateFormat(java.text.DateFormat) KettleFileException(org.pentaho.di.core.exception.KettleFileException) FileNotFoundException(java.io.FileNotFoundException) WrappedException(org.mozilla.javascript.WrappedException) EvaluatorException(org.mozilla.javascript.EvaluatorException) JavaScriptException(org.mozilla.javascript.JavaScriptException) IOException(java.io.IOException)

Example 60 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class ScriptValuesAddedFunctions method getFileExtension.

public static String getFileExtension(Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext) {
    try {
        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(Context.toString(ArgList[0]));
                String Extension = null;
                if (file.exists()) {
                    Extension = file.getName().getExtension().toString();
                } else {
                    Context.reportRuntimeError("file [" + Context.toString(ArgList[0]) + "] can not be found!");
                }
                return Extension;
            } catch (IOException e) {
                throw Context.reportRuntimeError("The function call getFileExtension throw an error : " + e.toString());
            } finally {
                if (file != null) {
                    try {
                        file.close();
                    } catch (Exception e) {
                    // Ignore errors
                    }
                }
            }
        } else {
            throw Context.reportRuntimeError("The function call getFileExtension is not valid.");
        }
    } catch (Exception e) {
        throw Context.reportRuntimeError(e.toString());
    }
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) FileNotFoundException(java.io.FileNotFoundException) WrappedException(org.mozilla.javascript.WrappedException) EvaluatorException(org.mozilla.javascript.EvaluatorException) JavaScriptException(org.mozilla.javascript.JavaScriptException) IOException(java.io.IOException)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)646 KettleException (org.pentaho.di.core.exception.KettleException)206 IOException (java.io.IOException)203 FileSystemException (org.apache.commons.vfs2.FileSystemException)173 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)104 KettleFileException (org.pentaho.di.core.exception.KettleFileException)97 Test (org.junit.Test)82 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)68 File (java.io.File)60 InputStream (java.io.InputStream)48 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)37 KettleStepException (org.pentaho.di.core.exception.KettleStepException)36 ArrayList (java.util.ArrayList)35 ResultFile (org.pentaho.di.core.ResultFile)33 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)32 Result (org.pentaho.di.core.Result)32 OutputStream (java.io.OutputStream)29 FileName (org.apache.commons.vfs2.FileName)29 KettleValueException (org.pentaho.di.core.exception.KettleValueException)29 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)28