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.

HSLFHyperlink.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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.usermodel;
  16. import java.util.ArrayList;
  17. import java.util.Arrays;
  18. import java.util.List;
  19. import java.util.ListIterator;
  20. import org.apache.poi.hslf.record.ExHyperlink;
  21. import org.apache.poi.hslf.record.ExObjList;
  22. import org.apache.poi.hslf.record.HSLFEscherClientDataRecord;
  23. import org.apache.poi.hslf.record.InteractiveInfo;
  24. import org.apache.poi.hslf.record.InteractiveInfoAtom;
  25. import org.apache.poi.hslf.record.Record;
  26. import org.apache.poi.hslf.record.TxInteractiveInfoAtom;
  27. import org.apache.poi.sl.usermodel.Hyperlink;
  28. /**
  29. * Represents a hyperlink in a PowerPoint document
  30. */
  31. public final class HSLFHyperlink implements Hyperlink {
  32. public static final byte LINK_NEXTSLIDE = InteractiveInfoAtom.LINK_NextSlide;
  33. public static final byte LINK_PREVIOUSSLIDE = InteractiveInfoAtom.LINK_PreviousSlide;
  34. public static final byte LINK_FIRSTSLIDE = InteractiveInfoAtom.LINK_FirstSlide;
  35. public static final byte LINK_LASTSLIDE = InteractiveInfoAtom.LINK_LastSlide;
  36. public static final byte LINK_SLIDENUMBER = InteractiveInfoAtom.LINK_SlideNumber;
  37. public static final byte LINK_URL = InteractiveInfoAtom.LINK_Url;
  38. public static final byte LINK_NULL = InteractiveInfoAtom.LINK_NULL;
  39. private int id=-1;
  40. private int type;
  41. private String address;
  42. private String label;
  43. private int startIndex, endIndex;
  44. /**
  45. * Gets the type of the hyperlink action.
  46. * Must be a <code>LINK_*</code> constant</code>
  47. *
  48. * @return the hyperlink URL
  49. * @see InteractiveInfoAtom
  50. */
  51. @Override
  52. public int getType() {
  53. return type;
  54. }
  55. public void setType(int val) {
  56. type = val;
  57. switch(type){
  58. case LINK_NEXTSLIDE:
  59. label = "NEXT";
  60. address = "1,-1,NEXT";
  61. break;
  62. case LINK_PREVIOUSSLIDE:
  63. label = "PREV";
  64. address = "1,-1,PREV";
  65. break;
  66. case LINK_FIRSTSLIDE:
  67. label = "FIRST";
  68. address = "1,-1,FIRST";
  69. break;
  70. case LINK_LASTSLIDE:
  71. label = "LAST";
  72. address = "1,-1,LAST";
  73. break;
  74. case LINK_SLIDENUMBER:
  75. break;
  76. default:
  77. label = "";
  78. address = "";
  79. break;
  80. }
  81. }
  82. @Override
  83. public String getAddress() {
  84. return address;
  85. }
  86. public void setAddress(HSLFSlide slide) {
  87. String href = slide._getSheetNumber() + ","+slide.getSlideNumber()+",Slide " + slide.getSlideNumber();
  88. setAddress(href);;
  89. setLabel("Slide " + slide.getSlideNumber());
  90. setType(HSLFHyperlink.LINK_SLIDENUMBER);
  91. }
  92. @Override
  93. public void setAddress(String str) {
  94. address = str;
  95. }
  96. public int getId() {
  97. return id;
  98. }
  99. public void setId(int id) {
  100. this.id = id;
  101. }
  102. @Override
  103. public String getLabel() {
  104. return label;
  105. }
  106. @Override
  107. public void setLabel(String str) {
  108. label = str;
  109. }
  110. /**
  111. * Gets the beginning character position
  112. *
  113. * @return the beginning character position
  114. */
  115. public int getStartIndex() {
  116. return startIndex;
  117. }
  118. /**
  119. * Sets the beginning character position
  120. *
  121. * @param startIndex the beginning character position
  122. */
  123. public void setStartIndex(int startIndex) {
  124. this.startIndex = startIndex;
  125. }
  126. /**
  127. * Gets the ending character position
  128. *
  129. * @return the ending character position
  130. */
  131. public int getEndIndex() {
  132. return endIndex;
  133. }
  134. /**
  135. * Sets the ending character position
  136. *
  137. * @param endIndex the ending character position
  138. */
  139. public void setEndIndex(int endIndex) {
  140. this.endIndex = endIndex;
  141. }
  142. /**
  143. * Find hyperlinks in a text shape
  144. *
  145. * @param shape <code>TextRun</code> to lookup hyperlinks in
  146. * @return found hyperlinks or <code>null</code> if not found
  147. */
  148. public static List<HSLFHyperlink> find(HSLFTextShape shape){
  149. return find(shape.getTextParagraphs());
  150. }
  151. /**
  152. * Find hyperlinks in a text paragraph
  153. *
  154. * @param paragraphs List of <code>TextParagraph</code> to lookup hyperlinks
  155. * @return found hyperlinks
  156. */
  157. @SuppressWarnings("resource")
  158. public static List<HSLFHyperlink> find(List<HSLFTextParagraph> paragraphs){
  159. List<HSLFHyperlink> lst = new ArrayList<HSLFHyperlink>();
  160. if (paragraphs == null || paragraphs.isEmpty()) return lst;
  161. HSLFTextParagraph firstPara = paragraphs.get(0);
  162. HSLFSlideShow ppt = firstPara.getSheet().getSlideShow();
  163. //document-level container which stores info about all links in a presentation
  164. ExObjList exobj = ppt.getDocumentRecord().getExObjList();
  165. if (exobj != null) {
  166. Record[] records = firstPara.getRecords();
  167. find(Arrays.asList(records), exobj, lst);
  168. }
  169. return lst;
  170. }
  171. /**
  172. * Find hyperlink assigned to the supplied shape
  173. *
  174. * @param shape <code>Shape</code> to lookup hyperlink in
  175. * @return found hyperlink or <code>null</code>
  176. */
  177. @SuppressWarnings("resource")
  178. public static HSLFHyperlink find(HSLFShape shape){
  179. HSLFSlideShow ppt = shape.getSheet().getSlideShow();
  180. //document-level container which stores info about all links in a presentation
  181. ExObjList exobj = ppt.getDocumentRecord().getExObjList();
  182. HSLFEscherClientDataRecord cldata = shape.getClientData(false);
  183. if (exobj != null && cldata != null) {
  184. List<HSLFHyperlink> lst = new ArrayList<HSLFHyperlink>();
  185. find(cldata.getHSLFChildRecords(), exobj, lst);
  186. return lst.isEmpty() ? null : (HSLFHyperlink)lst.get(0);
  187. }
  188. return null;
  189. }
  190. private static void find(List<? extends Record> records, ExObjList exobj, List<HSLFHyperlink> out){
  191. ListIterator<? extends Record> iter = records.listIterator();
  192. while (iter.hasNext()) {
  193. Record r = iter.next();
  194. // see if we have InteractiveInfo in the textrun's records
  195. if (!(r instanceof InteractiveInfo)) {
  196. continue;
  197. }
  198. InteractiveInfo hldr = (InteractiveInfo)r;
  199. InteractiveInfoAtom info = hldr.getInteractiveInfoAtom();
  200. int id = info.getHyperlinkID();
  201. ExHyperlink linkRecord = exobj.get(id);
  202. if (linkRecord == null) {
  203. continue;
  204. }
  205. HSLFHyperlink link = new HSLFHyperlink();
  206. link.setId(id);
  207. link.setType(info.getAction());
  208. link.setLabel(linkRecord.getLinkTitle());
  209. link.setAddress(linkRecord.getLinkURL());
  210. out.add(link);
  211. if (iter.hasNext()) {
  212. r = iter.next();
  213. if (!(r instanceof TxInteractiveInfoAtom)) {
  214. iter.previous();
  215. continue;
  216. }
  217. TxInteractiveInfoAtom txinfo = (TxInteractiveInfoAtom)r;
  218. link.setStartIndex(txinfo.getStartIndex());
  219. link.setEndIndex(txinfo.getEndIndex());
  220. }
  221. }
  222. }
  223. }