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.

XSSFObjectData.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.xssf.usermodel;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.PushbackInputStream;
  20. import javax.xml.namespace.QName;
  21. import org.apache.poi.POIXMLDocumentPart;
  22. import org.apache.poi.POIXMLException;
  23. import org.apache.poi.openxml4j.opc.PackagePart;
  24. import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
  25. import org.apache.poi.poifs.filesystem.DirectoryEntry;
  26. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  27. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  28. import org.apache.poi.ss.usermodel.ObjectData;
  29. import org.apache.poi.util.IOUtils;
  30. import org.apache.poi.util.POILogFactory;
  31. import org.apache.poi.util.POILogger;
  32. import org.apache.xmlbeans.XmlCursor;
  33. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShape;
  34. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTOleObject;
  35. /**
  36. * Represents binary object (i.e. OLE) data stored in the file. Eg. A GIF, JPEG etc...
  37. */
  38. public class XSSFObjectData extends XSSFSimpleShape implements ObjectData {
  39. private static final POILogger LOG = POILogFactory.getLogger(XSSFObjectData.class);
  40. /**
  41. * A default instance of CTShape used for creating new shapes.
  42. */
  43. private static CTShape prototype = null;
  44. private CTOleObject oleObject;
  45. protected XSSFObjectData(XSSFDrawing drawing, CTShape ctShape) {
  46. super(drawing, ctShape);
  47. }
  48. /**
  49. * Prototype with the default structure of a new auto-shape.
  50. */
  51. protected static CTShape prototype() {
  52. if(prototype == null) {
  53. prototype = XSSFSimpleShape.prototype();
  54. }
  55. return prototype;
  56. }
  57. @Override
  58. public String getOLE2ClassName() {
  59. return getOleObject().getProgId();
  60. }
  61. /**
  62. * @return the CTOleObject associated with the shape
  63. */
  64. public CTOleObject getOleObject() {
  65. if (oleObject == null) {
  66. long shapeId = getCTShape().getNvSpPr().getCNvPr().getId();
  67. oleObject = getSheet().readOleObject(shapeId);
  68. if (oleObject == null) {
  69. throw new POIXMLException("Ole object not found in sheet container - it's probably a control element");
  70. }
  71. }
  72. return oleObject;
  73. }
  74. @Override
  75. public byte[] getObjectData() throws IOException {
  76. InputStream is = getObjectPart().getInputStream();
  77. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  78. IOUtils.copy(is, bos);
  79. is.close();
  80. return bos.toByteArray();
  81. }
  82. /**
  83. * @return the package part of the object data
  84. */
  85. public PackagePart getObjectPart() {
  86. if (!getOleObject().isSetId()) {
  87. throw new POIXMLException("Invalid ole object found in sheet container");
  88. }
  89. POIXMLDocumentPart pdp = getSheet().getRelationById(getOleObject().getId());
  90. return (pdp == null) ? null : pdp.getPackagePart();
  91. }
  92. @Override
  93. public boolean hasDirectoryEntry() {
  94. InputStream is = null;
  95. try {
  96. is = getObjectPart().getInputStream();
  97. // If clearly doesn't do mark/reset, wrap up
  98. if (! is.markSupported()) {
  99. is = new PushbackInputStream(is, 8);
  100. }
  101. // Ensure that there is at least some data there
  102. byte[] header8 = IOUtils.peekFirst8Bytes(is);
  103. // Try to create
  104. return NPOIFSFileSystem.hasPOIFSHeader(header8);
  105. } catch (IOException e) {
  106. LOG.log(POILogger.WARN, "can't determine if directory entry exists", e);
  107. return false;
  108. } finally {
  109. IOUtils.closeQuietly(is);
  110. }
  111. }
  112. @Override
  113. @SuppressWarnings("resource")
  114. public DirectoryEntry getDirectory() throws IOException {
  115. InputStream is = null;
  116. try {
  117. is = getObjectPart().getInputStream();
  118. return new POIFSFileSystem(is).getRoot();
  119. } finally {
  120. IOUtils.closeQuietly(is);
  121. }
  122. }
  123. /**
  124. * The filename of the embedded image
  125. */
  126. @Override
  127. public String getFileName() {
  128. return getObjectPart().getPartName().getName();
  129. }
  130. protected XSSFSheet getSheet() {
  131. return (XSSFSheet)getDrawing().getParent();
  132. }
  133. @Override
  134. public XSSFPictureData getPictureData() {
  135. XmlCursor cur = getOleObject().newCursor();
  136. try {
  137. if (cur.toChild(XSSFRelation.NS_SPREADSHEETML, "objectPr")) {
  138. String blipId = cur.getAttributeText(new QName(PackageRelationshipTypes.CORE_PROPERTIES_ECMA376_NS, "id"));
  139. return (XSSFPictureData)getDrawing().getRelationById(blipId);
  140. }
  141. return null;
  142. } finally {
  143. cur.dispose();
  144. }
  145. }
  146. }