String[] readFieldNamesFromFile( String fileName, CsvInputMeta csvInputMeta ) throws KettleException { String delimiter = environmentSubstitute( csvInputMeta.getDelimiter() ); String enclosure = environmentSubstitute( csvInputMeta.getEnclosure() ); String realEncoding = environmentSubstitute( csvInputMeta.getEncoding() ); try ( FileObject fileObject = KettleVFS.getFileObject( fileName, getTransMeta() ); BOMInputStream inputStream = new BOMInputStream( KettleVFS.getInputStream( fileObject ), ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE ) ) { InputStreamReader reader = null; if ( Utils.isEmpty( realEncoding ) ) { reader = new InputStreamReader( inputStream ); } else { reader = new InputStreamReader( inputStream, realEncoding ); } EncodingType encodingType = EncodingType.guessEncodingType( reader.getEncoding() ); String line = TextFileInput.getLine( log, reader, encodingType, TextFileInputMeta.FILE_FORMAT_UNIX, new StringBuilder( 1000 ) ); String[] fieldNames = CsvInput.guessStringsFromLine( log, line, delimiter, enclosure, csvInputMeta.getEscapeCharacter() ); if ( !Utils.isEmpty( csvInputMeta.getEnclosure() ) ) { removeEnclosure( fieldNames, csvInputMeta.getEnclosure() ); } trimFieldNames( fieldNames ); return fieldNames; } catch ( IOException e ) { throw new KettleFileException( BaseMessages.getString( PKG, "CsvInput.Exception.CreateFieldMappingError" ), e ); } }
@Test ( expected = NullPointerException.class ) public void guessStringsFromLineWithEmptyLine() throws Exception { line = null; String[] saData = guessStringsFromLine( logChannelInterface, line, csvInputMeta.getDelimiter(), csvInputMeta.getEnclosure(), csvInputMeta.getEscapeCharacter() ); assertNull( saData ); for ( int i = 0; i < saData.length; i++ ) { return; } }
wEnclosure.setText( Const.NVL( inputMeta.getEnclosure(), "" ) ); wBufferSize.setText( Const.NVL( inputMeta.getBufferSize(), "" ) ); wLazyConversion.setSelection( inputMeta.isLazyConversionActive() );
data.delimiter = data.encodingType.getBytes( environmentSubstitute( meta.getDelimiter() ), realEncoding ); if ( Utils.isEmpty( meta.getEnclosure() ) ) { data.enclosure = null; } else { data.enclosure = data.encodingType.getBytes( environmentSubstitute( meta.getEnclosure() ), realEncoding );
@Test public void testClone() { final CsvInputMeta original = new CsvInputMeta(); original.setDelimiter( ";" ); original.setEnclosure( "'" ); final TextFileInputField[] originalFields = new TextFileInputField[ 1 ]; final TextFileInputField originalField = new TextFileInputField(); originalField.setName( "field" ); originalFields[ 0 ] = originalField; original.setInputFields( originalFields ); final CsvInputMeta clone = (CsvInputMeta) original.clone(); // verify that the clone and its input fields are "equal" to the originals, but not the same objects Assert.assertNotSame( original, clone ); Assert.assertEquals( original.getDelimiter(), clone.getDelimiter() ); Assert.assertEquals( original.getEnclosure(), clone.getEnclosure() ); Assert.assertNotSame( original.getInputFields(), clone.getInputFields() ); Assert.assertNotSame( original.getInputFields()[ 0 ], clone.getInputFields()[ 0 ] ); Assert.assertEquals( original.getInputFields()[ 0 ].getName(), clone.getInputFields()[ 0 ].getName() ); } }