01: /* ====================================================================
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: 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: package org.apache.poi.poifs.property;
19:
20: import java.io.IOException;
21:
22: import java.util.*;
23:
24: import org.apache.poi.poifs.common.POIFSConstants;
25: import org.apache.poi.poifs.storage.ListManagedBlock;
26:
27: /**
28: * Factory for turning an array of RawDataBlock instances containing
29: * Proprty data into an array of proper Property objects.
30: *
31: * The array produced may be sparse, in that any portion of data that
32: * should correspond to a Property, but which does not map to a proper
33: * Property (i.e., a DirectoryProperty, DocumentProperty, or
34: * RootProperty) will get mapped to a null Property in the array.
35: *
36: * @author Marc Johnson (mjohnson at apache dot org)
37: */
38:
39: class PropertyFactory {
40:
41: // no need for an accessible constructor
42: private PropertyFactory() {
43: }
44:
45: /**
46: * Convert raw data blocks to an array of Property's
47: *
48: * @param blocks to be converted
49: *
50: * @return the converted List of Property objects. May contain
51: * nulls, but will not be null
52: *
53: * @exception IOException if any of the blocks are empty
54: */
55:
56: static List convertToProperties(ListManagedBlock[] blocks)
57: throws IOException {
58: List properties = new ArrayList();
59:
60: for (int j = 0; j < blocks.length; j++) {
61: byte[] data = blocks[j].getData();
62: int property_count = data.length
63: / POIFSConstants.PROPERTY_SIZE;
64: int offset = 0;
65:
66: for (int k = 0; k < property_count; k++) {
67: switch (data[offset
68: + PropertyConstants.PROPERTY_TYPE_OFFSET]) {
69:
70: case PropertyConstants.DIRECTORY_TYPE:
71: properties.add(new DirectoryProperty(properties
72: .size(), data, offset));
73: break;
74:
75: case PropertyConstants.DOCUMENT_TYPE:
76: properties.add(new DocumentProperty(properties
77: .size(), data, offset));
78: break;
79:
80: case PropertyConstants.ROOT_TYPE:
81: properties.add(new RootProperty(properties.size(),
82: data, offset));
83: break;
84:
85: default:
86: properties.add(null);
87: break;
88: }
89: offset += POIFSConstants.PROPERTY_SIZE;
90: }
91: }
92: return properties;
93: }
94: } // end package scope class PropertyFactory
|