コード例 #1
0
  public void dispose(StepMetaInterface smi, StepDataInterface sdi) {
    meta = (ReplaceStringMeta) smi;
    data = (ReplaceStringData) sdi;

    data.outStreamNrs = null;
    data.patterns = null;
    data.replaceByString = null;
    data.replaceString = null;
    data.valueChange = null;
    super.dispose(smi, sdi);
  }
コード例 #2
0
  public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
    meta = (ReplaceStringMeta) smi;
    data = (ReplaceStringData) sdi;

    Object[] r = getRow(); // Get row from input rowset & set row busy!
    if (r == null) { // no more input to be expected...

      setOutputDone();
      return false;
    }

    if (first) {
      first = false;

      // What's the format of the output row?
      data.outputRowMeta = getInputRowMeta().clone();
      data.inputFieldsNr = data.outputRowMeta.size();
      meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);

      data.numFields = meta.getFieldInStream().length;
      data.inStreamNrs = new int[data.numFields];
      data.outStreamNrs = new String[data.numFields];
      data.patterns = new Pattern[data.numFields];
      data.replaceByString = new String[data.numFields];
      data.setEmptyString = new boolean[data.numFields];
      data.replaceFieldIndex = new int[data.numFields];

      for (int i = 0; i < data.numFields; i++) {
        data.inStreamNrs[i] = getInputRowMeta().indexOfValue(meta.getFieldInStream()[i]);
        if (data.inStreamNrs[i] < 0) {
          throw new KettleStepException(
              BaseMessages.getString(
                  PKG, "ReplaceString.Exception.FieldRequired", meta.getFieldInStream()[i]));
        }

        // check field type
        if (getInputRowMeta().getValueMeta(data.inStreamNrs[i]).getType()
            != ValueMetaInterface.TYPE_STRING) {
          throw new KettleStepException(
              BaseMessages.getString(
                  PKG, "ReplaceString.Exception.FieldTypeNotString", meta.getFieldInStream()[i]));
        }

        data.outStreamNrs[i] = environmentSubstitute(meta.getFieldOutStream()[i]);

        data.patterns[i] =
            buildPattern(
                meta.getUseRegEx()[i] != ReplaceStringMeta.USE_REGEX_YES,
                meta.getCaseSensitive()[i] == ReplaceStringMeta.CASE_SENSITIVE_YES,
                meta.getWholeWord()[i] == ReplaceStringMeta.WHOLE_WORD_YES,
                environmentSubstitute(meta.getReplaceString()[i]));

        String field = meta.getFieldReplaceByString()[i];
        if (!Utils.isEmpty(field)) {
          data.replaceFieldIndex[i] = getInputRowMeta().indexOfValue(field);
          if (data.replaceFieldIndex[i] < 0) {
            throw new KettleStepException(
                BaseMessages.getString(PKG, "ReplaceString.Exception.FieldRequired", field));
          }
        } else {
          data.replaceFieldIndex[i] = -1;
          data.replaceByString[i] = environmentSubstitute(meta.getReplaceByString()[i]);
        }
        data.setEmptyString[i] = meta.isSetEmptyString()[i];
      }
    } // end if first

    try {
      Object[] output = getOneRow(getInputRowMeta(), r);
      putRow(data.outputRowMeta, output);

      if (checkFeedback(getLinesRead())) {
        if (log.isDetailed()) {
          logDetailed(BaseMessages.getString(PKG, "ReplaceString.Log.LineNumber") + getLinesRead());
        }
      }
    } catch (KettleException e) {
      boolean sendToErrorRow = false;
      String errorMessage = null;

      if (getStepMeta().isDoingErrorHandling()) {
        sendToErrorRow = true;
        errorMessage = e.toString();
      } else {
        logError(BaseMessages.getString(PKG, "ReplaceString.Log.ErrorInStep", e.getMessage()));
        setErrors(1);
        stopAll();
        setOutputDone(); // signal end to receiver(s)
        return false;
      }
      if (sendToErrorRow) {
        // Simply add this row to the error row
        putError(getInputRowMeta(), r, 1, errorMessage, null, "ReplaceString001");
      }
    }
    return true;
  }