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.

XSSFHyperlink.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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.xssf.usermodel;
  16. import java.net.URI;
  17. import java.net.URISyntaxException;
  18. import org.apache.poi.common.usermodel.HyperlinkType;
  19. import org.apache.poi.openxml4j.opc.PackagePart;
  20. import org.apache.poi.openxml4j.opc.PackageRelationship;
  21. import org.apache.poi.ss.usermodel.Hyperlink;
  22. import org.apache.poi.ss.util.CellReference;
  23. import org.apache.poi.util.Internal;
  24. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHyperlink;
  25. /**
  26. * XSSF Implementation of a Hyperlink.
  27. * Note - unlike with HSSF, many kinds of hyperlink
  28. * are largely stored as relations of the sheet
  29. */
  30. public class XSSFHyperlink implements Hyperlink {
  31. final private HyperlinkType _type;
  32. final private PackageRelationship _externalRel;
  33. final private CTHyperlink _ctHyperlink; //contains a reference to the cell where the hyperlink is anchored, getRef()
  34. private String _location; //what the hyperlink refers to
  35. /**
  36. * Create a new XSSFHyperlink. This method is protected to be used only by
  37. * {@link XSSFCreationHelper#createHyperlink(int)}.
  38. *
  39. * @param type - the type of hyperlink to create, see {@link Hyperlink}
  40. * @deprecated POI 3.15 beta 3. Use {@link #XSSFHyperlink(HyperlinkType)} instead.
  41. */
  42. protected XSSFHyperlink(int type) {
  43. this(HyperlinkType.forInt(type));
  44. }
  45. /**
  46. * Create a new XSSFHyperlink. This method is protected to be used only by
  47. * {@link XSSFCreationHelper#createHyperlink(HyperlinkType)}.
  48. *
  49. * @param type - the type of hyperlink to create
  50. */
  51. protected XSSFHyperlink(HyperlinkType type) {
  52. _type = type;
  53. _ctHyperlink = CTHyperlink.Factory.newInstance();
  54. _externalRel = null;
  55. }
  56. /**
  57. * Create a XSSFHyperlink and initialize it from the supplied CTHyperlink bean and package relationship
  58. *
  59. * @param ctHyperlink the xml bean containing xml properties
  60. * @param hyperlinkRel the relationship in the underlying OPC package which stores the actual link's address
  61. */
  62. protected XSSFHyperlink(CTHyperlink ctHyperlink, PackageRelationship hyperlinkRel) {
  63. _ctHyperlink = ctHyperlink;
  64. _externalRel = hyperlinkRel;
  65. // Figure out the Hyperlink type and destination
  66. if (_externalRel == null) {
  67. // If it has a location, it's internal
  68. if (ctHyperlink.getLocation() != null) {
  69. _type = HyperlinkType.DOCUMENT;
  70. _location = ctHyperlink.getLocation();
  71. } else if (ctHyperlink.getId() != null) {
  72. throw new IllegalStateException("The hyperlink for cell "
  73. + ctHyperlink.getRef() + " references relation "
  74. + ctHyperlink.getId() + ", but that didn't exist!");
  75. } else {
  76. // hyperlink is internal and is not related to other parts
  77. _type = HyperlinkType.DOCUMENT;
  78. }
  79. } else {
  80. URI target = _externalRel.getTargetURI();
  81. _location = target.toString();
  82. if (ctHyperlink.getLocation() != null) {
  83. // URI fragment
  84. _location += "#" + ctHyperlink.getLocation();
  85. }
  86. // Try to figure out the type
  87. if (_location.startsWith("http://") || _location.startsWith("https://")
  88. || _location.startsWith("ftp://")) {
  89. _type = HyperlinkType.URL;
  90. } else if (_location.startsWith("mailto:")) {
  91. _type = HyperlinkType.EMAIL;
  92. } else {
  93. _type = HyperlinkType.FILE;
  94. }
  95. }
  96. }
  97. /**
  98. * Create a new XSSFHyperlink. This method is for Internal use only.
  99. * XSSFHyperlinks can be created by {@link XSSFCreationHelper}.
  100. * See the <a href="https://poi.apache.org/spreadsheet/quick-guide.html#Hyperlinks">spreadsheet quick-guide</a>
  101. * for an example.
  102. *
  103. * @param other the hyperlink to copy
  104. */
  105. @Internal //FIXME: change to protected if/when SXSSFHyperlink class is created
  106. public XSSFHyperlink(Hyperlink other) {
  107. if (other instanceof XSSFHyperlink) {
  108. XSSFHyperlink xlink = (XSSFHyperlink) other;
  109. _type = xlink.getTypeEnum();
  110. _location = xlink._location;
  111. _externalRel = xlink._externalRel;
  112. _ctHyperlink = (CTHyperlink) xlink._ctHyperlink.copy();
  113. }
  114. else {
  115. _type = other.getTypeEnum();
  116. _location = other.getAddress();
  117. _externalRel = null;
  118. _ctHyperlink = CTHyperlink.Factory.newInstance();
  119. setCellReference(new CellReference(other.getFirstRow(), other.getFirstColumn()));
  120. }
  121. }
  122. /**
  123. * @return the underlying CTHyperlink object
  124. */
  125. @Internal
  126. public CTHyperlink getCTHyperlink() {
  127. return _ctHyperlink;
  128. }
  129. /**
  130. * Do we need to a relation too, to represent
  131. * this hyperlink?
  132. */
  133. public boolean needsRelationToo() {
  134. return (_type != HyperlinkType.DOCUMENT);
  135. }
  136. /**
  137. * Generates the relation if required
  138. */
  139. protected void generateRelationIfNeeded(PackagePart sheetPart) {
  140. if (_externalRel == null && needsRelationToo()) {
  141. // Generate the relation
  142. PackageRelationship rel =
  143. sheetPart.addExternalRelationship(_location, XSSFRelation.SHEET_HYPERLINKS.getRelation());
  144. // Update the r:id
  145. _ctHyperlink.setId(rel.getId());
  146. }
  147. }
  148. /**
  149. * Return the type of this hyperlink
  150. *
  151. * @return the type of this hyperlink
  152. * @see HyperlinkType#forInt
  153. * @deprecated POI 3.15 beta 3. Use {@link #getTypeEnum()} instead.
  154. * getType will return a HyperlinkType enum in the future.
  155. */
  156. @Override
  157. public int getType() {
  158. return _type.getCode();
  159. }
  160. /**
  161. * Return the type of this hyperlink
  162. *
  163. * @return the type of this hyperlink
  164. */
  165. @Override
  166. public HyperlinkType getTypeEnum() {
  167. return _type;
  168. }
  169. /**
  170. * Get the reference of the cell this applies to,
  171. * es A55
  172. */
  173. public String getCellRef() {
  174. return _ctHyperlink.getRef();
  175. }
  176. /**
  177. * Hyperlink address. Depending on the hyperlink type it can be URL, e-mail, path to a file.
  178. * The is the hyperlink target.
  179. *
  180. * @return the address of this hyperlink
  181. */
  182. @Override
  183. public String getAddress() {
  184. return _location;
  185. }
  186. /**
  187. * Return text label for this hyperlink
  188. *
  189. * @return text to display
  190. */
  191. @Override
  192. public String getLabel() {
  193. return _ctHyperlink.getDisplay();
  194. }
  195. /**
  196. * Location within target. If target is a workbook (or this workbook) this shall refer to a
  197. * sheet and cell or a defined name. Can also be an HTML anchor if target is HTML file.
  198. *
  199. * @return location
  200. */
  201. public String getLocation() {
  202. return _ctHyperlink.getLocation();
  203. }
  204. /**
  205. * Sets text label for this hyperlink
  206. *
  207. * @param label text label for this hyperlink
  208. */
  209. @Override
  210. public void setLabel(String label) {
  211. _ctHyperlink.setDisplay(label);
  212. }
  213. /**
  214. * Location within target. If target is a workbook (or this workbook) this shall refer to a
  215. * sheet and cell or a defined name. Can also be an HTML anchor if target is HTML file.
  216. *
  217. * @param location - string representing a location of this hyperlink
  218. */
  219. public void setLocation(String location) {
  220. _ctHyperlink.setLocation(location);
  221. }
  222. /**
  223. * Hyperlink address. Depending on the hyperlink type it can be URL, e-mail, path to a file
  224. * This is the hyperlink target.
  225. *
  226. * @param address - the address of this hyperlink
  227. */
  228. @Override
  229. public void setAddress(String address) {
  230. validate(address);
  231. _location = address;
  232. //we must set location for internal hyperlinks
  233. if (_type == HyperlinkType.DOCUMENT) {
  234. setLocation(address);
  235. }
  236. }
  237. @SuppressWarnings("fall-through")
  238. private void validate(String address) {
  239. switch (_type) {
  240. // email, path to file and url must be valid URIs
  241. case EMAIL:
  242. case FILE:
  243. case URL:
  244. try {
  245. new URI(address);
  246. } catch (URISyntaxException e) {
  247. throw new IllegalArgumentException("Address of hyperlink must be a valid URI", e);
  248. }
  249. break;
  250. case DOCUMENT:
  251. // currently not evaluating anything.
  252. break;
  253. default:
  254. throw new IllegalStateException("Invalid Hyperlink type: " + _type);
  255. }
  256. }
  257. /**
  258. * Assigns this hyperlink to the given cell reference
  259. */
  260. @Internal
  261. public void setCellReference(String ref) {
  262. _ctHyperlink.setRef(ref);
  263. }
  264. @Internal
  265. protected void setCellReference(CellReference ref) {
  266. setCellReference(ref.formatAsString());
  267. }
  268. private CellReference buildCellReference() {
  269. String ref = _ctHyperlink.getRef();
  270. if (ref == null) {
  271. ref = "A1";
  272. }
  273. return new CellReference(ref);
  274. }
  275. /**
  276. * Return the column of the first cell that contains the hyperlink
  277. *
  278. * @return the 0-based column of the first cell that contains the hyperlink
  279. */
  280. @Override
  281. public int getFirstColumn() {
  282. return buildCellReference().getCol();
  283. }
  284. /**
  285. * Return the column of the last cell that contains the hyperlink
  286. *
  287. * @return the 0-based column of the last cell that contains the hyperlink
  288. */
  289. @Override
  290. public int getLastColumn() {
  291. return buildCellReference().getCol();
  292. }
  293. /**
  294. * Return the row of the first cell that contains the hyperlink
  295. *
  296. * @return the 0-based row of the cell that contains the hyperlink
  297. */
  298. @Override
  299. public int getFirstRow() {
  300. return buildCellReference().getRow();
  301. }
  302. /**
  303. * Return the row of the last cell that contains the hyperlink
  304. *
  305. * @return the 0-based row of the last cell that contains the hyperlink
  306. */
  307. @Override
  308. public int getLastRow() {
  309. return buildCellReference().getRow();
  310. }
  311. /**
  312. * Set the column of the first cell that contains the hyperlink
  313. *
  314. * @param col the 0-based column of the first cell that contains the hyperlink
  315. */
  316. @Override
  317. public void setFirstColumn(int col) {
  318. setCellReference(new CellReference( getFirstRow(), col ));
  319. }
  320. /**
  321. * Set the column of the last cell that contains the hyperlink.
  322. * For XSSF, a Hyperlink may only reference one cell
  323. *
  324. * @param col the 0-based column of the last cell that contains the hyperlink
  325. */
  326. @Override
  327. public void setLastColumn(int col) {
  328. setFirstColumn(col);
  329. }
  330. /**
  331. * Set the row of the first cell that contains the hyperlink
  332. *
  333. * @param row the 0-based row of the first cell that contains the hyperlink
  334. */
  335. @Override
  336. public void setFirstRow(int row) {
  337. setCellReference(new CellReference( row, getFirstColumn() ));
  338. }
  339. /**
  340. * Set the row of the last cell that contains the hyperlink.
  341. * For XSSF, a Hyperlink may only reference one cell
  342. *
  343. * @param row the 0-based row of the last cell that contains the hyperlink
  344. */
  345. @Override
  346. public void setLastRow(int row) {
  347. setFirstRow(row);
  348. }
  349. /**
  350. * @return additional text to help the user understand more about the hyperlink
  351. */
  352. public String getTooltip() {
  353. return _ctHyperlink.getTooltip();
  354. }
  355. /**
  356. * @param text additional text to help the user understand more about the hyperlink
  357. */
  358. public void setTooltip(String text) {
  359. _ctHyperlink.setTooltip(text);
  360. }
  361. }