Table.java
15.8 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.lang3.StringEscapeUtils
*/
package com.day.cq.wcm.foundation;
import com.day.cq.wcm.foundation.TableCSVBuilder;
import com.day.cq.wcm.foundation.TableXMLBuilder;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringEscapeUtils;
public class Table {
private LinkedList<Row> rows = new LinkedList();
private LinkedList<Column> cols = new LinkedList();
private Cell[][] data = new Cell[8][8];
private Tag caption;
private List<ColTag> colTags = new LinkedList<ColTag>();
private Attributes attributes = new Attributes();
public List<ColTag> getColTags() {
return this.colTags;
}
public Attributes getAttributes() {
return this.attributes;
}
public Table setAttribute(String name, String value) {
if (value == null) {
this.attributes.remove(name);
} else {
this.attributes.put(name, value);
}
return this;
}
public Tag getCaption() {
return this.caption;
}
public Tag setCaption(String text) {
if (this.caption == null) {
this.caption = new Tag("caption");
}
this.caption.setInnerHtml(text);
return this.caption;
}
public Cell getCell(int row, int col, boolean create) {
Cell cell = null;
if (row >= this.rows.size() || col >= this.cols.size()) {
if (create) {
this.setSize(Math.max(row + 1, this.rows.size()), Math.max(col + 1, this.cols.size()));
cell = this.data[row][col];
}
} else {
cell = this.data[row][col];
}
return cell;
}
public int[][] getIntData(int rowStart, int numRows, int colStart, int numCols) {
if (rowStart + numRows > this.rows.size()) {
numRows = this.rows.size() - rowStart;
}
if (colStart + numCols > this.cols.size()) {
numCols = this.cols.size() - colStart;
}
int[][] ret = new int[numRows][numCols];
for (int r = 0; r < numRows; ++r) {
for (int c = 0; c < numCols; ++c) {
ret[r][c] = this.data[r + rowStart][c + colStart].getIntValue();
}
}
return ret;
}
public int[][] getIntData() {
return this.getIntData(0, this.rows.size(), 0, this.cols.size());
}
public double[][] getDoubleData(int rowStart, int numRows, int colStart, int numCols) {
if (rowStart + numRows > this.rows.size()) {
numRows = this.rows.size() - rowStart;
}
if (colStart + numCols > this.cols.size()) {
numCols = this.cols.size() - colStart;
}
double[][] ret = new double[numRows][numCols];
for (int r = 0; r < numRows; ++r) {
for (int c = 0; c < numCols; ++c) {
ret[r][c] = this.data[r + rowStart][c + colStart].getDoubleValue();
}
}
return ret;
}
public double[][] getDoubleData() {
return this.getDoubleData(0, this.rows.size(), 0, this.cols.size());
}
public void setSize(int numRows, int numCols) {
if (numRows >= this.data.length || numCols >= this.data[0].length) {
Cell[][] newData = new Cell[this.data.length * 2][this.data[0].length * 2];
for (int r = 0; r < this.rows.size(); ++r) {
System.arraycopy(this.data[r], 0, newData[r], 0, this.cols.size());
}
this.data = newData;
}
while (this.rows.size() < numRows) {
Row row = new Row(this.rows.size());
this.rows.add(row);
for (Column col : this.cols) {
this.data[Row.access$100((Row)row)][Column.access$200((Column)col)] = new Cell(row, col);
}
}
while (this.rows.size() > numRows) {
this.rows.removeLast();
}
while (this.cols.size() < numCols) {
Column col = new Column(this.cols.size());
this.cols.add(col);
for (Row row : this.rows) {
this.data[Row.access$100((Row)row)][Column.access$200((Column)col)] = new Cell(row, col);
}
}
while (this.cols.size() > numCols) {
this.cols.removeLast();
}
}
public List<Row> getRows() {
return Collections.unmodifiableList(this.rows);
}
public List<Column> getColumns() {
return Collections.unmodifiableList(this.cols);
}
public Row getRow(int nr) {
return nr < this.rows.size() ? null : this.rows.get(nr);
}
public Column getColumn(int nr) {
return nr < this.cols.size() ? null : this.cols.get(nr);
}
public int getNumCols() {
return this.cols.size();
}
public int getNumRows() {
return this.rows.size();
}
public void clear() {
this.rows.clear();
this.cols.clear();
}
public static Table fromXML(String s) {
try {
return Table.fromXML(new StringReader(s));
}
catch (IOException e) {
throw new IllegalStateException(e);
}
}
public static Table fromXML(Reader r) throws IOException {
return new TableXMLBuilder().parse(r);
}
public static Table fromCSV(String s) {
return new TableCSVBuilder().parse(s);
}
public static Table fromCSV(String s, char delim) {
return new TableCSVBuilder(delim).parse(s);
}
public void toHtml(Writer out) throws IOException {
out.write("<table");
this.attributes.toHtml(out);
out.write(">\n");
if (this.caption != null) {
this.caption.toHtml(out);
out.write("\n");
}
for (Tag cg : this.colTags) {
cg.toHtml(out);
out.write("\n");
}
for (Row row : this.rows) {
out.write("<tr>");
for (Column col : this.cols) {
Cell cell = this.data[row.nr][col.nr];
if (cell.getSpanSource() != null) continue;
this.data[row.nr][col.nr].toHtml(out);
}
out.write("</tr>\n");
}
out.write("</table>\n");
}
public static class Attributes
extends HashMap<String, String> {
public void toHtml(Writer out) throws IOException {
for (Map.Entry e : this.entrySet()) {
out.write(" ");
out.write((String)e.getKey());
out.write("=\"");
out.write(StringEscapeUtils.escapeHtml4((String)((String)e.getValue())));
out.write("\"");
}
}
}
public static class ColTag
extends Tag {
public ColTag() {
super("col");
}
@Override
public Tag setInnerHtml(String innerHtml) {
return this;
}
}
public static class Tag {
protected final String name;
protected final Attributes attrs = new Attributes();
protected String innerHtml;
public Tag(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public String getInnerHtml() {
return this.innerHtml;
}
public Tag setInnerHtml(String innerHtml) {
this.innerHtml = innerHtml;
return this;
}
public Tag appendInnerHtml(char[] ch, int offset, int len) {
this.innerHtml = this.innerHtml == null ? new String(ch, offset, len) : this.innerHtml + new String(ch, offset, len);
return this;
}
public Attributes getAttributes() {
return this.attrs;
}
public Tag setAttribute(String name, String value) {
if (value == null) {
this.attrs.remove(name);
} else {
this.attrs.put(name, value);
}
return this;
}
public void toHtml(Writer out) throws IOException {
out.write("<");
out.write(this.name);
if (this.attrs != null) {
this.attrs.toHtml(out);
}
out.write(">");
if (this.innerHtml != null) {
out.write(this.innerHtml);
out.write("</");
out.write(this.name);
out.write(">");
}
}
}
public static class Cell {
private Row row;
private Column col;
private String text;
private Attributes attributes;
private boolean header;
private int colSpan = 1;
private int rowSpan = 1;
private Cell spanSource;
private Cell(Row row, Column col) {
this.row = row;
this.col = col;
}
public Row getRow() {
return this.row;
}
public Column getCol() {
return this.col;
}
public Cell getSpanSource() {
return this.spanSource;
}
public String getText() {
return this.text;
}
public Cell appendText(char[] ch, int offset, int len) {
this.text = this.text == null ? new String(ch, offset, len) : this.text + new String(ch, offset, len);
return this;
}
public int getIntValue() {
try {
return this.text == null ? 0 : Integer.parseInt(this.text);
}
catch (NumberFormatException e) {
return 0;
}
}
public double getDoubleValue() {
try {
return this.text == null ? 0.0 : Double.parseDouble(this.text);
}
catch (NumberFormatException e) {
return Double.NaN;
}
}
public int getColSpan() {
return this.colSpan;
}
public boolean isInSpan() {
return this.spanSource != null;
}
private Table getTable() {
return this.row.getTable();
}
public Cell setColSpan(int colSpan) {
if (colSpan == 0) {
colSpan = 1;
}
this.internalSetSpans(this.rowSpan, colSpan);
if (this.attributes == null) {
this.attributes = new Attributes();
}
if (colSpan > 1) {
this.attributes.put("colspan", String.valueOf(this.colSpan));
} else {
this.attributes.remove("colspan");
}
return this;
}
public int getRowSpan() {
return this.rowSpan;
}
public Cell setRowSpan(int rowSpan) {
if (rowSpan == 0) {
rowSpan = 1;
}
this.internalSetSpans(rowSpan, this.colSpan);
if (this.attributes == null) {
this.attributes = new Attributes();
}
if (rowSpan > 1) {
this.attributes.put("rowspan", String.valueOf(this.rowSpan));
} else {
this.attributes.remove("rowspan");
}
return this;
}
private void internalSetSpans(int rSpan, int cSpan) {
int r;
int c;
Table table = this.getTable();
int rowNr = this.row.getNr();
int colNr = this.col.getNr();
for (r = 0; r < this.rowSpan; ++r) {
for (c = 0; c < this.colSpan; ++c) {
table.getCell((int)(rowNr + r), (int)(colNr + c), (boolean)false).spanSource = null;
}
}
this.rowSpan = rSpan;
this.colSpan = cSpan;
for (r = 0; r < this.rowSpan; ++r) {
for (c = 0; c < this.colSpan; ++c) {
if (r + c <= 0) continue;
table.getCell((int)(rowNr + r), (int)(colNr + c), (boolean)true).spanSource = this;
}
}
}
public Cell setText(CharSequence text) {
this.text = text.toString();
return this;
}
public boolean isHeader() {
return this.header;
}
public Cell setHeader(boolean header) {
this.header = header;
return this;
}
public String getAttribute(String name) {
if (this.attributes == null) {
return null;
}
return (String)this.attributes.get(name);
}
public Cell setAttribute(String name, String value) {
if (this.attributes == null) {
this.attributes = new Attributes();
}
this.attributes.put(name, value);
if (name.equalsIgnoreCase("colspan")) {
this.setColSpan(Integer.parseInt(value));
} else if (name.equalsIgnoreCase("rowspan")) {
this.setRowSpan(Integer.parseInt(value));
}
return this;
}
public Cell clearAttributes() {
this.attributes = null;
this.internalSetSpans(1, 1);
return this;
}
public Map<String, String> getAttributes() {
if (this.attributes == null) {
return Collections.emptyMap();
}
return Collections.unmodifiableMap(this.attributes);
}
public void toHtml(Writer out) throws IOException {
String tag = this.header ? "th" : "td";
out.write("<");
out.write(tag);
if (this.attributes != null) {
this.attributes.toHtml(out);
}
out.write(">");
if (this.text != null) {
out.write(this.text);
}
out.write("</");
out.write(tag);
out.write(">");
}
}
public class Column {
private int nr;
private Column(int nr) {
this.nr = nr;
}
public int getNr() {
return this.nr;
}
public boolean isFirst() {
return this.nr == 0;
}
public boolean isLast() {
return this.nr == Table.this.cols.size() - 1;
}
public Table getTable() {
return Table.this;
}
public List<Cell> getCells() {
ArrayList<Cell> cells = new ArrayList<Cell>(Table.this.rows.size());
for (int r = 0; r < Table.this.rows.size(); ++r) {
Cell cell = Table.this.data[r][this.nr];
if (cell.isInSpan()) continue;
cells.add(cell);
}
return cells;
}
}
public class Row {
private int nr;
private Row(int nr) {
this.nr = nr;
}
public int getNr() {
return this.nr;
}
public boolean isFirst() {
return this.nr == 0;
}
public boolean isLast() {
return this.nr == Table.this.rows.size() - 1;
}
public Table getTable() {
return Table.this;
}
public List<Cell> getCells() {
ArrayList<Cell> cells = new ArrayList<Cell>(Table.this.cols.size());
for (int c = 0; c < Table.this.cols.size(); ++c) {
Cell cell = Table.this.data[this.nr][c];
if (cell.isInSpan()) continue;
cells.add(cell);
}
return cells;
}
public Row setHeader(boolean header) {
for (Cell c : this.getCells()) {
c.setHeader(header);
}
return this;
}
}
}