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 9.3KB

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