01: /* ====================================================================
02: Copyright 2002-2004 Apache Software Foundation
03:
04: Licensed under the Apache License, Version 2.0 (the "License");
05: you may not use this file except in compliance with the License.
06: You may obtain a copy of the License at
07:
08: http://www.apache.org/licenses/LICENSE-2.0
09:
10: Unless required by applicable law or agreed to in writing, software
11: distributed under the License is distributed on an "AS IS" BASIS,
12: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: See the License for the specific language governing permissions and
14: limitations under the License.
15: ==================================================================== */
16:
17: package org.apache.poi.hssf.usermodel;
18:
19: import java.io.IOException;
20: import java.util.Iterator;
21:
22: import org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord;
23: import org.apache.poi.hssf.record.ObjRecord;
24: import org.apache.poi.poifs.filesystem.DirectoryEntry;
25: import org.apache.poi.poifs.filesystem.Entry;
26: import org.apache.poi.poifs.filesystem.POIFSFileSystem;
27: import org.apache.poi.util.HexDump;
28:
29: /**
30: * Represents binary object (i.e. OLE) data stored in the file. Eg. A GIF, JPEG etc...
31: *
32: * @author Daniel Noll
33: */
34: public class HSSFObjectData {
35: /**
36: * Underlying object record ultimately containing a reference to the object.
37: */
38: private ObjRecord record;
39:
40: /**
41: * Reference to the filesystem, required for retrieving the object data.
42: */
43: private POIFSFileSystem poifs;
44:
45: /**
46: * Constructs object data by wrapping a lower level object record.
47: *
48: * @param record the low-level object record.
49: * @param poifs the filesystem, required for retrieving the object data.
50: */
51: public HSSFObjectData(ObjRecord record, POIFSFileSystem poifs) {
52: this .record = record;
53: this .poifs = poifs;
54: }
55:
56: /**
57: * Gets the object data.
58: *
59: * @return the object data as an OLE2 directory.
60: * @throws IOException if there was an error reading the data.
61: */
62: public DirectoryEntry getDirectory() throws IOException {
63: Iterator subRecordIter = record.getSubRecords().iterator();
64: while (subRecordIter.hasNext()) {
65: Object subRecord = subRecordIter.next();
66: if (subRecord instanceof EmbeddedObjectRefSubRecord) {
67: int streamId = ((EmbeddedObjectRefSubRecord) subRecord)
68: .getStreamId();
69: String streamName = "MBD" + HexDump.toHex(streamId);
70:
71: Entry entry = poifs.getRoot().getEntry(streamName);
72: if (entry instanceof DirectoryEntry) {
73: return (DirectoryEntry) entry;
74: } else {
75: throw new IOException("Stream " + streamName
76: + " was not an OLE2 directory");
77: }
78: }
79: }
80:
81: throw new IllegalStateException(
82: "Object data does not contain a reference to an embedded object OLE2 directory");
83: }
84: }
|