KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import java.util.*;
24
25 import org.apache.poi.poifs.common.POIFSConstants;
26 import org.apache.poi.poifs.storage.ListManagedBlock;
27
28 /**
29  * Factory for turning an array of RawDataBlock instances containing
30  * Proprty data into an array of proper Property objects.
31  *
32  * The array produced may be sparse, in that any portion of data that
33  * should correspond to a Property, but which does not map to a proper
34  * Property (i.e., a DirectoryProperty, DocumentProperty, or
35  * RootProperty) will get mapped to a null Property in the array.
36  *
37  * @author Marc Johnson (mjohnson at apache dot org)
38  */

39
40 class PropertyFactory
41 {
42
43     // no need for an accessible constructor
44
private PropertyFactory()
45     {
46     }
47
48     /**
49      * Convert raw data blocks to an array of Property's
50      *
51      * @param blocks to be converted
52      *
53      * @return the converted List of Property objects. May contain
54      * nulls, but will not be null
55      *
56      * @exception IOException if any of the blocks are empty
57      */

58
59     static List convertToProperties(ListManagedBlock [] blocks)
60         throws IOException JavaDoc
61     {
62         List properties = new ArrayList();
63
64         for (int j = 0; j < blocks.length; j++)
65         {
66             byte[] data = blocks[ j ].getData();
67             int property_count = data.length
68                                     / POIFSConstants.PROPERTY_SIZE;
69             int offset = 0;
70
71             for (int k = 0; k < property_count; k++)
72             {
73                 switch (data[ offset + PropertyConstants.PROPERTY_TYPE_OFFSET ])
74                 {
75
76                     case PropertyConstants.DIRECTORY_TYPE :
77                         properties
78                             .add(new DirectoryProperty(properties.size(),
79                                                        data, offset));
80                         break;
81
82                     case PropertyConstants.DOCUMENT_TYPE :
83                         properties.add(new DocumentProperty(properties.size(),
84                                                             data, offset));
85                         break;
86
87                     case PropertyConstants.ROOT_TYPE :
88                         properties.add(new RootProperty(properties.size(),
89                                                         data, offset));
90                         break;
91
92                     default :
93                         properties.add(null);
94                         break;
95                 }
96                 offset += POIFSConstants.PROPERTY_SIZE;
97             }
98         }
99         return properties;
100     }
101 } // end package scope class PropertyFactory
102

103
Popular Tags