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.

XWPFPictureData.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.xwpf.usermodel;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.util.Arrays;
  19. import org.apache.poi.ooxml.POIXMLDocumentPart;
  20. import org.apache.poi.ooxml.POIXMLException;
  21. import org.apache.poi.ooxml.POIXMLRelation;
  22. import org.apache.poi.openxml4j.opc.OPCPackage;
  23. import org.apache.poi.openxml4j.opc.PackagePart;
  24. import org.apache.poi.util.IOUtils;
  25. /**
  26. * Raw picture data, normally attached to a WordprocessingML Drawing.
  27. * As a rule, pictures are stored in the /word/media/ part of a WordprocessingML package.
  28. */
  29. public class XWPFPictureData extends POIXMLDocumentPart {
  30. private static final int DEFAULT_MAX_IMAGE_SIZE = 100_000_000;
  31. private static int MAX_IMAGE_SIZE = DEFAULT_MAX_IMAGE_SIZE;
  32. /**
  33. * @param length the max image size allowed for XSSF pictures
  34. */
  35. public static void setMaxImageSize(int length) {
  36. MAX_IMAGE_SIZE = length;
  37. }
  38. /**
  39. * @return the max image size allowed for XSSF pictures
  40. */
  41. public static int getMaxImageSize() {
  42. return MAX_IMAGE_SIZE;
  43. }
  44. /**
  45. * Relationships for each known picture type
  46. */
  47. protected static final POIXMLRelation[] RELATIONS;
  48. static {
  49. RELATIONS = new POIXMLRelation[13];
  50. RELATIONS[Document.PICTURE_TYPE_EMF] = XWPFRelation.IMAGE_EMF;
  51. RELATIONS[Document.PICTURE_TYPE_WMF] = XWPFRelation.IMAGE_WMF;
  52. RELATIONS[Document.PICTURE_TYPE_PICT] = XWPFRelation.IMAGE_PICT;
  53. RELATIONS[Document.PICTURE_TYPE_JPEG] = XWPFRelation.IMAGE_JPEG;
  54. RELATIONS[Document.PICTURE_TYPE_PNG] = XWPFRelation.IMAGE_PNG;
  55. RELATIONS[Document.PICTURE_TYPE_DIB] = XWPFRelation.IMAGE_DIB;
  56. RELATIONS[Document.PICTURE_TYPE_GIF] = XWPFRelation.IMAGE_GIF;
  57. RELATIONS[Document.PICTURE_TYPE_TIFF] = XWPFRelation.IMAGE_TIFF;
  58. RELATIONS[Document.PICTURE_TYPE_EPS] = XWPFRelation.IMAGE_EPS;
  59. RELATIONS[Document.PICTURE_TYPE_BMP] = XWPFRelation.IMAGE_BMP;
  60. RELATIONS[Document.PICTURE_TYPE_WPG] = XWPFRelation.IMAGE_WPG;
  61. }
  62. private Long checksum;
  63. /**
  64. * Create a new XWPFGraphicData node
  65. */
  66. protected XWPFPictureData() {
  67. super();
  68. }
  69. /**
  70. * Construct XWPFPictureData from a package part
  71. *
  72. * @param part the package part holding the drawing data,
  73. *
  74. * @since POI 3.14-Beta1
  75. */
  76. public XWPFPictureData(PackagePart part) {
  77. super(part);
  78. }
  79. @Override
  80. protected void onDocumentRead() throws IOException {
  81. super.onDocumentRead();
  82. }
  83. /**
  84. * Gets the picture data as a byte array.
  85. * <p>
  86. * Note, that this call might be expensive since all the picture data is copied into a temporary byte array.
  87. * You can grab the picture data directly from the underlying package part as follows:
  88. * <br>
  89. * <code>
  90. * InputStream is = getPackagePart().getInputStream();
  91. * </code>
  92. * </p>
  93. *
  94. * @return the Picture data.
  95. */
  96. public byte[] getData() {
  97. try (InputStream stream = getPackagePart().getInputStream()) {
  98. return IOUtils.toByteArrayWithMaxLength(stream, getMaxImageSize());
  99. } catch (IOException e) {
  100. throw new POIXMLException(e);
  101. }
  102. }
  103. /**
  104. * Returns the file name of the image, eg image7.jpg . The original filename
  105. * isn't always available, but if it can be found it's likely to be in the
  106. * CTDrawing
  107. */
  108. public String getFileName() {
  109. String name = getPackagePart().getPartName().getName();
  110. return name.substring(name.lastIndexOf('/') + 1);
  111. }
  112. /**
  113. * Suggests a file extension for this image.
  114. *
  115. * @return the file extension.
  116. */
  117. public String suggestFileExtension() {
  118. return getPackagePart().getPartName().getExtension();
  119. }
  120. /**
  121. * Return an integer constant that specifies type of this picture
  122. *
  123. * @return an integer constant that specifies type of this picture, returns 0 if an unknown type
  124. * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_EMF
  125. * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_WMF
  126. * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_PICT
  127. * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_JPEG
  128. * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_PNG
  129. * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_DIB
  130. */
  131. public int getPictureType() {
  132. String contentType = getPackagePart().getContentType();
  133. for (int i = 0; i < RELATIONS.length; i++) {
  134. if (RELATIONS[i] == null) {
  135. continue;
  136. }
  137. if (RELATIONS[i].getContentType().equals(contentType)) {
  138. return i;
  139. }
  140. }
  141. return 0;
  142. }
  143. public Long getChecksum() {
  144. if (this.checksum == null) {
  145. byte[] data;
  146. try (InputStream is = getPackagePart().getInputStream()) {
  147. data = IOUtils.toByteArrayWithMaxLength(is, getMaxImageSize());
  148. } catch (IOException e) {
  149. throw new POIXMLException(e);
  150. }
  151. this.checksum = IOUtils.calculateChecksum(data);
  152. }
  153. return this.checksum;
  154. }
  155. @Override
  156. public boolean equals(Object obj) {
  157. /*
  158. * In case two objects ARE equal, but its not the same instance, this
  159. * implementation will always run through the whole
  160. * byte-array-comparison before returning true. If this will turn into a
  161. * performance issue, two possible approaches are available:<br>
  162. * a) Use the checksum only and take the risk that two images might have
  163. * the same CRC32 sum, although they are not the same.<br>
  164. * b) Use a second (or third) checksum algorithm to minimise the chance
  165. * that two images have the same checksums but are not equal (e.g.
  166. * CRC32, MD5 and SHA-1 checksums, additionally compare the
  167. * data-byte-array lengths).
  168. */
  169. if (obj == this) {
  170. return true;
  171. }
  172. if (obj == null) {
  173. return false;
  174. }
  175. if (!(obj instanceof XWPFPictureData)) {
  176. return false;
  177. }
  178. XWPFPictureData picData = (XWPFPictureData) obj;
  179. PackagePart foreignPackagePart = picData.getPackagePart();
  180. PackagePart ownPackagePart = this.getPackagePart();
  181. if ((foreignPackagePart != null && ownPackagePart == null)
  182. || (foreignPackagePart == null && ownPackagePart != null)) {
  183. return false;
  184. }
  185. if (ownPackagePart != null) {
  186. OPCPackage foreignPackage = foreignPackagePart.getPackage();
  187. OPCPackage ownPackage = ownPackagePart.getPackage();
  188. if ((foreignPackage != null && ownPackage == null)
  189. || (foreignPackage == null && ownPackage != null)) {
  190. return false;
  191. }
  192. if (ownPackage != null) {
  193. if (!ownPackage.equals(foreignPackage)) {
  194. return false;
  195. }
  196. }
  197. }
  198. Long foreignChecksum = picData.getChecksum();
  199. Long localChecksum = getChecksum();
  200. if (localChecksum == null) {
  201. if (foreignChecksum != null) {
  202. return false;
  203. }
  204. } else {
  205. if (!(localChecksum.equals(foreignChecksum))) {
  206. return false;
  207. }
  208. }
  209. return Arrays.equals(this.getData(), picData.getData());
  210. }
  211. @Override
  212. public int hashCode() {
  213. Long checksum = getChecksum();
  214. return checksum == null ? super.hashCode() : checksum.hashCode();
  215. }
  216. /**
  217. * *PictureData objects store the actual content in the part directly without keeping a
  218. * copy like all others therefore we need to handle them differently.
  219. */
  220. @Override
  221. protected void prepareForCommit() {
  222. // do not clear the part here
  223. }
  224. }