use of org.pentaho.di.core.exception.KettleFileException in project pentaho-kettle by pentaho.
the class KettleVFS method getFileObject.
public static FileObject getFileObject(String vfsFilename, VariableSpace space, FileSystemOptions fsOptions) throws KettleFileException {
try {
FileSystemManager fsManager = getInstance().getFileSystemManager();
// We have one problem with VFS: if the file is in a subdirectory of the current one: somedir/somefile
// In that case, VFS doesn't parse the file correctly.
// We need to put file: in front of it to make it work.
// However, how are we going to verify this?
//
// We are going to see if the filename starts with one of the known protocols like file: zip: ram: smb: jar: etc.
// If not, we are going to assume it's a file.
//
boolean relativeFilename = true;
String[] schemes = fsManager.getSchemes();
for (int i = 0; i < schemes.length && relativeFilename; i++) {
if (vfsFilename.startsWith(schemes[i] + ":")) {
relativeFilename = false;
// We have a VFS URL, load any options for the file system driver
fsOptions = buildFsOptions(space, fsOptions, vfsFilename, schemes[i]);
}
}
String filename;
if (vfsFilename.startsWith("\\\\")) {
File file = new File(vfsFilename);
filename = file.toURI().toString();
} else {
if (relativeFilename) {
File file = new File(vfsFilename);
filename = file.getAbsolutePath();
} else {
filename = vfsFilename;
}
}
if (fsOptions != null) {
return fsManager.resolveFile(filename, fsOptions);
} else {
return fsManager.resolveFile(filename);
}
} catch (IOException e) {
throw new KettleFileException("Unable to get VFS File object for filename '" + cleanseFilename(vfsFilename) + "' : " + e.getMessage(), e);
}
}
use of org.pentaho.di.core.exception.KettleFileException in project pentaho-kettle by pentaho.
the class TransMeta method setInternalFilenameKettleVariables.
/**
* Sets the internal filename kettle variables.
*
* @param var
* the new internal filename kettle variables
*/
@Override
protected void setInternalFilenameKettleVariables(VariableSpace var) {
//
if (!Utils.isEmpty(filename)) {
try {
FileObject fileObject = KettleVFS.getFileObject(filename, var);
FileName fileName = fileObject.getName();
// The filename of the transformation
variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_NAME, fileName.getBaseName());
// The directory of the transformation
FileName fileDir = fileName.getParent();
variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_DIRECTORY, fileDir.getURI());
} catch (KettleFileException e) {
log.logError("Unexpected error setting internal filename variables!", e);
variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_DIRECTORY, "");
variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_NAME, "");
}
} else {
variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_DIRECTORY, "");
variables.setVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_NAME, "");
}
variables.setVariable(Const.INTERNAL_VARIABLE_ENTRY_CURRENT_DIRECTORY, variables.getVariable(repository != null ? Const.INTERNAL_VARIABLE_TRANSFORMATION_REPOSITORY_DIRECTORY : Const.INTERNAL_VARIABLE_TRANSFORMATION_FILENAME_DIRECTORY));
}
use of org.pentaho.di.core.exception.KettleFileException in project pentaho-kettle by pentaho.
the class BaseFileInputStep method filesFromPreviousStep.
/**
* Read files from previous step.
*/
private RowMetaInterface[] filesFromPreviousStep() throws KettleException {
RowMetaInterface[] infoStep = null;
data.files.getFiles().clear();
int idx = -1;
RowSet rowSet = findInputRowSet(meta.inputFiles.acceptingStepName);
Object[] fileRow = getRowFrom(rowSet);
while (fileRow != null) {
RowMetaInterface prevInfoFields = rowSet.getRowMeta();
if (idx < 0) {
if (meta.inputFiles.passingThruFields) {
data.passThruFields = new HashMap<FileObject, Object[]>();
infoStep = new RowMetaInterface[] { prevInfoFields };
data.nrPassThruFields = prevInfoFields.size();
}
idx = prevInfoFields.indexOfValue(meta.inputFiles.acceptingField);
if (idx < 0) {
logError(BaseMessages.getString(PKG, "TextFileInput.Log.Error.UnableToFindFilenameField", meta.inputFiles.acceptingField));
setErrors(getErrors() + 1);
stopAll();
return null;
}
}
String fileValue = prevInfoFields.getString(fileRow, idx);
try {
FileObject fileObject = KettleVFS.getFileObject(fileValue, getTransMeta());
data.files.addFile(fileObject);
if (meta.inputFiles.passingThruFields) {
data.passThruFields.put(fileObject, fileRow);
}
} catch (KettleFileException e) {
logError(BaseMessages.getString(PKG, "TextFileInput.Log.Error.UnableToCreateFileObject", fileValue), e);
}
// Grab another row
fileRow = getRowFrom(rowSet);
}
if (data.files.nrOfFiles() == 0) {
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "TextFileInput.Log.Error.NoFilesSpecified"));
}
return null;
}
return infoStep;
}
use of org.pentaho.di.core.exception.KettleFileException in project pentaho-kettle by pentaho.
the class TextFileInputUtils method getLine.
public static final String getLine(LogChannelInterface log, InputStreamReader reader, EncodingType encodingType, int formatNr, StringBuilder line) throws KettleFileException {
int c = 0;
line.setLength(0);
try {
switch(formatNr) {
case TextFileInputMeta.FILE_FORMAT_DOS:
while (c >= 0) {
c = reader.read();
if (encodingType.isReturn(c) || encodingType.isLinefeed(c)) {
// skip \n and \r
c = reader.read();
if (!encodingType.isReturn(c) && !encodingType.isLinefeed(c)) {
// so we have pulled a character from the next line
throw new KettleFileException(BaseMessages.getString(PKG, "TextFileInput.Log.SingleLineFound"));
}
return line.toString();
}
if (c >= 0) {
line.append((char) c);
}
}
break;
case TextFileInputMeta.FILE_FORMAT_UNIX:
while (c >= 0) {
c = reader.read();
if (encodingType.isLinefeed(c) || encodingType.isReturn(c)) {
return line.toString();
}
if (c >= 0) {
line.append((char) c);
}
}
break;
case TextFileInputMeta.FILE_FORMAT_MIXED:
// not for MAC OS 9 but works for Mac OS X. Mac OS 9 can use UNIX-Format
while (c >= 0) {
c = reader.read();
if (encodingType.isLinefeed(c)) {
return line.toString();
} else if (!encodingType.isReturn(c)) {
if (c >= 0) {
line.append((char) c);
}
}
}
break;
default:
break;
}
} catch (KettleFileException e) {
throw e;
} catch (Exception e) {
if (line.length() == 0) {
throw new KettleFileException(BaseMessages.getString(PKG, "TextFileInput.Log.Error.ExceptionReadingLine", e.toString()), e);
}
return line.toString();
}
if (line.length() > 0) {
return line.toString();
}
return null;
}
use of org.pentaho.di.core.exception.KettleFileException in project pentaho-kettle by pentaho.
the class FilesFromResultMeta method getFields.
public void getFields(RowMetaInterface r, String name, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space, Repository repository, IMetaStore metaStore) throws KettleStepException {
// Add the fields from a ResultFile
try {
ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, KettleVFS.getFileObject("foo.bar", space), "parentOrigin", "origin");
RowMetaAndData add = resultFile.getRow();
// Set the origin on the fields...
for (int i = 0; i < add.size(); i++) {
add.getValueMeta(i).setOrigin(name);
}
r.addRowMeta(add.getRowMeta());
} catch (KettleFileException e) {
throw new KettleStepException(e);
}
}
Aggregations