use of org.apache.poi.poifs.filesystem.DirectoryNode 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);
}
}
use of org.apache.poi.poifs.filesystem.DirectoryNode in project poi by apache.
the class EmbeddedExtractor method copyNodes.
protected static void copyNodes(DirectoryNode src, DirectoryNode dest) throws IOException {
for (Entry e : src) {
if (e instanceof DirectoryNode) {
DirectoryNode srcDir = (DirectoryNode) e;
DirectoryNode destDir = (DirectoryNode) dest.createDirectory(srcDir.getName());
destDir.setStorageClsid(srcDir.getStorageClsid());
copyNodes(srcDir, destDir);
} else {
InputStream is = src.createDocumentInputStream(e);
try {
dest.createDocument(e.getName(), is);
} finally {
is.close();
}
}
}
}
use of org.apache.poi.poifs.filesystem.DirectoryNode in project poi by apache.
the class HSSFWorkbook method addOlePackage.
/**
* Adds an OLE package manager object with the given POIFS to the sheet
*
* @param poiData an POIFS containing the embedded document, to be added
* @param label the label of the payload
* @param fileName the original filename
* @param command the command to open the payload
* @return the index of the added ole object
* @throws IOException if the object can't be embedded
*/
public int addOlePackage(POIFSFileSystem poiData, String label, String fileName, String command) throws IOException {
DirectoryNode root = poiData.getRoot();
Map<String, ClassID> olemap = getOleMap();
for (Map.Entry<String, ClassID> entry : olemap.entrySet()) {
if (root.hasEntry(entry.getKey())) {
root.setStorageClsid(entry.getValue());
break;
}
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
poiData.writeFilesystem(bos);
return addOlePackage(bos.toByteArray(), label, fileName, command);
}
use of org.apache.poi.poifs.filesystem.DirectoryNode in project poi by apache.
the class ExtractorFactory method getEmbededDocsTextExtractors.
/**
* Returns an array of text extractors, one for each of
* the embedded documents in the file (if there are any).
* If there are no embedded documents, you'll get back an
* empty array. Otherwise, you'll get one open
* {@link POITextExtractor} for each embedded file.
*/
public static POITextExtractor[] getEmbededDocsTextExtractors(POIOLE2TextExtractor ext) throws IOException, OpenXML4JException, XmlException {
// All the embedded directories we spotted
ArrayList<Entry> dirs = new ArrayList<Entry>();
// For anything else not directly held in as a POIFS directory
ArrayList<InputStream> nonPOIFS = new ArrayList<InputStream>();
// Find all the embedded directories
DirectoryEntry root = ext.getRoot();
if (root == null) {
throw new IllegalStateException("The extractor didn't know which POIFS it came from!");
}
if (ext instanceof ExcelExtractor) {
// These are in MBD... under the root
Iterator<Entry> it = root.getEntries();
while (it.hasNext()) {
Entry entry = it.next();
if (entry.getName().startsWith("MBD")) {
dirs.add(entry);
}
}
} else if (ext instanceof WordExtractor) {
// These are in ObjectPool -> _... under the root
try {
DirectoryEntry op = (DirectoryEntry) root.getEntry("ObjectPool");
Iterator<Entry> it = op.getEntries();
while (it.hasNext()) {
Entry entry = it.next();
if (entry.getName().startsWith("_")) {
dirs.add(entry);
}
}
} catch (FileNotFoundException e) {
logger.log(POILogger.INFO, "Ignoring FileNotFoundException while extracting Word document", e.getLocalizedMessage());
// ignored here
}
//} else if(ext instanceof PowerPointExtractor) {
// Tricky, not stored directly in poifs
// TODO
} else if (ext instanceof OutlookTextExtactor) {
// Stored in the Attachment blocks
MAPIMessage msg = ((OutlookTextExtactor) ext).getMAPIMessage();
for (AttachmentChunks attachment : msg.getAttachmentFiles()) {
if (attachment.getAttachData() != null) {
byte[] data = attachment.getAttachData().getValue();
nonPOIFS.add(new ByteArrayInputStream(data));
} else if (attachment.getAttachmentDirectory() != null) {
dirs.add(attachment.getAttachmentDirectory().getDirectory());
}
}
}
// Create the extractors
if (dirs.size() == 0 && nonPOIFS.size() == 0) {
return new POITextExtractor[0];
}
ArrayList<POITextExtractor> textExtractors = new ArrayList<POITextExtractor>();
for (Entry dir : dirs) {
textExtractors.add(createExtractor((DirectoryNode) dir));
}
for (InputStream nonPOIF : nonPOIFS) {
try {
textExtractors.add(createExtractor(nonPOIF));
} catch (IllegalArgumentException e) {
// Ignore, just means it didn't contain
// a format we support as yet
logger.log(POILogger.INFO, "Format not supported yet", e.getLocalizedMessage());
} catch (XmlException e) {
throw new IOException(e.getMessage(), e);
} catch (OpenXML4JException e) {
throw new IOException(e.getMessage(), e);
}
}
return textExtractors.toArray(new POITextExtractor[textExtractors.size()]);
}
use of org.apache.poi.poifs.filesystem.DirectoryNode in project poi by apache.
the class HSLFSlideShow method addEmbed.
/**
* Add a embedded object to this presentation
*
* @return 0-based index of the embedded object
*/
public int addEmbed(POIFSFileSystem poiData) {
DirectoryNode root = poiData.getRoot();
// prepare embedded data
if (new ClassID().equals(root.getStorageClsid())) {
// need to set class id
Map<String, ClassID> olemap = getOleMap();
ClassID classID = null;
for (Map.Entry<String, ClassID> entry : olemap.entrySet()) {
if (root.hasEntry(entry.getKey())) {
classID = entry.getValue();
break;
}
}
if (classID == null) {
throw new IllegalArgumentException("Unsupported embedded document");
}
root.setStorageClsid(classID);
}
ExEmbed exEmbed = new ExEmbed();
// remove unneccessary infos, so we don't need to specify the type
// of the ole object multiple times
Record[] children = exEmbed.getChildRecords();
exEmbed.removeChild(children[2]);
exEmbed.removeChild(children[3]);
exEmbed.removeChild(children[4]);
ExEmbedAtom eeEmbed = exEmbed.getExEmbedAtom();
eeEmbed.setCantLockServerB(true);
ExOleObjAtom eeAtom = exEmbed.getExOleObjAtom();
eeAtom.setDrawAspect(ExOleObjAtom.DRAW_ASPECT_VISIBLE);
eeAtom.setType(ExOleObjAtom.TYPE_EMBEDDED);
// eeAtom.setSubType(ExOleObjAtom.SUBTYPE_EXCEL);
// should be ignored?!?, see MS-PPT ExOleObjAtom, but Libre Office sets it ...
eeAtom.setOptions(1226240);
ExOleObjStg exOleObjStg = new ExOleObjStg();
try {
final String OLESTREAM_NAME = "Ole";
if (!root.hasEntry(OLESTREAM_NAME)) {
// the following data was taken from an example libre office document
// beside this "Ole" record there were several other records, e.g. CompObj,
// OlePresXXX, but it seems, that they aren't neccessary
byte[] oleBytes = { 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
poiData.createDocument(new ByteArrayInputStream(oleBytes), OLESTREAM_NAME);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
poiData.writeFilesystem(bos);
exOleObjStg.setData(bos.toByteArray());
} catch (IOException e) {
throw new HSLFException(e);
}
int psrId = addPersistentObject(exOleObjStg);
exOleObjStg.setPersistId(psrId);
eeAtom.setObjStgDataRef(psrId);
int objectId = addToObjListAtom(exEmbed);
eeAtom.setObjID(objectId);
return objectId;
}
Aggregations