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.

InteractiveInfoAtom.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 static org.apache.poi.util.GenericRecordUtil.safeEnum;
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  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. * Tne atom that holds metadata on Links in the document.
  27. * (The actual link is held Document.ExObjList.ExHyperlink)
  28. *
  29. * @author Nick Burch
  30. * @author Yegor Kozlov
  31. */
  32. public class InteractiveInfoAtom extends RecordAtom {
  33. //arbitrarily selected; may need to increase
  34. private static final int MAX_RECORD_LENGTH = 100_000;
  35. public enum Action {
  36. NONE,
  37. MACRO,
  38. RUN_PROGRAM,
  39. JUMP,
  40. HYPERLINK,
  41. OLE,
  42. MEDIA,
  43. CUSTOM_SHOW
  44. }
  45. public enum Jump {
  46. NONE,
  47. NEXT_SLIDE,
  48. PREVIOUS_SLIDE,
  49. FIRST_SLIDE,
  50. LAST_SLIDE,
  51. LAST_SLIDE_VIEWED,
  52. END_SHOW
  53. }
  54. public enum Link {
  55. NEXT_SLIDE,
  56. PREVIOUS_SLIDE,
  57. FIRST_SLIDE,
  58. LAST_SLIDE,
  59. CUSTOM_SHOW,
  60. SLIDE_NUMBER,
  61. URL,
  62. OTHER_PRESENTATION,
  63. OTHER_FILE,
  64. NULL
  65. }
  66. /**
  67. * Action Table
  68. */
  69. public static final byte ACTION_NONE = 0;
  70. public static final byte ACTION_MACRO = 1;
  71. public static final byte ACTION_RUNPROGRAM = 2;
  72. public static final byte ACTION_JUMP = 3;
  73. public static final byte ACTION_HYPERLINK = 4;
  74. public static final byte ACTION_OLE = 5;
  75. public static final byte ACTION_MEDIA = 6;
  76. public static final byte ACTION_CUSTOMSHOW = 7;
  77. /**
  78. * Jump Table
  79. */
  80. public static final byte JUMP_NONE = 0;
  81. public static final byte JUMP_NEXTSLIDE = 1;
  82. public static final byte JUMP_PREVIOUSSLIDE = 2;
  83. public static final byte JUMP_FIRSTSLIDE = 3;
  84. public static final byte JUMP_LASTSLIDE = 4;
  85. public static final byte JUMP_LASTSLIDEVIEWED = 5;
  86. public static final byte JUMP_ENDSHOW = 6;
  87. /**
  88. * Types of hyperlinks
  89. */
  90. public static final byte LINK_NextSlide = 0x00;
  91. public static final byte LINK_PreviousSlide = 0x01;
  92. public static final byte LINK_FirstSlide = 0x02;
  93. public static final byte LINK_LastSlide = 0x03;
  94. public static final byte LINK_CustomShow = 0x06;
  95. public static final byte LINK_SlideNumber = 0x07;
  96. public static final byte LINK_Url = 0x08;
  97. public static final byte LINK_OtherPresentation = 0x09;
  98. public static final byte LINK_OtherFile = 0x0A;
  99. public static final byte LINK_NULL = (byte)0xFF;
  100. private static final int[] FLAGS_MASKS = {
  101. 0x0001, 0x0002, 0x0004, 0x0008
  102. };
  103. private static final String[] FLAGS_NAMES = {
  104. "ANIMATED", "STOP_SOUND", "CUSTOM_SHOW_RETURN", "VISITED"
  105. };
  106. /**
  107. * Record header.
  108. */
  109. private byte[] _header;
  110. /**
  111. * Record data.
  112. */
  113. private byte[] _data;
  114. /**
  115. * Constructs a brand new link related atom record.
  116. */
  117. protected InteractiveInfoAtom() {
  118. _header = new byte[8];
  119. _data = new byte[16];
  120. LittleEndian.putShort(_header, 2, (short)getRecordType());
  121. LittleEndian.putInt(_header, 4, _data.length);
  122. // It is fine for the other values to be zero
  123. }
  124. /**
  125. * Constructs the link related atom record from its
  126. * source data.
  127. *
  128. * @param source the source data as a byte array.
  129. * @param start the start offset into the byte array.
  130. * @param len the length of the slice in the byte array.
  131. */
  132. protected InteractiveInfoAtom(byte[] source, int start, int len) {
  133. // Get the header.
  134. _header = new byte[8];
  135. System.arraycopy(source,start,_header,0,8);
  136. // Get the record data.
  137. _data = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH);
  138. System.arraycopy(source,start+8,_data,0,len-8);
  139. // Must be at least 16 bytes long
  140. if(_data.length < 16) {
  141. throw new IllegalArgumentException("The length of the data for a InteractiveInfoAtom must be at least 16 bytes, but was only " + _data.length);
  142. }
  143. // First 4 bytes - no idea, normally 0
  144. // Second 4 bytes - the id of the link (from 1 onwards)
  145. // Third 4 bytes - no idea, normally 4
  146. // Fourth 4 bytes - no idea, normally 8
  147. }
  148. /**
  149. * Gets the link number. You will normally look the
  150. * ExHyperlink with this number to get the details.
  151. * @return the link number
  152. */
  153. public int getHyperlinkID() {
  154. return LittleEndian.getInt(_data,4);
  155. }
  156. /**
  157. * Sets the persistent unique identifier of the link
  158. *
  159. * @param number the persistent unique identifier of the link
  160. */
  161. public void setHyperlinkID(int number) {
  162. LittleEndian.putInt(_data,4,number);
  163. }
  164. /**
  165. * a reference to a sound in the sound collection.
  166. */
  167. public int getSoundRef() {
  168. return LittleEndian.getInt(_data,0);
  169. }
  170. /**
  171. * a reference to a sound in the sound collection.
  172. *
  173. * @param val a reference to a sound in the sound collection
  174. */
  175. public void setSoundRef(int val) {
  176. LittleEndian.putInt(_data, 0, val);
  177. }
  178. /**
  179. * Hyperlink Action.
  180. * <p>
  181. * see <code>ACTION_*</code> constants for the list of actions
  182. * </p>
  183. *
  184. * @return hyperlink action.
  185. */
  186. public byte getAction() {
  187. return _data[8];
  188. }
  189. /**
  190. * Hyperlink Action
  191. * <p>
  192. * see <code>ACTION_*</code> constants for the list of actions
  193. * </p>
  194. *
  195. * @param val hyperlink action.
  196. */
  197. public void setAction(byte val) {
  198. _data[8] = val;
  199. }
  200. /**
  201. * Only valid when action == OLEAction. OLE verb to use, 0 = first verb, 1 = second verb, etc.
  202. */
  203. public byte getOleVerb() {
  204. return _data[9];
  205. }
  206. /**
  207. * Only valid when action == OLEAction. OLE verb to use, 0 = first verb, 1 = second verb, etc.
  208. */
  209. public void setOleVerb(byte val) {
  210. _data[9] = val;
  211. }
  212. /**
  213. * Jump
  214. * <p>
  215. * see <code>JUMP_*</code> constants for the list of actions
  216. * </p>
  217. *
  218. * @return jump
  219. */
  220. public byte getJump() {
  221. return _data[10];
  222. }
  223. /**
  224. * Jump
  225. * <p>
  226. * see <code>JUMP_*</code> constants for the list of actions
  227. * </p>
  228. *
  229. * @param val jump
  230. */
  231. public void setJump(byte val) {
  232. _data[10] = val;
  233. }
  234. /**
  235. * Flags
  236. * <p>
  237. * <li> Bit 1: Animated. If 1, then button is animated
  238. * <li> Bit 2: Stop sound. If 1, then stop current sound when button is pressed.
  239. * <li> Bit 3: CustomShowReturn. If 1, and this is a jump to custom show,
  240. * then return to this slide after custom show.
  241. * </p>
  242. */
  243. public byte getFlags() {
  244. return _data[11];
  245. }
  246. /**
  247. * Flags
  248. * <p>
  249. * <li> Bit 1: Animated. If 1, then button is animated
  250. * <li> Bit 2: Stop sound. If 1, then stop current sound when button is pressed.
  251. * <li> Bit 3: CustomShowReturn. If 1, and this is a jump to custom show,
  252. * then return to this slide after custom show.
  253. * </p>
  254. */
  255. public void setFlags(byte val) {
  256. _data[11] = val;
  257. }
  258. /**
  259. * hyperlink type
  260. *
  261. * @return hyperlink type
  262. */
  263. public byte getHyperlinkType() {
  264. return _data[12];
  265. }
  266. /**
  267. * hyperlink type
  268. *
  269. * @param val hyperlink type
  270. */
  271. public void setHyperlinkType(byte val) {
  272. _data[12] = val;
  273. }
  274. /**
  275. * Gets the record type.
  276. * @return the record type.
  277. */
  278. public long getRecordType() { return RecordTypes.InteractiveInfoAtom.typeID; }
  279. /**
  280. * Write the contents of the record back, so it can be written
  281. * to disk
  282. *
  283. * @param out the output stream to write to.
  284. * @throws IOException if an error occurs.
  285. */
  286. public void writeOut(OutputStream out) throws IOException {
  287. out.write(_header);
  288. out.write(_data);
  289. }
  290. @Override
  291. public Map<String, Supplier<?>> getGenericProperties() {
  292. return GenericRecordUtil.getGenericProperties(
  293. "hyperlinkID", this::getHyperlinkID,
  294. "soundRef", this::getSoundRef,
  295. "action", safeEnum(Action.values(), this::getAction),
  296. "jump", safeEnum(Jump.values(), this::getJump),
  297. "hyperlinkType", safeEnum(Link.values(), this::getHyperlinkType, Link.NULL),
  298. "flags", getBitsAsString(this::getFlags, FLAGS_MASKS, FLAGS_NAMES)
  299. );
  300. }
  301. }