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.

HSLFEscherClientDataRecord.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.hslf.record;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import org.apache.poi.ddf.EscherClientDataRecord;
  21. import org.apache.poi.ddf.EscherRecordFactory;
  22. import org.apache.poi.ddf.EscherSerializationListener;
  23. import org.apache.poi.hslf.exceptions.HSLFException;
  24. import org.apache.poi.util.IOUtils;
  25. import org.apache.poi.util.LittleEndian;
  26. /**
  27. * An atom record that specifies whether a shape is a placeholder shape.
  28. * The number, position, and type of placeholder shapes are determined by
  29. * the slide layout as specified in the SlideAtom record.
  30. *
  31. * @since POI 3.14-Beta2
  32. */
  33. public class HSLFEscherClientDataRecord extends EscherClientDataRecord {
  34. //arbitrarily selected; may need to increase
  35. private static final int MAX_RECORD_LENGTH = 1_000_000;
  36. private final List<org.apache.poi.hslf.record.Record> _childRecords = new ArrayList<>();
  37. public HSLFEscherClientDataRecord() {}
  38. public HSLFEscherClientDataRecord(HSLFEscherClientDataRecord other) {
  39. super(other);
  40. // TODO: for now only reference others children, later copy them when Record.copy is available
  41. // other._childRecords.stream().map(Record::copy).forEach(_childRecords::add);
  42. other._childRecords.addAll(other._childRecords);
  43. }
  44. public List<? extends Record> getHSLFChildRecords() {
  45. return _childRecords;
  46. }
  47. public void removeChild(Class<? extends Record> childClass) {
  48. _childRecords.removeIf(childClass::isInstance);
  49. }
  50. public void addChild(Record childRecord) {
  51. _childRecords.add(childRecord);
  52. }
  53. @Override
  54. public int fillFields(byte[] data, int offset, EscherRecordFactory recordFactory) {
  55. int bytesRemaining = readHeader( data, offset );
  56. byte[] remainingData = IOUtils.safelyAllocate(bytesRemaining, MAX_RECORD_LENGTH);
  57. System.arraycopy(data, offset+8, remainingData, 0, bytesRemaining);
  58. setRemainingData(remainingData);
  59. return bytesRemaining + 8;
  60. }
  61. @Override
  62. public int serialize(int offset, byte[] data, EscherSerializationListener listener) {
  63. listener.beforeRecordSerialize( offset, getRecordId(), this );
  64. LittleEndian.putShort(data, offset, getOptions());
  65. LittleEndian.putShort(data, offset+2, getRecordId());
  66. byte[] childBytes = getRemainingData();
  67. LittleEndian.putInt(data, offset+4, childBytes.length);
  68. System.arraycopy(childBytes, 0, data, offset+8, childBytes.length);
  69. int recordSize = 8+childBytes.length;
  70. listener.afterRecordSerialize( offset+recordSize, getRecordId(), recordSize, this );
  71. return recordSize;
  72. }
  73. @Override
  74. public int getRecordSize() {
  75. return 8 + getRemainingData().length;
  76. }
  77. @Override
  78. public byte[] getRemainingData() {
  79. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  80. try {
  81. for (org.apache.poi.hslf.record.Record r : _childRecords) {
  82. r.writeOut(bos);
  83. }
  84. } catch (IOException e) {
  85. throw new HSLFException(e);
  86. }
  87. return bos.toByteArray();
  88. }
  89. @Override
  90. public void setRemainingData( byte[] remainingData ) {
  91. _childRecords.clear();
  92. int offset = 0;
  93. while (offset < remainingData.length) {
  94. final org.apache.poi.hslf.record.Record r = Record.buildRecordAtOffset(remainingData, offset);
  95. if (r != null) {
  96. _childRecords.add(r);
  97. }
  98. long rlen = LittleEndian.getUInt(remainingData,offset+4);
  99. offset += 8 + rlen;
  100. }
  101. }
  102. public String getRecordName() {
  103. return "HSLFClientData";
  104. }
  105. @Override
  106. public HSLFEscherClientDataRecord copy() {
  107. return new HSLFEscherClientDataRecord(this);
  108. }
  109. }