Example usage for org.springframework.batch.item.file.transform FieldSet readRawString

List of usage examples for org.springframework.batch.item.file.transform FieldSet readRawString

Introduction

In this page you can find the example usage for org.springframework.batch.item.file.transform FieldSet readRawString.

Prototype

String readRawString(String name);

Source Link

Document

Read the String value from column with given 'name' including trailing whitespace (don't trim).

Usage

From source file:de.langmi.spring.batch.examples.readers.file.tokenizers.DelimitedLineTokenizerTests.java

/**
 * Standard tokenizer test with white space values.
 * Shows the differences for readString and readRawString, first one
 * trims whitespace./*  ww  w  .  j av a  2  s.  c o  m*/
 *
 * @throws Exception
 * @see http://forum.springsource.org/showthread.php?114765-Issue-FlatFileItemReader-automatically-Trimming-the-leading-and-trailing-spaces
 */
@Test
public void testWithSpaces() throws Exception {
    // set names only, standard configuration
    tokenizer.setNames(new String[] { "id", "value1", "value2" });
    // fire
    FieldSet fieldSet = tokenizer.tokenize("1,   foo   ,   bar");

    // assertions
    assertNotNull(fieldSet);
    assertTrue("field count", fieldSet.getFieldCount() == 3);
    assertEquals("1", fieldSet.readString("id"));
    // readString trims
    assertEquals("foo", fieldSet.readString("value1"));
    assertEquals("bar", fieldSet.readString("value2"));
    // readRawString does not trim
    assertEquals("   foo   ", fieldSet.readRawString("value1"));
    assertEquals("   bar", fieldSet.readRawString("value2"));

    // assertions with properties
    Properties props = fieldSet.getProperties();
    // the used fieldSet impl. trims whitespace in its getProperties method
    // see http://forum.springsource.org/showthread.php?114765-Issue-FlatFileItemReader-automatically-Trimming-the-leading-and-trailing-spaces
    // for follow up problems e.g. with BeanWrapperFieldSetMapper
    assertEquals("foo", props.getProperty("value1"));
    assertEquals("bar", props.getProperty("value2"));
}

From source file:org.cbioportal.annotation.pipeline.MutationFieldSetMapper.java

@Override
public MutationRecord mapFieldSet(FieldSet fs) throws BindException {
    MutationRecord record = new MutationRecord();
    Set<String> names = new HashSet(Arrays.asList(fs.getNames()));
    names.addAll(record.getHeader());/*from ww w .j  a  va2 s  .  co m*/
    for (String field : names) {
        try {
            record.getClass().getMethod("set" + field.toUpperCase(), String.class).invoke(record,
                    fs.readRawString(field));
        } catch (Exception e) {
            if (e.getClass().equals(NoSuchMethodException.class)) {
                record.addAdditionalProperty(field, fs.readRawString(field));
            } else {
                LOG.error("Something went wrong reading field " + field);
            }
        }
    }
    return record;
}