apache-poi/src/java/org/apache/poi/hssf/usermodel/HSSFObjectData.java
Josh Micich f55bd82eef Merged revisions 700479,700493,700916,701302,701569,701598,701747 via svnmerge from
https://svn.apache.org/repos/asf/poi/trunk

........
  r700479 | yegor | 2008-09-30 07:32:37 -0700 (Tue, 30 Sep 2008) | 1 line
  
  reverted the change made in r693085 , see bug #45859
........
  r700493 | yegor | 2008-09-30 08:11:26 -0700 (Tue, 30 Sep 2008) | 1 line
  
  initial support for creating hyperlinks in HSLF, units test are still to do
........
  r700916 | josh | 2008-10-01 13:56:21 -0700 (Wed, 01 Oct 2008) | 1 line
  
  Fixed bug in CellCacheEntry (support for caching blank evaluation results)
........
  r701302 | yegor | 2008-10-02 22:27:06 -0700 (Thu, 02 Oct 2008) | 1 line
  
  fixed bug #45889:rrayIndexOutOfBoundsException when constructing HSLF Table with a single row
........
  r701569 | josh | 2008-10-03 16:50:22 -0700 (Fri, 03 Oct 2008) | 1 line
  
  Fix for bug 45912 - ArrayIndexOutOfBoundsException in EmbeddedObjectRefSubRecord
........
  r701598 | josh | 2008-10-03 21:59:26 -0700 (Fri, 03 Oct 2008) | 1 line
  
  changed workbook reference to index in CellLocation
........
  r701747 | josh | 2008-10-04 21:43:48 -0700 (Sat, 04 Oct 2008) | 1 line
  
  Better bounds checking in RecordInputStream. Removed rarely used methods readShortArray and putShortArray
........


git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@703069 13f79535-47bb-0310-9956-ffa450edef68
2008-10-09 06:38:50 +00:00

126 lines
4.1 KiB
Java

/* ====================================================================
Copyright 2002-2004 Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord;
import org.apache.poi.hssf.record.ObjRecord;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.Entry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.HexDump;
/**
* Represents binary object (i.e. OLE) data stored in the file. Eg. A GIF, JPEG etc...
*
* @author Daniel Noll
*/
public final class HSSFObjectData
{
/**
* Underlying object record ultimately containing a reference to the object.
*/
private ObjRecord record;
/**
* Reference to the filesystem, required for retrieving the object data.
*/
private POIFSFileSystem poifs;
/**
* Constructs object data by wrapping a lower level object record.
*
* @param record the low-level object record.
* @param poifs the filesystem, required for retrieving the object data.
*/
public HSSFObjectData(ObjRecord record, POIFSFileSystem poifs)
{
this.record = record;
this.poifs = poifs;
}
/**
* Returns the OLE2 Class Name of the object
*/
public String getOLE2ClassName() {
return findObjectRecord().getOLEClassName();
}
/**
* Gets the object data. Only call for ones that have
* data though. See {@link #hasDirectoryEntry()}
*
* @return the object data as an OLE2 directory.
* @throws IOException if there was an error reading the data.
*/
public DirectoryEntry getDirectory() throws IOException {
EmbeddedObjectRefSubRecord subRecord = findObjectRecord();
int streamId = subRecord.getStreamId().intValue();
String streamName = "MBD" + HexDump.toHex(streamId);
Entry entry = poifs.getRoot().getEntry(streamName);
if (entry instanceof DirectoryEntry) {
return (DirectoryEntry) entry;
} else {
throw new IOException("Stream " + streamName + " was not an OLE2 directory");
}
}
/**
* Returns the data portion, for an ObjectData
* that doesn't have an associated POIFS Directory
* Entry
*/
public byte[] getObjectData() {
return findObjectRecord().getObjectData();
}
/**
* Does this ObjectData have an associated POIFS
* Directory Entry?
* (Not all do, those that don't have a data portion)
*/
public boolean hasDirectoryEntry() {
EmbeddedObjectRefSubRecord subRecord = findObjectRecord();
// 'stream id' field tells you
Integer streamId = subRecord.getStreamId();
return streamId != null && streamId.intValue() != 0;
}
/**
* Finds the EmbeddedObjectRefSubRecord, or throws an
* Exception if there wasn't one
*/
protected EmbeddedObjectRefSubRecord findObjectRecord() {
Iterator subRecordIter = record.getSubRecords().iterator();
while (subRecordIter.hasNext()) {
Object subRecord = subRecordIter.next();
if (subRecord instanceof EmbeddedObjectRefSubRecord) {
return (EmbeddedObjectRefSubRecord)subRecord;
}
}
throw new IllegalStateException("Object data does not contain a reference to an embedded object OLE2 directory");
}
}