From: Mark Johnson Date: Mon, 11 Feb 2002 04:15:29 +0000 (+0000) Subject: rewrote how-to X-Git-Tag: prelogging~127 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=18ffb78a0bfd439a7eb434085f40f5b7390eafca;p=poi.git rewrote how-to git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352085 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/xdocs/poifs/how-to.xml b/src/documentation/xdocs/poifs/how-to.xml new file mode 100644 index 0000000000..58040ace9a --- /dev/null +++ b/src/documentation/xdocs/poifs/how-to.xml @@ -0,0 +1,369 @@ + + + +
+ + + +
+ + +

This document describes how to use the POIFS APIs to read, write, and modify files that employ a POIFS-compatible data structure to organize their content.

+ +
    +
  • 02.10.2002 - completely rewritten from original documents on sourceforge
  • +
+
+ +

This document is intended for Java developers who need to use the POIFS APIs to read, write, or modify files that employ a POIFS-compatible data structure to organize their content. It is not necessary for developers to understand the POIFS data structures, and an explanation of those data structures is beyond the scope of this document. It is expected that the members of the target audience will understand the rudiments of a hierarchical file system, and familiarity with the event pattern employed by Java APIs such as AWT would be helpful.

+
+ +

This document attempts to be consistent in its terminology, which is defined here:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
TermDefinition
DirectoryA special file that may contain other directories and documents.
DirectoryEntryRepresentation of a directory within another directory.
DocumentA file containing data, such as word processing data or a spreadsheet workbook.
DocumentEntryRepresentation of a document within a directory.
EntryRepresentation of a file in a directory.
FileA named entity, managed and contained by the file system.
File SystemThe POIFS data structures, plus the contained directories and documents, which are maintained in a hierarchical directory structure.
Root DirectoryThe directory at the base of a file system. All file systems have a root directory. The POIFS APIs will not allow the root directory to be removed or renamed, but it can be accessed for the purpose of reading its contents or adding files (directories and documents) to it.
+
+
+ +

This section covers reading a file system. There are two ways to read a file system; these techniques are sketched out in the following table, and then explained in greater depth in the sections following the table.

+ + + + + + + + + + + + + + + + +
TechniqueAdvantagesDisadvantages
Conventional Reading +
    +
  • Simpler API similar to reading a conventional file system.
  • +
  • Can read documents in any order.
  • +
+
+
    +
  • All files are resident in memory, whether your application needs them or not.
  • +
+
Event-Driven Reading +
    +
  • Reduced footprint -- only the documents you care about are processed.
  • +
  • Improved performance -- no time is wasted reading the documents you're not interested in.
  • +
+
+
    +
  • More complicated API.
  • +
  • Need to know in advance which documents you want to read.
  • +
  • No control over the order in which the documents are read.
  • +
  • No way to go back and get additional documents except to re-read the file system, which may not be possible, e.g., if the file system is being read from an input stream that lacks random access support.
  • +
+
+ +

In this technique for reading, the entire file system is loaded into memory, and the entire directory tree can be walked by an application, reading specific documents at the application's leisure.

+ +

Before an application can read a file from the file system, the file system needs to be loaded into memory. This is done by using the org.apache.poi.poifs.filesystem.POIFSFileSystem class. Once the file system has been loaded into memory, the application may need the root directory. The following code fragment will accomplish this preparation stage:

+ +// need an open InputStream; for a file-based system, this would be appropriate: +// InputStream stream = new FileInputStream(fileName); +POIFSFileSystem fs; +try +{ + fs = new POIFSFileSystem(inputStream); +} +catch (IOException e) +{ + // an I/O error occurred, or the InputStream did not provide a compatible + // POIFS data structure +} +DirectoryEntry root = fs.getRoot(); +

Assuming no exception was thrown, the file system can then be read.

+

Note: loading the file system can take noticeable time, particularly for large file systems.

+
+ +

Once the file system has been loaded into memory and the root directory has been obtained, the root directory can be read. The following code fragment shows how to read the entries in an org.apache.poi.poifs.filesystem.DirectoryEntry instance:

+ +// dir is an instance of DirectoryEntry ... +for (Iterator iter = dir.getEntries(); iter.hasNext(); ) +{ + Entry entry = (Entry)iter.next(); + System.out.println("found entry: " + entry.getName()); + if (entry instanceof DirectoryEntry) + { + // .. recurse into this directory + } + else if (entry instanceof DocumentEntry) + { + // entry is a document, which you can read + } + else + { + // currently, either an Entry is a DirectoryEntry or a DocumentEntry, + // but in the future, there may be other entry subinterfaces. The + // internal data structure certainly allows for a lot more entry types. + } +} +
+ +

There are a couple of ways to read a document, depending on whether the document resides in the root directory or in another directory. Either way, you will obtain an org.apache.poi.poifs.filesystem.DocumentInputStream instance.

+ +

The DocumentInputStream class is a simple implementation of InputStream that makes a few guarantees worth noting:

+
    +
  • available() always returns the number of bytes in the document from your current position in the document.
  • +
  • markSupported() returns true.
  • +
  • mark(int limit) ignores the limit parameter; basically the method marks the current position in the document.
  • +
  • reset() takes you back to the position when mark() was last called, or to the beginning of the document if mark() has not been called.
  • +
  • skip(long n) will take you to your current position + n (but not past the end of the document).
  • +
+

The behavior of available means you can read in a document in a single read call like this:

+ +byte[] content = new byte[ stream.available() ]; +stream.read(content); +stream.close(); +

The combination of mark, reset, and skip provide the basic mechanisms needed for random access of the document contents.

+
+ +

If the document resides in the root directory, you can obtain a DocumentInputStream like this:

+ +// load file system +try +{ + DocumentInputStream stream = filesystem.createDocumentInputStream(documentName); + // process data from stream +} +catch (IOException e) +{ + // no such document, or the Entry represented by documentName is not a + // DocumentEntry +} +
+ +

A more generic technique for reading a document is to obtain an org.apache.poi.poifs.filesystem.DirectoryEntry instance for the directory containing the desired document (recall that you can use getRoot() to obtain the root directory from its file system). From that DirectoryEntry, you can then obtain a DocumentInputStream like this:

+ +DocumentEntry document = (DocumentEntry)directory.getEntry(documentName); +DocumentInputStream stream = new DocumentInputStream(document); + +
+
+
+ +

The event-driven API for reading documents is a little more complicated and requires that your application know, in advance, which files it wants to read. The benefit of using this API is that each document is in memory just long enough for your application to read it, and documents that you never read at all are not in memory at all. When you're finished reading the documents you wanted, the file system has no data structures associated with it at all and can be discarded.

+ +

The preparation phase involves creating an instance of org.apache.poi.poifs.eventfilesystem.POIFSReader and to then register one or more org.apache.poi.poifs.eventfilesystem.POIFSReaderListener instances with the POIFSReader.

+ +POIFSReader reader = new POIFSReader(); +// register for everything +reader.registerListener(myOmnivorousListener); +// register for selective files +reader.registerListener(myPickyListener, "foo"); +reader.registerListener(myPickyListener, "bar"); +// register for selective files +reader.registerListener(myOtherPickyListener, new POIFSDocumentPath(), + "fubar"); +reader.registerListener(myOtherPickyListener, new POIFSDocumentPath( + new String[] { "usr", "bin" ), "fubar"); +
+ +

org.apache.poi.poifs.eventfilesystem.POIFSReaderListener is an interface used to register for documents. When a matching document is read by the org.apache.poi.poifs.eventfilesystem.POIFSReader, the POIFSReaderListener instance receives an org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent instance, which contains an open DocumentInputStream and information about the document.

+

A POIFSReaderListener instance can register for individual documents, or it can register for all documents; once it has registered for all documents, subsequent (and previous!) registration requests for individual documents are ignored. There is no way to unregister a POIFSReaderListener.

+

Thus, it is possible to register a single POIFSReaderListener for multiple documents - one, some, or all documents. It is guaranteed that a single POIFSReaderListener will receive exactly one notification per registered document. There is no guarantee as to the order in which it will receive notification of its documents, as future implementations of POIFSReader are free to change the algorithm for walking the file system's directory structure.

+

It is also permitted to register more than one POIFSReaderListener for the same document. There is no guarantee of ordering for notification of POIFSReaderListener instances that have registered for the same document when POIFSReader processes that document.

+

It is guaranteed that all notifications occur in the same thread. A future enhancement may be made to provide multi-threaded notifications, but such an enhancement would very probably be made in a new reader class, a ThreadedPOIFSReader perhaps.

+

The following table describes the three ways to register a POIFSReaderListener for a document or set of documents:

+ + + + + + + + + + + + + + + + + +
Method SignatureWhat it does
registerListener(POIFSReaderListener listener)registers listener for all documents.
registerListener(POIFSReaderListener listener, String name)registers listener for a document with the specified name in the root directory.
registerListener(POIFSReaderListener listener, POIFSDocumentPath path, String name)registers listener for a document with the specified name in the directory described by path
+
+ +

The org.apache.poi.poifs.filesystem.POIFSDocumentPath class is used to describe a directory in a POIFS file system. Since there are no reserved characters in the name of a file in a POIFS file system, a more traditional string-based solution for describing a directory, with special characters delimiting the components of the directory name, is not feasible. The constructors for the class are used as follows:

+ + + + + + + + + + + + + + + + + + + + + + + + + +
Constructor exampleDirectory described
new POIFSDocumentPath()The root directory.
new POIFSDocumentPath(null)The root directory.
new POIFSDocumentPath(new String[ 0 ])The root directory.
new POIFSDocumentPath(new String[ ] { "foo", "bar"} )in Unix terminology, "/foo/bar".
new POIFSDocumentPath(new POIFSDocumentPath(new String[] { "foo" }), new String[ ] { "fu", "bar"} )in Unix terminology, "/foo/fu/bar".
+
+ +

Processing org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent events is relatively easy. After all of the POIFSReaderListener instances have been registered with POIFSReader, the POIFSReader.read(InputStream stream) method is called.

+

Assuming that there are no problems with the data, as the POIFSReader processes the documents in the specified InputStream's data, it calls registered POIFSReaderListener instances' processPOIFSReaderEvent method with a POIFSReaderEvent instance.

+

The POIFSReaderEvent instance contains information to identify the document (a POIFSDocumentPath object to identify the directory that the document is in, and the document name), and an open DocumentInputStream instance from which to read the document.

+
+
+
+ +

Writing a file system is very much like reading a file system in that there are multiple ways to do so. You can load an existing file system into memory and modify it (removing files, renaming files) and/or add new files to it, and write it, or you can start with a new, empty file system:

+ +POIFSFileSystem fs = new POIFSFileSystem(); + +

There are two restrictions on the names of files in a file system that must be considered when creating files:

+
    +
  1. The name of the file must not exceed 31 characters. If it does, the POIFS API will silently truncate the name to fit.
  2. +
  3. The name of the file must be unique within its containing directory. This seems pretty obvious, but if it isn't spelled out, there'll be hell to pay, to be sure. Uniqueness, of course, is determined after the name has been truncated, if the original name was too long to begin with.
  4. +
+
+ +

A document can be created by acquiring a DirectoryEntry and calling one of the two createDocument methods:

+ + + + + + + + + + + + + + + + +
Method SignatureAdvantagesDisadvantages
CreateDocument(String name, InputStream stream) +
    +
  • Simple API.
  • +
+
+
    +
  • Increased memory footprint (document is in memory until file system is written).
  • +
+
CreateDocument(String name, int size, POIFSWriterListener writer) +
    +
  • Decreased memory footprint (only very small documents are held in memory, and then only for a short time).
  • +
+
+
    +
  • More complex API.
  • +
  • Determining document size in advance may be difficult.
  • +
  • Lose control over when document is to be written.
  • +
+
+

Unlike reading, you don't have to choose between the in-memory and event-driven writing models; both can co-exist in the same file system.

+

Writing is initiated when the POIFSFileSystem instance's writeFilesystem() method is called with an OutputStream to write to.

+

The event-driven model is quite similar to the event-driven model for reading, in that the file system calls your org.apache.poi.poifs.filesystem.POIFSWriterListener when it's time to write your document, just as the POIFSReader calls your POIFSReaderListener when it's time to read your document. Internally, when writeFilesystem() is called, the final POIFS data structures are created and are written to the specified OutputStream. When the file system needs to write a document out that was created with the event-driven model, it calls the POIFSWriterListener back, calling its processPOIFSWriterEvent() method, passing an org.apache.poi.poifs.filesystem.POIFSWriterEvent instance. This object contains the POIFSDocumentPath and name of the document, its size, and an open org.apache.poi.poifs.filesystem.DocumentOutputStream to which to write. A DocumentOutputStream is a wrapper over the OutputStream that was provided to the POIFSFileSystem to write to, and has the responsibility of making sure that the document your application writes fits within the size you specified for it.

+
+ +

Creating a directory is similar to creating a document, except that there's only one way to do so:

+ +DirectoryEntry createdDir = existingDir.createDirectory(name); +
+ +

As with reading documents, it is possible to create a new document or directory in the root directory by using convenience methods of POIFSFileSystem.

+ + + + + + + + + + + + + + + + + +
DirectoryEntry Method SignaturePOIFSFileSystem Method Signature
createDocument(String name, InputStream stream)createDocument(InputStream stream, String name)
createDocument(String name, int size, POIFSWriterListener writer)createDocument(String name, int size, POIFSWriterListener writer)
createDirectory(String name)createDirectory(String name)
+
+
+ +

It is possible to modify an existing POIFS file system, whether it's one your application has loaded into memory, or one which you are creating on the fly.

+ +

Removing a document is simple: you get the Entry corresponding to the document and call its delete() method. This is a boolean method, but should always return true, indicating that the operation succeeded.

+
+ +

Removing a directory is also simple: you get the Entry corresponding to the directory and call its delete() method. This is a boolean method, but, unlike deleting a document, may not always return true, indicating that the operation succeeded. Here are the reasons why the operation may fail:

+
    +
  • The directory still has files in it (to check, call isEmpty() on its DirectoryEntry; is the return value false?)
  • +
  • The directory is the root directory. You cannot remove the root directory.
  • +
+
+ +

Regardless of whether the file is a directory or a document, it can be renamed, with one exception - the root directory has a special name that is expected by the components of a major software vendor's office suite, and the POIFS API will not let that name be changed. Renaming is done by acquiring the file's corresponding Entry instance and calling its renameTo method, passing in the new name.

+

Like delete, renameTo returns true if the operation succeeded, otherwise false. Reasons for failure include these:

+
    +
  • The new name is the same as another file in the same directory. And don't forget - if the new name is longer than 31 characters, it will be silently truncated. In its original length, the new name may have been unique, but truncated to 31 characters, it may not be unique any longer.
  • +
  • You tried to rename the root directory.
  • +
+
+
+ +
diff --git a/src/documentation/xdocs/poifs/html/how-to.html b/src/documentation/xdocs/poifs/html/how-to.html deleted file mode 100755 index df402e89e8..0000000000 --- a/src/documentation/xdocs/poifs/html/how-to.html +++ /dev/null @@ -1,190 +0,0 @@ - - - - - - - - - - - -

POIFS HOW TO

-

How to use POIFS directly

-

Andrew C. Oliver - December 14, 2001

-
-
10.31.2001- initial revision for - build POI 0.12.3 -
- 12.15.2001 - minor revisions - thread safety, entry modification, - name restrictions, and so on.
- 12.30.2001 - revised for POI 1.0-final - minor revisions -
-

-Capabilities

-
-
This release of POIFS contains the - full functionality to read, write and modify (by recreation) files - in the format most commonly referred to as OLE 2 Compound Document - Format (proabably tm - Microsoft). -
-

-Target Audience

-

This release candidate is intended for general use. It is -considered to be production-ready. It has not yet been extensively -tested (especially in a high load multi-threaded server situation), -though it's been unit tested quite a bit. This release is considered -to be "golden" as it has been used by HSSF and other users -without problems for some time, and has not changed recently. -

-

General Use

-

User API

-

High level description and overview

-

Files written with the POIFS library are referred to as POIFS file -systems (or sometimes archives). The OLE 2 Compound Document format -is designed to mimic many of the characteristics of a pre-modern file -system (most similar to FAT). We make the distinction between POIFS -written files and "native" written OLE 2 Compound Document -Format files because while we believe POIFS to be a full, correct and -complete implementation, most of this was accomplished through -researching other open source implementations and flat out guesses.

-

This overview is in no way intended to be complete (for a more -intense discussion please see POIFSFormat.html in this same -directory), it should give you a good idea into the principals of a -POIFS file system. Please note that specific file formats such as XLS -(HSSF) or DOC utilize POIFS file systems to contain their data, POIFS -itself does not know how to interpret the archived data.

-

Every POIFS file system contains a hierarchy of directories -starting with the root (there is always one, and only one, root). -Each directory, including the root, may contain one or more -directories and/or documents. Every directory and document has a -name. The root directory has a name, but unlike other directories, -its name is fixed and cannot be renamed.

-

The POIFS API was not designed to be, and is not, -thread-safe. Only one thread of control should ever -manipulate a specific POIFS file system over that file system's -lifetime. You can, of course, have multiple threads, each -manipulating a distinct POIFS file system instance.

-

Writing a new one

-

To create a new (from scratch) POIFS file system for writing to, -you simply create an instance of -net.sourceforge.poi.poifs.filesystem.Filesystem using -the default constructor (no arguments). Initially this POIFS file -system will be empty except for containing the essential root -directory.

-

From there you can create a directory entry by calling  -Filesystem.createDirectory(name), and passing in the name of -the directory. This will return an instance -of net.sourceforge.poi.poifs.filesystem.DirectoryEntry -. You can also create a document within the root directory by -calling  Filesystem.createDocument(name, inputstream), -and passing the name of the document and an instance -of java.io.InputStream from which the document's -data can be obtained. It is noted that, the most commonly used file -formats of the Microsoft Corporation such as DOC, XLS, etc. are all -POIFS-compatible file systems with documents stored in the root -directory.

-

Supposing the document is to be stored in a directory other than -the root, you take the instance of DirectoryEntry -that you created and call createDocument(name, -inputstream) on it instead. You can also create a child -directory by calling  createDirectory(name). -Alternatively you can call Filesystem.getRoot() and -use it just like any other directory entry.

-

When you've finished creating entries in the filesystem, simply -call  Filesystem.writeFilesystem(stream) passing in -an instance of  java.io.OutputStream. Be sure you -close the stream when you're done.

-
Names
-

The POIFS file system imposes two limitations on document and -directory names:

-
    -
  1. The names of documents and - directories must be unique within their containing directory. Pretty - obvious. -

    -
  2. Names are restricted to 31 characters. If you create a - directory or document with a name longer than that, it will be - silently truncated. When truncated, it may conflict with the name of - another directory or document, and the create operation will fail. -

    -
-
Why not Readers and Writers?
-

The POIFS file system uses Streams because HSSF, and virtually all -other applications that would use POIFS, deals with binary files, -which Streams handle correctly. Readers and Writers deal with text -and know how to handle 16-bit characters. If there is a demand for -providing support for Readers and Writers, let us know.

-

Here is some example code (excerpted and adapted from -net.sourceforge.poi.hssf.usermodel.Workbook class):

-
        byte[]     bytes        = getBytes();                                             // get the bytes for the document (elsewhere in the class)
-        FileOutputStream stream = new FileOutputStream("/home/reportsys/test/text.xls");  // create a new FileOuputStream
-        Filesystem fs           = new Filesystem();                                       // create a new POIFS Filesystem object
-        fs.createDocument(new ByteArrayInputStream(bytes), "Workbook");                   // create a new document in the root directory of the POIFS filesystem
-                                                                                          // close on ByteArrayInputStream is a no-op so we don't bother, no real file handle is used
-        fs.writeFilesystem(stream);                                                       // write the filesystem to the output stream.
-        Stream.close();                                                                   // close our stream (don't leak file handles its bad news)

-Reading or modifying an existing file

-

Reading in an exising POIFS file system is equally simple. Create -a new instance of net.sourceforge.poi.poifs.filesystem.Filesystem -by calling the Filesystem(java.io.InputStream) -constructor and passing in your file system's data (this would -probably be a FileInputStream , but it doesn't matter). -From there you can get documents from the root directory by calling -Filesystem.createDocumentInputStream(name) and passing a -string representing that document's name.

-

If you wish to walk the filesystem, the easiest thing to do is -DirectoryEntry.getEntries(). This will give you a -java.util.Iterator of Entry instances -(DirectoryEntry and DocumentEntry are -extensions of Entry) contained by the DirectoryEntry -. For instance you could call Filesystem.getRoot() to -retrieve a DirectoryEntry instance. From there you could -call DirectoryEntry.getEntries() and retrieve an -Iterator of those entries. Iterating through these -entries, you'd call getName() to check the name of the -entry and isDocumentEntry() or isDirectoryEntry() -to determine its type. Going the other way, given an Entry, -you can walk back up the directory chain by calling getParent(), -which returns the Entry's containing DirectoryEntry -(calling getParent() on the root directory returns a -null reference).

-

With a DocumentEntry, you can create an instance of -net.sourceforge.poi.poifs.filesystem.DocumentInputStream -, by passing the DocumentEntry as the only argument to -the constructor of DocumentInputStream.The -DocumentInputStream class is a simple extension of -java.io.InputStream that fully supports the InputStream -API, including the mark , reset, and skip -methods, providing a form of random access I/O.

-

To modify the file you would simply walk through the entries and -follow the same instructions for writing a POIFS file system from -scratch. There are also methods to delete an Entry -(note: you cannot delete the root directory, nor can you delete a -DirectoryEntry unless it's empty) and to rename an Entry -(but see the notes above). -

-

POIFS Logging facility

-

POIFS does not yet use log4j style logging.

-

Here is an example -

-
Paste log config example

-POIFS Developer's Tools

-

POIFS does not yet have developer's tools. -

-

What's Next?

-
    -
  1. Refactoring of the API to more - cleanly separate write from read. -

    -
  2. Add logging/tracing code -

    -
  3. Add tree viewer (probably Andy) -

    -
  4. Read/write support for creation and modification time stamps -

    -
-



-

- - \ No newline at end of file