@Test public void guessStringsFromLine() throws Exception { TextFileInputMeta inputMeta = Mockito.mock( TextFileInputMeta.class ); inputMeta.content = new TextFileInputMeta.Content(); inputMeta.content.fileType = "CSV"; String line = "\"\\\\valueA\"|\"valueB\\\\\"|\"val\\\\ueC\""; String[] strings = TextFileInputUtils .guessStringsFromLine( Mockito.mock( VariableSpace.class ), Mockito.mock( LogChannelInterface.class ), line, inputMeta, "|", "\"", "\\" ); Assert.assertNotNull( strings ); Assert.assertEquals( "\\valueA", strings[ 0 ] ); Assert.assertEquals( "valueB\\", strings[ 1 ] ); Assert.assertEquals( "val\\ueC", strings[ 2 ] ); }
public static final String[] guessStringsFromLine( VariableSpace space, LogChannelInterface log, String line, TextFileInputMeta inf, String delimiter, String enclosure, String escapeCharacter ) throws KettleException { List<String> strings = new ArrayList<>(); String pol; try { if ( line == null ) { return null; } if ( inf.content.fileType.equalsIgnoreCase( "CSV" ) ) { int pos = 0; int length = line.length(); boolean dencl = false; int len_encl = ( enclosure == null ? 0 : enclosure.length() ); int len_esc = ( escapeCharacter == null ? 0 : escapeCharacter.length() ); while ( pos < length ) { int from = pos; int next; boolean encl_found; boolean contains_escaped_enclosures = false; boolean contains_escaped_separators = false; boolean contains_escaped_escape = false; if ( len_encl > 0 && line.substring( from, from + len_encl ).equalsIgnoreCase( enclosure ) ) { if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.ConvertLineToRow", line.substring( from, from + len_encl ) ) ); } encl_found = true; int p = from + len_encl; boolean is_enclosure = len_encl > 0 && p + len_encl < length && line.substring( p, p + len_encl ).equalsIgnoreCase( enclosure ); boolean is_escape = len_esc > 0 && p + len_esc < length && line.substring( p, p + len_esc ).equalsIgnoreCase( escapeCharacter ); boolean enclosure_after = false; if ( ( is_enclosure || is_escape ) && p < length - 1 ) { String strnext = line.substring( p + len_encl, p + 2 * len_encl ); if ( strnext.equalsIgnoreCase( enclosure ) ) { p++; enclosure_after = true; dencl = true; if ( is_escape ) { contains_escaped_enclosures = true; } } else if ( strnext.equals( escapeCharacter ) ) { p++; if ( is_escape ) { contains_escaped_escape = true; } } } while ( ( !is_enclosure || enclosure_after ) && p < line.length() ) { p++; enclosure_after = false; is_enclosure = len_encl > 0 && p + len_encl < length && line.substring( p, p + len_encl ).equals( enclosure ); is_escape = len_esc > 0 && p + len_esc < length && line.substring( p, p + len_esc ).equals( escapeCharacter ); if ( ( is_enclosure || is_escape ) && p < length - 1 ) { String strnext = line.substring( p + len_encl, p + 2 * len_encl ); if ( strnext.equals( enclosure ) ) { p++; enclosure_after = true; dencl = true; if ( is_escape ) { contains_escaped_enclosures = true; } } else if ( strnext.equals( escapeCharacter ) ) { p++; if ( is_escape ) { contains_escaped_escape = true; } } } } if ( p >= length ) { next = p; } else { next = p + len_encl; } if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.EndOfEnclosure", "" + p ) ); } } else { encl_found = false; boolean found = false; int startpoint = from; do { next = line.indexOf( delimiter, startpoint ); if ( len_esc > 0 && next - len_esc > 0 ) { String before = line.substring( next - len_esc, next ); if ( escapeCharacter.equals( before ) ) { startpoint = next + 1; contains_escaped_separators = true; } else { found = true; } } else { found = true; } } while ( !found && next >= 0 ); } if ( next == -1 ) { next = length; } if ( encl_found ) { pol = line.substring( from + len_encl, next - len_encl ); if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.EnclosureFieldFound", "" + pol ) ); } } else { pol = line.substring( from, next ); if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.NormalFieldFound", "" + pol ) ); } } if ( dencl ) { StringBuilder sbpol = new StringBuilder( pol ); int idx = sbpol.indexOf( enclosure + enclosure ); while ( idx >= 0 ) { sbpol.delete( idx, idx + enclosure.length() ); idx = sbpol.indexOf( enclosure + enclosure ); } pol = sbpol.toString(); } if ( contains_escaped_enclosures ) { String replace = escapeCharacter + enclosure; String replaceWith = enclosure; pol = Const.replace( pol, replace, replaceWith ); } if ( contains_escaped_separators ) { String replace = escapeCharacter + delimiter; String replaceWith = delimiter; pol = Const.replace( pol, replace, replaceWith ); } if ( contains_escaped_escape ) { String replace = escapeCharacter + escapeCharacter; String replaceWith = escapeCharacter; pol = Const.replace( pol, replace, replaceWith ); } strings.add( pol ); pos = next + delimiter.length(); } if ( pos == length ) { if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.EndOfEmptyLineFound" ) ); } strings.add( "" ); } } else { for ( int i = 0; i < inf.inputFields.length; i++ ) { BaseFileField field = inf.inputFields[i]; int length = line.length(); if ( field.getPosition() + field.getLength() <= length ) { strings.add( line.substring( field.getPosition(), field.getPosition() + field.getLength() ) ); } else { if ( field.getPosition() < length ) { strings.add( line.substring( field.getPosition() ) ); } else { strings.add( "" ); } } } } } catch ( Exception e ) { throw new KettleException( BaseMessages.getString( PKG, "TextFileInput.Log.Error.ErrorConvertingLine", e .toString() ), e ); } return strings.toArray( new String[strings.size()] ); }
TextFileInputUtils { public static final String[] guessStringsFromLine( VariableSpace space, LogChannelInterface log, String line, TextFileInputMeta inf, String delimiter, String enclosure, String escapeCharacter ) throws KettleException { List<String> strings = new ArrayList<>(); String pol; try { if ( line == null ) { return null; } if ( inf.content.fileType.equalsIgnoreCase( "CSV" ) ) { int pos = 0; int length = line.length(); boolean dencl = false; int len_encl = ( enclosure == null ? 0 : enclosure.length() ); int len_esc = ( escapeCharacter == null ? 0 : escapeCharacter.length() ); while ( pos < length ) { int from = pos; int next; boolean encl_found; boolean contains_escaped_enclosures = false; boolean contains_escaped_separators = false; boolean contains_escaped_escape = false; if ( len_encl > 0 && line.substring( from, from + len_encl ).equalsIgnoreCase( enclosure ) ) { if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.ConvertLineToRow", line.substring( from, from + len_encl ) ) ); } encl_found = true; int p = from + len_encl; boolean is_enclosure = len_encl > 0 && p + len_encl < length && line.substring( p, p + len_encl ).equalsIgnoreCase( enclosure ); boolean is_escape = len_esc > 0 && p + len_esc < length && line.substring( p, p + len_esc ).equalsIgnoreCase( escapeCharacter ); boolean enclosure_after = false; if ( ( is_enclosure || is_escape ) && p < length - 1 ) { String strnext = line.substring( p + len_encl, p + 2 * len_encl ); if ( strnext.equalsIgnoreCase( enclosure ) ) { p++; enclosure_after = true; dencl = true; if ( is_escape ) { contains_escaped_enclosures = true; } } else if ( strnext.equals( escapeCharacter ) ) { p++; if ( is_escape ) { contains_escaped_escape = true; } } } while ( ( !is_enclosure || enclosure_after ) && p < line.length() ) { p++; enclosure_after = false; is_enclosure = len_encl > 0 && p + len_encl < length && line.substring( p, p + len_encl ).equals( enclosure ); is_escape = len_esc > 0 && p + len_esc < length && line.substring( p, p + len_esc ).equals( escapeCharacter ); if ( ( is_enclosure || is_escape ) && p < length - 1 ) { String strnext = line.substring( p + len_encl, p + 2 * len_encl ); if ( strnext.equals( enclosure ) ) { p++; enclosure_after = true; dencl = true; if ( is_escape ) { contains_escaped_enclosures = true; } } else if ( strnext.equals( escapeCharacter ) ) { p++; if ( is_escape ) { contains_escaped_escape = true; } } } } if ( p >= length ) { next = p; } else { next = p + len_encl; } if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.EndOfEnclosure", "" + p ) ); } } else { encl_found = false; boolean found = false; int startpoint = from; do { next = line.indexOf( delimiter, startpoint ); if ( len_esc > 0 && next - len_esc > 0 ) { String before = line.substring( next - len_esc, next ); if ( escapeCharacter.equals( before ) ) { startpoint = next + 1; contains_escaped_separators = true; } else { found = true; } } else { found = true; } } while ( !found && next >= 0 ); } if ( next == -1 ) { next = length; } if ( encl_found ) { pol = line.substring( from + len_encl, next - len_encl ); if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.EnclosureFieldFound", "" + pol ) ); } } else { pol = line.substring( from, next ); if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.NormalFieldFound", "" + pol ) ); } } if ( dencl ) { StringBuilder sbpol = new StringBuilder( pol ); int idx = sbpol.indexOf( enclosure + enclosure ); while ( idx >= 0 ) { sbpol.delete( idx, idx + enclosure.length() ); idx = sbpol.indexOf( enclosure + enclosure ); } pol = sbpol.toString(); } if ( contains_escaped_enclosures ) { String replace = escapeCharacter + enclosure; String replaceWith = enclosure; pol = Const.replace( pol, replace, replaceWith ); } if ( contains_escaped_separators ) { String replace = escapeCharacter + delimiter; String replaceWith = delimiter; pol = Const.replace( pol, replace, replaceWith ); } if ( contains_escaped_escape ) { String replace = escapeCharacter + escapeCharacter; String replaceWith = escapeCharacter; pol = Const.replace( pol, replace, replaceWith ); } strings.add( pol ); pos = next + delimiter.length(); } if ( pos == length ) { if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.EndOfEmptyLineFound" ) ); } strings.add( "" ); } } else { for ( int i = 0; i < inf.inputFields.length; i++ ) { BaseFileField field = inf.inputFields[i]; int length = line.length(); if ( field.getPosition() + field.getLength() <= length ) { strings.add( line.substring( field.getPosition(), field.getPosition() + field.getLength() ) ); } else { if ( field.getPosition() < length ) { strings.add( line.substring( field.getPosition() ) ); } else { strings.add( "" ); } } } } } catch ( Exception e ) { throw new KettleException( BaseMessages.getString( PKG, "TextFileInput.Log.Error.ErrorConvertingLine", e .toString() ), e ); } return strings.toArray( new String[strings.size()] ); } }
TextFileInputUtils { public static final String[] guessStringsFromLine( VariableSpace space, LogChannelInterface log, String line, TextFileInputMeta inf, String delimiter, String enclosure, String escapeCharacter ) throws KettleException { List<String> strings = new ArrayList<>(); String pol; try { if ( line == null ) { return null; } if ( inf.content.fileType.equalsIgnoreCase( "CSV" ) ) { int pos = 0; int length = line.length(); boolean dencl = false; int len_encl = ( enclosure == null ? 0 : enclosure.length() ); int len_esc = ( escapeCharacter == null ? 0 : escapeCharacter.length() ); while ( pos < length ) { int from = pos; int next; boolean encl_found; boolean contains_escaped_enclosures = false; boolean contains_escaped_separators = false; boolean contains_escaped_escape = false; if ( len_encl > 0 && line.substring( from, from + len_encl ).equalsIgnoreCase( enclosure ) ) { if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.ConvertLineToRow", line.substring( from, from + len_encl ) ) ); } encl_found = true; int p = from + len_encl; boolean is_enclosure = len_encl > 0 && p + len_encl < length && line.substring( p, p + len_encl ).equalsIgnoreCase( enclosure ); boolean is_escape = len_esc > 0 && p + len_esc < length && line.substring( p, p + len_esc ).equalsIgnoreCase( escapeCharacter ); boolean enclosure_after = false; if ( ( is_enclosure || is_escape ) && p < length - 1 ) { String strnext = line.substring( p + len_encl, p + 2 * len_encl ); if ( strnext.equalsIgnoreCase( enclosure ) ) { p++; enclosure_after = true; dencl = true; if ( is_escape ) { contains_escaped_enclosures = true; } } else if ( strnext.equals( escapeCharacter ) ) { p++; if ( is_escape ) { contains_escaped_escape = true; } } } while ( ( !is_enclosure || enclosure_after ) && p < line.length() ) { p++; enclosure_after = false; is_enclosure = len_encl > 0 && p + len_encl < length && line.substring( p, p + len_encl ).equals( enclosure ); is_escape = len_esc > 0 && p + len_esc < length && line.substring( p, p + len_esc ).equals( escapeCharacter ); if ( ( is_enclosure || is_escape ) && p < length - 1 ) { String strnext = line.substring( p + len_encl, p + 2 * len_encl ); if ( strnext.equals( enclosure ) ) { p++; enclosure_after = true; dencl = true; if ( is_escape ) { contains_escaped_enclosures = true; } } else if ( strnext.equals( escapeCharacter ) ) { p++; if ( is_escape ) { contains_escaped_escape = true; } } } } if ( p >= length ) { next = p; } else { next = p + len_encl; } if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.EndOfEnclosure", "" + p ) ); } } else { encl_found = false; boolean found = false; int startpoint = from; do { next = line.indexOf( delimiter, startpoint ); if ( len_esc > 0 && next - len_esc > 0 ) { String before = line.substring( next - len_esc, next ); if ( escapeCharacter.equals( before ) ) { startpoint = next + 1; contains_escaped_separators = true; } else { found = true; } } else { found = true; } } while ( !found && next >= 0 ); } if ( next == -1 ) { next = length; } if ( encl_found ) { pol = line.substring( from + len_encl, next - len_encl ); if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.EnclosureFieldFound", "" + pol ) ); } } else { pol = line.substring( from, next ); if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.NormalFieldFound", "" + pol ) ); } } if ( dencl ) { StringBuilder sbpol = new StringBuilder( pol ); int idx = sbpol.indexOf( enclosure + enclosure ); while ( idx >= 0 ) { sbpol.delete( idx, idx + enclosure.length() ); idx = sbpol.indexOf( enclosure + enclosure ); } pol = sbpol.toString(); } if ( contains_escaped_enclosures ) { String replace = escapeCharacter + enclosure; String replaceWith = enclosure; pol = Const.replace( pol, replace, replaceWith ); } if ( contains_escaped_separators ) { String replace = escapeCharacter + delimiter; String replaceWith = delimiter; pol = Const.replace( pol, replace, replaceWith ); } if ( contains_escaped_escape ) { String replace = escapeCharacter + escapeCharacter; String replaceWith = escapeCharacter; pol = Const.replace( pol, replace, replaceWith ); } strings.add( pol ); pos = next + delimiter.length(); } if ( pos == length ) { if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.EndOfEmptyLineFound" ) ); } strings.add( "" ); } } else { for ( int i = 0; i < inf.inputFields.length; i++ ) { BaseFileField field = inf.inputFields[i]; int length = line.length(); if ( field.getPosition() + field.getLength() <= length ) { strings.add( line.substring( field.getPosition(), field.getPosition() + field.getLength() ) ); } else { if ( field.getPosition() < length ) { strings.add( line.substring( field.getPosition() ) ); } else { strings.add( "" ); } } } } } catch ( Exception e ) { throw new KettleException( BaseMessages.getString( PKG, "TextFileInput.Log.Error.ErrorConvertingLine", e .toString() ), e ); } return strings.toArray( new String[strings.size()] ); }  }
TextFileInputUtils { public static final String[] guessStringsFromLine( VariableSpace space, LogChannelInterface log, String line, TextFileInputMeta inf, String delimiter, String enclosure, String escapeCharacter ) throws KettleException { List<String> strings = new ArrayList<>(); String pol; try { if ( line == null ) { return null; } if ( inf.content.fileType.equalsIgnoreCase( "CSV" ) ) { int pos = 0; int length = line.length(); boolean dencl = false; int len_encl = ( enclosure == null ? 0 : enclosure.length() ); int len_esc = ( escapeCharacter == null ? 0 : escapeCharacter.length() ); while ( pos < length ) { int from = pos; int next; boolean encl_found; boolean contains_escaped_enclosures = false; boolean contains_escaped_separators = false; boolean contains_escaped_escape = false; if ( len_encl > 0 && line.substring( from, from + len_encl ).equalsIgnoreCase( enclosure ) ) { if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.ConvertLineToRow", line.substring( from, from + len_encl ) ) ); } encl_found = true; int p = from + len_encl; boolean is_enclosure = len_encl > 0 && p + len_encl < length && line.substring( p, p + len_encl ).equalsIgnoreCase( enclosure ); boolean is_escape = len_esc > 0 && p + len_esc < length && line.substring( p, p + len_esc ).equalsIgnoreCase( escapeCharacter ); boolean enclosure_after = false; if ( ( is_enclosure || is_escape ) && p < length - 1 ) { String strnext = line.substring( p + len_encl, p + 2 * len_encl ); if ( strnext.equalsIgnoreCase( enclosure ) ) { p++; enclosure_after = true; dencl = true; if ( is_escape ) { contains_escaped_enclosures = true; } } else if ( strnext.equals( escapeCharacter ) ) { p++; if ( is_escape ) { contains_escaped_escape = true; } } } while ( ( !is_enclosure || enclosure_after ) && p < line.length() ) { p++; enclosure_after = false; is_enclosure = len_encl > 0 && p + len_encl < length && line.substring( p, p + len_encl ).equals( enclosure ); is_escape = len_esc > 0 && p + len_esc < length && line.substring( p, p + len_esc ).equals( escapeCharacter ); if ( ( is_enclosure || is_escape ) && p < length - 1 ) { String strnext = line.substring( p + len_encl, p + 2 * len_encl ); if ( strnext.equals( enclosure ) ) { p++; enclosure_after = true; dencl = true; if ( is_escape ) { contains_escaped_enclosures = true; } } else if ( strnext.equals( escapeCharacter ) ) { p++; if ( is_escape ) { contains_escaped_escape = true; } } } } if ( p >= length ) { next = p; } else { next = p + len_encl; } if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.EndOfEnclosure", "" + p ) ); } } else { encl_found = false; boolean found = false; int startpoint = from; do { next = line.indexOf( delimiter, startpoint ); if ( len_esc > 0 && next - len_esc > 0 ) { String before = line.substring( next - len_esc, next ); if ( escapeCharacter.equals( before ) ) { startpoint = next + 1; contains_escaped_separators = true; } else { found = true; } } else { found = true; } } while ( !found && next >= 0 ); } if ( next == -1 ) { next = length; } if ( encl_found ) { pol = line.substring( from + len_encl, next - len_encl ); if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.EnclosureFieldFound", "" + pol ) ); } } else { pol = line.substring( from, next ); if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.NormalFieldFound", "" + pol ) ); } } if ( dencl ) { StringBuilder sbpol = new StringBuilder( pol ); int idx = sbpol.indexOf( enclosure + enclosure ); while ( idx >= 0 ) { sbpol.delete( idx, idx + enclosure.length() ); idx = sbpol.indexOf( enclosure + enclosure ); } pol = sbpol.toString(); } if ( contains_escaped_enclosures ) { String replace = escapeCharacter + enclosure; String replaceWith = enclosure; pol = Const.replace( pol, replace, replaceWith ); } if ( contains_escaped_separators ) { String replace = escapeCharacter + delimiter; String replaceWith = delimiter; pol = Const.replace( pol, replace, replaceWith ); } if ( contains_escaped_escape ) { String replace = escapeCharacter + escapeCharacter; String replaceWith = escapeCharacter; pol = Const.replace( pol, replace, replaceWith ); } strings.add( pol ); pos = next + delimiter.length(); } if ( pos == length ) { if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.EndOfEmptyLineFound" ) ); } strings.add( "" ); } } else { for ( int i = 0; i < inf.inputFields.length; i++ ) { BaseFileField field = inf.inputFields[i]; int length = line.length(); if ( field.getPosition() + field.getLength() <= length ) { strings.add( line.substring( field.getPosition(), field.getPosition() + field.getLength() ) ); } else { if ( field.getPosition() < length ) { strings.add( line.substring( field.getPosition() ) ); } else { strings.add( "" ); } } } } } catch ( Exception e ) { throw new KettleException( BaseMessages.getString( PKG, "TextFileInput.Log.Error.ErrorConvertingLine", e .toString() ), e ); } return strings.toArray( new String[strings.size()] ); }  static final String[] guessStringsFromLine( VariableSpace space, LogChannelInterface log, String line,
      TextFileInputMeta inf, String delimiter, String enclosure, String escapeCharacter ); static final String getLine( LogChannelInterface log, InputStreamReader reader, int formatNr,
      StringBuilder line ); static final String getLine( LogChannelInterface log, InputStreamReader reader, EncodingType encodingType,
      int formatNr, StringBuilder line ); static final Object[] convertLineToRow( LogChannelInterface log, TextFileLine textFileLine,
      TextFileInputMeta info, Object[] passThruFields, int nrPassThruFields, RowMetaInterface outputRowMeta,
      RowMetaInterface convertRowMeta, String fname, long rowNr, String delimiter, String enclosure,
      String escapeCharacter, FileErrorHandler errorHandler,
      BaseFileInputAdditionalField additionalOutputFields, String shortFilename, String path,
      boolean hidden, Date modificationDateTime, String uri, String rooturi, String extension, Long size ); static final String[] convertLineToStrings( LogChannelInterface log, String line, TextFileInputMeta inf,
      String delimiter, String enclosure, String escapeCharacters ); }
TextFileInputUtils { public static final String[] guessStringsFromLine( VariableSpace space, LogChannelInterface log, String line, TextFileInputMeta inf, String delimiter, String enclosure, String escapeCharacter ) throws KettleException { List<String> strings = new ArrayList<>(); String pol; try { if ( line == null ) { return null; } if ( inf.content.fileType.equalsIgnoreCase( "CSV" ) ) { int pos = 0; int length = line.length(); boolean dencl = false; int len_encl = ( enclosure == null ? 0 : enclosure.length() ); int len_esc = ( escapeCharacter == null ? 0 : escapeCharacter.length() ); while ( pos < length ) { int from = pos; int next; boolean encl_found; boolean contains_escaped_enclosures = false; boolean contains_escaped_separators = false; boolean contains_escaped_escape = false; if ( len_encl > 0 && line.substring( from, from + len_encl ).equalsIgnoreCase( enclosure ) ) { if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.ConvertLineToRow", line.substring( from, from + len_encl ) ) ); } encl_found = true; int p = from + len_encl; boolean is_enclosure = len_encl > 0 && p + len_encl < length && line.substring( p, p + len_encl ).equalsIgnoreCase( enclosure ); boolean is_escape = len_esc > 0 && p + len_esc < length && line.substring( p, p + len_esc ).equalsIgnoreCase( escapeCharacter ); boolean enclosure_after = false; if ( ( is_enclosure || is_escape ) && p < length - 1 ) { String strnext = line.substring( p + len_encl, p + 2 * len_encl ); if ( strnext.equalsIgnoreCase( enclosure ) ) { p++; enclosure_after = true; dencl = true; if ( is_escape ) { contains_escaped_enclosures = true; } } else if ( strnext.equals( escapeCharacter ) ) { p++; if ( is_escape ) { contains_escaped_escape = true; } } } while ( ( !is_enclosure || enclosure_after ) && p < line.length() ) { p++; enclosure_after = false; is_enclosure = len_encl > 0 && p + len_encl < length && line.substring( p, p + len_encl ).equals( enclosure ); is_escape = len_esc > 0 && p + len_esc < length && line.substring( p, p + len_esc ).equals( escapeCharacter ); if ( ( is_enclosure || is_escape ) && p < length - 1 ) { String strnext = line.substring( p + len_encl, p + 2 * len_encl ); if ( strnext.equals( enclosure ) ) { p++; enclosure_after = true; dencl = true; if ( is_escape ) { contains_escaped_enclosures = true; } } else if ( strnext.equals( escapeCharacter ) ) { p++; if ( is_escape ) { contains_escaped_escape = true; } } } } if ( p >= length ) { next = p; } else { next = p + len_encl; } if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.EndOfEnclosure", "" + p ) ); } } else { encl_found = false; boolean found = false; int startpoint = from; do { next = line.indexOf( delimiter, startpoint ); if ( len_esc > 0 && next - len_esc > 0 ) { String before = line.substring( next - len_esc, next ); if ( escapeCharacter.equals( before ) ) { startpoint = next + 1; contains_escaped_separators = true; } else { found = true; } } else { found = true; } } while ( !found && next >= 0 ); } if ( next == -1 ) { next = length; } if ( encl_found ) { pol = line.substring( from + len_encl, next - len_encl ); if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.EnclosureFieldFound", "" + pol ) ); } } else { pol = line.substring( from, next ); if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.NormalFieldFound", "" + pol ) ); } } if ( dencl ) { StringBuilder sbpol = new StringBuilder( pol ); int idx = sbpol.indexOf( enclosure + enclosure ); while ( idx >= 0 ) { sbpol.delete( idx, idx + enclosure.length() ); idx = sbpol.indexOf( enclosure + enclosure ); } pol = sbpol.toString(); } if ( contains_escaped_enclosures ) { String replace = escapeCharacter + enclosure; String replaceWith = enclosure; pol = Const.replace( pol, replace, replaceWith ); } if ( contains_escaped_separators ) { String replace = escapeCharacter + delimiter; String replaceWith = delimiter; pol = Const.replace( pol, replace, replaceWith ); } if ( contains_escaped_escape ) { String replace = escapeCharacter + escapeCharacter; String replaceWith = escapeCharacter; pol = Const.replace( pol, replace, replaceWith ); } strings.add( pol ); pos = next + delimiter.length(); } if ( pos == length ) { if ( log.isRowLevel() ) { log.logRowlevel( BaseMessages.getString( PKG, "TextFileInput.Log.ConvertLineToRowTitle" ), BaseMessages .getString( PKG, "TextFileInput.Log.EndOfEmptyLineFound" ) ); } strings.add( "" ); } } else { for ( int i = 0; i < inf.inputFields.length; i++ ) { BaseFileField field = inf.inputFields[i]; int length = line.length(); if ( field.getPosition() + field.getLength() <= length ) { strings.add( line.substring( field.getPosition(), field.getPosition() + field.getLength() ) ); } else { if ( field.getPosition() < length ) { strings.add( line.substring( field.getPosition() ) ); } else { strings.add( "" ); } } } } } catch ( Exception e ) { throw new KettleException( BaseMessages.getString( PKG, "TextFileInput.Log.Error.ErrorConvertingLine", e .toString() ), e ); } return strings.toArray( new String[strings.size()] ); }  static final String[] guessStringsFromLine( VariableSpace space, LogChannelInterface log, String line,
      TextFileInputMeta inf, String delimiter, String enclosure, String escapeCharacter ); static final String getLine( LogChannelInterface log, InputStreamReader reader, int formatNr,
      StringBuilder line ); static final String getLine( LogChannelInterface log, InputStreamReader reader, EncodingType encodingType,
      int formatNr, StringBuilder line ); static final Object[] convertLineToRow( LogChannelInterface log, TextFileLine textFileLine,
      TextFileInputMeta info, Object[] passThruFields, int nrPassThruFields, RowMetaInterface outputRowMeta,
      RowMetaInterface convertRowMeta, String fname, long rowNr, String delimiter, String enclosure,
      String escapeCharacter, FileErrorHandler errorHandler,
      BaseFileInputAdditionalField additionalOutputFields, String shortFilename, String path,
      boolean hidden, Date modificationDateTime, String uri, String rooturi, String extension, Long size ); static final String[] convertLineToStrings( LogChannelInterface log, String line, TextFileInputMeta inf,
      String delimiter, String enclosure, String escapeCharacters );  }