Search in sources :

Example 1 with XSSFObjectData

use of org.apache.poi.xssf.usermodel.XSSFObjectData in project poi by apache.

the class EmbeddedExtractor method extractAll.

protected void extractAll(ShapeContainer<?> parent, List<EmbeddedData> embeddings) throws IOException {
    for (Shape shape : parent) {
        EmbeddedData data = null;
        if (shape instanceof ObjectData) {
            ObjectData od = (ObjectData) shape;
            try {
                if (od.hasDirectoryEntry()) {
                    data = extractOne((DirectoryNode) od.getDirectory());
                } else {
                    String contentType = CONTENT_TYPE_BYTES;
                    if (od instanceof XSSFObjectData) {
                        contentType = ((XSSFObjectData) od).getObjectPart().getContentType();
                    }
                    data = new EmbeddedData(od.getFileName(), od.getObjectData(), contentType);
                }
            } catch (Exception e) {
                LOG.log(POILogger.WARN, "Entry not found / readable - ignoring OLE embedding", e);
            }
        } else if (shape instanceof Picture) {
            data = extractOne((Picture) shape);
        } else if (shape instanceof ShapeContainer) {
            extractAll((ShapeContainer<?>) shape, embeddings);
        }
        if (data == null) {
            continue;
        }
        data.setShape(shape);
        String filename = data.getFilename();
        String extension = (filename == null || filename.lastIndexOf('.') == -1) ? ".bin" : filename.substring(filename.lastIndexOf('.'));
        // try to find an alternative name
        if (filename == null || "".equals(filename) || filename.startsWith("MBD") || filename.startsWith("Root Entry")) {
            filename = shape.getShapeName();
            if (filename != null) {
                filename += extension;
            }
        }
        // default to dummy name
        if (filename == null || "".equals(filename)) {
            filename = "picture_" + embeddings.size() + extension;
        }
        filename = filename.trim();
        data.setFilename(filename);
        embeddings.add(data);
    }
}
Also used : Shape(org.apache.poi.ss.usermodel.Shape) Picture(org.apache.poi.ss.usermodel.Picture) ObjectData(org.apache.poi.ss.usermodel.ObjectData) XSSFObjectData(org.apache.poi.xssf.usermodel.XSSFObjectData) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) XSSFObjectData(org.apache.poi.xssf.usermodel.XSSFObjectData) ShapeContainer(org.apache.poi.ss.usermodel.ShapeContainer) Ole10NativeException(org.apache.poi.poifs.filesystem.Ole10NativeException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)1 Ole10NativeException (org.apache.poi.poifs.filesystem.Ole10NativeException)1 ObjectData (org.apache.poi.ss.usermodel.ObjectData)1 Picture (org.apache.poi.ss.usermodel.Picture)1 Shape (org.apache.poi.ss.usermodel.Shape)1 ShapeContainer (org.apache.poi.ss.usermodel.ShapeContainer)1 XSSFObjectData (org.apache.poi.xssf.usermodel.XSSFObjectData)1