Package: CsvParser

CsvParser

nameinstructionbranchcomplexitylinemethod
CsvParser(CsvValueBuilder)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
parseValues(String)
M: 0 C: 93
100%
M: 0 C: 14
100%
M: 0 C: 8
100%
M: 0 C: 22
100%
M: 0 C: 1
100%

Coverage

1: package org.overture.interpreter.utilities.stdlibs;
2:
3: import java.util.LinkedList;
4: import java.util.List;
5:
6: import org.overture.interpreter.values.Value;
7:
8: public class CsvParser
9: {
10:         public static final String INVALID_CSV_MSG = "Invalid CSV data: cannot parse null";
11:         private CsvValueBuilder builder;
12:         
13:         public CsvParser(CsvValueBuilder builder)
14:         {
15:                 this.builder = builder;
16:         }
17:         
18:         public CsvResult parseValues(String line)
19:         {
20:                 List<Value> values = new LinkedList<>();
21:                 
22:•                if(line == null)
23:                 {
24:                         return new CsvResult(values, INVALID_CSV_MSG);
25:                 }
26:                 
27:•                if(line.isEmpty())
28:                 {
29:                         return new CsvResult(values);
30:                 }
31:                 
32:                 String lastError = null;
33:                 int last = 0;
34:•                for(int i = 0; i < line.length(); i++)
35:                 {
36:                         char c = line.charAt(i);
37:                         
38:•                        if(c == ',' || i == line.length() - 1)
39:                         {
40:                                 String cell;
41:                                 
42:•                                if(c == ',')
43:                                 {
44:                                         cell = line.substring(last,i);
45:                                 }
46:                                 else
47:                                 {
48:                                         cell = line.substring(last, line.length());
49:                                 }
50:                                 
51:                                 try
52:                                 {
53:                                         Value v = builder.createValue(cell);
54:                                         values.add(v);
55:                                         last = i + 1;
56:                                 }
57:                                 catch(Exception e)
58:                                 {
59:                                         // Proceed to next comma and try to parse value again
60:                                         // Happens for values such as {1,2},{3,4}
61:                                         lastError = e.getMessage();
62:                                 }
63:                         }
64:                 }
65:                 
66:•                if(last == line.length())
67:                 {
68:                         return new CsvResult(values);
69:                 }
70:                 else
71:                 {
72:                         return new CsvResult(values, lastError);
73:                 }
74:         }
75: }