Start on refactoring ready to support NPOIFS Directory/Document nodes

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1053269 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2010-12-28 06:21:40 +00:00
parent 35c54e42b9
commit 63cfee8179
6 changed files with 131 additions and 44 deletions

View File

@ -34,7 +34,6 @@ import org.apache.poi.poifs.property.Property;
* *
* @author Marc Johnson (mjohnson at apache dot org) * @author Marc Johnson (mjohnson at apache dot org)
*/ */
public class DirectoryNode public class DirectoryNode
extends EntryNode extends EntryNode
implements DirectoryEntry, POIFSViewable, Iterable<Entry> implements DirectoryEntry, POIFSViewable, Iterable<Entry>
@ -45,8 +44,11 @@ public class DirectoryNode
// Our list of entries, kept sorted to preserve order // Our list of entries, kept sorted to preserve order
private ArrayList<Entry> _entries; private ArrayList<Entry> _entries;
// Only one of these two will exist
// the POIFSFileSystem we belong to // the POIFSFileSystem we belong to
private POIFSFileSystem _filesystem; private POIFSFileSystem _ofilesystem;
// the NPOIFSFileSytem we belong to
private NPOIFSFileSystem _nfilesystem;
// the path described by this document // the path described by this document
private POIFSDocumentPath _path; private POIFSDocumentPath _path;
@ -59,10 +61,32 @@ public class DirectoryNode
* @param filesystem the POIFSFileSystem we belong to * @param filesystem the POIFSFileSystem we belong to
* @param parent the parent of this entry * @param parent the parent of this entry
*/ */
DirectoryNode(final DirectoryProperty property, DirectoryNode(final DirectoryProperty property,
final POIFSFileSystem filesystem, final POIFSFileSystem filesystem,
final DirectoryNode parent) final DirectoryNode parent)
{
this(property, parent);
_ofilesystem = filesystem;
}
/**
* create a DirectoryNode. This method is not public by design; it
* is intended strictly for the internal use of this package
*
* @param property the DirectoryProperty for this DirectoryEntry
* @param nfilesystem the NPOIFSFileSystem we belong to
* @param parent the parent of this entry
*/
DirectoryNode(final DirectoryProperty property,
final NPOIFSFileSystem nfilesystem,
final DirectoryNode parent)
{
this(property, parent);
_nfilesystem = nfilesystem;
}
private DirectoryNode(final DirectoryProperty property,
final DirectoryNode parent)
{ {
super(property, parent); super(property, parent);
if (parent == null) if (parent == null)
@ -76,7 +100,6 @@ public class DirectoryNode
property.getName() property.getName()
}); });
} }
_filesystem = filesystem;
_byname = new HashMap<String, Entry>(); _byname = new HashMap<String, Entry>();
_entries = new ArrayList<Entry>(); _entries = new ArrayList<Entry>();
Iterator<Property> iter = property.getChildren(); Iterator<Property> iter = property.getChildren();
@ -88,8 +111,12 @@ public class DirectoryNode
if (child.isDirectory()) if (child.isDirectory())
{ {
childNode = new DirectoryNode(( DirectoryProperty ) child, DirectoryProperty childDir = (DirectoryProperty) child;
_filesystem, this); if(_ofilesystem != null) {
childNode = new DirectoryNode(childDir, _ofilesystem, this);
} else {
childNode = new DirectoryNode(childDir, _nfilesystem, this);
}
} }
else else
{ {
@ -113,10 +140,17 @@ public class DirectoryNode
/** /**
* @return the filesystem that this belongs to * @return the filesystem that this belongs to
*/ */
public POIFSFileSystem getFileSystem() public POIFSFileSystem getFileSystem()
{ {
return _filesystem; return _ofilesystem;
}
/**
* @return the filesystem that this belongs to
*/
public NPOIFSFileSystem getNFileSystem()
{
return _nfilesystem;
} }
/** /**
@ -161,7 +195,13 @@ public class DirectoryNode
DocumentNode rval = new DocumentNode(property, this); DocumentNode rval = new DocumentNode(property, this);
(( DirectoryProperty ) getProperty()).addChild(property); (( DirectoryProperty ) getProperty()).addChild(property);
_filesystem.addDocument(document);
if(_ofilesystem != null) {
_ofilesystem.addDocument(document);
} else {
_nfilesystem.addDocument(document);
}
_entries.add(rval); _entries.add(rval);
_byname.put(property.getName(), rval); _byname.put(property.getName(), rval);
return rval; return rval;
@ -211,8 +251,13 @@ public class DirectoryNode
if (rval) if (rval)
{ {
_entries.remove(entry); _entries.remove(entry);
_byname.remove(entry.getName()); _byname.remove(entry.getName());
_filesystem.remove(entry);
if(_ofilesystem != null) {
_ofilesystem.remove(entry);
} else {
_nfilesystem.remove(entry);
}
} }
return rval; return rval;
} }
@ -341,12 +386,18 @@ public class DirectoryNode
public DirectoryEntry createDirectory(final String name) public DirectoryEntry createDirectory(final String name)
throws IOException throws IOException
{ {
DirectoryNode rval;
DirectoryProperty property = new DirectoryProperty(name); DirectoryProperty property = new DirectoryProperty(name);
DirectoryNode rval = new DirectoryNode(property, _filesystem,
this); if(_ofilesystem != null) {
rval = new DirectoryNode(property, _ofilesystem, this);
_ofilesystem.addDirectory(property);
} else {
rval = new DirectoryNode(property, _nfilesystem, this);
_nfilesystem.addDirectory(property);
}
(( DirectoryProperty ) getProperty()).addChild(property); (( DirectoryProperty ) getProperty()).addChild(property);
_filesystem.addDirectory(property);
_entries.add(rval); _entries.add(rval);
_byname.put(name, rval); _byname.put(name, rval);
return rval; return rval;

View File

@ -19,7 +19,6 @@
package org.apache.poi.poifs.filesystem; package org.apache.poi.poifs.filesystem;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -45,15 +44,11 @@ import org.apache.poi.poifs.nio.DataSource;
import org.apache.poi.poifs.nio.FileBackedDataSource; import org.apache.poi.poifs.nio.FileBackedDataSource;
import org.apache.poi.poifs.property.DirectoryProperty; import org.apache.poi.poifs.property.DirectoryProperty;
import org.apache.poi.poifs.property.NPropertyTable; import org.apache.poi.poifs.property.NPropertyTable;
import org.apache.poi.poifs.property.Property;
import org.apache.poi.poifs.storage.BATBlock; import org.apache.poi.poifs.storage.BATBlock;
import org.apache.poi.poifs.storage.BlockAllocationTableWriter; import org.apache.poi.poifs.storage.BlockAllocationTableWriter;
import org.apache.poi.poifs.storage.BlockList;
import org.apache.poi.poifs.storage.BlockWritable;
import org.apache.poi.poifs.storage.HeaderBlock; import org.apache.poi.poifs.storage.HeaderBlock;
import org.apache.poi.poifs.storage.HeaderBlockConstants; import org.apache.poi.poifs.storage.HeaderBlockConstants;
import org.apache.poi.poifs.storage.HeaderBlockWriter; import org.apache.poi.poifs.storage.HeaderBlockWriter;
import org.apache.poi.poifs.storage.SmallBlockTableWriter;
import org.apache.poi.poifs.storage.BATBlock.BATBlockAndIndex; import org.apache.poi.poifs.storage.BATBlock.BATBlockAndIndex;
import org.apache.poi.util.CloseIgnoringInputStream; import org.apache.poi.util.CloseIgnoringInputStream;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
@ -464,6 +459,26 @@ public class NPOIFSFileSystem extends BlockStore
return _mini_store; return _mini_store;
} }
/**
* add a new POIFSDocument to the FileSytem
*
* @param document the POIFSDocument being added
*/
void addDocument(final POIFSDocument document)
{
_property_table.addProperty(document.getDocumentProperty());
}
/**
* add a new DirectoryProperty to the FileSystem
*
* @param directory the DirectoryProperty being added
*/
void addDirectory(final DirectoryProperty directory)
{
_property_table.addProperty(directory);
}
/** /**
* Create a new document to be added to the root directory * Create a new document to be added to the root directory
* *
@ -610,17 +625,14 @@ public class NPOIFSFileSystem extends BlockStore
} }
/** /**
* get the root entry * Get the root entry
* *
* @return the root entry * @return the root entry
*/ */
public DirectoryNode getRoot() public DirectoryNode getRoot()
{ {
if (_root == null) if (_root == null) {
{ _root = new DirectoryNode(_property_table.getRoot(), this, null);
// TODO
// _root = new DirectoryNode(_property_table.getRoot(), this, null);
} }
return _root; return _root;
} }

View File

@ -54,7 +54,7 @@ public final class POIFSDocument implements BATManaged, BlockWritable, POIFSView
// one of these stores will be valid // one of these stores will be valid
private SmallBlockStore _small_store; private SmallBlockStore _small_store;
private BigBlockStore _big_store; private BigBlockStore _big_store;
/** /**
* Constructor from large blocks * Constructor from large blocks

View File

@ -57,7 +57,7 @@ public final class TestDocumentInputStream extends TestCase {
_workbook = new DocumentNode( _workbook = new DocumentNode(
document.getDocumentProperty(), document.getDocumentProperty(),
new DirectoryNode( new DirectoryNode(
new DirectoryProperty("Root Entry"), null, null)); new DirectoryProperty("Root Entry"), (POIFSFileSystem)null, null));
} }
private DocumentNode _workbook; private DocumentNode _workbook;

View File

@ -49,7 +49,7 @@ public final class TestDocumentNode extends TestCase {
POIFSDocument document = new POIFSDocument("document", rawBlocks, POIFSDocument document = new POIFSDocument("document", rawBlocks,
2000); 2000);
DocumentProperty property2 = document.getDocumentProperty(); DocumentProperty property2 = document.getDocumentProperty();
DirectoryNode parent = new DirectoryNode(property1, null, null); DirectoryNode parent = new DirectoryNode(property1, (POIFSFileSystem)null, null);
DocumentNode node = new DocumentNode(property2, parent); DocumentNode node = new DocumentNode(property2, parent);
// verify we can retrieve the document // verify we can retrieve the document

View File

@ -402,14 +402,36 @@ public final class TestNPOIFSFileSystem extends TestCase {
public void testListEntries() throws Exception { public void testListEntries() throws Exception {
NPOIFSFileSystem fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi")); NPOIFSFileSystem fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi"));
NPOIFSFileSystem fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi")); NPOIFSFileSystem fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB}) { NPOIFSFileSystem fsC = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi"));
// TODO NPOIFSFileSystem fsD = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi"));
} for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB,fsC,fsD}) {
DirectoryEntry root = fs.getRoot();
fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi")); assertEquals(5, root.getEntryCount());
fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi"));
for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB}) { // Check by the names
// TODO Entry thumbnail = root.getEntry("Thumbnail");
Entry dsi = root.getEntry("\u0005DocumentSummaryInformation");
Entry si = root.getEntry("\u0005SummaryInformation");
Entry image = root.getEntry("Image");
Entry tags = root.getEntry("Tags");
assertEquals(false, thumbnail.isDirectoryEntry());
assertEquals(false, dsi.isDirectoryEntry());
assertEquals(false, si.isDirectoryEntry());
assertEquals(true, image.isDirectoryEntry());
assertEquals(false, tags.isDirectoryEntry());
// Check via the iterator
Iterator<Entry> it = root.getEntries();
assertEquals(thumbnail.getName(), it.next().getName());
assertEquals(dsi.getName(), it.next().getName());
assertEquals(si.getName(), it.next().getName());
assertEquals(image.getName(), it.next().getName());
assertEquals(tags.getName(), it.next().getName());
// Look inside another
DirectoryEntry imageD = (DirectoryEntry)image;
assertEquals(7, imageD.getEntryCount());
} }
} }
@ -420,14 +442,16 @@ public final class TestNPOIFSFileSystem extends TestCase {
public void testGetDocumentEntry() throws Exception { public void testGetDocumentEntry() throws Exception {
NPOIFSFileSystem fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi")); NPOIFSFileSystem fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize512.zvi"));
NPOIFSFileSystem fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi")); NPOIFSFileSystem fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB}) { NPOIFSFileSystem fsC = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi"));
// TODO NPOIFSFileSystem fsD = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi"));
} for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB,fsC,fsD}) {
DirectoryEntry root = fs.getRoot();
fsA = new NPOIFSFileSystem(_inst.getFile("BlockSize4096.zvi")); Entry dsi = root.getEntry("\u0005DocumentSummaryInformation");
fsB = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize4096.zvi"));
for(NPOIFSFileSystem fs : new NPOIFSFileSystem[] {fsA,fsB}) { assertEquals(true, dsi.isDocumentEntry());
// TODO DocumentEntry doc = (DocumentEntry)dsi;
} }
} }