An application that wants to open a document in a POI filesystem
(POIFS) proceeds as shown by the following code fragment. (The full
source code of the sample application is available in the
examples section of the POI source tree as
ReadTitle.java.)
import java.io.*;
import org.apache.poi.hpsf.*;
import org.apache.poi.poifs.eventfilesystem.*;
// ...
public static void main(String[] args)
throws IOException
{
final String filename = args[0];
POIFSReader r = new POIFSReader();
r.registerListener(new MyPOIFSReaderListener(),
"\005SummaryInformation");
r.read(new FileInputStream(filename));
}
|
The first interesting statement is
POIFSReader r = new POIFSReader();
|
It creates a
org.apache.poi.poifs.eventfilesystem.POIFSReader instance
which we shall need to read the POI filesystem. Before the application
actually opens the POI filesystem we have to tell the
POIFSReader which documents we are interested in. In this
case the application should do something with the document
\005SummaryInformation.
r.registerListener(new MyPOIFSReaderListener(),
"\005SummaryInformation");
|
This method call registers a
org.apache.poi.poifs.eventfilesystem.POIFSReaderListener
with the POIFSReader . The POIFSReaderListener
interface specifies the method processPOIFSReaderEvent
which processes a document. The class
MyPOIFSReaderListener implements the
POIFSReaderListener and thus the
processPOIFSReaderEvent method. The eventing POI filesystem
calls this method when it finds the \005SummaryInformation
document. In the sample application MyPOIFSReaderListener is
a static class in the ReadTitle.java source file.)
Now everything is prepared and reading the POI filesystem can
start:
r.read(new FileInputStream(filename));
|
The following source code fragment shows the
MyPOIFSReaderListener class and how it retrieves the
title.
static class MyPOIFSReaderListener implements POIFSReaderListener
{
public void processPOIFSReaderEvent(POIFSReaderEvent e)
{
SummaryInformation si = null;
try
{
si = (SummaryInformation)
PropertySetFactory.create(e.getStream());
}
catch (Exception ex)
{
throw new RuntimeException
("Property set stream \"" +
event.getPath() + event.getName() + "\": " + ex);
}
final String title = si.getTitle();
if (title != null)
System.out.println("Title: \"" + title + "\"");
else
System.out.println("Document has no title.");
}
}
|
The line
SummaryInformation si = null;
|
declares a SummaryInformation variable and initializes it
with null . We need an instance of this class to access the
title. The instance is created in a try block:
si = (SummaryInformation)
PropertySetFactory.create(e.getStream());
|
The expression e.getStream() returns the input stream
containing the bytes of the property set stream named
\005SummaryInformation. This stream is passed into the
create method of the factory class
org.apache.poi.hpsf.PropertySetFactory which returns
a org.apache.poi.hpsf.PropertySet instance. It is more or
less safe to cast this result to SummaryInformation , a
convenience class with methods like getTitle() ,
getAuthor() etc.
The PropertySetFactory.create method may throw all sorts
of exceptions. We'll deal with them in the next sections. For now we just
catch all exceptions and throw a RuntimeException
containing the message text of the origin exception.
If all goes well, the sample application retrieves the title and prints
it to the standard output. As you can see you must be prepared for the
case that the POI filesystem does not have a title.
final String title = si.getTitle();
if (title != null)
System.out.println("Title: \"" + title + "\"");
else
System.out.println("Document has no title.");
|
Please note that a Microsoft Office document does not necessarily
contain the \005SummaryInformation stream. The documents created
by the Microsoft Office suite have one, as far as I know. However, an
Excel spreadsheet exported from StarOffice 5.2 won't have a
\005SummaryInformation stream. In this case the applications
won't throw an exception but simply does not call the
processPOIFSReaderEvent method. You have been warned!
|