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());
}
}
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());
}
}
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());
}
}
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());
}
}
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());
}
}
Aggregations