KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > property > PropertyTable


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.property;
20
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 import java.util.*;
25
26 import org.apache.poi.poifs.common.POIFSConstants;
27 import org.apache.poi.poifs.filesystem.BATManaged;
28 import org.apache.poi.poifs.storage.BlockWritable;
29 import org.apache.poi.poifs.storage.PropertyBlock;
30 import org.apache.poi.poifs.storage.RawDataBlock;
31 import org.apache.poi.poifs.storage.RawDataBlockList;
32
33 /**
34  * This class embodies the Property Table for the filesystem; this is
35  * basically the dsirectory for all of the documents in the
36  * filesystem.
37  *
38  * @author Marc Johnson (mjohnson at apache dot org)
39  */

40
41 public class PropertyTable
42     implements BATManaged, BlockWritable
43 {
44     private int _start_block;
45     private List _properties;
46     private BlockWritable[] _blocks;
47
48     /**
49      * Default constructor
50      */

51
52     public PropertyTable()
53     {
54         _start_block = POIFSConstants.END_OF_CHAIN;
55         _properties = new ArrayList();
56         addProperty(new RootProperty());
57         _blocks = null;
58     }
59
60     /**
61      * reading constructor (used when we've read in a file and we want
62      * to extract the property table from it). Populates the
63      * properties thoroughly
64      *
65      * @param startBlock the first block of the property table
66      * @param blockList the list of blocks
67      *
68      * @exception IOException if anything goes wrong (which should be
69      * a result of the input being NFG)
70      */

71
72     public PropertyTable(final int startBlock,
73                          final RawDataBlockList blockList)
74         throws IOException JavaDoc
75     {
76         _start_block = POIFSConstants.END_OF_CHAIN;
77         _blocks = null;
78         _properties =
79             PropertyFactory
80                 .convertToProperties(blockList.fetchBlocks(startBlock));
81         populatePropertyTree(( DirectoryProperty ) _properties.get(0));
82     }
83
84     /**
85      * Add a property to the list of properties we manage
86      *
87      * @param property the new Property to manage
88      */

89
90     public void addProperty(final Property property)
91     {
92         _properties.add(property);
93     }
94
95     /**
96      * Remove a property from the list of properties we manage
97      *
98      * @param property the Property to be removed
99      */

100
101     public void removeProperty(final Property property)
102     {
103         _properties.remove(property);
104     }
105
106     /**
107      * Get the root property
108      *
109      * @return the root property
110      */

111
112     public RootProperty getRoot()
113     {
114
115         // it's always the first element in the List
116
return ( RootProperty ) _properties.get(0);
117     }
118
119     /**
120      * Prepare to be written
121      */

122
123     public void preWrite()
124     {
125         Property[] properties =
126             ( Property [] ) _properties.toArray(new Property[ 0 ]);
127
128         // give each property its index
129
for (int k = 0; k < properties.length; k++)
130         {
131             properties[ k ].setIndex(k);
132         }
133
134         // allocate the blocks for the property table
135
_blocks = PropertyBlock.createPropertyBlockArray(_properties);
136
137         // prepare each property for writing
138
for (int k = 0; k < properties.length; k++)
139         {
140             properties[ k ].preWrite();
141         }
142     }
143
144     /**
145      * Get the start block for the property table
146      *
147      * @return start block index
148      */

149
150     public int getStartBlock()
151     {
152         return _start_block;
153     }
154
155     private void populatePropertyTree(DirectoryProperty root)
156         throws IOException JavaDoc
157     {
158         int index = root.getChildIndex();
159
160         if (!Property.isValidIndex(index))
161         {
162
163             // property has no children
164
return;
165         }
166         Stack children = new Stack();
167
168         children.push(_properties.get(index));
169         while (!children.empty())
170         {
171             Property property = ( Property ) children.pop();
172
173             root.addChild(property);
174             if (property.isDirectory())
175             {
176                 populatePropertyTree(( DirectoryProperty ) property);
177             }
178             index = property.getPreviousChildIndex();
179             if (Property.isValidIndex(index))
180             {
181                 children.push(_properties.get(index));
182             }
183             index = property.getNextChildIndex();
184             if (Property.isValidIndex(index))
185             {
186                 children.push(_properties.get(index));
187             }
188         }
189     }
190
191     /* ********** START implementation of BATManaged ********** */
192
193     /**
194      * Return the number of BigBlock's this instance uses
195      *
196      * @return count of BigBlock instances
197      */

198
199     public int countBlocks()
200     {
201         return (_blocks == null) ? 0
202                                  : _blocks.length;
203     }
204
205     /**
206      * Set the start block for this instance
207      *
208      * @param index index into the array of BigBlock instances making
209      * up the the filesystem
210      */

211
212     public void setStartBlock(final int index)
213     {
214         _start_block = index;
215     }
216
217     /* ********** END implementation of BATManaged ********** */
218     /* ********** START implementation of BlockWritable ********** */
219
220     /**
221      * Write the storage to an OutputStream
222      *
223      * @param stream the OutputStream to which the stored data should
224      * be written
225      *
226      * @exception IOException on problems writing to the specified
227      * stream
228      */

229
230     public void writeBlocks(final OutputStream JavaDoc stream)
231         throws IOException JavaDoc
232     {
233         if (_blocks != null)
234         {
235             for (int j = 0; j < _blocks.length; j++)
236             {
237                 _blocks[ j ].writeBlocks(stream);
238             }
239         }
240     }
241
242     /* ********** END implementation of BlockWritable ********** */
243 } // end public class PropertyTable
244

245
Popular Tags