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.

SlidePersistAtom.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 static org.apache.poi.util.GenericRecordUtil.getBitsAsString;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. import java.util.Map;
  20. import java.util.function.Supplier;
  21. import org.apache.poi.util.GenericRecordUtil;
  22. import org.apache.poi.util.IOUtils;
  23. import org.apache.poi.util.LittleEndian;
  24. /**
  25. * A SlidePersist Atom (type 1011). Holds information on the text of a
  26. * given slide, which are stored in the same SlideListWithText
  27. */
  28. public final class SlidePersistAtom extends RecordAtom {
  29. //arbitrarily selected; may need to increase
  30. private static final int MAX_RECORD_LENGTH = 32;
  31. private static final long _type = 1011l;
  32. private static final int HAS_SHAPES_OTHER_THAN_PLACEHOLDERS = 4;
  33. private static final int[] FLAGS_MASKS = { HAS_SHAPES_OTHER_THAN_PLACEHOLDERS };
  34. private static final String[] FLAGS_NAMES = { "HAS_SHAPES_OTHER_THAN_PLACEHOLDERS" };
  35. private final byte[] _header = new byte[8];
  36. /** Slide reference ID. Should correspond to the PersistPtr "sheet ID" of the matching slide/notes record */
  37. private int refID;
  38. private int flags;
  39. /** Number of placeholder texts that will follow in the SlideListWithText */
  40. private int numPlaceholderTexts;
  41. /** The internal identifier (256+), which is used to tie slides and notes together */
  42. private int slideIdentifier;
  43. /** Reserved fields. Who knows what they do */
  44. private byte[] reservedFields;
  45. public int getRefID() {
  46. return refID;
  47. }
  48. public int getSlideIdentifier() {
  49. return slideIdentifier;
  50. }
  51. public int getNumPlaceholderTexts() {
  52. return numPlaceholderTexts;
  53. }
  54. public boolean getHasShapesOtherThanPlaceholders() {
  55. return (flags & HAS_SHAPES_OTHER_THAN_PLACEHOLDERS) != 0;
  56. }
  57. // Only set these if you know what you're doing!
  58. public void setRefID(int id) {
  59. refID = id;
  60. }
  61. public void setSlideIdentifier(int id) {
  62. slideIdentifier = id;
  63. }
  64. /* *************** record code follows ********************** */
  65. /**
  66. * For the SlidePersist Atom
  67. */
  68. protected SlidePersistAtom(byte[] source, int start, int len) {
  69. // Sanity Checking
  70. if(len < 8) { len = 8; }
  71. // Get the header
  72. System.arraycopy(source,start,_header,0,8);
  73. // Grab the reference ID
  74. refID = LittleEndian.getInt(source,start+8);
  75. // Next up is a set of flags, but only bit 3 is used!
  76. flags = LittleEndian.getInt(source,start+12);
  77. // Now the number of Placeholder Texts
  78. numPlaceholderTexts = LittleEndian.getInt(source,start+16);
  79. // Last useful one is the unique slide identifier
  80. slideIdentifier = LittleEndian.getInt(source,start+20);
  81. // Finally you have typically 4 or 8 bytes of reserved fields,
  82. // all zero running from 24 bytes in to the end
  83. reservedFields = IOUtils.safelyAllocate(len-24, MAX_RECORD_LENGTH);
  84. System.arraycopy(source,start+24,reservedFields,0,reservedFields.length);
  85. }
  86. /**
  87. * Create a new SlidePersistAtom, for use with a new Slide
  88. */
  89. public SlidePersistAtom() {
  90. LittleEndian.putUShort(_header, 0, 0);
  91. LittleEndian.putUShort(_header, 2, (int)_type);
  92. LittleEndian.putInt(_header, 4, 20);
  93. flags = HAS_SHAPES_OTHER_THAN_PLACEHOLDERS;
  94. reservedFields = new byte[4];
  95. }
  96. /**
  97. * We are of type 1011
  98. */
  99. public long getRecordType() { return _type; }
  100. /**
  101. * Write the contents of the record back, so it can be written
  102. * to disk
  103. */
  104. public void writeOut(OutputStream out) throws IOException {
  105. // Header - size or type unchanged
  106. out.write(_header);
  107. // Write out our fields
  108. writeLittleEndian(refID,out);
  109. writeLittleEndian(flags,out);
  110. writeLittleEndian(numPlaceholderTexts,out);
  111. writeLittleEndian(slideIdentifier,out);
  112. out.write(reservedFields);
  113. }
  114. @Override
  115. public Map<String, Supplier<?>> getGenericProperties() {
  116. return GenericRecordUtil.getGenericProperties(
  117. "refID", this::getRefID,
  118. "flags", getBitsAsString(() -> flags, FLAGS_MASKS, FLAGS_NAMES),
  119. "numPlaceholderTexts", this::getNumPlaceholderTexts,
  120. "slideIdentifier", this::getSlideIdentifier
  121. );
  122. }
  123. }