TreeReaderListenerpublic class TreeReaderListener extends Object implements POIFSReaderListenerOrganizes document information in a tree model in order to be
e.g. displayed in a Swing {@link javax.swing.JTree}. An instance of this
class is created with a root tree node ({@link MutableTreeNode}) and
registered as a {@link POIFSReaderListener} with a {@link
org.apache.poi.poifs.eventfilesystem.POIFSReader}. While the latter processes
a POI filesystem it calls this class' {@link #processPOIFSReaderEvent} for
each document it has been registered for. This method appends the document it
processes at the appropriate position into the tree rooted at the
above mentioned root tree node.
The root tree node should be the root tree node of a {@link
javax.swing.tree.TreeModel}.
A top-level element in the tree model, i.e. an immediate child
node of the root node, describes a POI filesystem as such. It is
suggested to use the file's name (as seen by the operating system)
but it could be any other string.
The value of a tree node is a {@link DocumentDescriptor}. Unlike
a {@link org.apache.poi.poifs.filesystem.POIFSDocument} which may be as heavy
as many megabytes, an instance of {@link DocumentDescriptor} is a
light-weight object and contains only some meta-information about a
document. |
Fields Summary |
---|
protected MutableTreeNode | rootNode The tree's root node. POI filesystems get attached to this
node as children. | protected Map | pathToNode Maps filenames and POI document paths to their associated
tree nodes. | protected String | filename The name of the file this {@link TreeReaderListener}
processes. It is used to identify a top-level element in the
tree. Alternatively any other string can be used. It is just a
label which should identify a POI filesystem. | private int | nrOfBytes The number of bytes to dump. |
Constructors Summary |
---|
public TreeReaderListener(String filename, MutableTreeNode rootNode)Creates a {@link TreeReaderListener} which should then be
registered with a
{@link org.apache.poi.poifs.eventfilesystem.POIFSReader}.
this.filename = filename;
this.rootNode = rootNode;
pathToNode = new HashMap(15); // Should be a reasonable guess.
|
Methods Summary |
---|
private javax.swing.tree.MutableTreeNode | getNode(org.apache.poi.poifs.filesystem.POIFSDocumentPath path, java.lang.String fsName, javax.swing.tree.MutableTreeNode root)Locates the parent node for a document entry in the tree
model. If the parent node does not yet exist it will be
created, too. This is done recursively, if needed.
MutableTreeNode n = (MutableTreeNode) pathToNode.get(path);
if (n != null)
/* Node found in map, just return it. */
return n;
if (path.length() == 0)
{
/* This is the root path of the POI filesystem. Its tree
* node is resp. must be located below the tree node of
* the POI filesystem itself. This is a tree node with the
* POI filesystem's name (this the operating system file's
* name) as its key it the path-to-node map. */
n = (MutableTreeNode) pathToNode.get(fsName);
if (n == null)
{
/* A tree node for the POI filesystem does not yet
* exist. */
n = new DefaultMutableTreeNode(fsName);
pathToNode.put(fsName, n);
root.insert(n, 0);
}
return n;
}
else
{
/* The path is somewhere down in the POI filesystem's
* hierarchy. We need the tree node of this path's parent
* and attach our new node to it. */
final String name = path.getComponent(path.length() - 1);
final POIFSDocumentPath parentPath = path.getParent();
final MutableTreeNode parentNode =
getNode(parentPath, fsName, root);
n = new DefaultMutableTreeNode(name);
pathToNode.put(path, n);
parentNode.insert(n, 0);
return n;
}
| public int | getNrOfBytes()
return nrOfBytes;
| public void | processPOIFSReaderEvent(org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent event)A document in the POI filesystem has been opened for
reading. This method retrieves properties of the document and
adds them to a tree model.
DocumentDescriptor d;
final DocumentInputStream is = event.getStream();
if (!is.markSupported())
throw new UnsupportedOperationException(is.getClass().getName() +
" does not support mark().");
/* Try do handle this document as a property set. We receive
* an exception if is no property set and handle it as a
* document of some other format. We are not concerned about
* that document's details. */
try
{
d = new PropertySetDescriptor(event.getName(), event.getPath(),
is, nrOfBytes);
}
catch (HPSFException ex)
{
d = new DocumentDescriptor(event.getName(), event.getPath(),
is, nrOfBytes);
}
catch (Throwable t)
{
System.err.println
("Unexpected exception while processing " +
event.getName() + " in " + event.getPath().toString());
t.printStackTrace(System.err);
throw new RuntimeException(t.getMessage());
}
try
{
is.close();
}
catch (IOException ex)
{
System.err.println
("Unexpected exception while closing " +
event.getName() + " in " + event.getPath().toString());
ex.printStackTrace(System.err);
}
final MutableTreeNode parentNode = getNode(d.path, filename, rootNode);
final MutableTreeNode nameNode = new DefaultMutableTreeNode(d.name);
parentNode.insert(nameNode, 0);
final MutableTreeNode dNode = new DefaultMutableTreeNode(d);
nameNode.insert(dNode, 0);
| public void | setNrOfBytes(int nrOfBytes)
this.nrOfBytes = nrOfBytes;
|
|