Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Релиз 2022.2 #1

Merged
merged 8 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Предоставляет реализацию `Table Wrapper API` для удобного доступа к табличным данным, сохраненным в файлах формата `csv`.
Пример создания таблиц из файла `1.csv`
```java
ReportPage reportPage = ...;
ReportPage reportPage = new CsvReportPage(Path.of("1.csv"));

Table table1 = reportPage.create("Table 1 description", ...);
...
Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@
<dependency>
<groupId>com.github.spacious-team</groupId>
<artifactId>table-wrapper-api</artifactId>
<version>2021.6.1</version>
<version>2022.2</version>
</dependency>
<dependency>
<groupId>com.univocity</groupId>
<artifactId>univocity-parsers</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Table Wrapper Xml SpreadsheetML Impl
* Copyright (C) 2022 Vitalii Ananev <spacious-team@ya.ru>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package org.spacious_team.table_wrapper.csv;

import lombok.Getter;
import lombok.Setter;
import org.spacious_team.table_wrapper.api.CellDataAccessObject;
import org.spacious_team.table_wrapper.csv.CsvTableCell.RowAndIndex;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class CsvCellDataAccessObject implements CellDataAccessObject<RowAndIndex, CsvTableRow> {
public static final CsvCellDataAccessObject INSTANCE = new CsvCellDataAccessObject();
@Setter
@Getter
public static DateTimeFormatter dateTimeFormatter = null;

@Override
public RowAndIndex getCell(CsvTableRow row, Integer cellIndex) {
return row.getCell(cellIndex).getRowAndIndex();
}

@Override
public String getValue(RowAndIndex cell) {
int columnIndex = cell.getColumnIndex();
String[] row = cell.getRow();
return (columnIndex < row.length) ? row[columnIndex] : null;
}

@Override
public Instant getInstantValue(RowAndIndex cell) {
String value = getValue(cell);
DateTimeFormatter formatter = (dateTimeFormatter != null) ?
dateTimeFormatter :
DateTimeFormatParser.getFor(value);
LocalDateTime dateTime = (value.length() == 10) ?
LocalDate.parse(value, formatter).atTime(12, 0) :
LocalDateTime.parse(value, formatter);
return dateTime
.atZone(ZoneOffset.systemDefault())
.toInstant();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Table Wrapper Xml SpreadsheetML Impl
* Copyright (C) 2022 Vitalii Ananev <spacious-team@ya.ru>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package org.spacious_team.table_wrapper.csv;

import com.univocity.parsers.common.record.Record;
import com.univocity.parsers.csv.CsvParser;
import com.univocity.parsers.csv.CsvParserSettings;
import lombok.RequiredArgsConstructor;
import org.spacious_team.table_wrapper.api.AbstractReportPage;
import org.spacious_team.table_wrapper.api.TableCellAddress;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.function.BiPredicate;

import static java.nio.charset.StandardCharsets.UTF_8;

public class CsvReportPage extends AbstractReportPage<CsvTableRow> {

private final String[][] rows;

/**
* Field and line delimiter detected automatically. UTF-8 encoding file expected.
*/
public CsvReportPage(Path path) throws IOException {
this(Files.newInputStream(path, StandardOpenOption.READ), UTF_8, getDefaultCsvParserSettings());
}

/**
* Closes inputStream if success
*/
public CsvReportPage(InputStream inputStream, Charset charset, CsvParserSettings csvParserSettings) throws IOException {
try (Reader inputReader = new InputStreamReader(inputStream, charset)) {
CsvParser parser = new CsvParser(csvParserSettings);
rows = parser.parseAll(inputReader).toArray(new String[0][]);
}
}

public CsvReportPage(String[][] cells) {
this.rows = cells;
}

public static CsvParserSettings getDefaultCsvParserSettings() {
CsvParserSettings settings = new CsvParserSettings();
settings.detectFormatAutomatically();
return settings;
}

@Override
public TableCellAddress find(Object value, int startRow, int endRow, int startColumn, int endColumn, BiPredicate<String, Object> predicate) {
return CsvTableHelper.find(rows, value, startRow, endRow, startColumn, endColumn, predicate);
}

@Override
public CsvTableRow getRow(int i) {
return (i >= rows.length) ? null : new CsvTableRow(rows[i], i);
}

@Override
public int getLastRowNum() {
return rows.length - 1;
}
}
53 changes: 53 additions & 0 deletions src/main/java/org/spacious_team/table_wrapper/csv/CsvTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Table Wrapper Xml SpreadsheetML Impl
* Copyright (C) 2022 Vitalii Ananev <spacious-team@ya.ru>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package org.spacious_team.table_wrapper.csv;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.ToString;
import org.spacious_team.table_wrapper.api.AbstractReportPage;
import org.spacious_team.table_wrapper.api.AbstractTable;
import org.spacious_team.table_wrapper.api.CellDataAccessObject;
import org.spacious_team.table_wrapper.api.Table;
import org.spacious_team.table_wrapper.api.TableCellRange;
import org.spacious_team.table_wrapper.api.TableColumnDescription;

@ToString(callSuper = true)
public class CsvTable extends AbstractTable<CsvTableRow> {

@Getter(AccessLevel.PROTECTED)
private final CellDataAccessObject<?, CsvTableRow> cellDataAccessObject = CsvCellDataAccessObject.INSTANCE;

protected CsvTable(AbstractReportPage<CsvTableRow> reportPage,
String tableName,
TableCellRange tableRange,
Class<? extends TableColumnDescription> headerDescription,
int headersRowCount) {
super(reportPage, tableName, tableRange, headerDescription, headersRowCount);
}

public CsvTable(AbstractTable<CsvTableRow> table, int appendDataRowsToTop, int appendDataRowsToBottom) {
super(table, appendDataRowsToTop, appendDataRowsToBottom);
}

@Override
public Table subTable(int topRows, int bottomRows) {
return new CsvTable(this, topRows, bottomRows);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Table Wrapper Xml SpreadsheetML Impl
* Copyright (C) 2022 Vitalii Ananev <spacious-team@ya.ru>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package org.spacious_team.table_wrapper.csv;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.spacious_team.table_wrapper.api.AbstractTableCell;

public class CsvTableCell extends AbstractTableCell<CsvTableCell.RowAndIndex> {

@Getter
private final RowAndIndex rowAndIndex;

public static CsvTableCell of(String[] row, int columnIndex) {
return new CsvTableCell(new RowAndIndex(row, columnIndex));
}

public CsvTableCell(RowAndIndex rowAndIndex) {
super(rowAndIndex, CsvCellDataAccessObject.INSTANCE);
this.rowAndIndex = rowAndIndex;
}

@Override
public int getColumnIndex() {
return rowAndIndex.getColumnIndex();
}

@Getter
@RequiredArgsConstructor
static class RowAndIndex {
final String[] row;
final int columnIndex;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Table Wrapper Xml SpreadsheetML Impl
* Copyright (C) 2022 Vitalii Ananev <spacious-team@ya.ru>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package org.spacious_team.table_wrapper.csv;

import org.spacious_team.table_wrapper.api.AbstractTableFactory;
import org.spacious_team.table_wrapper.api.ReportPage;
import org.spacious_team.table_wrapper.api.Table;
import org.spacious_team.table_wrapper.api.TableColumnDescription;

public class CsvTableFactory extends AbstractTableFactory<CsvReportPage> {

public CsvTableFactory() {
super(CsvReportPage.class);
}

@Override
public Table create(ReportPage reportPage,
String tableName,
String lastRowString,
Class<? extends TableColumnDescription> headerDescription,
int headersRowCount) {
return new CsvTable(cast(reportPage),
tableName,
reportPage.getTableCellRange(tableName, headersRowCount, lastRowString),
headerDescription,
headersRowCount);
}

@Override
public Table create(ReportPage reportPage,
String tableName,
Class<? extends TableColumnDescription> headerDescription,
int headersRowCount) {
return new CsvTable(cast(reportPage),
tableName,
reportPage.getTableCellRange(tableName, headersRowCount),
headerDescription,
headersRowCount);
}

@Override
public Table createNameless(ReportPage reportPage,
String providedTableName,
String firstRowString,
String lastRowString,
Class<? extends TableColumnDescription> headerDescription,
int headersRowCount) {
return new CsvTable(cast(reportPage),
providedTableName,
reportPage.getTableCellRange(firstRowString, headersRowCount, lastRowString)
.addRowsToTop(1),
headerDescription,
headersRowCount);
}

@Override
public Table createNameless(ReportPage reportPage,
String providedTableName,
String firstRowString,
Class<? extends TableColumnDescription> headerDescription,
int headersRowCount) {
return new CsvTable(cast(reportPage),
providedTableName,
reportPage.getTableCellRange(firstRowString, headersRowCount)
.addRowsToTop(1),
headerDescription,
headersRowCount);
}
}
Loading