1
2
3
4
5
6
7
8
9
10
11 package org.csveed.report;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16
17
18
19 public class RowReport {
20
21
22 private String row;
23
24
25 private int start;
26
27
28 private int end;
29
30
31
32
33
34
35
36
37
38
39
40 public RowReport(String row, int start, int end) {
41 this.row = row;
42 this.start = start;
43 this.end = start == end && start < row.length() ? start + 1 : end;
44 }
45
46
47
48
49
50
51 public String getRow() {
52 return row;
53 }
54
55
56
57
58
59
60 public int getStart() {
61 return start;
62 }
63
64
65
66
67
68
69 public int getEnd() {
70 return end;
71 }
72
73
74
75
76
77
78 public List<RowPart> tokenize() {
79 List<RowPart> lines = new ArrayList<>();
80 if (start > 0) {
81 lines.add(new RowPart(row.substring(0, start), false));
82 }
83 if (end - start > 0) {
84 lines.add(new RowPart(row.substring(start, end), true));
85 }
86 if (end < row.length()) {
87 lines.add(new RowPart(row.substring(end), false));
88 }
89 return lines;
90 }
91
92
93
94
95
96
97 public List<String> getPrintableLines() {
98 List<String> lines = new ArrayList<>();
99
100 List<RowPart> parts = tokenize();
101
102 lines.add(createContentLine(parts));
103 lines.add(createFocusLine(parts));
104
105 return lines;
106 }
107
108
109
110
111
112
113
114
115
116 private String createContentLine(List<RowPart> parts) {
117 StringBuilder line = new StringBuilder();
118 for (RowPart token : parts) {
119 line.append(token.getToken());
120 }
121 return line.toString();
122 }
123
124
125
126
127
128
129
130
131
132 private String createFocusLine(List<RowPart> parts) {
133 StringBuilder line = new StringBuilder();
134 boolean placedMarkers = false;
135 for (RowPart token : parts) {
136 if (token.isHighlight()) {
137 line.append(printUnderscoredPart(token));
138 placedMarkers = true;
139 } else {
140 line.append(printEmptyPart(token));
141 }
142 }
143 if (!placedMarkers) {
144 line.append('^');
145 }
146 return line.toString();
147 }
148
149
150
151
152
153
154
155
156
157 private String printEmptyPart(RowPart token) {
158 StringBuilder linePart = new StringBuilder();
159 for (int i = 0; i < token.getToken().length(); i++) {
160 linePart.append(' ');
161 }
162 return linePart.toString();
163 }
164
165
166
167
168
169
170
171
172
173 private String printUnderscoredPart(RowPart token) {
174 StringBuilder linePart = new StringBuilder();
175 for (int i = 0; i < token.getToken().length(); i++) {
176 if (i == 0 || i == token.getToken().length() - 1) {
177 linePart.append('^');
178 } else {
179 linePart.append('-');
180 }
181 }
182 return linePart.toString();
183 }
184
185 }