KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > filesystem > POIFSFileSystem


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.poifs.filesystem;
20
21 import java.io.*;
22
23 import java.util.*;
24
25 import org.apache.poi.poifs.common.POIFSConstants;
26 import org.apache.poi.poifs.dev.POIFSViewable;
27 import org.apache.poi.poifs.property.DirectoryProperty;
28 import org.apache.poi.poifs.property.DocumentProperty;
29 import org.apache.poi.poifs.property.Property;
30 import org.apache.poi.poifs.property.PropertyTable;
31 import org.apache.poi.poifs.storage.BATBlock;
32 import org.apache.poi.poifs.storage.BlockAllocationTableReader;
33 import org.apache.poi.poifs.storage.BlockAllocationTableWriter;
34 import org.apache.poi.poifs.storage.BlockList;
35 import org.apache.poi.poifs.storage.BlockWritable;
36 import org.apache.poi.poifs.storage.HeaderBlockReader;
37 import org.apache.poi.poifs.storage.HeaderBlockWriter;
38 import org.apache.poi.poifs.storage.RawDataBlock;
39 import org.apache.poi.poifs.storage.RawDataBlockList;
40 import org.apache.poi.poifs.storage.SmallBlockTableReader;
41 import org.apache.poi.poifs.storage.SmallBlockTableWriter;
42 import org.apache.poi.poifs.storage.SmallDocumentBlock;
43
44 /**
45  * This is the main class of the POIFS system; it manages the entire
46  * life cycle of the filesystem.
47  *
48  * @author Marc Johnson (mjohnson at apache dot org)
49  */

50
51 public class POIFSFileSystem
52     implements POIFSViewable
53 {
54     private PropertyTable _property_table;
55     private List _documents;
56     private DirectoryNode _root;
57
58     /**
59      * Constructor, intended for writing
60      */

61
62     public POIFSFileSystem()
63     {
64         _property_table = new PropertyTable();
65         _documents = new ArrayList();
66         _root = null;
67     }
68
69     /**
70      * Create a POIFSFileSystem from an InputStream
71      *
72      * @param stream the InputStream from which to read the data
73      *
74      * @exception IOException on errors reading, or on invalid data
75      */

76
77     public POIFSFileSystem(final InputStream stream)
78         throws IOException
79     {
80         this();
81
82         // read the header block from the stream
83
HeaderBlockReader header_block_reader = new HeaderBlockReader(stream);
84
85         // read the rest of the stream into blocks
86
RawDataBlockList data_blocks = new RawDataBlockList(stream);
87
88         // set up the block allocation table (necessary for the
89
// data_blocks to be manageable
90
new BlockAllocationTableReader(header_block_reader.getBATCount(),
91                                        header_block_reader.getBATArray(),
92                                        header_block_reader.getXBATCount(),
93                                        header_block_reader.getXBATIndex(),
94                                        data_blocks);
95
96         // get property table from the document
97
PropertyTable properties =
98             new PropertyTable(header_block_reader.getPropertyStart(),
99                               data_blocks);
100
101         // init documents
102
processProperties(SmallBlockTableReader
103             .getSmallDocumentBlocks(data_blocks, properties
104                 .getRoot(), header_block_reader
105                     .getSBATStart()), data_blocks, properties.getRoot()
106                         .getChildren(), null);
107     }
108
109     /**
110      * Create a new document to be added to the root directory
111      *
112      * @param stream the InputStream from which the document's data
113      * will be obtained
114      * @param name the name of the new POIFSDocument
115      *
116      * @return the new DocumentEntry
117      *
118      * @exception IOException on error creating the new POIFSDocument
119      */

120
121     public DocumentEntry createDocument(final InputStream stream,
122                                         final String JavaDoc name)
123         throws IOException
124     {
125         return getRoot().createDocument(name, stream);
126     }
127
128     /**
129      * create a new DocumentEntry in the root entry; the data will be
130      * provided later
131      *
132      * @param name the name of the new DocumentEntry
133      * @param size the size of the new DocumentEntry
134      * @param writer the writer of the new DocumentEntry
135      *
136      * @return the new DocumentEntry
137      *
138      * @exception IOException
139      */

140
141     public DocumentEntry createDocument(final String JavaDoc name, final int size,
142                                         final POIFSWriterListener writer)
143         throws IOException
144     {
145         return getRoot().createDocument(name, size, writer);
146     }
147
148     /**
149      * create a new DirectoryEntry in the root directory
150      *
151      * @param name the name of the new DirectoryEntry
152      *
153      * @return the new DirectoryEntry
154      *
155      * @exception IOException on name duplication
156      */

157
158     public DirectoryEntry createDirectory(final String JavaDoc name)
159         throws IOException
160     {
161         return getRoot().createDirectory(name);
162     }
163
164     /**
165      * Write the filesystem out
166      *
167      * @param stream the OutputStream to which the filesystem will be
168      * written
169      *
170      * @exception IOException thrown on errors writing to the stream
171      */

172
173     public void writeFilesystem(final OutputStream stream)
174         throws IOException
175     {
176
177         // get the property table ready
178
_property_table.preWrite();
179
180         // create the small block store, and the SBAT
181
SmallBlockTableWriter sbtw =
182             new SmallBlockTableWriter(_documents, _property_table.getRoot());
183
184         // create the block allocation table
185
BlockAllocationTableWriter bat =
186             new BlockAllocationTableWriter();
187
188         // create a list of BATManaged objects: the documents plus the
189
// property table and the small block table
190
List bm_objects = new ArrayList();
191
192         bm_objects.addAll(_documents);
193         bm_objects.add(_property_table);
194         bm_objects.add(sbtw);
195         bm_objects.add(sbtw.getSBAT());
196
197         // walk the list, allocating space for each and assigning each
198
// a starting block number
199
Iterator iter = bm_objects.iterator();
200
201         while (iter.hasNext())
202         {
203             BATManaged bmo = ( BATManaged ) iter.next();
204             int block_count = bmo.countBlocks();
205
206             if (block_count != 0)
207             {
208                 bmo.setStartBlock(bat.allocateSpace(block_count));
209             }
210             else
211             {
212
213                 // Either the BATManaged object is empty or its data
214
// is composed of SmallBlocks; in either case,
215
// allocating space in the BAT is inappropriate
216
}
217         }
218
219         // allocate space for the block allocation table and take its
220
// starting block
221
int batStartBlock = bat.createBlocks();
222
223         // get the extended block allocation table blocks
224
HeaderBlockWriter header_block_writer = new HeaderBlockWriter();
225         BATBlock[] xbat_blocks =
226             header_block_writer.setBATBlocks(bat.countBlocks(),
227                                              batStartBlock);
228
229         // set the property table start block
230
header_block_writer.setPropertyStart(_property_table.getStartBlock());
231
232         // set the small block allocation table start block
233
header_block_writer.setSBATStart(sbtw.getSBAT().getStartBlock());
234
235         // set the small block allocation table block count
236
header_block_writer.setSBATBlockCount(sbtw.getSBATBlockCount());
237
238         // the header is now properly initialized. Make a list of
239
// writers (the header block, followed by the documents, the
240
// property table, the small block store, the small block
241
// allocation table, the block allocation table, and the
242
// extended block allocation table blocks)
243
List writers = new ArrayList();
244
245         writers.add(header_block_writer);
246         writers.addAll(_documents);
247         writers.add(_property_table);
248         writers.add(sbtw);
249         writers.add(sbtw.getSBAT());
250         writers.add(bat);
251         for (int j = 0; j < xbat_blocks.length; j++)
252         {
253             writers.add(xbat_blocks[ j ]);
254         }
255
256         // now, write everything out
257
iter = writers.iterator();
258         while (iter.hasNext())
259         {
260             BlockWritable writer = ( BlockWritable ) iter.next();
261
262             writer.writeBlocks(stream);
263         }
264     }
265
266     /**
267      * read in a file and write it back out again
268      *
269      * @param args names of the files; arg[ 0 ] is the input file,
270      * arg[ 1 ] is the output file
271      *
272      * @exception IOException
273      */

274
275     public static void main(String JavaDoc args[])
276         throws IOException
277     {
278         if (args.length != 2)
279         {
280             System.err.println(
281                 "two arguments required: input filename and output filename");
282             System.exit(1);
283         }
284         FileInputStream istream = new FileInputStream(args[ 0 ]);
285         FileOutputStream ostream = new FileOutputStream(args[ 1 ]);
286
287         new POIFSFileSystem(istream).writeFilesystem(ostream);
288         istream.close();
289         ostream.close();
290     }
291
292     /**
293      * get the root entry
294      *
295      * @return the root entry
296      */

297
298     public DirectoryEntry getRoot()
299     {
300         if (_root == null)
301         {
302             _root = new DirectoryNode(_property_table.getRoot(), this, null);
303         }
304         return _root;
305     }
306
307     /**
308      * open a document in the root entry's list of entries
309      *
310      * @param documentName the name of the document to be opened
311      *
312      * @return a newly opened DocumentInputStream
313      *
314      * @exception IOException if the document does not exist or the
315      * name is that of a DirectoryEntry
316      */

317
318     public DocumentInputStream createDocumentInputStream(
319             final String JavaDoc documentName)
320         throws IOException
321     {
322         Entry document = getRoot().getEntry(documentName);
323
324         if (!document.isDocumentEntry())
325         {
326             throw new IOException("Entry '" + documentName
327                                   + "' is not a DocumentEntry");
328         }
329         return new DocumentInputStream(( DocumentEntry ) document);
330     }
331
332     /**
333      * add a new POIFSDocument
334      *
335      * @param document the POIFSDocument being added
336      */

337
338     void addDocument(final POIFSDocument document)
339     {
340         _documents.add(document);
341         _property_table.addProperty(document.getDocumentProperty());
342     }
343
344     /**
345      * add a new DirectoryProperty
346      *
347      * @param directory the DirectoryProperty being added
348      */

349
350     void addDirectory(final DirectoryProperty directory)
351     {
352         _property_table.addProperty(directory);
353     }
354
355     /**
356      * remove an entry
357      *
358      * @param entry to be removed
359      */

360
361     void remove(EntryNode entry)
362     {
363         _property_table.removeProperty(entry.getProperty());
364         if (entry.isDocumentEntry())
365         {
366             _documents.remove((( DocumentNode ) entry).getDocument());
367         }
368     }
369
370     private void processProperties(final BlockList small_blocks,
371                                    final BlockList big_blocks,
372                                    final Iterator properties,
373                                    final DirectoryNode dir)
374         throws IOException
375     {
376         while (properties.hasNext())
377         {
378             Property property = ( Property ) properties.next();
379             String JavaDoc name = property.getName();
380             DirectoryNode parent = (dir == null)
381                                      ? (( DirectoryNode ) getRoot())
382                                      : dir;
383
384             if (property.isDirectory())
385             {
386                 DirectoryNode new_dir =
387                     ( DirectoryNode ) parent.createDirectory(name);
388
389                 new_dir.setStorageClsid( property.getStorageClsid() );
390
391                 processProperties(
392                     small_blocks, big_blocks,
393                     (( DirectoryProperty ) property).getChildren(), new_dir);
394             }
395             else
396             {
397                 int startBlock = property.getStartBlock();
398                 int size = property.getSize();
399                 POIFSDocument document = null;
400
401                 if (property.shouldUseSmallBlocks())
402                 {
403                     document =
404                         new POIFSDocument(name, small_blocks
405                             .fetchBlocks(startBlock), size);
406                 }
407                 else
408                 {
409                     document =
410                         new POIFSDocument(name,
411                                           big_blocks.fetchBlocks(startBlock),
412                                           size);
413                 }
414                 parent.createDocument(document);
415             }
416         }
417     }
418
419     /* ********** START begin implementation of POIFSViewable ********** */
420
421     /**
422      * Get an array of objects, some of which may implement
423      * POIFSViewable
424      *
425      * @return an array of Object; may not be null, but may be empty
426      */

427
428     public Object JavaDoc [] getViewableArray()
429     {
430         if (preferArray())
431         {
432             return (( POIFSViewable ) getRoot()).getViewableArray();
433         }
434         else
435         {
436             return new Object JavaDoc[ 0 ];
437         }
438     }
439
440     /**
441      * Get an Iterator of objects, some of which may implement
442      * POIFSViewable
443      *
444      * @return an Iterator; may not be null, but may have an empty
445      * back end store
446      */

447
448     public Iterator getViewableIterator()
449     {
450         if (!preferArray())
451         {
452             return (( POIFSViewable ) getRoot()).getViewableIterator();
453         }
454         else
455         {
456             return Collections.EMPTY_LIST.iterator();
457         }
458     }
459
460     /**
461      * Give viewers a hint as to whether to call getViewableArray or
462      * getViewableIterator
463      *
464      * @return true if a viewer should call getViewableArray, false if
465      * a viewer should call getViewableIterator
466      */

467
468     public boolean preferArray()
469     {
470         return (( POIFSViewable ) getRoot()).preferArray();
471     }
472
473     /**
474      * Provides a short description of the object, to be used when a
475      * POIFSViewable object has not provided its contents.
476      *
477      * @return short description
478      */

479
480     public String JavaDoc getShortDescription()
481     {
482         return "POIFS FileSystem";
483     }
484
485     /* ********** END begin implementation of POIFSViewable ********** */
486 } // end public class POIFSFileSystem
487

488
Popular Tags