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.

DrawingGroupRecord.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.List;
  17. import java.util.Map;
  18. import java.util.function.Supplier;
  19. import org.apache.poi.ddf.EscherRecord;
  20. import org.apache.poi.ddf.EscherRecordTypes;
  21. import org.apache.poi.ddf.NullEscherSerializationListener;
  22. import org.apache.poi.util.LittleEndian;
  23. /**
  24. * Specifies a group of drawing objects.
  25. * <p>
  26. * Contains a single {@link EscherRecordTypes#DGG_CONTAINER OfficeArtDggContainer} that specifies the group of drawing
  27. * objects. Get the {@link org.apache.poi.ddf.EscherContainerRecord} representation via {@link #getEscherContainer()}.
  28. * <p>
  29. * Referred to as an {@code MsoDrawingGroup} in {@code [MS-XLS].pdf v20190618}.
  30. */
  31. public final class DrawingGroupRecord extends AbstractEscherHolderRecord {
  32. public static final short sid = 0xEB;
  33. static final int MAX_RECORD_SIZE = 8228;
  34. private static final int MAX_DATA_SIZE = MAX_RECORD_SIZE - 4;
  35. public DrawingGroupRecord() {}
  36. public DrawingGroupRecord(DrawingGroupRecord other) {
  37. super(other);
  38. }
  39. public DrawingGroupRecord( RecordInputStream in ) {
  40. super( in );
  41. }
  42. protected String getRecordName()
  43. {
  44. return "MSODRAWINGGROUP";
  45. }
  46. public short getSid()
  47. {
  48. return sid;
  49. }
  50. public int serialize(int offset, byte[] data)
  51. {
  52. byte[] rawData = getRawData();
  53. if (getEscherRecords().size() == 0 && rawData != null)
  54. {
  55. return writeData( offset, data, rawData );
  56. }
  57. byte[] buffer = new byte[getRawDataSize()];
  58. int pos = 0;
  59. for (EscherRecord r : getEscherRecords()) {
  60. pos += r.serialize(pos, buffer, new NullEscherSerializationListener());
  61. }
  62. return writeData( offset, data, buffer );
  63. }
  64. /**
  65. * Process the bytes into escher records.
  66. * (Not done by default in case we break things,
  67. * unless you set the "poi.deserialize.escher"
  68. * system property)
  69. */
  70. public void processChildRecords() {
  71. convertRawBytesToEscherRecords();
  72. }
  73. public int getRecordSize() {
  74. // TODO - convert this to a RecordAggregate
  75. return grossSizeFromDataSize(getRawDataSize());
  76. }
  77. private int getRawDataSize() {
  78. List<EscherRecord> escherRecords = getEscherRecords();
  79. byte[] rawData = getRawData();
  80. if (escherRecords.size() == 0 && rawData != null)
  81. {
  82. return rawData.length;
  83. }
  84. int size = 0;
  85. for (EscherRecord r : escherRecords) {
  86. size += r.getRecordSize();
  87. }
  88. return size;
  89. }
  90. static int grossSizeFromDataSize(int dataSize)
  91. {
  92. return dataSize + ( (dataSize - 1) / MAX_DATA_SIZE + 1 ) * 4;
  93. }
  94. private int writeData( int offset, byte[] data, byte[] rawData )
  95. {
  96. int writtenActualData = 0;
  97. int writtenRawData = 0;
  98. while (writtenRawData < rawData.length)
  99. {
  100. int segmentLength = Math.min( rawData.length - writtenRawData, MAX_DATA_SIZE);
  101. if (writtenRawData / MAX_DATA_SIZE >= 2)
  102. writeContinueHeader( data, offset, segmentLength );
  103. else
  104. writeHeader( data, offset, segmentLength );
  105. writtenActualData += 4;
  106. offset += 4;
  107. System.arraycopy( rawData, writtenRawData, data, offset, segmentLength );
  108. offset += segmentLength;
  109. writtenRawData += segmentLength;
  110. writtenActualData += segmentLength;
  111. }
  112. return writtenActualData;
  113. }
  114. private void writeHeader( byte[] data, int offset, int sizeExcludingHeader )
  115. {
  116. LittleEndian.putShort(data, offset, getSid());
  117. LittleEndian.putShort(data, offset + 2, (short) sizeExcludingHeader);
  118. }
  119. private void writeContinueHeader( byte[] data, int offset, int sizeExcludingHeader )
  120. {
  121. LittleEndian.putShort(data, offset, ContinueRecord.sid);
  122. LittleEndian.putShort(data, offset + 2, (short) sizeExcludingHeader);
  123. }
  124. @Override
  125. public DrawingGroupRecord copy() {
  126. return new DrawingGroupRecord(this);
  127. }
  128. @Override
  129. public HSSFRecordTypes getGenericRecordType() {
  130. return HSSFRecordTypes.DRAWING_GROUP;
  131. }
  132. @Override
  133. public Map<String, Supplier<?>> getGenericProperties() {
  134. return null;
  135. }
  136. }