Added classes to generate rst tables for documentation

This commit is contained in:
Luca Frosini 2022-12-10 16:21:37 +01:00
parent 87afd8e5a2
commit 401745d7dc
5 changed files with 392 additions and 0 deletions

View File

@ -0,0 +1,57 @@
package org.gcube.informationsystem.utils.documentation.table;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class Cell {
private Row row;
private int size;
private String text;
/**
* Cell Position in the row
*/
private Integer cellPosition;
public Cell() {
this.size = 0;
this.text = "";
this.row = null;
this.cellPosition = null;
}
public int getSize() {
return size;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
this.size = text.length();
if(row!=null) {
this.row.update(this);
}
}
protected void setRow(Row row) {
if(this.row==null) {
this.row = row;
}
}
protected Integer getCellPosition() {
return cellPosition;
}
protected void setPosition(Integer cellPosition) {
if(this.cellPosition==null) {
this.cellPosition = cellPosition;
}
}
}

View File

@ -0,0 +1,122 @@
package org.gcube.informationsystem.utils.documentation.table;
import java.util.ArrayList;
import java.util.List;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class Row {
private RowType rowType;
private Table table;
/**
* Row Position in the table
*/
private Integer rowPosition;
private List<Cell> cells;
private List<Integer> cellSizes;
public Row() {
this.rowType = RowType.NORMAL;
this.cells = new ArrayList<>();
this.cellSizes = new ArrayList<>();
}
public Row(RowType rowType) {
this.rowType = rowType;
this.cells = new ArrayList<>();
this.cellSizes = new ArrayList<>();
}
public void setRowType(RowType rowType) {
this.rowType = rowType;
}
public RowType getRowType() {
return rowType;
}
// public void update() {
// for(int position=0; position<cells.size(); position++) {
// Integer size = cells.get(position).getSize();
// if(position>0 && cells.get(position)==cells.get(position-1)) {
// size=null;
// }
// cellSizes.add(position, size);
// }
// }
public void update(Cell cell) {
Integer size = cell.getSize();
int cellPosition = cell.getCellPosition();
if(cellSizes.size()-1<cellPosition) {
cellSizes.add(cellPosition, size);
}else {
cellSizes.set(cellPosition, size);
}
if(table!=null) {
table.updateMaxSizePerCell(this);
}
}
public void appendCell(Cell cell) {
int position = cells.size();
setCell(position, cell);
}
protected void setCell(int position, Cell cell) {
if(position>cells.size()) {
throw new RuntimeException("You can't add the cell in position " + position);
}
cells.add(position, cell);
if(position==0 || cell!=cells.get(position-1)) {
cell.setRow(this);
cell.setPosition(position);
update(cell);
}else {
cellSizes.add(position, 0);
}
}
public List<Cell> getCells() {
return cells;
}
public Cell getCell(int position) {
try {
return cells.get(position);
}catch (IndexOutOfBoundsException e) {
return null;
}
}
public List<Integer> getCellSizes() {
return cellSizes;
}
public Integer getCellSize(int position) {
try {
return cellSizes.get(position);
}catch (IndexOutOfBoundsException e) {
return null;
}
}
protected void setTable(Table table) {
if(this.table==null) {
this.table = table;
}
}
protected Integer getRowPosition() {
return rowPosition;
}
protected void setRowPosition(Integer rowPosition) {
this.rowPosition = rowPosition;
}
}

View File

@ -0,0 +1,16 @@
package org.gcube.informationsystem.utils.documentation.table;
public enum RowType {
HEADING(Table.ROW_SEPARATOR_HEADING),
NORMAL(Table.ROW_SEPARATOR);
protected String rowSeparator;
private RowType(String rowSeparator) {
this.rowSeparator = rowSeparator;
}
public String getRowSeparator() {
return rowSeparator;
}
}

View File

@ -0,0 +1,118 @@
package org.gcube.informationsystem.utils.documentation.table;
import java.util.ArrayList;
import java.util.List;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class Table {
public static final String COLUMN_SEPARATOR = "+";
public static final String ROW_SEPARATOR = "-";
public static final String ROW_SEPARATOR_HEADING = "=";
private List<Row> rows;
private List<Integer> maxSizePerCell;
public Table() {
rows = new ArrayList<>();
maxSizePerCell = new ArrayList<>();
}
protected void updateMaxSizePerCell(Row row) {
List<Integer> cellSizes = row.getCellSizes();
for(int i=0; i<cellSizes.size(); i++) {
Integer cellSize = cellSizes.get(i);
cellSize = cellSize!=null ? cellSize : 0;
cellSize += 2; // We need to consider one space before and one after to calculate total column size
Integer maxSize = 0;
try {
maxSize = maxSizePerCell.get(i);
if(cellSize>=maxSize) {
maxSizePerCell.set(i, cellSize);
}
} catch (IndexOutOfBoundsException e) {
maxSizePerCell.add(i, cellSize);
}
}
}
public void appendRow(Row row) {
rows.add(row);
row.setRowPosition(rows.size()-1);
row.setTable(this);
updateMaxSizePerCell(row);
}
private StringBuffer addRowSeparator(String separator) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("\t");
stringBuffer.append(COLUMN_SEPARATOR);
for(int i=0; i<maxSizePerCell.size(); i++) {
for(int j=0; j<maxSizePerCell.get(i); j++) {
stringBuffer.append(separator);
}
stringBuffer.append(COLUMN_SEPARATOR);
}
stringBuffer.append("\n");
return stringBuffer;
}
private StringBuffer addRowSeparator(Row row, String separator) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("\t");
stringBuffer.append(COLUMN_SEPARATOR);
for(int i=0; i<maxSizePerCell.size(); i++) {
for(int j=0; j<maxSizePerCell.get(i); j++) {
stringBuffer.append(separator);
}
stringBuffer.append(COLUMN_SEPARATOR);
}
stringBuffer.append("\n");
return stringBuffer;
}
private StringBuffer addRow(Row row) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("\t");
for(int i=0; i<maxSizePerCell.size(); i++) {
Cell cell = row.getCell(i);
int sizeTosatisfy = maxSizePerCell.get(i);
if(i>0 && cell==row.getCell(i-1)) {
for(int j=0; j<sizeTosatisfy+1; j++) {
stringBuffer.append(" ");
}
}else {
stringBuffer.append(COLUMN_SEPARATOR);
int remaining = sizeTosatisfy - cell.getSize();
stringBuffer.append(" ");
stringBuffer.append(cell.getText());
for(int j=0; j<remaining-1; j++) {
stringBuffer.append(" ");
}
}
}
stringBuffer.append(COLUMN_SEPARATOR);
stringBuffer.append("\n");
return stringBuffer;
}
public StringBuffer generateTable() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("\n\n");
stringBuffer.append(".. table::");
stringBuffer.append("\n\n");
stringBuffer.append(addRowSeparator(RowType.NORMAL.getRowSeparator()));
for(Row row : rows) {
stringBuffer.append(addRow(row));
stringBuffer.append(addRowSeparator(row, row.getRowType().getRowSeparator()));
}
stringBuffer.append("\n\n");
return stringBuffer;
}
}

View File

@ -0,0 +1,79 @@
package org.gcube.informationsystem.utils.discovery.documentation.table;
import org.gcube.informationsystem.utils.documentation.table.Cell;
import org.gcube.informationsystem.utils.documentation.table.Row;
import org.gcube.informationsystem.utils.documentation.table.RowType;
import org.gcube.informationsystem.utils.documentation.table.Table;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class TableTest {
private static final Logger logger = LoggerFactory.getLogger(TableTest.class);
@Test
public void test() throws Exception {
Table table = new Table();
Row row1 = new Row(RowType.HEADING);
table.appendRow(row1);
Cell cell11 = new Cell();
cell11.setText("Column 1");
row1.appendCell(cell11);
Cell cell12 = new Cell();
cell12.setText("Column 2");
row1.appendCell(cell12);
Cell cell13 = new Cell();
cell13.setText("Column 3");
row1.appendCell(cell13);
Row row2 = new Row();
table.appendRow(row2);
Cell cell21 = new Cell();
cell21.setText("Row 2 Content 1");
row2.appendCell(cell21);
Cell cell22 = new Cell();
cell22.setText("Row 2 Content 2");
row2.appendCell(cell22);
Cell cell23 = new Cell();
cell23.setText("Row 2 Content 3");
row2.appendCell(cell23);
Row row3 = new Row();
table.appendRow(row3);
Cell cell31 = new Cell();
cell31.setText("Row 3 Content 31");
row3.appendCell(cell31);
Cell cell32 = new Cell();
cell32.setText("Row 3 Content 32");
row3.appendCell(cell32);
Cell cell33 = new Cell();
cell33.setText("Row 3 Content 33");
row3.appendCell(cell33);
StringBuffer stringBuffer = table.generateTable();
logger.info(stringBuffer.toString());
cell11.setText("Longer Longer Column 1");
cell33.setText("Longer Longer Row 3 Content 33");
stringBuffer = table.generateTable();
logger.info(stringBuffer.toString());
}
}