Search in sources :

Example 11 with LocalFile

use of org.apache.commons.vfs2.provider.local.LocalFile in project pentaho-kettle by pentaho.

the class CsvInputDialog method getCSV.

// Get the data layout
private void getCSV() {
    InputStream inputStream = null;
    try {
        CsvInputMeta meta = new CsvInputMeta();
        getInfo(meta);
        String filename = transMeta.environmentSubstitute(meta.getFilename());
        String delimiter = transMeta.environmentSubstitute(meta.getDelimiter());
        String enclosure = transMeta.environmentSubstitute(meta.getEnclosure());
        FileObject fileObject = KettleVFS.getFileObject(filename);
        if (!(fileObject instanceof LocalFile)) {
            // 
            throw new KettleException(BaseMessages.getString(PKG, "CsvInput.Log.OnlyLocalFilesAreSupported"));
        }
        wFields.table.removeAll();
        inputStream = KettleVFS.getInputStream(fileObject);
        String realEncoding = transMeta.environmentSubstitute(meta.getEncoding());
        InputStreamReader reader;
        if (Utils.isEmpty(realEncoding)) {
            reader = new InputStreamReader(inputStream);
        } else {
            reader = new InputStreamReader(inputStream, realEncoding);
        }
        EncodingType encodingType = EncodingType.guessEncodingType(reader.getEncoding());
        // Read a line of data to determine the number of rows...
        // 
        String line = TextFileInput.getLine(log, reader, encodingType, TextFileInputMeta.FILE_FORMAT_UNIX, new StringBuilder(1000));
        // Split the string, header or data into parts...
        // 
        String[] fieldNames = CsvInput.guessStringsFromLine(log, line, delimiter, enclosure, meta.getEscapeCharacter());
        if (!meta.isHeaderPresent()) {
            // Don't use field names from the header...
            // Generate field names F1 ... F10
            // 
            DecimalFormat df = new DecimalFormat("000");
            for (int i = 0; i < fieldNames.length; i++) {
                fieldNames[i] = "Field_" + df.format(i);
            }
        } else {
            if (!Utils.isEmpty(meta.getEnclosure())) {
                for (int i = 0; i < fieldNames.length; i++) {
                    if (fieldNames[i].startsWith(meta.getEnclosure()) && fieldNames[i].endsWith(meta.getEnclosure()) && fieldNames[i].length() > 1) {
                        fieldNames[i] = fieldNames[i].substring(1, fieldNames[i].length() - 1);
                    }
                }
            }
        }
        // 
        for (int i = 0; i < fieldNames.length; i++) {
            fieldNames[i] = Const.trim(fieldNames[i]);
        }
        // 
        for (int i = 0; i < fieldNames.length; i++) {
            TableItem item = new TableItem(wFields.table, SWT.NONE);
            item.setText(1, fieldNames[i]);
            item.setText(2, ValueMetaFactory.getValueMetaName(ValueMetaInterface.TYPE_STRING));
        }
        wFields.removeEmptyRows();
        wFields.setRowNums();
        wFields.optWidth(true);
        // Now we can continue reading the rows of data and we can guess the
        // Sample a few lines to determine the correct type of the fields...
        // 
        String shellText = BaseMessages.getString(PKG, "CsvInputDialog.LinesToSample.DialogTitle");
        String lineText = BaseMessages.getString(PKG, "CsvInputDialog.LinesToSample.DialogMessage");
        EnterNumberDialog end = new EnterNumberDialog(shell, 100, shellText, lineText);
        int samples = end.open();
        if (samples >= 0) {
            getInfo(meta);
            TextFileCSVImportProgressDialog pd = new TextFileCSVImportProgressDialog(shell, meta, transMeta, reader, samples, true);
            String message = pd.open();
            if (message != null) {
                wFields.removeAll();
                // OK, what's the result of our search?
                getData(meta, false);
                wFields.removeEmptyRows();
                wFields.setRowNums();
                wFields.optWidth(true);
                EnterTextDialog etd = new EnterTextDialog(shell, BaseMessages.getString(PKG, "CsvInputDialog.ScanResults.DialogTitle"), BaseMessages.getString(PKG, "CsvInputDialog.ScanResults.DialogMessage"), message, true);
                etd.setReadOnly();
                etd.open();
            // asyncUpdatePreview();
            }
        }
    } catch (IOException e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "CsvInputDialog.IOError.DialogTitle"), BaseMessages.getString(PKG, "CsvInputDialog.IOError.DialogMessage"), e);
    } catch (KettleException e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "System.Dialog.Error.Title"), BaseMessages.getString(PKG, "CsvInputDialog.ErrorGettingFileDesc.DialogMessage"), e);
    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {
        // Ignore close errors
        }
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) TextFileCSVImportProgressDialog(org.pentaho.di.ui.trans.steps.textfileinput.TextFileCSVImportProgressDialog) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) DecimalFormat(java.text.DecimalFormat) TableItem(org.eclipse.swt.widgets.TableItem) CsvInputMeta(org.pentaho.di.trans.steps.csvinput.CsvInputMeta) EncodingType(org.pentaho.di.trans.steps.textfileinput.EncodingType) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) IOException(java.io.IOException) SWTException(org.eclipse.swt.SWTException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) KettleException(org.pentaho.di.core.exception.KettleException) IOException(java.io.IOException) LocalFile(org.apache.commons.vfs2.provider.local.LocalFile) EnterTextDialog(org.pentaho.di.ui.core.dialog.EnterTextDialog) FileObject(org.apache.commons.vfs2.FileObject) EnterNumberDialog(org.pentaho.di.ui.core.dialog.EnterNumberDialog)

Example 12 with LocalFile

use of org.apache.commons.vfs2.provider.local.LocalFile in project pentaho-kettle by pentaho.

the class ParGzipCsvInputDialog method getCSV.

// Get the data layout
private void getCSV() {
    InputStream inputStream = null;
    try {
        ParGzipCsvInputMeta meta = new ParGzipCsvInputMeta();
        getInfo(meta);
        String filename = transMeta.environmentSubstitute(meta.getFilename());
        FileObject fileObject = KettleVFS.getFileObject(filename);
        if (!(fileObject instanceof LocalFile)) {
            // 
            throw new KettleException(BaseMessages.getString(PKG, "ParGzipCsvInput.Log.OnlyLocalFilesAreSupported"));
        }
        wFields.table.removeAll();
        inputStream = new GZIPInputStream(KettleVFS.getInputStream(fileObject));
        InputStreamReader reader = new InputStreamReader(inputStream);
        EncodingType encodingType = EncodingType.guessEncodingType(reader.getEncoding());
        // Read a line of data to determine the number of rows...
        // 
        String line = TextFileInput.getLine(log, reader, encodingType, TextFileInputMeta.FILE_FORMAT_MIXED, new StringBuilder(1000));
        // Split the string, header or data into parts...
        // 
        String[] fieldNames = Const.splitString(line, meta.getDelimiter());
        if (!meta.isHeaderPresent()) {
            // Don't use field names from the header...
            // Generate field names F1 ... F10
            // 
            DecimalFormat df = new DecimalFormat("000");
            for (int i = 0; i < fieldNames.length; i++) {
                fieldNames[i] = "Field_" + df.format(i);
            }
        } else {
            if (!Utils.isEmpty(meta.getEnclosure())) {
                for (int i = 0; i < fieldNames.length; i++) {
                    if (fieldNames[i].startsWith(meta.getEnclosure()) && fieldNames[i].endsWith(meta.getEnclosure()) && fieldNames[i].length() > 1) {
                        fieldNames[i] = fieldNames[i].substring(1, fieldNames[i].length() - 1);
                    }
                }
            }
        }
        // 
        for (int i = 0; i < fieldNames.length; i++) {
            fieldNames[i] = Const.trim(fieldNames[i]);
        }
        // 
        for (int i = 0; i < fieldNames.length; i++) {
            TableItem item = new TableItem(wFields.table, SWT.NONE);
            item.setText(1, fieldNames[i]);
            item.setText(2, ValueMetaFactory.getValueMetaName(ValueMetaInterface.TYPE_STRING));
        }
        wFields.removeEmptyRows();
        wFields.setRowNums();
        wFields.optWidth(true);
        // Now we can continue reading the rows of data and we can guess the
        // Sample a few lines to determine the correct type of the fields...
        // 
        String shellText = BaseMessages.getString(PKG, "ParGzipCsvInputDialog.LinesToSample.DialogTitle");
        String lineText = BaseMessages.getString(PKG, "ParGzipCsvInputDialog.LinesToSample.DialogMessage");
        EnterNumberDialog end = new EnterNumberDialog(shell, 100, shellText, lineText);
        int samples = end.open();
        if (samples >= 0) {
            getInfo(meta);
            TextFileCSVImportProgressDialog pd = new TextFileCSVImportProgressDialog(shell, meta, transMeta, reader, samples, true);
            String message = pd.open();
            if (message != null) {
                wFields.removeAll();
                // OK, what's the result of our search?
                getData(meta);
                wFields.removeEmptyRows();
                wFields.setRowNums();
                wFields.optWidth(true);
                EnterTextDialog etd = new EnterTextDialog(shell, BaseMessages.getString(PKG, "ParGzipCsvInputDialog.ScanResults.DialogTitle"), BaseMessages.getString(PKG, "ParGzipCsvInputDialog.ScanResults.DialogMessage"), message, true);
                etd.setReadOnly();
                etd.open();
            }
        }
    } catch (IOException e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "ParGzipCsvInputDialog.IOError.DialogTitle"), BaseMessages.getString(PKG, "ParGzipCsvInputDialog.IOError.DialogMessage"), e);
    } catch (KettleException e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "System.Dialog.Error.Title"), BaseMessages.getString(PKG, "ParGzipCsvInputDialog.ErrorGettingFileDesc.DialogMessage"), e);
    } finally {
        try {
            inputStream.close();
        } catch (Exception e) {
        // Ignore errors
        }
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) TextFileCSVImportProgressDialog(org.pentaho.di.ui.trans.steps.textfileinput.TextFileCSVImportProgressDialog) InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) DecimalFormat(java.text.DecimalFormat) TableItem(org.eclipse.swt.widgets.TableItem) EncodingType(org.pentaho.di.trans.steps.textfileinput.EncodingType) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) IOException(java.io.IOException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) KettleException(org.pentaho.di.core.exception.KettleException) IOException(java.io.IOException) GZIPInputStream(java.util.zip.GZIPInputStream) LocalFile(org.apache.commons.vfs2.provider.local.LocalFile) EnterTextDialog(org.pentaho.di.ui.core.dialog.EnterTextDialog) FileObject(org.apache.commons.vfs2.FileObject) EnterNumberDialog(org.pentaho.di.ui.core.dialog.EnterNumberDialog) ParGzipCsvInputMeta(org.pentaho.di.trans.steps.parallelgzipcsv.ParGzipCsvInputMeta)

Example 13 with LocalFile

use of org.apache.commons.vfs2.provider.local.LocalFile in project javautils by jiadongpo.

the class FtpVFS method testftp2.

public void testftp2() throws Exception {
    FileSystemManager fsManager = VFS.getManager();
    FileObject fo = fsManager.resolveFile("ftp://ci:Zj4xyBkgjd@10.151.30.10:21/apps/tomcat7-40-tomcat-air-ticket-merchant/logs");
    // 得到远程文件列表
    FileObject[] children = fo.getChildren();
    for (int i = 0; i < children.length; i++) {
        FileObject f = children[i];
        FileContent c = f.getContent();
        File localFile = new File(f.getName().getBaseName());
        FileOutputStream out = new FileOutputStream(localFile);
        // 写入本地
        org.apache.commons.io.IOUtils.copy(c.getInputStream(), out);
        // 或使用写入
        FileObject obj = fsManager.resolveFile(this.getTargetResourceURL() + f.getName().getBaseName());
        if (!obj.exists()) {
            obj.createFile();
            obj.copyFrom(f, Selectors.SELECT_SELF);
        }
        final long size = (f.getType() == FileType.FILE) ? c.getSize() : -1;
        final long date = (f.getType() == FileType.FILE) ? c.getLastModifiedTime() : -1;
        System.out.println(f.getName().getPath() + " date:" + date + " Size:" + size);
    }
}
Also used : FileContent(org.apache.commons.vfs2.FileContent) FileOutputStream(java.io.FileOutputStream) FileObject(org.apache.commons.vfs2.FileObject) FileSystemManager(org.apache.commons.vfs2.FileSystemManager) File(java.io.File)

Example 14 with LocalFile

use of org.apache.commons.vfs2.provider.local.LocalFile in project spoofax by metaborg.

the class StrategoRuntimeFacet method available.

/**
 * Checks if all CTree and JAR files exist, returns errors if not.
 *
 * @return Errors, or empty if there are no errors.
 * @throws IOException
 *             When a file operation fails.
 */
public Iterable<String> available(IResourceService resourceService) throws IOException {
    final Collection<String> errors = Lists.newLinkedList();
    for (FileObject file : ctreeFiles) {
        if (!file.exists()) {
            final String message = logger.format("Stratego CTree file {} does not exist", file);
            errors.add(message);
        }
    }
    for (FileObject file : jarFiles) {
        if (!file.exists()) {
            final String message = logger.format("Stratego JAR file {} does not exist", file);
            errors.add(message);
        } else {
            final File localFile = resourceService.localFile(file);
            try (final JarFile jarFile = new JarFile(localFile, false, ZipFile.OPEN_READ)) {
                if (!jarFile.entries().hasMoreElements()) {
                    final String message = logger.format("Stratego JAR file {} is empty", file);
                    errors.add(message);
                }
            }
        }
    }
    return errors;
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 15 with LocalFile

use of org.apache.commons.vfs2.provider.local.LocalFile in project spoofax by metaborg.

the class LocalReplicatePrimitive method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) {
    if (!(current instanceof IStrategoString)) {
        return null;
    }
    final IStrategoString currentStr = (IStrategoString) current;
    final String path = currentStr.stringValue();
    final FileObject resource = resourceService.resolve(path);
    final File localFile = resourceService.localFile(resource);
    return factory.makeString(localFile.getPath());
}
Also used : IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) FileObject(org.apache.commons.vfs2.FileObject) File(java.io.File)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)16 LocalFile (org.apache.commons.vfs2.provider.local.LocalFile)10 File (java.io.File)9 IOException (java.io.IOException)6 KettleException (org.pentaho.di.core.exception.KettleException)6 KettleFileException (org.pentaho.di.core.exception.KettleFileException)4 KettleValueException (org.pentaho.di.core.exception.KettleValueException)4 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 FileContent (org.apache.commons.vfs2.FileContent)3 ResultFile (org.pentaho.di.core.ResultFile)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 DecimalFormat (java.text.DecimalFormat)2 CheckedInputStream (java.util.zip.CheckedInputStream)2 FileSystemException (org.apache.commons.vfs2.FileSystemException)2 FileSystemManager (org.apache.commons.vfs2.FileSystemManager)2 TableItem (org.eclipse.swt.widgets.TableItem)2 Result (org.pentaho.di.core.Result)2 Database (org.pentaho.di.core.database.Database)2