Search in sources :

Example 1 with FileObject

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();
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) Table(org.eclipse.swt.widgets.Table) InputStreamReader(java.io.InputStreamReader) CompressionInputStream(org.pentaho.di.core.compress.CompressionInputStream) CompressionInputStream(org.pentaho.di.core.compress.CompressionInputStream) InputStream(java.io.InputStream) TableItem(org.eclipse.swt.widgets.TableItem) EncodingType(org.pentaho.di.trans.steps.fileinput.text.EncodingType) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) IOException(java.io.IOException) KettleException(org.pentaho.di.core.exception.KettleException) IOException(java.io.IOException) MessageBox(org.eclipse.swt.widgets.MessageBox) CompressionProvider(org.pentaho.di.core.compress.CompressionProvider) TextFileInputMeta(org.pentaho.di.trans.steps.fileinput.text.TextFileInputMeta) EnterTextDialog(org.pentaho.di.ui.core.dialog.EnterTextDialog) FileObject(org.apache.commons.vfs2.FileObject) EnterNumberDialog(org.pentaho.di.ui.core.dialog.EnterNumberDialog) FileInputList(org.pentaho.di.core.fileinput.FileInputList)

Example 2 with FileObject

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
            }
        }
    }
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) IOException(java.io.IOException)

Example 3 with FileObject

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);
            }
        }
    }
}
Also used : FileSelector(org.apache.commons.vfs2.FileSelector) Document(org.w3c.dom.Document) FileSelectInfo(org.apache.commons.vfs2.FileSelectInfo) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) IOException(java.io.IOException) Variables(org.pentaho.di.core.variables.Variables) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) FileObject(org.apache.commons.vfs2.FileObject) FileInputList(org.pentaho.di.core.fileinput.FileInputList)

Example 4 with 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;
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 5 with FileObject

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;
}
Also used : LocalFile(org.apache.commons.vfs2.provider.local.LocalFile) FileObject(org.apache.commons.vfs2.FileObject) CheckedInputStream(java.util.zip.CheckedInputStream) Adler32(java.util.zip.Adler32) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)643 KettleException (org.pentaho.di.core.exception.KettleException)206 IOException (java.io.IOException)205 FileSystemException (org.apache.commons.vfs2.FileSystemException)172 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)50 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