您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

XSSFHyperlink.java 12KB

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