Skip to content

Commit bdd9127

Browse files
authored
Merge pull request #1 from spacious-team/develop
Релиз 2022.2
2 parents 5d4f554 + 48a1db6 commit bdd9127

File tree

10 files changed

+594
-2
lines changed

10 files changed

+594
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Предоставляет реализацию `Table Wrapper API` для удобного доступа к табличным данным, сохраненным в файлах формата `csv`.
66
Пример создания таблиц из файла `1.csv`
77
```java
8-
ReportPage reportPage = ...;
8+
ReportPage reportPage = new CsvReportPage(Path.of("1.csv"));
99

1010
Table table1 = reportPage.create("Table 1 description", ...);
1111
...

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@
7171
<dependency>
7272
<groupId>com.github.spacious-team</groupId>
7373
<artifactId>table-wrapper-api</artifactId>
74-
<version>2021.6.1</version>
74+
<version>2022.2</version>
75+
</dependency>
76+
<dependency>
77+
<groupId>com.univocity</groupId>
78+
<artifactId>univocity-parsers</artifactId>
79+
<version>2.9.1</version>
7580
</dependency>
7681
<dependency>
7782
<groupId>org.projectlombok</groupId>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Table Wrapper Xml SpreadsheetML Impl
3+
* Copyright (C) 2022 Vitalii Ananev <spacious-team@ya.ru>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as
7+
* published by the Free Software Foundation, either version 3 of the
8+
* License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package org.spacious_team.table_wrapper.csv;
20+
21+
import lombok.Getter;
22+
import lombok.Setter;
23+
import org.spacious_team.table_wrapper.api.CellDataAccessObject;
24+
import org.spacious_team.table_wrapper.csv.CsvTableCell.RowAndIndex;
25+
26+
import java.time.Instant;
27+
import java.time.LocalDate;
28+
import java.time.LocalDateTime;
29+
import java.time.ZoneOffset;
30+
import java.time.format.DateTimeFormatter;
31+
32+
public class CsvCellDataAccessObject implements CellDataAccessObject<RowAndIndex, CsvTableRow> {
33+
public static final CsvCellDataAccessObject INSTANCE = new CsvCellDataAccessObject();
34+
@Setter
35+
@Getter
36+
public static DateTimeFormatter dateTimeFormatter = null;
37+
38+
@Override
39+
public RowAndIndex getCell(CsvTableRow row, Integer cellIndex) {
40+
return row.getCell(cellIndex).getRowAndIndex();
41+
}
42+
43+
@Override
44+
public String getValue(RowAndIndex cell) {
45+
int columnIndex = cell.getColumnIndex();
46+
String[] row = cell.getRow();
47+
return (columnIndex < row.length) ? row[columnIndex] : null;
48+
}
49+
50+
@Override
51+
public Instant getInstantValue(RowAndIndex cell) {
52+
String value = getValue(cell);
53+
DateTimeFormatter formatter = (dateTimeFormatter != null) ?
54+
dateTimeFormatter :
55+
DateTimeFormatParser.getFor(value);
56+
LocalDateTime dateTime = (value.length() == 10) ?
57+
LocalDate.parse(value, formatter).atTime(12, 0) :
58+
LocalDateTime.parse(value, formatter);
59+
return dateTime
60+
.atZone(ZoneOffset.systemDefault())
61+
.toInstant();
62+
}
63+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Table Wrapper Xml SpreadsheetML Impl
3+
* Copyright (C) 2022 Vitalii Ananev <spacious-team@ya.ru>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as
7+
* published by the Free Software Foundation, either version 3 of the
8+
* License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package org.spacious_team.table_wrapper.csv;
20+
21+
import com.univocity.parsers.common.record.Record;
22+
import com.univocity.parsers.csv.CsvParser;
23+
import com.univocity.parsers.csv.CsvParserSettings;
24+
import lombok.RequiredArgsConstructor;
25+
import org.spacious_team.table_wrapper.api.AbstractReportPage;
26+
import org.spacious_team.table_wrapper.api.TableCellAddress;
27+
28+
import java.io.IOException;
29+
import java.io.InputStream;
30+
import java.io.InputStreamReader;
31+
import java.io.Reader;
32+
import java.nio.charset.Charset;
33+
import java.nio.file.Files;
34+
import java.nio.file.Path;
35+
import java.nio.file.StandardOpenOption;
36+
import java.util.function.BiPredicate;
37+
38+
import static java.nio.charset.StandardCharsets.UTF_8;
39+
40+
public class CsvReportPage extends AbstractReportPage<CsvTableRow> {
41+
42+
private final String[][] rows;
43+
44+
/**
45+
* Field and line delimiter detected automatically. UTF-8 encoding file expected.
46+
*/
47+
public CsvReportPage(Path path) throws IOException {
48+
this(Files.newInputStream(path, StandardOpenOption.READ), UTF_8, getDefaultCsvParserSettings());
49+
}
50+
51+
/**
52+
* Closes inputStream if success
53+
*/
54+
public CsvReportPage(InputStream inputStream, Charset charset, CsvParserSettings csvParserSettings) throws IOException {
55+
try (Reader inputReader = new InputStreamReader(inputStream, charset)) {
56+
CsvParser parser = new CsvParser(csvParserSettings);
57+
rows = parser.parseAll(inputReader).toArray(new String[0][]);
58+
}
59+
}
60+
61+
public CsvReportPage(String[][] cells) {
62+
this.rows = cells;
63+
}
64+
65+
public static CsvParserSettings getDefaultCsvParserSettings() {
66+
CsvParserSettings settings = new CsvParserSettings();
67+
settings.detectFormatAutomatically();
68+
return settings;
69+
}
70+
71+
@Override
72+
public TableCellAddress find(Object value, int startRow, int endRow, int startColumn, int endColumn, BiPredicate<String, Object> predicate) {
73+
return CsvTableHelper.find(rows, value, startRow, endRow, startColumn, endColumn, predicate);
74+
}
75+
76+
@Override
77+
public CsvTableRow getRow(int i) {
78+
return (i >= rows.length) ? null : new CsvTableRow(rows[i], i);
79+
}
80+
81+
@Override
82+
public int getLastRowNum() {
83+
return rows.length - 1;
84+
}
85+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Table Wrapper Xml SpreadsheetML Impl
3+
* Copyright (C) 2022 Vitalii Ananev <spacious-team@ya.ru>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as
7+
* published by the Free Software Foundation, either version 3 of the
8+
* License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package org.spacious_team.table_wrapper.csv;
20+
21+
import lombok.AccessLevel;
22+
import lombok.Getter;
23+
import lombok.ToString;
24+
import org.spacious_team.table_wrapper.api.AbstractReportPage;
25+
import org.spacious_team.table_wrapper.api.AbstractTable;
26+
import org.spacious_team.table_wrapper.api.CellDataAccessObject;
27+
import org.spacious_team.table_wrapper.api.Table;
28+
import org.spacious_team.table_wrapper.api.TableCellRange;
29+
import org.spacious_team.table_wrapper.api.TableColumnDescription;
30+
31+
@ToString(callSuper = true)
32+
public class CsvTable extends AbstractTable<CsvTableRow> {
33+
34+
@Getter(AccessLevel.PROTECTED)
35+
private final CellDataAccessObject<?, CsvTableRow> cellDataAccessObject = CsvCellDataAccessObject.INSTANCE;
36+
37+
protected CsvTable(AbstractReportPage<CsvTableRow> reportPage,
38+
String tableName,
39+
TableCellRange tableRange,
40+
Class<? extends TableColumnDescription> headerDescription,
41+
int headersRowCount) {
42+
super(reportPage, tableName, tableRange, headerDescription, headersRowCount);
43+
}
44+
45+
public CsvTable(AbstractTable<CsvTableRow> table, int appendDataRowsToTop, int appendDataRowsToBottom) {
46+
super(table, appendDataRowsToTop, appendDataRowsToBottom);
47+
}
48+
49+
@Override
50+
public Table subTable(int topRows, int bottomRows) {
51+
return new CsvTable(this, topRows, bottomRows);
52+
}
53+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Table Wrapper Xml SpreadsheetML Impl
3+
* Copyright (C) 2022 Vitalii Ananev <spacious-team@ya.ru>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as
7+
* published by the Free Software Foundation, either version 3 of the
8+
* License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package org.spacious_team.table_wrapper.csv;
20+
21+
import lombok.Getter;
22+
import lombok.RequiredArgsConstructor;
23+
import org.spacious_team.table_wrapper.api.AbstractTableCell;
24+
25+
public class CsvTableCell extends AbstractTableCell<CsvTableCell.RowAndIndex> {
26+
27+
@Getter
28+
private final RowAndIndex rowAndIndex;
29+
30+
public static CsvTableCell of(String[] row, int columnIndex) {
31+
return new CsvTableCell(new RowAndIndex(row, columnIndex));
32+
}
33+
34+
public CsvTableCell(RowAndIndex rowAndIndex) {
35+
super(rowAndIndex, CsvCellDataAccessObject.INSTANCE);
36+
this.rowAndIndex = rowAndIndex;
37+
}
38+
39+
@Override
40+
public int getColumnIndex() {
41+
return rowAndIndex.getColumnIndex();
42+
}
43+
44+
@Getter
45+
@RequiredArgsConstructor
46+
static class RowAndIndex {
47+
final String[] row;
48+
final int columnIndex;
49+
}
50+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Table Wrapper Xml SpreadsheetML Impl
3+
* Copyright (C) 2022 Vitalii Ananev <spacious-team@ya.ru>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as
7+
* published by the Free Software Foundation, either version 3 of the
8+
* License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
package org.spacious_team.table_wrapper.csv;
20+
21+
import org.spacious_team.table_wrapper.api.AbstractTableFactory;
22+
import org.spacious_team.table_wrapper.api.ReportPage;
23+
import org.spacious_team.table_wrapper.api.Table;
24+
import org.spacious_team.table_wrapper.api.TableColumnDescription;
25+
26+
public class CsvTableFactory extends AbstractTableFactory<CsvReportPage> {
27+
28+
public CsvTableFactory() {
29+
super(CsvReportPage.class);
30+
}
31+
32+
@Override
33+
public Table create(ReportPage reportPage,
34+
String tableName,
35+
String lastRowString,
36+
Class<? extends TableColumnDescription> headerDescription,
37+
int headersRowCount) {
38+
return new CsvTable(cast(reportPage),
39+
tableName,
40+
reportPage.getTableCellRange(tableName, headersRowCount, lastRowString),
41+
headerDescription,
42+
headersRowCount);
43+
}
44+
45+
@Override
46+
public Table create(ReportPage reportPage,
47+
String tableName,
48+
Class<? extends TableColumnDescription> headerDescription,
49+
int headersRowCount) {
50+
return new CsvTable(cast(reportPage),
51+
tableName,
52+
reportPage.getTableCellRange(tableName, headersRowCount),
53+
headerDescription,
54+
headersRowCount);
55+
}
56+
57+
@Override
58+
public Table createNameless(ReportPage reportPage,
59+
String providedTableName,
60+
String firstRowString,
61+
String lastRowString,
62+
Class<? extends TableColumnDescription> headerDescription,
63+
int headersRowCount) {
64+
return new CsvTable(cast(reportPage),
65+
providedTableName,
66+
reportPage.getTableCellRange(firstRowString, headersRowCount, lastRowString)
67+
.addRowsToTop(1),
68+
headerDescription,
69+
headersRowCount);
70+
}
71+
72+
@Override
73+
public Table createNameless(ReportPage reportPage,
74+
String providedTableName,
75+
String firstRowString,
76+
Class<? extends TableColumnDescription> headerDescription,
77+
int headersRowCount) {
78+
return new CsvTable(cast(reportPage),
79+
providedTableName,
80+
reportPage.getTableCellRange(firstRowString, headersRowCount)
81+
.addRowsToTop(1),
82+
headerDescription,
83+
headersRowCount);
84+
}
85+
}

0 commit comments

Comments
 (0)