@Test public void testProcessRow() throws Exception { ReplaceStringData data = new ReplaceStringData(); ReplaceString replaceString = Mockito.spy( new ReplaceString( stepMockHelper.stepMeta, data, 0, stepMockHelper.transMeta, stepMockHelper.trans ) ); RowMetaInterface inputRowMeta = new RowMeta(); byte[] array = { 0, 97, 0, 65, -1, 65, -1, 33 }; byte[] matcharray = { -1, 33 }; String match = new String( matcharray , "UTF-16BE" ); Object[] _row = new Object[] { new String( array, "UTF-16BE" ), "another data" }; doReturn( _row ).when( replaceString ).getRow(); inputRowMeta.addValueMeta( 0, new ValueMetaString( "string" ) ); ReplaceStringMeta meta = stepMockHelper.processRowsStepMetaInterface; doReturn( new String[] { "string" } ).when( meta ).getFieldInStream(); doReturn( new String[] { "output" } ).when( meta ).getFieldOutStream(); doReturn( new int[] { 1 } ).when( meta ).isUnicode(); doReturn( new int[] { 0 } ).when( meta ).getUseRegEx(); doReturn( new int[] { 0 } ).when( meta ).getCaseSensitive(); doReturn( new int[] { 0 } ).when( meta ).getWholeWord(); doReturn( new String[] { match } ).when( meta ).getReplaceString(); doReturn( new String[] { "" } ).when( meta ).getFieldReplaceByString(); doReturn( new String[] { "matched" } ).when( meta ).getReplaceByString(); doReturn( new boolean[] { false } ).when( meta ).isSetEmptyString(); replaceString.init( meta, data ); replaceString.setInputRowMeta( inputRowMeta ); data.outputRowMeta = inputRowMeta; data.inputFieldsNr = 1; data.numFields = 1; data.inStreamNrs = new int[] { 0 }; data.replaceFieldIndex = new int[] { -1 }; data.outStreamNrs = new String[] { "", "1" }; data.replaceByString = new String[] { "1" }; data.setEmptyString = new boolean[] { false, false }; replaceString.processRow( meta, data ); System.out.println( replaceString.getRow()[1] ); assertTrue( "Expected: aAmatchedmatched","aAmatchedmatched".equals( replaceString.getRow()[1] ) ); }
public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws KettleException { meta = (ReplaceStringMeta) smi; data = (ReplaceStringData) sdi; Object[] r = getRow(); if ( r == null ) { setOutputDone(); return false; } if ( first ) { first = false; 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] ) ); } 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] ), meta.isUnicode()[i] == ReplaceStringMeta.IS_UNICODE_YES ); 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]; } } 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(); return false; } if ( sendToErrorRow ) { putError( getInputRowMeta(), r, 1, errorMessage, null, "ReplaceString001" ); } } return true; }
ReplaceString extends BaseStep implements StepInterface { public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws KettleException { meta = (ReplaceStringMeta) smi; data = (ReplaceStringData) sdi; Object[] r = getRow(); if ( r == null ) { setOutputDone(); return false; } if ( first ) { first = false; 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] ) ); } 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] ), meta.isUnicode()[i] == ReplaceStringMeta.IS_UNICODE_YES ); 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]; } } 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(); return false; } if ( sendToErrorRow ) { putError( getInputRowMeta(), r, 1, errorMessage, null, "ReplaceString001" ); } } return true; } }
ReplaceString extends BaseStep implements StepInterface { public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws KettleException { meta = (ReplaceStringMeta) smi; data = (ReplaceStringData) sdi; Object[] r = getRow(); if ( r == null ) { setOutputDone(); return false; } if ( first ) { first = false; 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] ) ); } 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] ), meta.isUnicode()[i] == ReplaceStringMeta.IS_UNICODE_YES ); 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]; } } 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(); return false; } if ( sendToErrorRow ) { putError( getInputRowMeta(), r, 1, errorMessage, null, "ReplaceString001" ); } } return true; } ReplaceString( StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr, TransMeta transMeta,
    Trans trans ); }
ReplaceString extends BaseStep implements StepInterface { public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws KettleException { meta = (ReplaceStringMeta) smi; data = (ReplaceStringData) sdi; Object[] r = getRow(); if ( r == null ) { setOutputDone(); return false; } if ( first ) { first = false; 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] ) ); } 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] ), meta.isUnicode()[i] == ReplaceStringMeta.IS_UNICODE_YES ); 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]; } } 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(); return false; } if ( sendToErrorRow ) { putError( getInputRowMeta(), r, 1, errorMessage, null, "ReplaceString001" ); } } return true; } ReplaceString( StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr, TransMeta transMeta,
    Trans trans ); static String replaceString( String originalString, Pattern pattern, String replaceByString ); boolean processRow( StepMetaInterface smi, StepDataInterface sdi ); boolean init( StepMetaInterface smi, StepDataInterface sdi ); void dispose( StepMetaInterface smi, StepDataInterface sdi ); }
ReplaceString extends BaseStep implements StepInterface { public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws KettleException { meta = (ReplaceStringMeta) smi; data = (ReplaceStringData) sdi; Object[] r = getRow(); if ( r == null ) { setOutputDone(); return false; } if ( first ) { first = false; 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] ) ); } 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] ), meta.isUnicode()[i] == ReplaceStringMeta.IS_UNICODE_YES ); 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]; } } 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(); return false; } if ( sendToErrorRow ) { putError( getInputRowMeta(), r, 1, errorMessage, null, "ReplaceString001" ); } } return true; } ReplaceString( StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr, TransMeta transMeta,
    Trans trans ); static String replaceString( String originalString, Pattern pattern, String replaceByString ); boolean processRow( StepMetaInterface smi, StepDataInterface sdi ); boolean init( StepMetaInterface smi, StepDataInterface sdi ); void dispose( StepMetaInterface smi, StepDataInterface sdi );  }