use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.
the class TextFileInputDialog method getCSV.
// Get the data layout
private void getCSV() {
TextFileInputMeta meta = new TextFileInputMeta();
getInfo(meta, true);
// CSV without separator defined
if (meta.content.fileType.equalsIgnoreCase("CSV") && (meta.content.separator == null || meta.content.separator.isEmpty())) {
MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
mb.setMessage(BaseMessages.getString(PKG, "TextFileInput.Exception.NoSeparator"));
mb.setText(BaseMessages.getString(PKG, "TextFileInputDialog.DialogTitle"));
mb.open();
return;
}
TextFileInputMeta previousMeta = (TextFileInputMeta) meta.clone();
FileInputList textFileList = meta.getFileInputList(transMeta);
InputStream fileInputStream;
CompressionInputStream inputStream = null;
StringBuilder lineStringBuilder = new StringBuilder(256);
int fileFormatType = meta.getFileFormatTypeNr();
String delimiter = transMeta.environmentSubstitute(meta.content.separator);
String enclosure = transMeta.environmentSubstitute(meta.content.enclosure);
String escapeCharacter = transMeta.environmentSubstitute(meta.content.escapeCharacter);
if (textFileList.nrOfFiles() > 0) {
int clearFields = meta.content.header ? SWT.YES : SWT.NO;
int nrInputFields = meta.inputFields.length;
if (nrInputFields > 0) {
MessageBox mb = new MessageBox(shell, SWT.YES | SWT.NO | SWT.CANCEL | SWT.ICON_QUESTION);
mb.setMessage(BaseMessages.getString(PKG, "TextFileInputDialog.ClearFieldList.DialogMessage"));
mb.setText(BaseMessages.getString(PKG, "TextFileInputDialog.ClearFieldList.DialogTitle"));
clearFields = mb.open();
if (clearFields == SWT.CANCEL) {
return;
}
}
try {
wFields.table.removeAll();
FileObject fileObject = textFileList.getFile(0);
fileInputStream = KettleVFS.getInputStream(fileObject);
Table table = wFields.table;
CompressionProvider provider = CompressionProviderFactory.getInstance().createCompressionProviderInstance(meta.content.fileCompression);
inputStream = provider.createInputStream(fileInputStream);
InputStreamReader reader;
if (meta.getEncoding() != null && meta.getEncoding().length() > 0) {
reader = new InputStreamReader(inputStream, meta.getEncoding());
} else {
reader = new InputStreamReader(inputStream);
}
EncodingType encodingType = EncodingType.guessEncodingType(reader.getEncoding());
// Scan the header-line, determine fields...
String line = TextFileInputUtils.getLine(log, reader, encodingType, fileFormatType, lineStringBuilder);
if (line != null) {
// Estimate the number of input fields...
// Chop up the line using the delimiter
String[] fields = TextFileInputUtils.guessStringsFromLine(transMeta, log, line, meta, delimiter, enclosure, escapeCharacter);
for (int i = 0; i < fields.length; i++) {
String field = fields[i];
if (field == null || field.length() == 0 || !meta.content.header) {
field = "Field" + (i + 1);
} else {
// Trim the field
field = Const.trim(field);
// Replace all spaces & - with underscore _
field = Const.replace(field, " ", "_");
field = Const.replace(field, "-", "_");
}
TableItem item = new TableItem(table, SWT.NONE);
item.setText(1, field);
// The default type is String...
item.setText(2, "String");
}
wFields.setRowNums();
wFields.optWidth(true);
// Copy it...
getInfo(meta, true);
// Sample a few lines to determine the correct type of the fields...
String shellText = BaseMessages.getString(PKG, "TextFileInputDialog.LinesToSample.DialogTitle");
String lineText = BaseMessages.getString(PKG, "TextFileInputDialog.LinesToSample.DialogMessage");
EnterNumberDialog end = new EnterNumberDialog(shell, 100, shellText, lineText);
int samples = end.open();
if (samples >= 0) {
getInfo(meta, true);
TextFileCSVImportProgressDialog pd = new TextFileCSVImportProgressDialog(shell, meta, transMeta, reader, samples, clearFields == SWT.YES);
String message = pd.open();
if (message != null) {
wFields.removeAll();
// OK, what's the result of our search?
getData(meta);
//
if (clearFields == SWT.NO) {
getFieldsData(previousMeta, true);
wFields.table.setSelection(previousMeta.inputFields.length, wFields.table.getItemCount() - 1);
}
wFields.removeEmptyRows();
wFields.setRowNums();
wFields.optWidth(true);
EnterTextDialog etd = new EnterTextDialog(shell, BaseMessages.getString(PKG, "TextFileInputDialog.ScanResults.DialogTitle"), BaseMessages.getString(PKG, "TextFileInputDialog.ScanResults.DialogMessage"), message, true);
etd.setReadOnly();
etd.open();
}
}
} else {
MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
mb.setMessage(BaseMessages.getString(PKG, "TextFileInputDialog.UnableToReadHeaderLine.DialogMessage"));
mb.setText(BaseMessages.getString(PKG, "System.Dialog.Error.Title"));
mb.open();
}
} catch (IOException e) {
new ErrorDialog(shell, BaseMessages.getString(PKG, "TextFileInputDialog.IOError.DialogTitle"), BaseMessages.getString(PKG, "TextFileInputDialog.IOError.DialogMessage"), e);
} catch (KettleException e) {
new ErrorDialog(shell, BaseMessages.getString(PKG, "System.Dialog.Error.Title"), BaseMessages.getString(PKG, "TextFileInputDialog.ErrorGettingFileDesc.DialogMessage"), e);
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
// Ignore errors
}
}
} else {
MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
mb.setMessage(BaseMessages.getString(PKG, "TextFileInputDialog.NoValidFileFound.DialogMessage"));
mb.setText(BaseMessages.getString(PKG, "System.Dialog.Error.Title"));
mb.open();
}
}
use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.
the class JobEntryCheckFilesLocked method ProcessFile.
private void ProcessFile(String filename, String wildcard) {
FileObject filefolder = null;
String realFilefoldername = environmentSubstitute(filename);
String realwilcard = environmentSubstitute(wildcard);
try {
filefolder = KettleVFS.getFileObject(realFilefoldername);
FileObject[] files = new FileObject[] { filefolder };
if (filefolder.exists()) {
// the file or folder exists
if (filefolder.getType() == FileType.FOLDER) {
// It's a folder
if (isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobEntryCheckFilesLocked.ProcessingFolder", realFilefoldername));
}
// Retrieve all files
files = filefolder.findFiles(new TextFileSelector(filefolder.toString(), realwilcard));
if (isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobEntryCheckFilesLocked.TotalFilesToCheck", String.valueOf(files.length)));
}
} else {
// It's a file
if (isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "JobEntryCheckFilesLocked.ProcessingFile", realFilefoldername));
}
}
// Check files locked
checkFilesLocked(files);
} else {
// We can not find thsi file
logBasic(BaseMessages.getString(PKG, "JobEntryCheckFilesLocked.FileNotExist", realFilefoldername));
}
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "JobEntryCheckFilesLocked.CouldNotProcess", realFilefoldername, e.getMessage()));
} finally {
if (filefolder != null) {
try {
filefolder.close();
} catch (IOException ex) {
// Ignore
}
}
}
}
use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.
the class MessagesSourceCrawler method crawl.
public void crawl() throws Exception {
for (final String sourceDirectory : sourceDirectories) {
FileObject folder = KettleVFS.getFileObject(sourceDirectory);
FileObject[] javaFiles = folder.findFiles(new FileSelector() {
@Override
public boolean traverseDescendents(FileSelectInfo info) throws Exception {
return true;
}
@Override
public boolean includeFile(FileSelectInfo info) throws Exception {
return info.getFile().getName().getExtension().equals("java");
}
});
for (FileObject javaFile : javaFiles) {
/**
* We don't want the Messages.java files, there is nothing in there for us.
*/
boolean skip = false;
for (String filename : filesToAvoid) {
if (javaFile.getName().getBaseName().equals(filename)) {
skip = true;
}
}
if (skip) {
// don't process this file.
continue;
}
// For each of these files we look for keys...
//
lookForOccurrencesInFile(sourceDirectory, javaFile);
}
}
//
for (SourceCrawlerXMLFolder xmlFolder : xmlFolders) {
String[] xmlDirs = { xmlFolder.getFolder() };
String[] xmlMasks = { xmlFolder.getWildcard() };
String[] xmlReq = { "N" };
// search sub-folders too
boolean[] xmlSubdirs = { true };
FileInputList xulFileInputList = FileInputList.createFileList(new Variables(), xmlDirs, xmlMasks, xmlReq, xmlSubdirs);
for (FileObject fileObject : xulFileInputList.getFiles()) {
try {
Document doc = XMLHandler.loadXMLFile(fileObject);
//
for (SourceCrawlerXMLElement xmlElement : xmlFolder.getElements()) {
addLabelOccurrences(xmlFolder.getDefaultSourceFolder(), fileObject, doc.getElementsByTagName(xmlElement.getSearchElement()), xmlFolder.getKeyPrefix(), xmlElement.getKeyTag(), xmlElement.getKeyAttribute(), xmlFolder.getDefaultPackage(), xmlFolder.getPackageExceptions());
}
} catch (KettleXMLException e) {
log.logError("Unable to open XUL / XML document: " + fileObject);
}
}
}
}
use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.
the class ValueDataUtil method isXMLFileWellFormed.
/**
* Checks an xml file is well formed.
*
* @param metaA
* The ValueMetaInterface
* @param dataA
* The value (filename)
* @return true if the file is well formed.
*/
public static boolean isXMLFileWellFormed(ValueMetaInterface metaA, Object dataA) {
if (dataA == null) {
return false;
}
String filename = dataA.toString();
FileObject file = null;
try {
file = KettleVFS.getFileObject(filename);
return XMLCheck.isXMLFileWellFormed(file);
} catch (Exception e) {
// ignore - we'll return false although would be nice to log it.
} finally {
if (file != null) {
try {
file.close();
} catch (Exception e) {
// Ignore
}
}
}
return false;
}
use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.
the class ValueDataUtil method ChecksumAdler32.
public static Long ChecksumAdler32(ValueMetaInterface metaA, Object dataA) {
long checksum = 0;
FileObject file = null;
try {
file = KettleVFS.getFileObject(dataA.toString());
CheckedInputStream cis = null;
// Computer Adler-32 checksum
cis = new CheckedInputStream(((LocalFile) file).getInputStream(), new Adler32());
byte[] buf = new byte[128];
int readSize = 0;
do {
readSize = cis.read(buf);
} while (readSize >= 0);
checksum = cis.getChecksum().getValue();
} catch (Exception e) {
// throw new Exception(e);
} finally {
if (file != null) {
try {
file.close();
file = null;
} catch (Exception e) {
// Ignore
}
}
}
return checksum;
}
Aggregations