Search in sources :

Example 1 with OdfDocument

use of org.odftoolkit.odfdom.doc.OdfDocument in project OpenRefine by OpenRefine.

the class OdsImporter method createParserUIInitializationData.

@Override
public JSONObject createParserUIInitializationData(ImportingJob job, List<JSONObject> fileRecords, String format) {
    JSONObject options = super.createParserUIInitializationData(job, fileRecords, format);
    JSONArray sheetRecords = new JSONArray();
    JSONUtilities.safePut(options, "sheetRecords", sheetRecords);
    OdfDocument odfDoc = null;
    try {
        JSONObject firstFileRecord = fileRecords.get(0);
        File file = ImportingUtilities.getFile(job, firstFileRecord);
        InputStream is = new FileInputStream(file);
        odfDoc = OdfDocument.loadDocument(is);
        List<OdfTable> tables = odfDoc.getTableList();
        int sheetCount = tables.size();
        boolean hasData = false;
        for (int i = 0; i < sheetCount; i++) {
            OdfTable sheet = tables.get(i);
            int rows = sheet.getRowCount();
            JSONObject sheetRecord = new JSONObject();
            JSONUtilities.safePut(sheetRecord, "name", sheet.getTableName());
            JSONUtilities.safePut(sheetRecord, "rows", rows);
            if (hasData) {
                JSONUtilities.safePut(sheetRecord, "selected", false);
            } else if (rows > 0) {
                JSONUtilities.safePut(sheetRecord, "selected", true);
                hasData = true;
            }
            JSONUtilities.append(sheetRecords, sheetRecord);
        }
    } catch (FileNotFoundException e) {
        logger.info("File not found", e);
    } catch (Exception e) {
        // ODF throws *VERY* wide exceptions
        logger.info("Error reading ODF spreadsheet", e);
    } finally {
        if (odfDoc != null) {
            odfDoc.close();
        }
    }
    return options;
}
Also used : OdfDocument(org.odftoolkit.odfdom.doc.OdfDocument) JSONObject(org.json.JSONObject) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JSONArray(org.json.JSONArray) FileNotFoundException(java.io.FileNotFoundException) OdfTable(org.odftoolkit.odfdom.doc.table.OdfTable) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with OdfDocument

use of org.odftoolkit.odfdom.doc.OdfDocument in project structr by structr.

the class ODFExporter method exportImage.

public static void exportImage(final ODFExporter thisNode, final String uuid) {
    final File output = thisNode.getResultDocument();
    try {
        final App app = StructrApp.getInstance();
        final Image result = app.nodeQuery(Image.class).and(GraphObject.id, uuid).getFirst();
        String imageName = result.getProperty(new StringProperty("name"));
        String contentType = result.getProperty(new StringProperty("contentType"));
        String templateImagePath = null;
        OdfDocument doc = OdfDocument.loadDocument(output.getFileOnDisk().getAbsolutePath());
        NodeList nodes = doc.getContentRoot().getElementsByTagName(ODF_IMAGE_PARENT_NAME);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node currentNode = nodes.item(i);
            NamedNodeMap attrs = currentNode.getAttributes();
            Node fieldName = attrs.getNamedItem(ODF_IMAGE_ATTRIBUTE_PARENT_IMAGE_NAME);
            if (fieldName != null && fieldName.getTextContent().equals(imageName)) {
                NamedNodeMap childAttrs = currentNode.getFirstChild().getAttributes();
                Node filePath = childAttrs.getNamedItem(ODF_IMAGE_ATTRIBUTE_FILE_PATH);
                templateImagePath = filePath.getTextContent();
                filePath.setTextContent(ODF_IMAGE_DIRECTORY + imageName);
            }
        }
        OdfPackage pkg = doc.getPackage();
        if (templateImagePath != null && templateImagePath.length() > 0) {
            pkg.remove(templateImagePath);
        }
        pkg.insert(new URI(result.getFileOnDisk().getAbsolutePath()), ODF_IMAGE_DIRECTORY + imageName, contentType);
        pkg.save(output.getFileOnDisk().getAbsolutePath());
        pkg.close();
        doc.close();
    } catch (Exception e) {
        logger.error("Error while exporting image to document", e);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) AbstractNode(org.structr.core.entity.AbstractNode) StringProperty(org.structr.core.property.StringProperty) Image(org.structr.web.entity.Image) URI(java.net.URI) FrameworkException(org.structr.common.error.FrameworkException) OdfPackage(org.odftoolkit.odfdom.pkg.OdfPackage) OdfDocument(org.odftoolkit.odfdom.doc.OdfDocument) File(org.structr.web.entity.File)

Example 3 with OdfDocument

use of org.odftoolkit.odfdom.doc.OdfDocument in project OpenRefine by OpenRefine.

the class OdsImporter method createParserUIInitializationData.

@Override
public ObjectNode createParserUIInitializationData(ImportingJob job, List<ObjectNode> fileRecords, String format) {
    ObjectNode options = super.createParserUIInitializationData(job, fileRecords, format);
    ArrayNode sheetRecords = ParsingUtilities.mapper.createArrayNode();
    JSONUtilities.safePut(options, "sheetRecords", sheetRecords);
    OdfDocument odfDoc = null;
    try {
        for (int index = 0; index < fileRecords.size(); index++) {
            ObjectNode fileRecord = fileRecords.get(index);
            File file = ImportingUtilities.getFile(job, fileRecord);
            InputStream is = new FileInputStream(file);
            odfDoc = OdfDocument.loadDocument(is);
            List<OdfTable> tables = odfDoc.getTableList();
            int sheetCount = tables.size();
            for (int i = 0; i < sheetCount; i++) {
                OdfTable sheet = tables.get(i);
                int rows = sheet.getRowCount();
                ObjectNode sheetRecord = ParsingUtilities.mapper.createObjectNode();
                JSONUtilities.safePut(sheetRecord, "name", file.getName() + "#" + sheet.getTableName());
                JSONUtilities.safePut(sheetRecord, "fileNameAndSheetIndex", file.getName() + "#" + i);
                JSONUtilities.safePut(sheetRecord, "rows", rows);
                if (rows > 0) {
                    JSONUtilities.safePut(sheetRecord, "selected", true);
                } else {
                    JSONUtilities.safePut(sheetRecord, "selected", false);
                }
                JSONUtilities.append(sheetRecords, sheetRecord);
            }
        }
    } catch (FileNotFoundException e) {
        logger.info("File not found", e);
    } catch (Exception e) {
        // ODF throws *VERY* wide exceptions
        logger.info("Error reading ODF spreadsheet", e);
    } finally {
        if (odfDoc != null) {
            odfDoc.close();
        }
    }
    return options;
}
Also used : OdfDocument(org.odftoolkit.odfdom.doc.OdfDocument) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) OdfTable(org.odftoolkit.odfdom.doc.table.OdfTable) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 4 with OdfDocument

use of org.odftoolkit.odfdom.doc.OdfDocument 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 5 with OdfDocument

use of org.odftoolkit.odfdom.doc.OdfDocument 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

OdfDocument (org.odftoolkit.odfdom.doc.OdfDocument)6 FileNotFoundException (java.io.FileNotFoundException)4 IOException (java.io.IOException)4 OdfTable (org.odftoolkit.odfdom.doc.table.OdfTable)4 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 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 JSONObject (org.json.JSONObject)2 OdfTableCell (org.odftoolkit.odfdom.doc.table.OdfTableCell)2 OdfTableRow (org.odftoolkit.odfdom.doc.table.OdfTableRow)2 FrameworkException (org.structr.common.error.FrameworkException)2 File (org.structr.web.entity.File)2 URI (java.net.URI)1