View Javadoc
1   package org.csveed.row;
2   
3   import java.util.ArrayList;
4   import java.util.Iterator;
5   import java.util.List;
6   import java.util.Map;
7   import java.util.TreeMap;
8   
9   import org.csveed.common.Column;
10  import org.csveed.report.RowReport;
11  import org.slf4j.Logger;
12  import org.slf4j.LoggerFactory;
13  
14  public class LineWithInfo implements Line {
15  
16      private static final Logger LOG = LoggerFactory.getLogger(LineWithInfo.class);
17  
18      private List<String> cells = new ArrayList<>();
19  
20      private StringBuilder originalLine = new StringBuilder();
21  
22      private Map<Column, CellPositionInRow> cellPositions = new TreeMap<>();
23  
24      private int printLength;
25  
26      private ColumnColumn currentColumn = new Column();
27  
28      public void addCell(String cell) {
29          this.cells.add(cell);
30          if (cell == null || "".equals(cell)) {
31              markStartOfColumn();
32          }
33          markEndOfColumn();
34      }
35  
36      @Override
37      public Iterator<String> iterator() {
38          return cells.iterator();
39      }
40  
41      public void markStartOfColumn() {
42          LOG.debug("Start of column: {}", printLength);
43          getCellPosition(currentColumn).setStart(printLength);
44      }
45  
46      protected void markEndOfColumn() {
47          LOG.debug("End of column: {}", printLength);
48          getCellPosition(currentColumn).setEnd(printLength);
49          currentColumn = currentColumn.nextColumn();
50      }
51  
52      protected CellPositionInRow getCellPosition(Column column) {
53          CellPositionInRow cellPosition = cellPositions.get(column);
54          if (cellPosition == null) {
55              cellPosition = new CellPositionInRow();
56              cellPositions.put(column, cellPosition);
57          }
58          return cellPosition;
59      }
60  
61      public void addCharacter(int symbol) {
62          String printableChar = convertToPrintable(symbol);
63          originalLine.append(printableChar);
64          printLength += printableChar.length();
65      }
66  
67      public String convertToPrintable(int symbol) {
68          if (symbol == -1) {
69              return "[EOF]";
70          } else if (symbol == '\b') {
71              return "\\b";
72          } else if (symbol == '\f') {
73              return "\\f";
74          } else if (symbol == '\n') {
75              return "\\n";
76          } else if (symbol == '\r') {
77              return "\\r";
78          } else if (symbol == '\t') {
79              return "\\t";
80          } else {
81              return Character.toString((char)symbol);
82          }
83      }
84  
85      @Override
86      public int size() {
87          return this.cells.size();
88      }
89  
90      @Override
91      public String get(int index) {
92          return this.cells.get(index);
93      }
94  
95      @Override
96      public RowReport reportOnColumn(Column column) {
97          CellPositionInRow cellPosition = cellPositions.get(column);
98          if (cellPosition == null) {
99              return null;
100         }
101         return new RowReport(originalLine.toString(), cellPosition.getStart(), cellPosition.getEnd());
102     }
103 
104     @Override
105     public RowReport reportOnEndOfLine() {
106         return new RowReport(originalLine.toString(), printLength, printLength);
107     }
108 
109 }