From 401745d7dc18afb9a947de0a9a1e08f742920073 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Sat, 10 Dec 2022 16:21:37 +0100 Subject: [PATCH] Added classes to generate rst tables for documentation --- .../utils/documentation/table/Cell.java | 57 ++++++++ .../utils/documentation/table/Row.java | 122 ++++++++++++++++++ .../utils/documentation/table/RowType.java | 16 +++ .../utils/documentation/table/Table.java | 118 +++++++++++++++++ .../documentation/table/TableTest.java | 79 ++++++++++++ 5 files changed, 392 insertions(+) create mode 100644 src/main/java/org/gcube/informationsystem/utils/documentation/table/Cell.java create mode 100644 src/main/java/org/gcube/informationsystem/utils/documentation/table/Row.java create mode 100644 src/main/java/org/gcube/informationsystem/utils/documentation/table/RowType.java create mode 100644 src/main/java/org/gcube/informationsystem/utils/documentation/table/Table.java create mode 100644 src/test/java/org/gcube/informationsystem/utils/discovery/documentation/table/TableTest.java diff --git a/src/main/java/org/gcube/informationsystem/utils/documentation/table/Cell.java b/src/main/java/org/gcube/informationsystem/utils/documentation/table/Cell.java new file mode 100644 index 0000000..fd69856 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/utils/documentation/table/Cell.java @@ -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; + } + } + +} diff --git a/src/main/java/org/gcube/informationsystem/utils/documentation/table/Row.java b/src/main/java/org/gcube/informationsystem/utils/documentation/table/Row.java new file mode 100644 index 0000000..86fdc7d --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/utils/documentation/table/Row.java @@ -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 cells; + private List 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; position0 && 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()-1cells.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 getCells() { + return cells; + } + + public Cell getCell(int position) { + try { + return cells.get(position); + }catch (IndexOutOfBoundsException e) { + return null; + } + } + + public List 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; + } +} diff --git a/src/main/java/org/gcube/informationsystem/utils/documentation/table/RowType.java b/src/main/java/org/gcube/informationsystem/utils/documentation/table/RowType.java new file mode 100644 index 0000000..033317b --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/utils/documentation/table/RowType.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/org/gcube/informationsystem/utils/documentation/table/Table.java b/src/main/java/org/gcube/informationsystem/utils/documentation/table/Table.java new file mode 100644 index 0000000..c879fa1 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/utils/documentation/table/Table.java @@ -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 rows; + private List maxSizePerCell; + + public Table() { + rows = new ArrayList<>(); + maxSizePerCell = new ArrayList<>(); + } + + protected void updateMaxSizePerCell(Row row) { + List cellSizes = row.getCellSizes(); + for(int i=0; i=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; i0 && cell==row.getCell(i-1)) { + for(int j=0; j