Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

XSSFHyperlink.java 14KB

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