Search in sources :

Example 11 with OdfTableCell

use of org.odftoolkit.odfdom.doc.table.OdfTableCell 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 12 with OdfTableCell

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

the class OdsImporter method extractCell.

protected static Cell extractCell(OdfTableCell cell, Map<String, Recon> reconMap) {
    Serializable value = extractCell(cell);
    if (value != null) {
        Recon recon = null;
        // TODO: cell.getHyperlink();
        String hyperlink = "";
        if (hyperlink != null) {
            // TODO: hyperlink.getAddress();
            String url = hyperlink;
            if (url.startsWith("http://") || url.startsWith("https://")) {
                final String sig = "freebase.com/view";
                int i = url.indexOf(sig);
                if (i > 0) {
                    String id = url.substring(i + sig.length());
                    int q = id.indexOf('?');
                    if (q > 0) {
                        id = id.substring(0, q);
                    }
                    int h = id.indexOf('#');
                    if (h > 0) {
                        id = id.substring(0, h);
                    }
                    if (reconMap.containsKey(id)) {
                        recon = reconMap.get(id);
                        recon.judgmentBatchSize++;
                    } else {
                        recon = new Recon(0, null, null);
                        recon.service = "import";
                        recon.match = new ReconCandidate(id, value.toString(), new String[0], 100);
                        recon.matchRank = 0;
                        recon.judgment = Judgment.Matched;
                        recon.judgmentAction = "auto";
                        recon.judgmentBatchSize = 1;
                        recon.addCandidate(recon.match);
                        reconMap.put(id, recon);
                    }
                }
            }
        }
        return new Cell(value, recon);
    } else {
        return null;
    }
}
Also used : Serializable(java.io.Serializable) Recon(com.google.refine.model.Recon) OdfTableCell(org.odftoolkit.odfdom.doc.table.OdfTableCell) Cell(com.google.refine.model.Cell) ReconCandidate(com.google.refine.model.ReconCandidate)

Example 13 with OdfTableCell

use of org.odftoolkit.odfdom.doc.table.OdfTableCell 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 14 with OdfTableCell

use of org.odftoolkit.odfdom.doc.table.OdfTableCell 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 15 with OdfTableCell

use of org.odftoolkit.odfdom.doc.table.OdfTableCell 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)16 OdfTable (org.odftoolkit.odfdom.doc.table.OdfTable)14 AtividadesEnsinoService (com.tomasio.projects.trainning.interfaces.AtividadesEnsinoService)5 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 CursoDTO (com.tomasio.projects.trainning.dto.CursoDTO)4 OrganizationalService (com.tomasio.projects.trainning.interfaces.OrganizationalService)4 List (java.util.List)4 OdfTableRow (org.odftoolkit.odfdom.doc.table.OdfTableRow)4 Cell (com.google.refine.model.Cell)3 Recon (com.google.refine.model.Recon)3 TurmaDTO (com.tomasio.projects.trainning.dto.TurmaDTO)3 Map (java.util.Map)3 AnotacaoDTO (com.tomasio.projects.trainning.dto.AnotacaoDTO)2 FaseDTO (com.tomasio.projects.trainning.dto.FaseDTO)2 FolhaRostoDTO (com.tomasio.projects.trainning.dto.FolhaRostoDTO)2 TurmaPlanejadaDTO (com.tomasio.projects.trainning.dto.TurmaPlanejadaDTO)2 FileNotFoundException (java.io.FileNotFoundException)2 SimpleDateFormat (java.text.SimpleDateFormat)2