選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

OfficeArtContent.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.hwpf.model;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import org.apache.logging.log4j.LogManager;
  19. import org.apache.poi.ddf.DefaultEscherRecordFactory;
  20. import org.apache.poi.ddf.EscherContainerRecord;
  21. import org.apache.poi.ddf.EscherRecord;
  22. import org.apache.poi.ddf.EscherRecordFactory;
  23. import org.apache.poi.ddf.EscherRecordTypes;
  24. import org.apache.poi.util.Internal;
  25. import static org.apache.logging.log4j.util.Unbox.box;
  26. /**
  27. * Information about drawings in the document.
  28. * <p>
  29. * The {@code delay stream} referenced in {@code [MS-ODRAW]} is the {@code WordDocument} stream.
  30. */
  31. @Internal
  32. public final class OfficeArtContent {
  33. /**
  34. * {@link EscherRecordTypes#DGG_CONTAINER} containing drawing group information for the document.
  35. */
  36. private final EscherContainerRecord drawingGroupData = new EscherContainerRecord();
  37. /**
  38. * {@link EscherRecordTypes#DG_CONTAINER} for drawings in the Main Document.
  39. * <p>
  40. * {@code null} to indicate that the document does not have a {@link EscherRecordTypes#DG_CONTAINER} for the Main
  41. * Document.
  42. */
  43. private EscherContainerRecord mainDocumentDgContainer;
  44. /**
  45. * {@link EscherRecordTypes#DG_CONTAINER} for drawings in the Header Document.
  46. * <p>
  47. * {@code null} to indicate that the document does not have a {@link EscherRecordTypes#DG_CONTAINER} for the Header
  48. * Document.
  49. */
  50. private EscherContainerRecord headerDocumentDgContainer;
  51. public OfficeArtContent(byte[] data, int offset, int size) {
  52. fillEscherRecords(data, offset, size);
  53. }
  54. /**
  55. * Parses the records out of the given data.
  56. *
  57. * The thing to be aware of here is that if {@code size} is {@code 0}, the document does not contain images.
  58. *
  59. * @see FileInformationBlock#getLcbDggInfo()
  60. */
  61. private void fillEscherRecords(byte[] data, int offset, int size) {
  62. if (size == 0) return;
  63. EscherRecordFactory recordFactory = new DefaultEscherRecordFactory();
  64. int pos = offset;
  65. pos += drawingGroupData.fillFields(data, pos, recordFactory);
  66. assert drawingGroupData.getRecordId() == EscherRecordTypes.DGG_CONTAINER.typeID;
  67. /*
  68. * After the drawingGroupData there is an array (2 slots max) that has data about drawings. According to the
  69. * spec, the first slot is for the Main Document, the second for the Header Document. Additionally, the
  70. * OfficeArtWordDrawing structure has a byte (dgglbl) that indicates whether the structure is for the Main or
  71. * Header Document. In practice we've seen documents such as 61911.doc where the order of array entries does not
  72. * match the dgglbl byte. As the byte is more likely to be reliable, we base the parsing off of that rather than
  73. * array order.
  74. */
  75. // This should loop at most twice
  76. while (pos < offset + size) {
  77. // Named this way to match section 2.9.172 of [MS-DOC] - v20191119.
  78. byte dgglbl = data[pos];
  79. assert dgglbl == 0x00 || dgglbl == 0x01;
  80. pos++;
  81. EscherContainerRecord dgContainer = new EscherContainerRecord();
  82. pos+= dgContainer.fillFields(data, pos, recordFactory);
  83. assert dgContainer.getRecordId() == EscherRecordTypes.DG_CONTAINER.typeID;
  84. switch (dgglbl) {
  85. case 0x00:
  86. mainDocumentDgContainer = dgContainer;
  87. break;
  88. case 0x01:
  89. headerDocumentDgContainer = dgContainer;
  90. break;
  91. default:
  92. LogManager.getLogger(OfficeArtContent.class).atWarn()
  93. .log("dgglbl {} for OfficeArtWordDrawing is out of bounds [0, 1]", box(dgglbl));
  94. }
  95. }
  96. assert pos == offset + size;
  97. }
  98. private List<? extends EscherContainerRecord> getDgContainers() {
  99. List<EscherContainerRecord> dgContainers = new ArrayList<>(2);
  100. if (mainDocumentDgContainer != null) {
  101. dgContainers.add(mainDocumentDgContainer);
  102. }
  103. if (headerDocumentDgContainer != null) {
  104. dgContainers.add(headerDocumentDgContainer);
  105. }
  106. return dgContainers;
  107. }
  108. /**
  109. * @return The {@link EscherRecordTypes#BSTORE_CONTAINER} or {@code null} if the document doesn't have one.
  110. */
  111. public EscherContainerRecord getBStoreContainer() {
  112. return drawingGroupData.getChildById(EscherRecordTypes.BSTORE_CONTAINER.typeID);
  113. }
  114. public List<? extends EscherContainerRecord> getSpgrContainers()
  115. {
  116. List<EscherContainerRecord> spgrContainers = new ArrayList<>(
  117. 1);
  118. for ( EscherContainerRecord dgContainer : getDgContainers() )
  119. {
  120. for ( EscherRecord escherRecord : dgContainer )
  121. {
  122. if ( escherRecord.getRecordId() == (short) 0xF003 )
  123. {
  124. spgrContainers.add( (EscherContainerRecord) escherRecord );
  125. }
  126. }
  127. }
  128. return spgrContainers;
  129. }
  130. public List<? extends EscherContainerRecord> getSpContainers()
  131. {
  132. List<EscherContainerRecord> spContainers = new ArrayList<>(
  133. 1);
  134. for ( EscherContainerRecord spgrContainer : getSpgrContainers() )
  135. {
  136. for ( EscherRecord escherRecord : spgrContainer )
  137. {
  138. if ( escherRecord.getRecordId() == (short) 0xF004 )
  139. {
  140. spContainers.add( (EscherContainerRecord) escherRecord );
  141. }
  142. }
  143. }
  144. return spContainers;
  145. }
  146. @Override
  147. public String toString() {
  148. return "OfficeArtContent{" +
  149. "drawingGroupData=" + drawingGroupData +
  150. ", mainDocumentDgContainer=" + mainDocumentDgContainer +
  151. ", headerDocumentDgContainer=" + headerDocumentDgContainer +
  152. '}';
  153. }
  154. }