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.

HeadersFootersAtom.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.Arrays;
  20. import java.util.Map;
  21. import java.util.function.Supplier;
  22. import org.apache.poi.util.GenericRecordUtil;
  23. import org.apache.poi.util.IOUtils;
  24. import org.apache.poi.util.LittleEndian;
  25. /**
  26. * An atom record that specifies options for displaying headers and footers
  27. * on a presentation slide or notes slide.
  28. */
  29. public final class HeadersFootersAtom extends RecordAtom {
  30. /**
  31. * A bit that specifies whether the date is displayed in the footer.
  32. * @see #getMask()
  33. * @see #setMask(int)
  34. */
  35. public static final int fHasDate = 1;
  36. /**
  37. * A bit that specifies whether the current datetime is used for displaying the datetime.
  38. * @see #getMask()
  39. * @see #setMask(int)
  40. */
  41. public static final int fHasTodayDate = 2;
  42. /**
  43. * A bit that specifies whether the date specified in UserDateAtom record
  44. * is used for displaying the datetime.
  45. *
  46. * @see #getMask()
  47. * @see #setMask(int)
  48. */
  49. public static final int fHasUserDate = 4;
  50. /**
  51. * A bit that specifies whether the slide number is displayed in the footer.
  52. *
  53. * @see #getMask()
  54. * @see #setMask(int)
  55. */
  56. public static final int fHasSlideNumber = 8;
  57. /**
  58. * bit that specifies whether the header text is displayed.
  59. *
  60. * @see #getMask()
  61. * @see #setMask(int)
  62. */
  63. public static final int fHasHeader = 16;
  64. /**
  65. * bit that specifies whether the footer text is displayed.
  66. *
  67. * @see #getMask()
  68. * @see #setMask(int)
  69. */
  70. public static final int fHasFooter = 32;
  71. private static final int[] PLACEHOLDER_MASKS = {
  72. fHasDate,
  73. fHasTodayDate,
  74. fHasUserDate,
  75. fHasSlideNumber,
  76. fHasHeader,
  77. fHasFooter
  78. };
  79. private static final String[] PLACEHOLDER_NAMES = {
  80. "DATE", "TODAY_DATE", "USER_DATE", "SLIDE_NUMBER", "HEADER", "FOOTER"
  81. };
  82. /**
  83. * record header
  84. */
  85. private final byte[] _header;
  86. /**
  87. * record data
  88. */
  89. private final byte[] _recdata;
  90. /**
  91. * Build an instance of {@code HeadersFootersAtom} from on-disk data
  92. */
  93. HeadersFootersAtom(byte[] source, int start, int len) {
  94. // Get the header
  95. _header = Arrays.copyOfRange(source, start, start+8);
  96. // Grab the record data
  97. _recdata = IOUtils.safelyClone(source, start+8, len-8, getMaxRecordLength());
  98. }
  99. /**
  100. * Create a new instance of {@code HeadersFootersAtom}
  101. */
  102. public HeadersFootersAtom() {
  103. _recdata = new byte[4];
  104. _header = new byte[8];
  105. LittleEndian.putShort(_header, 2, (short)getRecordType());
  106. LittleEndian.putInt(_header, 4, _recdata.length);
  107. }
  108. @Override
  109. public long getRecordType() {
  110. return RecordTypes.HeadersFootersAtom.typeID;
  111. }
  112. /**
  113. * Write the contents of the record back, so it can be written to disk
  114. */
  115. @Override
  116. public void writeOut(OutputStream out) throws IOException {
  117. out.write(_header);
  118. out.write(_recdata);
  119. }
  120. /**
  121. * A signed integer that specifies the format ID to be used to style the datetime.
  122. * <p>
  123. * It MUST be in the range [0, 12]. <p>
  124. * This value is converted into a string as specified by the index field of the DateTimeMCAtom record.
  125. * It MUST be ignored unless fHasTodayDate is TRUE.
  126. *
  127. * @return A signed integer that specifies the format ID to be used to style the datetime.
  128. */
  129. public int getFormatId(){
  130. return LittleEndian.getShort(_recdata, 0);
  131. }
  132. /**
  133. * A signed integer that specifies the format ID to be used to style the datetime.
  134. *
  135. * @param formatId A signed integer that specifies the format ID to be used to style the datetime.
  136. */
  137. public void setFormatId(int formatId){
  138. LittleEndian.putUShort(_recdata, 0, formatId);
  139. }
  140. /**
  141. * A bit mask specifying options for displaying headers and footers
  142. *
  143. * <ul>
  144. * <li> A - {@link #fHasDate} (1 bit): A bit that specifies whether the date is displayed in the footer.
  145. * <li> B - {@link #fHasTodayDate} (1 bit): A bit that specifies whether the current datetime is used for
  146. * displaying the datetime.
  147. * <li> C - {@link #fHasUserDate} (1 bit): A bit that specifies whether the date specified in UserDateAtom record
  148. * is used for displaying the datetime.
  149. * <li> D - {@link #fHasSlideNumber} (1 bit): A bit that specifies whether the slide number is displayed in the footer.
  150. * <li> E - {@link #fHasHeader} (1 bit): A bit that specifies whether the header text specified by HeaderAtom
  151. * record is displayed.
  152. * <li> F - {@link #fHasFooter} (1 bit): A bit that specifies whether the footer text specified by FooterAtom
  153. * record is displayed.
  154. * <li> reserved (10 bits): MUST be zero and MUST be ignored.
  155. * </ul>
  156. *
  157. * @return A bit mask specifying options for displaying headers and footers
  158. */
  159. public int getMask(){
  160. return LittleEndian.getShort(_recdata, 2);
  161. }
  162. /**
  163. * A bit mask specifying options for displaying headers and footers
  164. *
  165. * @param mask A bit mask specifying options for displaying headers and footers
  166. */
  167. public void setMask(int mask){
  168. LittleEndian.putUShort(_recdata, 2, mask);
  169. }
  170. /**
  171. * @param bit the bit to check
  172. * @return whether the specified flag is set
  173. */
  174. public boolean getFlag(int bit){
  175. return (getMask() & bit) != 0;
  176. }
  177. /**
  178. * @param bit the bit to set
  179. * @param value whether the specified bit is set
  180. */
  181. public void setFlag(int bit, boolean value){
  182. int mask = getMask();
  183. if(value) mask |= bit;
  184. else mask &= ~bit;
  185. setMask(mask);
  186. }
  187. public String toString(){
  188. return
  189. "HeadersFootersAtom\n" +
  190. "\tFormatId: " + getFormatId() + "\n" +
  191. "\tMask : " + getMask() + "\n" +
  192. "\t fHasDate : " + getFlag(fHasDate) + "\n" +
  193. "\t fHasTodayDate : " + getFlag(fHasTodayDate) + "\n" +
  194. "\t fHasUserDate : " + getFlag(fHasUserDate) + "\n" +
  195. "\t fHasSlideNumber : " + getFlag(fHasSlideNumber) + "\n" +
  196. "\t fHasHeader : " + getFlag(fHasHeader) + "\n" +
  197. "\t fHasFooter : " + getFlag(fHasFooter) + "\n";
  198. }
  199. @Override
  200. public Map<String, Supplier<?>> getGenericProperties() {
  201. return GenericRecordUtil.getGenericProperties(
  202. "formatIndex", this::getFormatId,
  203. "flags", getBitsAsString(this::getMask, PLACEHOLDER_MASKS, PLACEHOLDER_NAMES)
  204. );
  205. }
  206. }