Search in sources :

Example 1 with Wini

use of org.ini4j.Wini in project pentaho-kettle by pentaho.

the class PropertyInput method openNextFile.

private boolean openNextFile() {
    InputStream fis = null;
    try {
        if (!meta.isFileField()) {
            if (data.filenr >= data.files.nrOfFiles()) {
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "PropertyInput.Log.FinishedProcessing"));
                }
                return false;
            }
            // Is this the last file?
            data.last_file = (data.filenr == data.files.nrOfFiles() - 1);
            data.file = data.files.getFile(data.filenr);
            // Move file pointer ahead!
            data.filenr++;
        } else {
            // Get row from input rowset & set row busy!
            data.readrow = getRow();
            if (data.readrow == null) {
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "PropertyInput.Log.FinishedProcessing"));
                }
                return false;
            }
            if (first) {
                first = false;
                data.inputRowMeta = getInputRowMeta();
                data.outputRowMeta = data.inputRowMeta.clone();
                meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
                // Get total previous fields
                data.totalpreviousfields = data.inputRowMeta.size();
                // Create convert meta-data objects that will contain Date & Number formatters
                data.convertRowMeta = data.outputRowMeta.cloneToType(ValueMetaInterface.TYPE_STRING);
                // Check is filename field is provided
                if (Utils.isEmpty(meta.getDynamicFilenameField())) {
                    logError(BaseMessages.getString(PKG, "PropertyInput.Log.NoField"));
                    throw new KettleException(BaseMessages.getString(PKG, "PropertyInput.Log.NoField"));
                }
                // cache the position of the field
                if (data.indexOfFilenameField < 0) {
                    data.indexOfFilenameField = getInputRowMeta().indexOfValue(meta.getDynamicFilenameField());
                    if (data.indexOfFilenameField < 0) {
                        // The field is unreachable !
                        logError(BaseMessages.getString(PKG, "PropertyInput.Log.ErrorFindingField") + "[" + meta.getDynamicFilenameField() + "]");
                        throw new KettleException(BaseMessages.getString(PKG, "PropertyInput.Exception.CouldnotFindField", meta.getDynamicFilenameField()));
                    }
                }
            }
            // End if first
            String filename = getInputRowMeta().getString(data.readrow, data.indexOfFilenameField);
            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "PropertyInput.Log.FilenameInStream", meta.getDynamicFilenameField(), filename));
            }
            data.file = KettleVFS.getFileObject(filename, getTransMeta());
        // Check if file exists!
        }
        // Check if file is empty
        // long fileSize= data.file.getContent().getSize();
        data.filename = KettleVFS.getFilename(data.file);
        // Add additional fields?
        if (meta.getShortFileNameField() != null && meta.getShortFileNameField().length() > 0) {
            data.shortFilename = data.file.getName().getBaseName();
        }
        if (meta.getPathField() != null && meta.getPathField().length() > 0) {
            data.path = KettleVFS.getFilename(data.file.getParent());
        }
        if (meta.isHiddenField() != null && meta.isHiddenField().length() > 0) {
            data.hidden = data.file.isHidden();
        }
        if (meta.getExtensionField() != null && meta.getExtensionField().length() > 0) {
            data.extension = data.file.getName().getExtension();
        }
        if (meta.getLastModificationDateField() != null && meta.getLastModificationDateField().length() > 0) {
            data.lastModificationDateTime = new Date(data.file.getContent().getLastModifiedTime());
        }
        if (meta.getUriField() != null && meta.getUriField().length() > 0) {
            data.uriName = Const.optionallyDecodeUriString(data.file.getName().getURI());
        }
        if (meta.getRootUriField() != null && meta.getRootUriField().length() > 0) {
            data.rootUriName = data.file.getName().getRootURI();
        }
        if (meta.getSizeField() != null && meta.getSizeField().length() > 0) {
            data.size = new Long(data.file.getContent().getSize());
        }
        if (meta.resetRowNumber()) {
            data.rownr = 0;
        }
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "PropertyInput.Log.OpeningFile", data.file.toString()));
        }
        if (meta.isAddResultFile()) {
            // Add this to the result file names...
            ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, data.file, getTransMeta().getName(), getStepname());
            resultFile.setComment(BaseMessages.getString(PKG, "PropertyInput.Log.FileAddedResult"));
            addResultFile(resultFile);
        }
        fis = data.file.getContent().getInputStream();
        if (data.propfiles) {
            // load properties file
            data.pro = new Properties();
            data.pro.load(fis);
            data.it = data.pro.keySet().iterator();
        } else {
            // create wini object
            data.wini = new Wini();
            if (!Utils.isEmpty(data.realEncoding)) {
                data.wini.getConfig().setFileEncoding(Charset.forName(data.realEncoding));
            }
            // load INI file
            data.wini.load(fis);
            if (data.realSection != null) {
                // just one section
                data.iniSection = data.wini.get(data.realSection);
                if (data.iniSection == null) {
                    throw new KettleException(BaseMessages.getString(PKG, "PropertyInput.Error.CanNotFindSection", data.realSection, "" + data.file.getName()));
                }
            } else {
                // We need to fetch all sections
                data.itSection = data.wini.keySet().iterator();
                data.iniSection = data.wini.get(data.itSection.next().toString());
            }
            data.iniIt = data.iniSection.keySet().iterator();
        }
        if (log.isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "PropertyInput.Log.FileOpened", data.file.toString()));
            logDetailed(BaseMessages.getString(PKG, "PropertyInput.log.TotalKey", "" + (data.propfiles ? data.pro.size() : data.iniSection.size()), KettleVFS.getFilename(data.file)));
        }
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "PropertyInput.Log.UnableToOpenFile", "" + data.filenr, data.file.toString(), e.toString()));
        stopAll();
        setErrors(1);
        return false;
    } finally {
        BaseStep.closeQuietly(fis);
    }
    return true;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) Wini(org.ini4j.Wini) InputStream(java.io.InputStream) ResultFile(org.pentaho.di.core.ResultFile) Properties(java.util.Properties) Date(java.util.Date) KettleException(org.pentaho.di.core.exception.KettleException)

Example 2 with Wini

use of org.ini4j.Wini in project pentaho-kettle by pentaho.

the class PropertyInputDialog method getSections.

private void getSections() {
    Wini wini = new Wini();
    PropertyInputMeta meta = new PropertyInputMeta();
    try {
        getInfo(meta);
        FileInputList fileInputList = meta.getFiles(transMeta);
        if (fileInputList.nrOfFiles() > 0) {
            // Check the first file
            if (fileInputList.getFile(0).exists()) {
                // Open the file (only first file) in readOnly ...
                // 
                wini = new Wini(KettleVFS.getInputStream(fileInputList.getFile(0)));
                Iterator<String> itSection = wini.keySet().iterator();
                String[] sectionsList = new String[wini.keySet().size()];
                int i = 0;
                while (itSection.hasNext()) {
                    sectionsList[i] = itSection.next().toString();
                    i++;
                }
                Const.sortStrings(sectionsList);
                EnterSelectionDialog dialog = new EnterSelectionDialog(shell, sectionsList, BaseMessages.getString(PKG, "PropertyInputDialog.Dialog.SelectASection.Title"), BaseMessages.getString(PKG, "PropertyInputDialog.Dialog.SelectASection.Message"));
                String sectionname = dialog.open();
                if (sectionname != null) {
                    wSection.setText(sectionname);
                }
            } else {
                // The file not exists !
                throw new KettleException(BaseMessages.getString(PKG, "PropertyInputDialog.Exception.FileDoesNotExist", KettleVFS.getFilename(fileInputList.getFile(0))));
            }
        } else {
            // No file specified
            MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
            mb.setMessage(BaseMessages.getString(PKG, "PropertyInputDialog.FilesMissing.DialogMessage"));
            mb.setText(BaseMessages.getString(PKG, "System.Dialog.Error.Title"));
            mb.open();
        }
    } catch (Throwable e) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "PropertyInputDialog.UnableToGetListOfSections.Title"), BaseMessages.getString(PKG, "PropertyInputDialog.UnableToGetListOfSections.Message"), e);
    } finally {
        if (wini != null) {
            wini.clear();
        }
        wini = null;
        meta = null;
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) Wini(org.ini4j.Wini) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) PropertyInputMeta(org.pentaho.di.trans.steps.propertyinput.PropertyInputMeta) FileInputList(org.pentaho.di.core.fileinput.FileInputList) EnterSelectionDialog(org.pentaho.di.ui.core.dialog.EnterSelectionDialog) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 3 with Wini

use of org.ini4j.Wini in project isf-jclient by mikrohash.

the class Configs method loadWini.

private static void loadWini() {
    File f = FileManager.getFile(CONFIG_FILE_NAME);
    try {
        if (!f.exists()) {
            f.createNewFile();
            wini = new Wini(f);
            initWini();
            saveWini();
        } else {
            wini = new Wini(f);
            load();
        }
    } catch (IOException e) {
        UIM.logWrn(R.STR.getString("config_loading_fail"));
        UIM.logException(e, true);
    }
}
Also used : Wini(org.ini4j.Wini) IOException(java.io.IOException) File(java.io.File)

Aggregations

Wini (org.ini4j.Wini)3 KettleException (org.pentaho.di.core.exception.KettleException)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Date (java.util.Date)1 Properties (java.util.Properties)1 MessageBox (org.eclipse.swt.widgets.MessageBox)1 ResultFile (org.pentaho.di.core.ResultFile)1 FileInputList (org.pentaho.di.core.fileinput.FileInputList)1 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)1 PropertyInputMeta (org.pentaho.di.trans.steps.propertyinput.PropertyInputMeta)1 EnterSelectionDialog (org.pentaho.di.ui.core.dialog.EnterSelectionDialog)1 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)1