/** Holds further metadata on our document */
private DocumentSummaryInformation dsInf;
/** The directory that our document lives in */
- protected DirectoryNode directory;
+ private DirectoryNode directory;
/** For our own logging use */
private static final POILogger logger = POILogFactory.getLogger(POIDocument.class);
if (directory != null) {
if (directory.getNFileSystem() != null) {
directory.getNFileSystem().close();
- directory = null;
+ clearDirectory();
}
}
}
public DirectoryNode getDirectory() {
return directory;
}
+
+ /**
+ * Clear/unlink the attached directory entry
+ */
+ @Internal
+ protected void clearDirectory() {
+ directory = null;
+ }
+
+ /**
+ * check if we were created by POIFS otherwise create a new dummy POIFS
+ * for storing the package data
+ *
+ * @return {@code true} if dummy directory was created, {@code false} otherwise
+ */
+ @Internal
+ protected boolean initDirectory() {
+ if (directory == null) {
+ directory = new NPOIFSFileSystem().getRoot();
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Replaces the attached directory, e.g. if this document is written
+ * to a new POIFSFileSystem
+ *
+ * @param newDirectory the new directory
+ * @return the old/previous directory
+ */
+ @Internal
+ protected DirectoryNode replaceDirectory(DirectoryNode newDirectory) {
+ DirectoryNode dn = directory;
+ directory = newDirectory;
+ return dn;
+ }
}
*/
public DirectoryEntry getRoot()
{
- return document.directory;
+ return document.getDirectory();
}
}
* Write out to the currently open file the properties changes, but nothing else
*/
public void write() throws IOException {
- NPOIFSFileSystem fs = directory.getFileSystem();
+ NPOIFSFileSystem fs = getDirectory().getFileSystem();
validateInPlaceWritePossible();
writeProperties(fs, null);
writeProperties(fs, excepts);
// Copy over everything else unchanged
- FilteringDirectoryNode src = new FilteringDirectoryNode(directory, excepts);
+ FilteringDirectoryNode src = new FilteringDirectoryNode(getDirectory(), excepts);
FilteringDirectoryNode dest = new FilteringDirectoryNode(fs.getRoot(), excepts);
EntryUtils.copyNodes(src, dest);
import org.apache.poi.util.LittleEndianByteArrayOutputStream;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
+import org.apache.poi.util.Removal;
/**
* High level representation of a workbook. This is the first object most users
// If we're not preserving nodes, don't track the
// POIFS any more
if(! preserveNodes) {
- this.directory = null;
+ clearDirectory();
}
_sheets = new ArrayList<HSSFSheet>(INITIAL_CAPACITY);
@Override
public void write() throws IOException {
validateInPlaceWritePossible();
+ final DirectoryNode dir = getDirectory();
// Update the Workbook stream in the file
- DocumentNode workbookNode = (DocumentNode)directory.getEntry(
- getWorkbookDirEntryName(directory));
+ DocumentNode workbookNode = (DocumentNode)dir.getEntry(
+ getWorkbookDirEntryName(dir));
NPOIFSDocument workbookDoc = new NPOIFSDocument(workbookNode);
workbookDoc.replaceContents(new ByteArrayInputStream(getBytes()));
writeProperties();
// Sync with the File on disk
- directory.getFileSystem().writeFilesystem();
+ dir.getFileSystem().writeFilesystem();
}
/**
// Copy over all the other nodes to our new poifs
EntryUtils.copyNodes(
- new FilteringDirectoryNode(this.directory, excepts)
+ new FilteringDirectoryNode(getDirectory(), excepts)
, new FilteringDirectoryNode(fs.getRoot(), excepts)
);
// YK: preserve StorageClsid, it is important for embedded workbooks,
// see Bugzilla 47920
- fs.getRoot().setStorageClsid(this.directory.getStorageClsid());
+ fs.getRoot().setStorageClsid(getDirectory().getStorageClsid());
}
}
public int addOlePackage(byte[] oleData, String label, String fileName, String command)
throws IOException {
// check if we were created by POIFS otherwise create a new dummy POIFS for storing the package data
- if (directory == null) {
- directory = new NPOIFSFileSystem().getRoot();
+ if (initDirectory()) {
preserveNodes = true;
}
DirectoryEntry oleDir = null;
do {
String storageStr = "MBD"+ HexDump.toHex(++storageId);
- if (!directory.hasEntry(storageStr)) {
- oleDir = directory.createDirectory(storageStr);
+ if (!getDirectory().hasEntry(storageStr)) {
+ oleDir = getDirectory().createDirectory(storageStr);
oleDir.setStorageClsid(ClassID.OLE10_PACKAGE);
}
} while (oleDir == null);
return workbook.changeExternalReference(oldUrl, newUrl);
}
+ /**
+ * @deprecated POI 3.16 beta 1. use {@link POIDocument#getDirectory()} instead
+ */
+ @Removal(version="3.18")
public DirectoryNode getRootDirectory(){
- return directory;
+ return getDirectory();
}
@Internal
*/
@Beta
public class AesZipFileZipEntrySource implements ZipEntrySource {
- private static POILogger LOG = POILogFactory.getLogger(AesZipFileZipEntrySource.class);
+ private static final POILogger LOG = POILogFactory.getLogger(AesZipFileZipEntrySource.class);
private final File tmpFile;
private final ZipFile zipFile;
|| checkForTestAnnotation(testclass)) {
out.add(testclass);
}
- } catch (Throwable e) {
+ } catch (Throwable e) { // NOSONAR
System.out.println("Class " + cls + " is not in classpath");
return;
}
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
+import org.apache.poi.util.Removal;
/**
* This class contains the main functionality for the Powerpoint file
/**
* Returns the directory in the underlying POIFSFileSystem for the
* document that is open.
+ *
+ * @deprecated POI 3.16 beta 1. use {@link POIDocument#getDirectory()} instead
*/
+ @Removal(version="3.18")
protected DirectoryNode getPOIFSDirectory() {
- return directory;
+ return getDirectory();
}
/**
private void readPowerPointStream() throws IOException {
// Get the main document stream
DocumentEntry docProps =
- (DocumentEntry) directory.getEntry("PowerPoint Document");
+ (DocumentEntry) getDirectory().getEntry("PowerPoint Document");
// Grab the document stream
int len = docProps.getSize();
- InputStream is = directory.createDocumentInputStream("PowerPoint Document");
+ InputStream is = getDirectory().createDocumentInputStream("PowerPoint Document");
try {
_docstream = IOUtils.toByteArray(is, len);
} finally {
*/
private void readCurrentUserStream() {
try {
- currentUser = new CurrentUserAtom(directory);
+ currentUser = new CurrentUserAtom(getDirectory());
} catch (IOException ie) {
logger.log(POILogger.ERROR, "Error finding Current User Atom:\n" + ie);
currentUser = new CurrentUserAtom();
_pictures = new ArrayList<HSLFPictureData>();
// if the presentation doesn't contain pictures - will use a null set instead
- if (!directory.hasEntry("Pictures")) return;
+ if (!getDirectory().hasEntry("Pictures")) return;
HSLFSlideShowEncrypted decryptData = new HSLFSlideShowEncrypted(getDocumentEncryptionAtom());
- DocumentEntry entry = (DocumentEntry) directory.getEntry("Pictures");
- DocumentInputStream is = directory.createDocumentInputStream(entry);
+ DocumentEntry entry = (DocumentEntry) getDirectory().getEntry("Pictures");
+ DocumentInputStream is = getDirectory().createDocumentInputStream(entry);
byte[] pictstream = IOUtils.toByteArray(is, entry.getSize());
is.close();
// Write the PowerPoint streams to the current FileSystem
// No need to do anything to other streams, already there!
- write(directory.getFileSystem(), false);
+ write(getDirectory().getFileSystem(), false);
// Sync with the File on disk
- directory.getFileSystem().writeFilesystem();
+ getDirectory().getFileSystem().writeFilesystem();
}
/**
// If requested, copy over any other streams we spot, eg Macros
if (copyAllOtherNodes) {
- EntryUtils.copyNodes(directory.getFileSystem(), outFS, writtenEntries);
+ EntryUtils.copyNodes(getDirectory().getFileSystem(), outFS, writtenEntries);
}
}
@Override
public void close() throws IOException {
- NPOIFSFileSystem fs = directory.getFileSystem();
+ NPOIFSFileSystem fs = getDirectory().getFileSystem();
if (fs != null) {
fs.close();
}
validateInPlaceWritePossible();
// Update the Document+Properties streams in the file
- write(directory.getFileSystem(), false);
+ write(getDirectory().getFileSystem(), false);
// Sync with the File on disk
- directory.getFileSystem().writeFilesystem();
+ getDirectory().getFileSystem().writeFilesystem();
}
/**
boolean objectPoolWritten = false;
boolean tableWritten = false;
boolean propertiesWritten = false;
- for (Entry entry : directory) {
+ for (Entry entry : getDirectory()) {
if ( entry.getName().equals( STREAM_WORD_DOCUMENT ) )
{
if ( !docWritten )
if ( !objectPoolWritten && copyOtherEntries )
_objectPool.writeTo( pfs.getRoot() );
- this.directory = pfs.getRoot();
-
/*
* since we updated all references in FIB and etc, using new arrays to
* access data
*/
- this.directory = pfs.getRoot();
+ replaceDirectory(pfs.getRoot());
this._tableStream = tableStream.toByteArray();
this._dataStream = dataBuf;
}
package org.apache.poi;
-import junit.framework.TestCase;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
-import java.io.*;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import org.apache.poi.hpsf.HPSFPropertiesOnlyDocument;
import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
import org.apache.poi.hwpf.HWPFTestDataSamples;
-import org.apache.poi.poifs.filesystem.*;
+import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.junit.Before;
+import org.junit.Test;
/**
* Tests that POIDocument correctly loads and saves the common
*
* This is part 2 of 2 of the tests - it only does the POIDocuments
* which are part of the scratchpad (not main)
- *
- * @author Nick Burch (nick at torchbox dot com)
*/
-public final class TestPOIDocumentScratchpad extends TestCase {
+public final class TestPOIDocumentScratchpad {
// The POI Documents to work on
private POIDocument doc;
private POIDocument doc2;
* Set things up, using a PowerPoint document and
* a Word Document for our testing
*/
- @Override
- public void setUp() throws Exception {
+ @Before
+ public void setUp() throws IOException {
doc = new HSLFSlideShowImpl(POIDataSamples.getSlideShowInstance().openResourceAsStream("basic_test_ppt_file.ppt"));
-
doc2 = HWPFTestDataSamples.openSampleFile("test2.doc");
}
+ @Test
public void testReadProperties() {
+ testReadPropertiesHelper(doc);
+ }
+
+ private void testReadPropertiesHelper(POIDocument docPH) {
// We should have both sets
- assertNotNull(doc.getDocumentSummaryInformation());
- assertNotNull(doc.getSummaryInformation());
+ assertNotNull(docPH.getDocumentSummaryInformation());
+ assertNotNull(docPH.getSummaryInformation());
// Check they are as expected for the test doc
- assertEquals("Hogwarts", doc.getSummaryInformation().getAuthor());
- assertEquals(10598, doc.getDocumentSummaryInformation().getByteCount());
+ assertEquals("Hogwarts", docPH.getSummaryInformation().getAuthor());
+ assertEquals(10598, docPH.getDocumentSummaryInformation().getByteCount());
}
+ @Test
public void testReadProperties2() {
// Check again on the word one
assertNotNull(doc2.getDocumentSummaryInformation());
assertEquals(0, doc2.getDocumentSummaryInformation().getByteCount());
}
- public void testWriteProperties() throws Exception {
+ @Test
+ public void testWriteProperties() throws IOException {
// Just check we can write them back out into a filesystem
NPOIFSFileSystem outFS = new NPOIFSFileSystem();
doc.writeProperties(outFS);
// Should now hold them
assertNotNull(outFS.createDocumentInputStream("\005SummaryInformation"));
assertNotNull(outFS.createDocumentInputStream("\005DocumentSummaryInformation"));
+ outFS.close();
}
- public void testWriteReadProperties() throws Exception {
+ @Test
+ public void testWriteReadProperties() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Write them out
POIFSFileSystem inFS = new POIFSFileSystem(bais);
// Check they're still there
- doc.directory = inFS.getRoot();
- doc.readProperties();
+ POIDocument ppt = new HPSFPropertiesOnlyDocument(inFS);
+ ppt.readProperties();
// Delegate test
- testReadProperties();
+ testReadPropertiesHelper(ppt);
+
+ ppt.close();
+ inFS.close();
}
}
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import org.apache.poi.hpsf.HPSFPropertiesOnlyDocument;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
@Test
public void readProperties() {
+ readPropertiesHelper(doc);
+ }
+
+ private void readPropertiesHelper(POIDocument docWB) {
// We should have both sets
- assertNotNull(doc.getDocumentSummaryInformation());
- assertNotNull(doc.getSummaryInformation());
+ assertNotNull(docWB.getDocumentSummaryInformation());
+ assertNotNull(docWB.getSummaryInformation());
// Check they are as expected for the test doc
- assertEquals("Administrator", doc.getSummaryInformation().getAuthor());
- assertEquals(0, doc.getDocumentSummaryInformation().getByteCount());
+ assertEquals("Administrator", docWB.getSummaryInformation().getAuthor());
+ assertEquals(0, docWB.getDocumentSummaryInformation().getByteCount());
}
@Test
}
@Test
- public void writeProperties() throws Exception {
+ public void writeProperties() throws IOException {
// Just check we can write them back out into a filesystem
NPOIFSFileSystem outFS = new NPOIFSFileSystem();
doc.readProperties();
}
@Test
- public void WriteReadProperties() throws Exception {
+ public void WriteReadProperties() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Write them out
OPOIFSFileSystem inFS = new OPOIFSFileSystem(bais);
// Check they're still there
- doc.directory = inFS.getRoot();
- doc.readProperties();
-
+ POIDocument doc3 = new HPSFPropertiesOnlyDocument(inFS);
+ doc3.readProperties();
+
// Delegate test
- readProperties();
+ readPropertiesHelper(doc3);
+ doc3.close();
}
@Test