View Javadoc
1   package org.csveed.report;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   public class RowReport {
7   
8       private String row;
9   
10      private int start;
11  
12      private int end;
13  
14      public RowReport(String row, int start, int end) {
15          this.row = row;
16          this.start = start;
17          this.end = start == end && start < row.length() ? start + 1 : end;
18      }
19  
20      public String getRow() {
21          return row;
22      }
23  
24      public int getStart() {
25          return start;
26      }
27  
28      public int getEnd() {
29          return end;
30      }
31  
32      public List<RowPart> tokenize() {
33          List<RowPart> lines = new ArrayList<>();
34          if (start > 0) {
35              lines.add(new RowPart(row.substring(0, start), false));
36          }
37          if (end - start > 0) {
38              lines.add(new RowPart(row.substring(start, end), true));
39          }
40          if (end < row.length()) {
41              lines.add(new RowPart(row.substring(end), false));
42          }
43          return lines;
44      }
45  
46      public List<String> getPrintableLines() {
47          List<String> lines = new ArrayList<>();
48  
49          List<RowPart> parts = tokenize();
50  
51          lines.add(createContentLine(parts));
52          lines.add(createFocusLine(parts));
53  
54          return lines;
55      }
56  
57      private String createContentLine(List<RowPart> parts) {
58          StringBuilder line = new StringBuilder();
59          for (RowPart token : parts) {
60              line.append(token.getToken());
61          }
62          return line.toString();
63      }
64  
65      private String createFocusLine(List<RowPart> parts) {
66          StringBuilder line = new StringBuilder();
67          boolean placedMarkers = false;
68          for (RowPart token : parts) {
69              if (token.isHighlight()) {
70                  line.append(printUnderscoredPart(token));
71                  placedMarkers = true;
72              } else {
73                  line.append(printEmptyPart(token));
74              }
75          }
76          if (!placedMarkers) { // Essentially only at the end-of-line
77              line.append('^');
78          }
79          return line.toString();
80      }
81  
82      private String printEmptyPart(RowPart token) {
83          StringBuilder linePart = new StringBuilder();
84          for (int i = 0; i < token.getToken().length(); i++) {
85              linePart.append(' ');
86          }
87          return linePart.toString();
88      }
89  
90      private String printUnderscoredPart(RowPart token) {
91          StringBuilder linePart = new StringBuilder();
92          for (int i = 0; i < token.getToken().length(); i++) {
93              if (i == 0 || i == token.getToken().length() - 1) {
94                  linePart.append('^');
95              } else {
96                  linePart.append('-');
97              }
98          }
99          return linePart.toString();
100     }
101 
102 }