Search in sources :

Example 1 with OdfTableRow

use of org.odftoolkit.odfdom.doc.table.OdfTableRow in project OpenRefine by OpenRefine.

the class OdsExporter method export.

@Override
public void export(final Project project, Properties params, Engine engine, OutputStream outputStream) throws IOException {
    final OdfSpreadsheetDocument odfDoc;
    try {
        odfDoc = OdfSpreadsheetDocument.newSpreadsheetDocument();
    } catch (Exception e) {
        throw new IOException("Failed to create spreadsheet", e);
    }
    TabularSerializer serializer = new TabularSerializer() {

        OdfTable table;

        // int rowCount = 0;
        @Override
        public void startFile(JsonNode options) {
            table = OdfTable.newTable(odfDoc);
            table.setTableName(ProjectManager.singleton.getProjectMetadata(project.id).getName());
        }

        @Override
        public void endFile() {
        }

        @Override
        public void addRow(List<CellData> cells, boolean isHeader) {
            OdfTableRow r = table.appendRow();
            for (int i = 0; i < cells.size(); i++) {
                // implicitly creates cell
                OdfTableCell c = r.getCellByIndex(i);
                CellData cellData = cells.get(i);
                if (cellData != null && cellData.text != null && cellData.value != null) {
                    Object v = cellData.value;
                    if (v instanceof Number) {
                        c.setDoubleValue(((Number) v).doubleValue());
                    } else if (v instanceof Boolean) {
                        c.setBooleanValue(((Boolean) v).booleanValue());
                    } else if (v instanceof OffsetDateTime) {
                        OffsetDateTime odt = (OffsetDateTime) v;
                        c.setDateValue(ParsingUtilities.offsetDateTimeToCalendar(odt));
                    } else {
                        c.setStringValue(cellData.text);
                    }
                    if (cellData.link != null) {
                    // TODO: How do we do output hyperlinks?
                    }
                }
            }
        }
    };
    CustomizableTabularExporterUtilities.exportRows(project, engine, params, serializer);
    try {
        odfDoc.save(outputStream);
    } catch (Exception e) {
        throw new IOException("Error saving spreadsheet", e);
    }
    outputStream.flush();
}
Also used : OdfSpreadsheetDocument(org.odftoolkit.odfdom.doc.OdfSpreadsheetDocument) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) IOException(java.io.IOException) OdfTableCell(org.odftoolkit.odfdom.doc.table.OdfTableCell) OffsetDateTime(java.time.OffsetDateTime) OdfTable(org.odftoolkit.odfdom.doc.table.OdfTable) List(java.util.List) OdfTableRow(org.odftoolkit.odfdom.doc.table.OdfTableRow)

Example 2 with OdfTableRow

use of org.odftoolkit.odfdom.doc.table.OdfTableRow in project pentaho-kettle by pentaho.

the class OdfSheet method getRow.

public KCell[] getRow(int rownr) {
    if (rownr >= nrOfRows) {
        throw new ArrayIndexOutOfBoundsException("Read beyond last row: " + rownr);
    }
    OdfTableRow row = table.getRowByIndex(rownr);
    int cols = findNrColumns(row);
    OdfCell[] xlsCells = new OdfCell[cols];
    for (int i = 0; i < cols; i++) {
        OdfTableCell cell = row.getCellByIndex(i);
        if (cell != null) {
            xlsCells[i] = new OdfCell(cell);
        }
    }
    return xlsCells;
}
Also used : OdfTableCell(org.odftoolkit.odfdom.doc.table.OdfTableCell) OdfTableRow(org.odftoolkit.odfdom.doc.table.OdfTableRow)

Example 3 with OdfTableRow

use of org.odftoolkit.odfdom.doc.table.OdfTableRow in project OpenRefine by OpenRefine.

the class OdsImporter method parseOneFile.

@Override
public void parseOneFile(Project project, ProjectMetadata metadata, ImportingJob job, String fileSource, InputStream inputStream, int limit, JSONObject options, List<Exception> exceptions) {
    OdfDocument odfDoc;
    try {
        odfDoc = OdfDocument.loadDocument(inputStream);
    } catch (Exception e) {
        // Ugh! could they throw any wider exception?
        exceptions.add(e);
        return;
    }
    List<OdfTable> tables = odfDoc.getTableList();
    int[] sheets = JSONUtilities.getIntArray(options, "sheets");
    for (int sheetIndex : sheets) {
        final OdfTable table = tables.get(sheetIndex);
        final int lastRow = table.getRowCount();
        TableDataReader dataReader = new TableDataReader() {

            int nextRow = 0;

            Map<String, Recon> reconMap = new HashMap<String, Recon>();

            @Override
            public List<Object> getNextRowOfCells() throws IOException {
                if (nextRow > lastRow) {
                    return null;
                }
                List<Object> cells = new ArrayList<Object>();
                OdfTableRow row = table.getRowByIndex(nextRow++);
                if (row != null) {
                    int lastCell = row.getCellCount();
                    for (int cellIndex = 0; cellIndex <= lastCell; cellIndex++) {
                        Cell cell = null;
                        OdfTableCell sourceCell = row.getCellByIndex(cellIndex);
                        if (sourceCell != null) {
                            cell = extractCell(sourceCell, reconMap);
                        }
                        cells.add(cell);
                    }
                }
                return cells;
            }
        };
        TabularImportingParserBase.readTable(project, metadata, job, dataReader, fileSource + "#" + table.getTableName(), limit, options, exceptions);
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) OdfTableCell(org.odftoolkit.odfdom.doc.table.OdfTableCell) OdfDocument(org.odftoolkit.odfdom.doc.OdfDocument) OdfTable(org.odftoolkit.odfdom.doc.table.OdfTable) JSONObject(org.json.JSONObject) Recon(com.google.refine.model.Recon) HashMap(java.util.HashMap) Map(java.util.Map) OdfTableCell(org.odftoolkit.odfdom.doc.table.OdfTableCell) Cell(com.google.refine.model.Cell) OdfTableRow(org.odftoolkit.odfdom.doc.table.OdfTableRow)

Example 4 with OdfTableRow

use of org.odftoolkit.odfdom.doc.table.OdfTableRow in project OpenRefine by OpenRefine.

the class OdsImporter method parseOneFile.

@Override
public void parseOneFile(Project project, ProjectMetadata metadata, ImportingJob job, String fileSource, InputStream inputStream, int limit, ObjectNode options, List<Exception> exceptions) {
    OdfDocument odfDoc;
    try {
        odfDoc = OdfDocument.loadDocument(inputStream);
    } catch (Exception e) {
        // Ugh! could they throw any wider exception?
        exceptions.add(e);
        return;
    }
    List<OdfTable> tables = odfDoc.getTableList();
    ArrayNode sheets = JSONUtilities.getArray(options, "sheets");
    for (int i = 0; i < sheets.size(); i++) {
        String[] fileNameAndSheetIndex = new String[2];
        ObjectNode sheetObj = JSONUtilities.getObjectElement(sheets, i);
        // value is fileName#sheetIndex
        fileNameAndSheetIndex = sheetObj.get("fileNameAndSheetIndex").asText().split("#");
        if (!fileNameAndSheetIndex[0].equals(fileSource))
            continue;
        final OdfTable table = tables.get(Integer.parseInt(fileNameAndSheetIndex[1]));
        final int lastRow = table.getRowCount();
        TableDataReader dataReader = new TableDataReader() {

            int nextRow = 0;

            Map<String, Recon> reconMap = new HashMap<String, Recon>();

            @Override
            public List<Object> getNextRowOfCells() throws IOException {
                if (nextRow > lastRow) {
                    return null;
                }
                List<Object> cells = new ArrayList<Object>();
                OdfTableRow row = table.getRowByIndex(nextRow++);
                int maxCol = 0;
                if (row != null) {
                    int lastCell = row.getCellCount();
                    for (int cellIndex = 0; cellIndex <= lastCell; cellIndex++) {
                        Cell cell = null;
                        OdfTableCell sourceCell = row.getCellByIndex(cellIndex);
                        if (sourceCell != null) {
                            cell = extractCell(sourceCell, reconMap);
                        }
                        cells.add(cell);
                        if (cell != null && cellIndex > maxCol) {
                            maxCol = cellIndex;
                        }
                    }
                }
                // Right truncate null cells
                return cells.subList(0, maxCol + 1);
            }
        };
        TabularImportingParserBase.readTable(project, metadata, job, dataReader, fileSource + "#" + table.getTableName(), limit, options, exceptions);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) OdfTableCell(org.odftoolkit.odfdom.doc.table.OdfTableCell) OdfDocument(org.odftoolkit.odfdom.doc.OdfDocument) OdfTable(org.odftoolkit.odfdom.doc.table.OdfTable) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) HashMap(java.util.HashMap) Map(java.util.Map) Recon(com.google.refine.model.Recon) OdfTableCell(org.odftoolkit.odfdom.doc.table.OdfTableCell) Cell(com.google.refine.model.Cell) OdfTableRow(org.odftoolkit.odfdom.doc.table.OdfTableRow)

Aggregations

OdfTableCell (org.odftoolkit.odfdom.doc.table.OdfTableCell)4 OdfTableRow (org.odftoolkit.odfdom.doc.table.OdfTableRow)4 IOException (java.io.IOException)3 OdfTable (org.odftoolkit.odfdom.doc.table.OdfTable)3 Cell (com.google.refine.model.Cell)2 Recon (com.google.refine.model.Recon)2 FileNotFoundException (java.io.FileNotFoundException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 OdfDocument (org.odftoolkit.odfdom.doc.OdfDocument)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 OffsetDateTime (java.time.OffsetDateTime)1 List (java.util.List)1 JSONObject (org.json.JSONObject)1 OdfSpreadsheetDocument (org.odftoolkit.odfdom.doc.OdfSpreadsheetDocument)1