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 12KB

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