You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HSSFObjectData.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.usermodel;
  16. import java.io.IOException;
  17. import java.util.Iterator;
  18. import org.apache.poi.ddf.*;
  19. import org.apache.poi.hssf.record.*;
  20. import org.apache.poi.poifs.filesystem.DirectoryEntry;
  21. import org.apache.poi.poifs.filesystem.Entry;
  22. import org.apache.poi.ss.usermodel.ObjectData;
  23. import org.apache.poi.util.HexDump;
  24. /**
  25. * Represents binary object (i.e. OLE) data stored in the file. Eg. A GIF, JPEG etc...
  26. * <p/>
  27. * Right now, 13, july, 2012 can not be created from scratch
  28. */
  29. public final class HSSFObjectData extends HSSFPicture implements ObjectData {
  30. /**
  31. * Reference to the filesystem root, required for retrieving the object data.
  32. */
  33. private final DirectoryEntry _root;
  34. public HSSFObjectData(EscherContainerRecord spContainer, ObjRecord objRecord, DirectoryEntry _root) {
  35. super(spContainer, objRecord);
  36. this._root = _root;
  37. }
  38. @Override
  39. public String getOLE2ClassName() {
  40. return findObjectRecord().getOLEClassName();
  41. }
  42. @Override
  43. public DirectoryEntry getDirectory() throws IOException {
  44. EmbeddedObjectRefSubRecord subRecord = findObjectRecord();
  45. int streamId = subRecord.getStreamId().intValue();
  46. String streamName = "MBD" + HexDump.toHex(streamId);
  47. Entry entry = _root.getEntry(streamName);
  48. if (entry instanceof DirectoryEntry) {
  49. return (DirectoryEntry) entry;
  50. }
  51. throw new IOException("Stream " + streamName + " was not an OLE2 directory");
  52. }
  53. @Override
  54. public byte[] getObjectData() {
  55. return findObjectRecord().getObjectData();
  56. }
  57. @Override
  58. public boolean hasDirectoryEntry() {
  59. EmbeddedObjectRefSubRecord subRecord = findObjectRecord();
  60. // 'stream id' field tells you
  61. Integer streamId = subRecord.getStreamId();
  62. return streamId != null && streamId.intValue() != 0;
  63. }
  64. /**
  65. * Finds the EmbeddedObjectRefSubRecord, or throws an
  66. * Exception if there wasn't one
  67. */
  68. protected EmbeddedObjectRefSubRecord findObjectRecord() {
  69. Iterator<SubRecord> subRecordIter = getObjRecord().getSubRecords().iterator();
  70. while (subRecordIter.hasNext()) {
  71. Object subRecord = subRecordIter.next();
  72. if (subRecord instanceof EmbeddedObjectRefSubRecord) {
  73. return (EmbeddedObjectRefSubRecord) subRecord;
  74. }
  75. }
  76. throw new IllegalStateException("Object data does not contain a reference to an embedded object OLE2 directory");
  77. }
  78. @Override
  79. protected EscherContainerRecord createSpContainer() {
  80. throw new IllegalStateException("HSSFObjectData cannot be created from scratch");
  81. }
  82. @Override
  83. protected ObjRecord createObjRecord() {
  84. throw new IllegalStateException("HSSFObjectData cannot be created from scratch");
  85. }
  86. @Override
  87. protected void afterRemove(HSSFPatriarch patriarch) {
  88. throw new IllegalStateException("HSSFObjectData cannot be created from scratch");
  89. }
  90. @Override
  91. void afterInsert(HSSFPatriarch patriarch) {
  92. EscherAggregate agg = patriarch.getBoundAggregate();
  93. agg.associateShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID), getObjRecord());
  94. EscherBSERecord bse =
  95. patriarch.getSheet().getWorkbook().getWorkbook().getBSERecord(getPictureIndex());
  96. bse.setRef(bse.getRef() + 1);
  97. }
  98. @Override
  99. protected HSSFShape cloneShape() {
  100. EscherContainerRecord spContainer = new EscherContainerRecord();
  101. byte[] inSp = getEscherContainer().serialize();
  102. spContainer.fillFields(inSp, 0, new DefaultEscherRecordFactory());
  103. ObjRecord obj = (ObjRecord) getObjRecord().cloneViaReserialise();
  104. return new HSSFObjectData(spContainer, obj, _root);
  105. }
  106. }