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.

EscherContainerRecord.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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.ddf;
  16. import java.io.PrintWriter;
  17. import java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.function.Supplier;
  23. import org.apache.logging.log4j.LogManager;
  24. import org.apache.logging.log4j.Logger;
  25. import org.apache.poi.util.GenericRecordUtil;
  26. import org.apache.poi.util.HexDump;
  27. import org.apache.poi.util.LittleEndian;
  28. import static org.apache.logging.log4j.util.Unbox.box;
  29. /**
  30. * Escher container records store other escher records as children.
  31. * The container records themselves never store any information beyond
  32. * the standard header used by all escher records. This one record is
  33. * used to represent many different types of records.
  34. */
  35. public final class EscherContainerRecord extends EscherRecord implements Iterable<EscherRecord> {
  36. public static final short DGG_CONTAINER = EscherRecordTypes.DGG_CONTAINER.typeID;
  37. public static final short BSTORE_CONTAINER = EscherRecordTypes.BSTORE_CONTAINER.typeID;
  38. public static final short DG_CONTAINER = EscherRecordTypes.DG_CONTAINER.typeID;
  39. public static final short SPGR_CONTAINER = EscherRecordTypes.SPGR_CONTAINER.typeID;
  40. public static final short SP_CONTAINER = EscherRecordTypes.SP_CONTAINER.typeID;
  41. public static final short SOLVER_CONTAINER = EscherRecordTypes.SOLVER_CONTAINER.typeID;
  42. private static final Logger LOGGER = LogManager.getLogger(EscherContainerRecord.class);
  43. /**
  44. * in case if document contains any charts we have such document structure:
  45. * BOF
  46. * ...
  47. * DrawingRecord
  48. * ...
  49. * ObjRecord|TxtObjRecord
  50. * ...
  51. * EOF
  52. * ...
  53. * BOF(Chart begin)
  54. * ...
  55. * DrawingRecord
  56. * ...
  57. * ObjRecord|TxtObjRecord
  58. * ...
  59. * EOF
  60. * So, when we call EscherAggregate.createAggregate() we have not all needed data.
  61. * When we got warning "WARNING: " + bytesRemaining + " bytes remaining but no space left"
  62. * we should save value of bytesRemaining
  63. * and add it to container size when we serialize it
  64. */
  65. private int _remainingLength;
  66. private final List<EscherRecord> _childRecords = new ArrayList<>();
  67. public EscherContainerRecord() {}
  68. public EscherContainerRecord(EscherContainerRecord other) {
  69. super(other);
  70. _remainingLength = other._remainingLength;
  71. other._childRecords.stream().map(EscherRecord::copy).forEach(_childRecords::add);
  72. }
  73. @Override
  74. public int fillFields(byte[] data, int pOffset, EscherRecordFactory recordFactory) {
  75. int bytesRemaining = readHeader(data, pOffset);
  76. int bytesWritten = 8;
  77. int offset = pOffset + 8;
  78. while (bytesRemaining > 0 && offset < data.length) {
  79. EscherRecord child = recordFactory.createRecord(data, offset);
  80. int childBytesWritten = child.fillFields(data, offset, recordFactory);
  81. bytesWritten += childBytesWritten;
  82. offset += childBytesWritten;
  83. bytesRemaining -= childBytesWritten;
  84. addChildRecord(child);
  85. if (offset >= data.length && bytesRemaining > 0) {
  86. _remainingLength = bytesRemaining;
  87. LOGGER.atWarn().log("Not enough Escher data: {} bytes remaining but no space left", box(bytesRemaining));
  88. }
  89. }
  90. return bytesWritten;
  91. }
  92. @Override
  93. public int serialize( int offset, byte[] data, EscherSerializationListener listener )
  94. {
  95. listener.beforeRecordSerialize( offset, getRecordId(), this );
  96. LittleEndian.putShort(data, offset, getOptions());
  97. LittleEndian.putShort(data, offset+2, getRecordId());
  98. int remainingBytes = 0;
  99. for (EscherRecord r : this) {
  100. remainingBytes += r.getRecordSize();
  101. }
  102. remainingBytes += _remainingLength;
  103. LittleEndian.putInt(data, offset+4, remainingBytes);
  104. int pos = offset+8;
  105. for (EscherRecord r : this) {
  106. pos += r.serialize(pos, data, listener );
  107. }
  108. listener.afterRecordSerialize( pos, getRecordId(), pos - offset, this );
  109. return pos - offset;
  110. }
  111. @Override
  112. public int getRecordSize() {
  113. int childRecordsSize = 0;
  114. for (EscherRecord r : this) {
  115. childRecordsSize += r.getRecordSize();
  116. }
  117. return 8 + childRecordsSize;
  118. }
  119. /**
  120. * Do any of our (top level) children have the given recordId?
  121. *
  122. * @param recordId the recordId of the child
  123. *
  124. * @return true, if any child has the given recordId
  125. */
  126. public boolean hasChildOfType(short recordId) {
  127. return _childRecords.stream().anyMatch(r -> r.getRecordId() == recordId);
  128. }
  129. @Override
  130. public EscherRecord getChild( int index ) {
  131. return _childRecords.get(index);
  132. }
  133. /**
  134. * @return a copy of the list of all the child records of the container.
  135. */
  136. @Override
  137. public List<EscherRecord> getChildRecords() {
  138. return new ArrayList<>(_childRecords);
  139. }
  140. /**
  141. * @return an iterator over the child records
  142. */
  143. @Override
  144. public Iterator<EscherRecord> iterator() {
  145. return Collections.unmodifiableList(_childRecords).iterator();
  146. }
  147. /**
  148. * replaces the internal child list with the contents of the supplied <tt>childRecords</tt>
  149. */
  150. @Override
  151. public void setChildRecords(List<EscherRecord> childRecords) {
  152. if (childRecords == _childRecords) {
  153. throw new IllegalStateException("Child records private data member has escaped");
  154. }
  155. _childRecords.clear();
  156. _childRecords.addAll(childRecords);
  157. }
  158. /**
  159. * Removes the given escher record from the child list
  160. *
  161. * @param toBeRemoved the escher record to be removed
  162. * @return true, if the record was found and removed
  163. */
  164. public boolean removeChildRecord(EscherRecord toBeRemoved) {
  165. return _childRecords.remove(toBeRemoved);
  166. }
  167. /**
  168. * Returns all of our children which are also
  169. * EscherContainers (may be 0, 1, or vary rarely 2 or 3)
  170. *
  171. * @return EscherContainer children
  172. */
  173. public List<EscherContainerRecord> getChildContainers() {
  174. List<EscherContainerRecord> containers = new ArrayList<>();
  175. for (EscherRecord r : this) {
  176. if(r instanceof EscherContainerRecord) {
  177. containers.add((EscherContainerRecord) r);
  178. }
  179. }
  180. return containers;
  181. }
  182. @Override
  183. public String getRecordName() {
  184. final short id = getRecordId();
  185. EscherRecordTypes t = EscherRecordTypes.forTypeID(id);
  186. return (t != EscherRecordTypes.UNKNOWN) ? t.recordName : "Container 0x" + HexDump.toHex(id);
  187. }
  188. @Override
  189. public void display(PrintWriter w, int indent) {
  190. super.display(w, indent);
  191. for (EscherRecord escherRecord : this) {
  192. escherRecord.display(w, indent + 1);
  193. }
  194. }
  195. /**
  196. * Append a child record
  197. *
  198. * @param record the record to be added
  199. */
  200. public void addChildRecord(EscherRecord record) {
  201. _childRecords.add(record);
  202. }
  203. /**
  204. * Add a child record before the record with given recordId
  205. *
  206. * @param record the record to be added
  207. * @param insertBeforeRecordId the recordId of the next sibling
  208. */
  209. public void addChildBefore(EscherRecord record, int insertBeforeRecordId) {
  210. int idx = 0;
  211. for (EscherRecord rec : this) {
  212. if(rec.getRecordId() == (short)insertBeforeRecordId) {
  213. break;
  214. }
  215. // TODO - keep looping? Do we expect multiple matches?
  216. idx++;
  217. }
  218. _childRecords.add(idx, record);
  219. }
  220. public <T extends EscherRecord> T getChildById( short recordId ) {
  221. for ( EscherRecord childRecord : this ) {
  222. if ( childRecord.getRecordId() == recordId ) {
  223. @SuppressWarnings( "unchecked" )
  224. final T result = (T) childRecord;
  225. return result;
  226. }
  227. }
  228. return null;
  229. }
  230. /**
  231. * Recursively find records with the specified record ID
  232. *
  233. * @param recordId the recordId to be searched for
  234. * @param out - list to store found records
  235. */
  236. public void getRecordsById(short recordId, List<EscherRecord> out){
  237. for (EscherRecord r : this) {
  238. if(r instanceof EscherContainerRecord) {
  239. EscherContainerRecord c = (EscherContainerRecord)r;
  240. c.getRecordsById(recordId, out );
  241. } else if (r.getRecordId() == recordId){
  242. out.add(r);
  243. }
  244. }
  245. }
  246. @Override
  247. public Map<String, Supplier<?>> getGenericProperties() {
  248. return GenericRecordUtil.getGenericProperties(
  249. "base", super::getGenericProperties,
  250. "isContainer", this::isContainerRecord
  251. );
  252. }
  253. @Override
  254. public Enum getGenericRecordType() {
  255. return EscherRecordTypes.forTypeID(getRecordId());
  256. }
  257. @Override
  258. public EscherContainerRecord copy() {
  259. return new EscherContainerRecord(this);
  260. }
  261. }