FixedWidthCSVTokenizer implements CSVTokenizer { @Override public List<String> tokenizeLine(String line, CSVStrategy strategy, BufferedReader reader) throws IOException { final List<String> tokens = new ArrayList<String>(); for (int i = 0; i<fields.length; i += 2) { int min = fields[i]; int max = fields[i+1]; if (Math.max(min,max) >= line.length()) { throw new IOException("Field delimited by positions [" + min + "," + max + "] out of range for line [" + line + "] of size [" + line.length() + "]"); } String token = line.substring(min, max+1); if (nullString != null && nullString.equals(token.trim())) { tokens.add(null); } else { tokens.add(token); } } return tokens; } FixedWidthCSVTokenizer(int []fields, String nullString); @Override List<String> tokenizeLine(String line, CSVStrategy strategy, BufferedReader reader);  }
@Test public void testTokenizeLine() throws IOException { String line = "012345678901234567890"; int fields[] = new int[]{0,0, 19,20, 5,10, 5,15}; FixedWidthCSVTokenizer fwt = new FixedWidthCSVTokenizer(fields, null); List<String> tokens = fwt.tokenizeLine(line, null, null); String[] results = new String[] {"0", "90", "567890", "56789012345"}; assertEquals(4, tokens.size()); int c = 0; for (String token: tokens) { assertEquals(results[c++], token); } fwt = new FixedWidthCSVTokenizer(fields, "90"); tokens = fwt.tokenizeLine(line, null, null); assertNull(tokens.get(1)); assertNotNull(tokens.get(0)); fwt = new FixedWidthCSVTokenizer(fields, "0"); tokens = fwt.tokenizeLine(line, null, null); assertNull(tokens.get(0)); assertNotNull(tokens.get(1)); boolean fail = true; fields = new int[]{1,0,2}; try { fwt = new FixedWidthCSVTokenizer(fields, null); } catch (IllegalArgumentException e) { fail = false; } assertFalse(fail); fail = true; fields = new int[]{1,0}; try { fwt = new FixedWidthCSVTokenizer(fields, null); fwt.tokenizeLine(line, null, null); } catch (IllegalArgumentException e) { fail = false; } assertFalse(fail); fail = true; fields = new int[]{20,21}; fwt = new FixedWidthCSVTokenizer(fields, null); try { fwt.tokenizeLine(line, null, null); } catch (IOException e) { fail = false; } assertFalse(fail); fail = true; fields = new int[]{-1,1}; try { fwt = new FixedWidthCSVTokenizer(fields, null); fwt.tokenizeLine(line, null, null); } catch (IllegalArgumentException e) { fail = false; } assertFalse(fail); fail = true; fields = new int[]{-1,1}; try { fwt = new FixedWidthCSVTokenizer(fields, null); fwt.tokenizeLine(line, null, null); } catch (IllegalArgumentException e) { fail = false; } assertFalse(fail); }
@Test public void testNull() throws IOException { String line = " - "; int fields[] = new int[]{0,3, 3,4}; FixedWidthCSVTokenizer fwt = new FixedWidthCSVTokenizer(fields, "-"); List<String> tokens = fwt.tokenizeLine(line, null, null); assertNull(tokens.get(0)); assertEquals(" ", tokens.get(1)); fwt = new FixedWidthCSVTokenizer(fields, " "); tokens = fwt.tokenizeLine(line, null, null); assertNull(tokens.get(1)); assertEquals(" - ", tokens.get(0)); }