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.

AbstractEscherHolderRecord.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.hssf.record;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import org.apache.poi.ddf.DefaultEscherRecordFactory;
  19. import org.apache.poi.ddf.EscherContainerRecord;
  20. import org.apache.poi.ddf.EscherRecord;
  21. import org.apache.poi.ddf.EscherRecordFactory;
  22. import org.apache.poi.ddf.NullEscherSerializationListener;
  23. import org.apache.poi.hssf.util.LazilyConcatenatedByteArray;
  24. import org.apache.poi.util.LittleEndian;
  25. /**
  26. * The escher container record is used to hold escher records. It is abstract and
  27. * must be subclassed for maximum benefit.
  28. * <p>
  29. * Child records are deserialized on-demand unless the {@code poi.deserialize.escher} System Property is defined.
  30. */
  31. public abstract class AbstractEscherHolderRecord extends Record {
  32. private static boolean DESERIALIZE;
  33. static {
  34. try {
  35. DESERIALIZE = (System.getProperty("poi.deserialize.escher") != null);
  36. } catch (SecurityException e) {
  37. DESERIALIZE = false;
  38. }
  39. }
  40. private final List<EscherRecord> escherRecords = new ArrayList<>();
  41. private final LazilyConcatenatedByteArray rawDataContainer = new LazilyConcatenatedByteArray();
  42. public AbstractEscherHolderRecord() {}
  43. public AbstractEscherHolderRecord(AbstractEscherHolderRecord other) {
  44. other.escherRecords.stream().map(EscherRecord::copy).forEach(escherRecords::add);
  45. rawDataContainer.concatenate(other.rawDataContainer);
  46. }
  47. public AbstractEscherHolderRecord(RecordInputStream in) {
  48. if (!DESERIALIZE) {
  49. rawDataContainer.concatenate(in.readRemainder());
  50. } else {
  51. byte[] data = in.readAllContinuedRemainder();
  52. convertToEscherRecords( 0, data.length, data );
  53. }
  54. }
  55. protected void convertRawBytesToEscherRecords() {
  56. if (!DESERIALIZE) {
  57. byte[] rawData = getRawData();
  58. convertToEscherRecords(0, rawData.length, rawData);
  59. }
  60. }
  61. private void convertToEscherRecords( int offset, int size, byte[] data )
  62. {
  63. escherRecords.clear();
  64. EscherRecordFactory recordFactory = new DefaultEscherRecordFactory();
  65. int pos = offset;
  66. while ( pos < offset + size )
  67. {
  68. EscherRecord r = recordFactory.createRecord(data, pos);
  69. int bytesRead = r.fillFields(data, pos, recordFactory );
  70. escherRecords.add(r);
  71. pos += bytesRead;
  72. }
  73. }
  74. protected abstract String getRecordName();
  75. @Override
  76. public int serialize(int offset, byte[] data) {
  77. byte[] rawData = getRawData();
  78. LittleEndian.putShort(data, offset, getSid());
  79. offset += 2;
  80. LittleEndian.putShort(data, offset, (short) (getRecordSize() - 4));
  81. offset += 2;
  82. if (escherRecords.isEmpty() && rawData != null) {
  83. System.arraycopy(rawData, 0, data, offset, rawData.length);
  84. return rawData.length + 4;
  85. }
  86. NullEscherSerializationListener listener = new NullEscherSerializationListener();
  87. for (EscherRecord r : escherRecords) {
  88. offset += r.serialize(offset, data, listener);
  89. }
  90. return getRecordSize();
  91. }
  92. @Override
  93. public int getRecordSize() {
  94. byte[] rawData = getRawData();
  95. if (escherRecords.size() == 0 && rawData != null) {
  96. // XXX: It should be possible to derive this without concatenating the array, too.
  97. return rawData.length;
  98. }
  99. int size = 0;
  100. for (EscherRecord r : escherRecords) {
  101. size += r.getRecordSize();
  102. }
  103. return size;
  104. }
  105. @Override
  106. public abstract short getSid();
  107. @Override
  108. public abstract AbstractEscherHolderRecord copy();
  109. public void addEscherRecord(int index, EscherRecord element)
  110. {
  111. escherRecords.add( index, element );
  112. }
  113. public boolean addEscherRecord(EscherRecord element)
  114. {
  115. return escherRecords.add( element );
  116. }
  117. public List<EscherRecord> getEscherRecords()
  118. {
  119. return escherRecords;
  120. }
  121. public void clearEscherRecords()
  122. {
  123. escherRecords.clear();
  124. }
  125. /**
  126. * If we have a EscherContainerRecord as one of our
  127. * children (and most top level escher holders do),
  128. * then return that.
  129. *
  130. * @return the EscherContainerRecord or {@code null} if no child is a container record
  131. */
  132. public EscherContainerRecord getEscherContainer() {
  133. for (EscherRecord er : escherRecords) {
  134. if(er instanceof EscherContainerRecord) {
  135. return (EscherContainerRecord)er;
  136. }
  137. }
  138. return null;
  139. }
  140. /**
  141. * Descends into all our children, returning the
  142. * first EscherRecord with the given id, or null
  143. * if none found
  144. *
  145. * @param id the record to look for
  146. *
  147. * @return the record or {@code null} if it can't be found
  148. */
  149. public EscherRecord findFirstWithId(short id) {
  150. return findFirstWithId(id, getEscherRecords());
  151. }
  152. private EscherRecord findFirstWithId(short id, List<EscherRecord> records) {
  153. // Check at our level
  154. for (EscherRecord r : records) {
  155. if(r.getRecordId() == id) {
  156. return r;
  157. }
  158. }
  159. // Then check our children in turn
  160. for (EscherRecord r : records) {
  161. if(r.isContainerRecord()) {
  162. EscherRecord found = findFirstWithId(id, r.getChildRecords());
  163. if(found != null) {
  164. return found;
  165. }
  166. }
  167. }
  168. // Not found in this lot
  169. return null;
  170. }
  171. public EscherRecord getEscherRecord(int index)
  172. {
  173. return escherRecords.get(index);
  174. }
  175. /**
  176. * Big drawing group records are split but it's easier to deal with them
  177. * as a whole group so we need to join them together.
  178. *
  179. * @param record the record data to concatenate to the end
  180. */
  181. public void join( AbstractEscherHolderRecord record )
  182. {
  183. rawDataContainer.concatenate(record.getRawData());
  184. }
  185. public void processContinueRecord( byte[] record )
  186. {
  187. rawDataContainer.concatenate(record);
  188. }
  189. public byte[] getRawData()
  190. {
  191. return rawDataContainer.toArray();
  192. }
  193. public void setRawData( byte[] rawData )
  194. {
  195. rawDataContainer.clear();
  196. rawDataContainer.concatenate(rawData);
  197. }
  198. /**
  199. * Convert raw data to escher records.
  200. */
  201. public void decode()
  202. {
  203. if (escherRecords.isEmpty()) {
  204. byte[] rawData = getRawData();
  205. convertToEscherRecords(0, rawData.length, rawData);
  206. }
  207. }
  208. @Override
  209. public List<EscherRecord> getGenericChildren() {
  210. return escherRecords;
  211. }
  212. }