use of org.apache.poi.poifs.filesystem.DirectoryNode in project poi by apache.
the class WorkbookFactory method create.
/**
* Creates a Workbook from the given NPOIFSFileSystem, which may
* be password protected
*
* @param fs The {@link NPOIFSFileSystem} to read the document from
* @param password The password that should be used or null if no password is necessary.
*
* @return The created Workbook
*
* @throws IOException if an error occurs while reading the data
* @throws InvalidFormatException if the contents of the file cannot be parsed into a {@link Workbook}
*/
private static Workbook create(final NPOIFSFileSystem fs, String password) throws IOException, InvalidFormatException {
DirectoryNode root = fs.getRoot();
// Encrypted OOXML files go inside OLE2 containers, is this one?
if (root.hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) {
InputStream stream = DocumentFactoryHelper.getDecryptedStream(fs, password);
OPCPackage pkg = OPCPackage.open(stream);
return create(pkg);
}
// So, treat it as a regular HSSF XLS one
if (password != null) {
Biff8EncryptionKey.setCurrentUserPassword(password);
}
try {
return new HSSFWorkbook(root, true);
} finally {
Biff8EncryptionKey.setCurrentUserPassword(null);
}
}
use of org.apache.poi.poifs.filesystem.DirectoryNode in project tika by apache.
the class OfficeParser method parse.
/**
* Extracts properties and text from an MS Document input stream
*/
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
configure(context);
XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
xhtml.startDocument();
final DirectoryNode root;
TikaInputStream tstream = TikaInputStream.cast(stream);
NPOIFSFileSystem mustCloseFs = null;
try {
if (tstream == null) {
mustCloseFs = new NPOIFSFileSystem(new CloseShieldInputStream(stream));
root = mustCloseFs.getRoot();
} else {
final Object container = tstream.getOpenContainer();
if (container instanceof NPOIFSFileSystem) {
root = ((NPOIFSFileSystem) container).getRoot();
} else if (container instanceof DirectoryNode) {
root = (DirectoryNode) container;
} else {
NPOIFSFileSystem fs = null;
if (tstream.hasFile()) {
fs = new NPOIFSFileSystem(tstream.getFile(), true);
} else {
fs = new NPOIFSFileSystem(new CloseShieldInputStream(tstream));
}
//tstream will close the fs, no need to close this below
tstream.setOpenContainer(fs);
root = fs.getRoot();
}
}
parse(root, context, metadata, xhtml);
OfficeParserConfig officeParserConfig = context.get(OfficeParserConfig.class);
if (officeParserConfig.getExtractMacros()) {
//now try to get macros
extractMacros(root.getNFileSystem(), xhtml, EmbeddedDocumentUtil.getEmbeddedDocumentExtractor(context));
}
} finally {
IOUtils.closeQuietly(mustCloseFs);
}
xhtml.endDocument();
}
use of org.apache.poi.poifs.filesystem.DirectoryNode in project poi by apache.
the class TestNonStandardWorkbookStreamNames method testWritePreserve.
/**
* Test that when we write out preserving nodes, we go back to the
* correct case
*/
@Test
public void testWritePreserve() throws IOException {
// Open the workbook, not preserving nodes
InputStream is = HSSFTestDataSamples.openSampleFileStream(xlsA);
HSSFWorkbook wb = new HSSFWorkbook(is, true);
is.close();
// Check now it can be opened
HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb);
wb.close();
DirectoryNode root = wb2.getDirectory();
// Check that we have the new entries
assertTrue(root.hasEntry("Workbook"));
assertFalse(root.hasEntry("BOOK"));
assertFalse(root.hasEntry("WORKBOOK"));
// As we preserved, should also have a few other streams
assertTrue(root.hasEntry("\005SummaryInformation"));
wb2.close();
}
use of org.apache.poi.poifs.filesystem.DirectoryNode in project poi by apache.
the class TestOLE2Embeding method testReallyEmbedSomething.
@Test
public void testReallyEmbedSomething() throws Exception {
HSSFWorkbook wb1 = new HSSFWorkbook();
HSSFSheet sheet = wb1.createSheet();
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
byte[] pictureData = HSSFTestDataSamples.getTestDataFileContent("logoKarmokar4.png");
byte[] picturePPT = POIDataSamples.getSlideShowInstance().readFile("clock.jpg");
int imgIdx = wb1.addPicture(pictureData, HSSFWorkbook.PICTURE_TYPE_PNG);
POIFSFileSystem pptPoifs = getSamplePPT();
int pptIdx = wb1.addOlePackage(pptPoifs, "Sample-PPT", "sample.ppt", "sample.ppt");
POIFSFileSystem xlsPoifs = getSampleXLS();
int imgPPT = wb1.addPicture(picturePPT, HSSFWorkbook.PICTURE_TYPE_JPEG);
int xlsIdx = wb1.addOlePackage(xlsPoifs, "Sample-XLS", "sample.xls", "sample.xls");
int txtIdx = wb1.addOlePackage(getSampleTXT(), "Sample-TXT", "sample.txt", "sample.txt");
int rowoffset = 5;
int coloffset = 5;
CreationHelper ch = wb1.getCreationHelper();
HSSFClientAnchor anchor = (HSSFClientAnchor) ch.createClientAnchor();
anchor.setAnchor((short) (2 + coloffset), 1 + rowoffset, 0, 0, (short) (3 + coloffset), 5 + rowoffset, 0, 0);
anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);
patriarch.createObjectData(anchor, pptIdx, imgPPT);
anchor = (HSSFClientAnchor) ch.createClientAnchor();
anchor.setAnchor((short) (5 + coloffset), 1 + rowoffset, 0, 0, (short) (6 + coloffset), 5 + rowoffset, 0, 0);
anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);
patriarch.createObjectData(anchor, xlsIdx, imgIdx);
anchor = (HSSFClientAnchor) ch.createClientAnchor();
anchor.setAnchor((short) (3 + coloffset), 10 + rowoffset, 0, 0, (short) (5 + coloffset), 11 + rowoffset, 0, 0);
anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);
patriarch.createObjectData(anchor, txtIdx, imgIdx);
anchor = (HSSFClientAnchor) ch.createClientAnchor();
anchor.setAnchor((short) (1 + coloffset), -2 + rowoffset, 0, 0, (short) (7 + coloffset), 14 + rowoffset, 0, 0);
anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);
HSSFSimpleShape circle = patriarch.createSimpleShape(anchor);
circle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
circle.setNoFill(true);
// if (false) {
// FileOutputStream fos = new FileOutputStream("embed.xls");
// wb.write(fos);
// fos.close();
// }
HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
HSSFObjectData od = wb2.getAllEmbeddedObjects().get(0);
Ole10Native ole10 = Ole10Native.createFromEmbeddedOleObject((DirectoryNode) od.getDirectory());
bos.reset();
pptPoifs.writeFilesystem(bos);
assertArrayEquals(ole10.getDataBuffer(), bos.toByteArray());
od = wb2.getAllEmbeddedObjects().get(1);
ole10 = Ole10Native.createFromEmbeddedOleObject((DirectoryNode) od.getDirectory());
bos.reset();
xlsPoifs.writeFilesystem(bos);
assertArrayEquals(ole10.getDataBuffer(), bos.toByteArray());
od = wb2.getAllEmbeddedObjects().get(2);
ole10 = Ole10Native.createFromEmbeddedOleObject((DirectoryNode) od.getDirectory());
assertArrayEquals(ole10.getDataBuffer(), getSampleTXT());
xlsPoifs.close();
pptPoifs.close();
wb2.close();
}
use of org.apache.poi.poifs.filesystem.DirectoryNode in project poi by apache.
the class HPBFDumper method dumpQuill.
public void dumpQuill() throws IOException {
DirectoryNode quillDir = (DirectoryNode) fs.getRoot().getEntry("Quill");
DirectoryNode quillSubDir = (DirectoryNode) quillDir.getEntry("QuillSub");
dump001CompObj(quillSubDir);
dumpCONTENTSraw(quillSubDir);
dumpCONTENTSguessed(quillSubDir);
}
Aggregations