Repository navigation
Expand file tree
/
Copy pathTalonFormatter.java
More file actions
235 lines (217 loc) · 9.46 KB
/
Copy pathTalonFormatter.java
File metadata and controls
235 lines (217 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package ec.gob.sri.dimm.ats.ui.editores;
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.regex.*;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.SWT;
public class TalonFormatter {
public static void setMonospaceFont(Object text) { } // legacy stub
public static void setMonospaceFontV2(Object text) {
try {
StyledText st = (StyledText) text;
Display disp = st.getDisplay();
FontData oldFd = st.getFont().getFontData()[0];
int h = oldFd.getHeight();
if (h < 6 || h > 48) h = 10;
String[] names = {"Monospace", "Liberation Mono", "DejaVu Sans Mono",
"Courier New", "Courier 10 Pitch", "Bitstream Vera Sans Mono",
"Andale Mono", "FreeMono", "Luxi Mono", "DejaVu LGC Sans Mono",
"Noto Mono", "Droid Sans Mono", "Ubuntu Mono", "Menlo",
"Consolas", "Source Code Pro", "Monaco", "Inconsolata",
"Fira Code", "JetBrains Mono"};
for (String name : names) {
try {
Font f = new Font(disp, name, h, SWT.NORMAL);
st.setFont(f);
return;
} catch (Exception e2) { }
}
} catch (Exception e) {
System.err.println("[TalonFormatter] setMonospaceFontV2 error: " + e);
}
}
public static String readFileContent(File file) {
try {
String html = new String(Files.readAllBytes(file.toPath()));
StringBuilder result = new StringBuilder();
int pos = 0;
while (true) {
int ti = html.indexOf("<table", pos);
if (ti < 0) break;
result.append(processText(html.substring(pos, ti)));
int te = html.indexOf("</table>", ti);
if (te < 0) { result.append(processText(html.substring(ti))); pos = html.length(); break; }
te += "</table>".length();
result.append('\n');
result.append(formatTable(html.substring(ti, te)));
result.append('\n');
pos = te;
}
result.append(processText(html.substring(pos)));
return result.toString().trim();
} catch (Exception e) {
return "Error al cargar el contenido del tal\u00F3n resumen.";
}
}
static String processText(String t) {
t = t.replaceAll("(?i)</?(p|div|br|li|h[1-6]|thead|tbody|tfoot|caption|dl|dt|dd|ol|ul|section|article|nav|header|footer|aside|details|summary|figure|figcaption)[^>]*>", "\n");
t = t.replaceAll("<[^>]+>", " ");
t = decodeEntities(t);
t = t.replaceAll("[ \\t]+", " ");
String[] lines = t.split("\n");
StringBuilder sb = new StringBuilder(t.length());
for (String r : lines) {
String line = r.trim();
if (!line.isEmpty()) sb.append(line).append('\n');
}
return sb.toString();
}
static String formatTable(String tableHtml) {
List<List<String>> rows = new ArrayList<>();
List<Integer> headerRows = new ArrayList<>();
Matcher rm = Pattern.compile("(?i)<tr[^>]*>(.*?)</tr>").matcher(tableHtml);
while (rm.find()) {
String rc = rm.group(1);
List<String> cells = new ArrayList<>();
boolean hasTH = false;
Matcher cm = Pattern.compile("(?i)<(t[hd])([^>]*)>(.*?)</\\1>").matcher(rc);
while (cm.find()) {
String tag = cm.group(1);
String attrs = cm.group(2);
String raw = cm.group(3).replaceAll("<[^>]+>", " ").replaceAll("\\s+", " ").trim();
String decoded = decodeEntities(raw);
if ("th".equalsIgnoreCase(tag)) hasTH = true;
int colspan = 1;
Matcher colM = Pattern.compile("(?i)colspan\\s*=\\s*['\"]?(\\d+)['\"]?").matcher(attrs);
if (colM.find()) colspan = Integer.parseInt(colM.group(1));
cells.add(decoded);
for (int i = 1; i < colspan; i++) cells.add("");
}
if (!cells.isEmpty()) {
rows.add(cells);
if (hasTH) headerRows.add(rows.size() - 1);
}
}
if (rows.isEmpty()) return processText(tableHtml);
// Layout table (no <th>): simple text rows, no dashes
if (headerRows.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (List<String> r : rows) {
List<String> nonEmpty = new ArrayList<>();
for (String v : r) if (!v.isEmpty()) nonEmpty.add(v);
if (nonEmpty.isEmpty()) continue;
for (int c = 0; c < nonEmpty.size(); c++) {
if (c > 0) sb.append(" ");
sb.append(nonEmpty.get(c));
}
sb.append('\n');
}
return sb.toString();
}
int maxCols = 0;
for (List<String> r : rows) maxCols = Math.max(maxCols, r.size());
// Filter section titles (1 non-empty cell) out of headerRows
List<Integer> realHeaders = new ArrayList<>();
for (int idx : headerRows) {
List<String> r = rows.get(idx);
int nonEmpty = 0;
for (String v : r) if (!v.isEmpty()) nonEmpty++;
if (nonEmpty >= 2) realHeaders.add(idx);
}
int headerRow = realHeaders.isEmpty() ? 0 : realHeaders.get(0);
int[] widths = new int[maxCols];
for (int ri = 0; ri < rows.size(); ri++) {
List<String> r = rows.get(ri);
int nonEmpty = 0;
for (String v : r) if (!v.isEmpty()) nonEmpty++;
if (nonEmpty <= 1) continue;
for (int c = 0; c < r.size(); c++) {
widths[c] = Math.max(widths[c], r.get(c).length());
}
}
boolean[] numCol = new boolean[maxCols];
Arrays.fill(numCol, true);
for (int ri = 0; ri < rows.size(); ri++) {
if (ri == headerRow) continue;
List<String> r = rows.get(ri);
for (int c = 0; c < r.size(); c++) {
String v = r.get(c);
if (!v.isEmpty() && !v.matches("[\\d.,\\-]+")) numCol[c] = false;
}
}
StringBuilder sb = new StringBuilder();
for (int ri = 0; ri < rows.size(); ri++) {
List<String> r = rows.get(ri);
// Section title rows (1 non-empty cell spanning all cols): plain text
int nonEmpty = 0;
for (String v : r) if (!v.isEmpty()) nonEmpty++;
if (nonEmpty <= 1) {
for (String v : r) if (!v.isEmpty()) { sb.append(v); break; }
sb.append('\n');
continue;
}
for (int c = 0; c < maxCols; c++) {
if (c > 0) sb.append(" ");
String val = c < r.size() ? r.get(c) : "";
if (numCol[c]) {
for (int p = val.length(); p < widths[c]; p++) sb.append(' ');
sb.append(val);
} else {
sb.append(val);
for (int p = val.length(); p < widths[c]; p++) sb.append(' ');
}
}
sb.append('\n');
if (ri == headerRow) {
for (int c = 0; c < maxCols; c++) {
if (c > 0) sb.append(" ");
for (int p = 0; p < widths[c]; p++) sb.append('-');
}
sb.append('\n');
}
}
return sb.toString();
}
private static String decodeEntities(String s) {
StringBuilder sb = new StringBuilder(s.length());
int i = 0;
while (i < s.length()) {
int amp = s.indexOf('&', i);
if (amp < 0) { sb.append(s.substring(i)); break; }
int semi = s.indexOf(';', amp);
if (semi < 0) { sb.append(s.substring(i)); break; }
sb.append(s.substring(i, amp));
String ent = s.substring(amp + 1, semi);
switch (ent) {
case "amp": sb.append('&'); break;
case "lt": sb.append('<'); break;
case "gt": sb.append('>'); break;
case "quot": sb.append('"'); break;
case "apos": sb.append('\''); break;
case "nbsp": sb.append(' '); break;
case "Oacute": sb.append('\u00D3'); break;
case "oacute": sb.append('\u00F3'); break;
case "Iacute": sb.append('\u00CD'); break;
case "iacute": sb.append('\u00ED'); break;
case "Eacute": sb.append('\u00C9'); break;
case "eacute": sb.append('\u00E9'); break;
case "Aacute": sb.append('\u00C1'); break;
case "aacute": sb.append('\u00E1'); break;
case "Uacute": sb.append('\u00DA'); break;
case "uacute": sb.append('\u00FA'); break;
case "Ntilde": sb.append('\u00D1'); break;
case "ntilde": sb.append('\u00F1'); break;
default: sb.append('&').append(ent).append(';'); break;
}
i = semi + 1;
}
return sb.toString();
}
public static void main(String[] args) throws Exception {
System.out.println(readFileContent(new File(args[0])));
}
}