use of org.apache.poi.poifs.filesystem.DirectoryNode in project poi by apache.
the class TestExtractor method assertExtractFromEmbedded.
private PowerPointExtractor assertExtractFromEmbedded(DirectoryNode root, String entryName, String expected) throws IOException {
DirectoryNode dir = (DirectoryNode) root.getEntry(entryName);
assertTrue(dir.hasEntry(HSLFSlideShow.POWERPOINT_DOCUMENT));
// Check the first file
HSLFSlideShowImpl ppt = new HSLFSlideShowImpl(dir);
PowerPointExtractor ppe = new PowerPointExtractor(ppt);
assertEquals(expected, ppe.getText(true, false));
return ppe;
}
use of org.apache.poi.poifs.filesystem.DirectoryNode in project poi by apache.
the class TestExtractor method testDifferentPOIFS.
/**
* Tests that we can work with both {@link POIFSFileSystem}
* and {@link NPOIFSFileSystem}
*/
@SuppressWarnings("resource")
@Test
public void testDifferentPOIFS() throws IOException {
// Open the two filesystems
File pptFile = slTests.getFile("basic_test_ppt_file.ppt");
InputStream is1 = new FileInputStream(pptFile);
OPOIFSFileSystem opoifs = new OPOIFSFileSystem(is1);
is1.close();
NPOIFSFileSystem npoifs = new NPOIFSFileSystem(pptFile);
DirectoryNode[] files = { opoifs.getRoot(), npoifs.getRoot() };
// Open directly
for (DirectoryNode dir : files) {
PowerPointExtractor extractor = new PowerPointExtractor(dir);
assertEquals(expectText, extractor.getText());
}
// Open via a HSLFSlideShow
for (DirectoryNode dir : files) {
HSLFSlideShowImpl slideshow = new HSLFSlideShowImpl(dir);
PowerPointExtractor extractor = new PowerPointExtractor(slideshow);
assertEquals(expectText, extractor.getText());
extractor.close();
slideshow.close();
}
npoifs.close();
}
use of org.apache.poi.poifs.filesystem.DirectoryNode in project poi by apache.
the class HSSFPatriarch method createObjectData.
@Override
public HSSFObjectData createObjectData(ClientAnchor anchor, int storageId, int pictureIndex) {
ObjRecord obj = new ObjRecord();
CommonObjectDataSubRecord ftCmo = new CommonObjectDataSubRecord();
ftCmo.setObjectType(CommonObjectDataSubRecord.OBJECT_TYPE_PICTURE);
// ftCmo.setObjectId(oleShape.getShapeId()); ... will be set by onCreate(...)
ftCmo.setLocked(true);
ftCmo.setPrintable(true);
ftCmo.setAutofill(true);
ftCmo.setAutoline(true);
ftCmo.setReserved1(0);
ftCmo.setReserved2(0);
ftCmo.setReserved3(0);
obj.addSubRecord(ftCmo);
// FtCf (pictFormat)
FtCfSubRecord ftCf = new FtCfSubRecord();
HSSFPictureData pictData = getSheet().getWorkbook().getAllPictures().get(pictureIndex - 1);
switch(pictData.getFormat()) {
case Workbook.PICTURE_TYPE_WMF:
case Workbook.PICTURE_TYPE_EMF:
// this needs patch #49658 to be applied to actually work
ftCf.setFlags(FtCfSubRecord.METAFILE_BIT);
break;
case Workbook.PICTURE_TYPE_DIB:
case Workbook.PICTURE_TYPE_PNG:
case Workbook.PICTURE_TYPE_JPEG:
case Workbook.PICTURE_TYPE_PICT:
ftCf.setFlags(FtCfSubRecord.BITMAP_BIT);
break;
default:
throw new IllegalStateException("Invalid picture type: " + pictData.getFormat());
}
obj.addSubRecord(ftCf);
// FtPioGrbit (pictFlags)
FtPioGrbitSubRecord ftPioGrbit = new FtPioGrbitSubRecord();
ftPioGrbit.setFlagByBit(FtPioGrbitSubRecord.AUTO_PICT_BIT, true);
obj.addSubRecord(ftPioGrbit);
EmbeddedObjectRefSubRecord ftPictFmla = new EmbeddedObjectRefSubRecord();
ftPictFmla.setUnknownFormulaData(new byte[] { 2, 0, 0, 0, 0 });
ftPictFmla.setOleClassname("Paket");
ftPictFmla.setStorageId(storageId);
obj.addSubRecord(ftPictFmla);
obj.addSubRecord(new EndSubRecord());
String entryName = "MBD" + HexDump.toHex(storageId);
DirectoryEntry oleRoot;
try {
DirectoryNode dn = _sheet.getWorkbook().getDirectory();
if (dn == null) {
throw new FileNotFoundException();
}
oleRoot = (DirectoryEntry) dn.getEntry(entryName);
} catch (FileNotFoundException e) {
throw new IllegalStateException("trying to add ole shape without actually adding data first - use HSSFWorkbook.addOlePackage first", e);
}
// create picture shape, which need to be minimal modified for oleshapes
HSSFPicture shape = new HSSFPicture(null, (HSSFClientAnchor) anchor);
shape.setPictureIndex(pictureIndex);
EscherContainerRecord spContainer = shape.getEscherContainer();
EscherSpRecord spRecord = spContainer.getChildById(EscherSpRecord.RECORD_ID);
spRecord.setFlags(spRecord.getFlags() | EscherSpRecord.FLAG_OLESHAPE);
HSSFObjectData oleShape = new HSSFObjectData(spContainer, obj, oleRoot);
addShape(oleShape);
onCreate(oleShape);
return oleShape;
}
use of org.apache.poi.poifs.filesystem.DirectoryNode in project poi by apache.
the class HSSFWorkbook method write.
/**
* Write out this workbook to the currently open {@link File} via the
* writeable {@link POIFSFileSystem} it was opened as.
*
* <p>This will fail (with an {@link IllegalStateException} if the
* Workbook was opened read-only, opened from an {@link InputStream}
* instead of a File, or if this is not the root document. For those cases,
* you must use {@link #write(OutputStream)} or {@link #write(File)} to
* write to a brand new document.
*/
@Override
public void write() throws IOException {
validateInPlaceWritePossible();
final DirectoryNode dir = getDirectory();
// Update the Workbook stream in the file
DocumentNode workbookNode = (DocumentNode) dir.getEntry(getWorkbookDirEntryName(dir));
NPOIFSDocument workbookDoc = new NPOIFSDocument(workbookNode);
workbookDoc.replaceContents(new ByteArrayInputStream(getBytes()));
// Update the properties streams in the file
writeProperties();
// Sync with the File on disk
dir.getFileSystem().writeFilesystem();
}
use of org.apache.poi.poifs.filesystem.DirectoryNode in project poi by apache.
the class TestHSSFWorkbook method wordDocEmbeddedInXls.
@Test
public void wordDocEmbeddedInXls() throws IOException {
// Open the two filesystems
DirectoryNode[] files = new DirectoryNode[2];
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream("WithEmbeddedObjects.xls"));
try {
files[0] = poifsFileSystem.getRoot();
NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(HSSFTestDataSamples.getSampleFile("WithEmbeddedObjects.xls"));
try {
files[1] = npoifsFileSystem.getRoot();
// Check the embedded parts
for (DirectoryNode root : files) {
HSSFWorkbook hw = new HSSFWorkbook(root, true);
List<HSSFObjectData> objects = hw.getAllEmbeddedObjects();
boolean found = false;
for (HSSFObjectData embeddedObject : objects) {
if (embeddedObject.hasDirectoryEntry()) {
DirectoryEntry dir = embeddedObject.getDirectory();
if (dir instanceof DirectoryNode) {
DirectoryNode dNode = (DirectoryNode) dir;
if (hasEntry(dNode, "WordDocument")) {
found = true;
}
}
}
}
assertTrue(found);
hw.close();
}
} finally {
npoifsFileSystem.close();
}
} finally {
poifsFileSystem.close();
}
}
Aggregations