Search in sources :

Example 16 with OdfTable

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

use of org.odftoolkit.odfdom.doc.table.OdfTable 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)

Example 18 with OdfTable

use of org.odftoolkit.odfdom.doc.table.OdfTable in project structr by structr.

the class ODSExporter method writeCollectionToCells.

static void writeCollectionToCells(final OdfTable sheet, final OdfTableCell startCell, final Collection col) {
    int rowIndex, colIndex;
    colIndex = startCell.getColumnIndex();
    rowIndex = startCell.getRowIndex();
    Iterator<Collection> colIt = col.iterator();
    while (colIt.hasNext()) {
        Object obj = colIt.next();
        if (obj instanceof String[]) {
            String[] arr = (String[]) obj;
            List<String> list = new ArrayList<>(Arrays.asList(arr));
            StringJoiner sj = new StringJoiner(",");
            list.forEach(s -> sj.add(s));
            writeObjectToCell(sheet.getCellByPosition(colIndex, rowIndex), sj.toString());
        } else if (obj instanceof Collection) {
            Collection nestedCol = (Collection) obj;
            StringJoiner sj = new StringJoiner(",");
            nestedCol.forEach(s -> sj.add(s.toString()));
            writeObjectToCell(sheet.getCellByPosition(colIndex, rowIndex), sj.toString());
        } else {
            writeObjectToCell(sheet.getCellByPosition(colIndex, rowIndex), obj);
        }
        rowIndex++;
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) Arrays(java.util.Arrays) SecurityContext(org.structr.common.SecurityContext) HashMap(java.util.HashMap) OdfSpreadsheetDocument(org.odftoolkit.odfdom.doc.OdfSpreadsheetDocument) OdfTable(org.odftoolkit.odfdom.doc.table.OdfTable) OdfTableCell(org.odftoolkit.odfdom.doc.table.OdfTableCell) ArrayList(java.util.ArrayList) File(org.structr.web.entity.File) FrameworkException(org.structr.common.error.FrameworkException) App(org.structr.core.app.App) StringProperty(org.structr.core.property.StringProperty) Map(java.util.Map) URI(java.net.URI) GraphObjectMap(org.structr.core.GraphObjectMap) Result(org.structr.core.Result) AbstractNode(org.structr.core.entity.AbstractNode) JsonObjectType(org.structr.schema.json.JsonObjectType) Iterator(java.util.Iterator) Collection(java.util.Collection) GraphObject(org.structr.core.GraphObject) List(java.util.List) JsonSchema(org.structr.schema.json.JsonSchema) StringJoiner(java.util.StringJoiner) Entry(java.util.Map.Entry) VirtualType(org.structr.transform.VirtualType) SchemaService(org.structr.schema.SchemaService) ArrayList(java.util.ArrayList) Collection(java.util.Collection) GraphObject(org.structr.core.GraphObject) StringJoiner(java.util.StringJoiner)

Aggregations

OdfTable (org.odftoolkit.odfdom.doc.table.OdfTable)18 OdfTableCell (org.odftoolkit.odfdom.doc.table.OdfTableCell)14 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 AtividadesEnsinoService (com.tomasio.projects.trainning.interfaces.AtividadesEnsinoService)5 List (java.util.List)5 CursoDTO (com.tomasio.projects.trainning.dto.CursoDTO)4 OrganizationalService (com.tomasio.projects.trainning.interfaces.OrganizationalService)4 FileNotFoundException (java.io.FileNotFoundException)4 OdfDocument (org.odftoolkit.odfdom.doc.OdfDocument)4 TurmaDTO (com.tomasio.projects.trainning.dto.TurmaDTO)3 File (java.io.File)3 Map (java.util.Map)3 OdfSpreadsheetDocument (org.odftoolkit.odfdom.doc.OdfSpreadsheetDocument)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 Cell (com.google.refine.model.Cell)2 Recon (com.google.refine.model.Recon)2 AnotacaoDTO (com.tomasio.projects.trainning.dto.AnotacaoDTO)2