Ejemplo n.º 1
0
 private boolean openNextFile() {
   try {
     if (data.filenr >= data.files.nrOfFiles()) {
       if (log.isDetailed())
         logDetailed(BaseMessages.getString(PKG, "JsonInput.Log.FinishedProcessing"));
       return false;
     }
     // Close previous file if needed
     if (data.file != null) data.file.close();
     // get file
     data.file = (FileObject) data.files.getFile(data.filenr);
     if (meta.isIgnoreEmptyFile() && data.file.getContent().getSize() == 0) {
       // log only basic as a warning (was before logError)
       logBasic(
           BaseMessages.getString(PKG, "JsonInput.Error.FileSizeZero", "" + data.file.getName()));
       openNextFile();
     }
     readFileOrString();
   } catch (Exception e) {
     logError(
         BaseMessages.getString(
             PKG,
             "JsonInput.Log.UnableToOpenFile",
             "" + data.filenr,
             data.file.toString(),
             e.toString()));
     stopAll();
     setErrors(1);
     return false;
   }
   return true;
 }
Ejemplo n.º 2
0
  private boolean ReadNextString() {

    try {
      data.readrow = getRow(); // Grab another row ...

      if (data.readrow == null) {
        // finished processing!
        if (log.isDetailed())
          logDetailed(BaseMessages.getString(PKG, "JsonInput.Log.FinishedProcessing"));
        return false;
      }

      if (first) {
        first = false;

        data.inputRowMeta = getInputRowMeta();
        data.outputRowMeta = data.inputRowMeta.clone();
        meta.getFields(data.outputRowMeta, getStepname(), null, null, this);

        // Get total previous fields
        data.totalpreviousfields = data.inputRowMeta.size();

        // Create convert meta-data objects that will contain Date & Number formatters
        data.convertRowMeta = data.outputRowMeta.clone();
        for (int i = 0; i < data.convertRowMeta.size(); i++)
          data.convertRowMeta.getValueMeta(i).setType(ValueMetaInterface.TYPE_STRING);

        // For String to <type> conversions, we allocate a conversion meta data row as well...
        //
        data.convertRowMeta = data.outputRowMeta.clone();
        for (int i = 0; i < data.convertRowMeta.size(); i++) {
          data.convertRowMeta.getValueMeta(i).setType(ValueMetaInterface.TYPE_STRING);
        }

        // Check if source field is provided
        if (Const.isEmpty(meta.getFieldValue())) {
          logError(BaseMessages.getString(PKG, "JsonInput.Log.NoField"));
          throw new KettleException(BaseMessages.getString(PKG, "JsonInput.Log.NoField"));
        }

        // cache the position of the field
        if (data.indexSourceField < 0) {
          data.indexSourceField = getInputRowMeta().indexOfValue(meta.getFieldValue());
          if (data.indexSourceField < 0) {
            // The field is unreachable !
            logError(
                BaseMessages.getString(
                    PKG,
                    "JsonInput.Log.ErrorFindingField",
                    meta.getFieldValue())); // $NON-NLS-1$ //$NON-NLS-2$
            throw new KettleException(
                BaseMessages.getString(
                    PKG,
                    "JsonInput.Exception.CouldnotFindField",
                    meta.getFieldValue())); // $NON-NLS-1$ //$NON-NLS-2$
          }
        }
      }

      // get source field value
      String fieldValue = getInputRowMeta().getString(data.readrow, data.indexSourceField);

      if (log.isDetailed())
        logDetailed(
            BaseMessages.getString(
                PKG, "JsonInput.Log.SourceValue", meta.getFieldValue(), fieldValue));

      if (meta.getIsAFile()) {

        // source is a file.
        data.file = KettleVFS.getFileObject(fieldValue, getTransMeta());
        if (meta.isIgnoreEmptyFile() && data.file.getContent().getSize() == 0) {
          // log only basic as a warning (was before logError)
          logBasic(
              BaseMessages.getString(PKG, "JsonInput.Error.FileSizeZero", data.file.getName()));
          ReadNextString();
        }
      } else {
        // read string
        data.stringToParse = fieldValue;
      }

      readFileOrString();
    } catch (Exception e) {
      logError(BaseMessages.getString(PKG, "JsonInput.Log.UnexpectedError", e.toString()));
      stopAll();
      logError(Const.getStackTracker(e));
      setErrors(1);
      return false;
    }
    return true;
  }