Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

XSSFBHyperlinksTable.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.binary;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.util.ArrayList;
  19. import java.util.BitSet;
  20. import java.util.Comparator;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.TreeMap;
  25. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  26. import org.apache.poi.openxml4j.opc.PackagePart;
  27. import org.apache.poi.openxml4j.opc.PackageRelationship;
  28. import org.apache.poi.ss.util.CellAddress;
  29. import org.apache.poi.ss.util.CellRangeAddress;
  30. import org.apache.poi.ss.util.CellRangeUtil;
  31. import org.apache.poi.util.Internal;
  32. import org.apache.poi.xssf.usermodel.XSSFRelation;
  33. @Internal
  34. public class XSSFBHyperlinksTable {
  35. private final static BitSet RECORDS = new BitSet();
  36. static {
  37. RECORDS.set(XSSFBRecordType.BrtHLink.getId());
  38. }
  39. private final List<XSSFHyperlinkRecord> hyperlinkRecords = new ArrayList<XSSFHyperlinkRecord>();
  40. //cache the relId to hyperlink url from the sheet's .rels
  41. private Map<String, String> relIdToHyperlink = new HashMap<String, String>();
  42. public XSSFBHyperlinksTable(PackagePart sheetPart) throws IOException {
  43. //load the urls from the sheet .rels
  44. loadUrlsFromSheetRels(sheetPart);
  45. //now load the hyperlinks from the bottom of the sheet
  46. HyperlinkSheetScraper scraper = new HyperlinkSheetScraper(sheetPart.getInputStream());
  47. scraper.parse();
  48. }
  49. /**
  50. *
  51. * @return a map of the hyperlinks. The key is the top left cell address in their CellRange
  52. */
  53. public Map<CellAddress, List<XSSFHyperlinkRecord>> getHyperLinks() {
  54. Map<CellAddress, List<XSSFHyperlinkRecord>> hyperlinkMap =
  55. new TreeMap<CellAddress, List<XSSFHyperlinkRecord>>(new TopLeftCellAddressComparator());
  56. for (XSSFHyperlinkRecord hyperlinkRecord : hyperlinkRecords) {
  57. CellAddress cellAddress = new CellAddress(hyperlinkRecord.getCellRangeAddress().getFirstRow(),
  58. hyperlinkRecord.getCellRangeAddress().getFirstColumn());
  59. List<XSSFHyperlinkRecord> list = hyperlinkMap.get(cellAddress);
  60. if (list == null) {
  61. list = new ArrayList<XSSFHyperlinkRecord>();
  62. }
  63. list.add(hyperlinkRecord);
  64. hyperlinkMap.put(cellAddress, list);
  65. }
  66. return hyperlinkMap;
  67. }
  68. /**
  69. *
  70. * @param cellAddress cell address to find
  71. * @return null if not a hyperlink
  72. */
  73. public List<XSSFHyperlinkRecord> findHyperlinkRecord(CellAddress cellAddress) {
  74. List<XSSFHyperlinkRecord> overlapping = null;
  75. CellRangeAddress targetCellRangeAddress = new CellRangeAddress(cellAddress.getRow(),
  76. cellAddress.getRow(),
  77. cellAddress.getColumn(),
  78. cellAddress.getColumn());
  79. for (XSSFHyperlinkRecord record : hyperlinkRecords) {
  80. if (CellRangeUtil.intersect(targetCellRangeAddress, record.getCellRangeAddress()) != CellRangeUtil.NO_INTERSECTION) {
  81. if (overlapping == null) {
  82. overlapping = new ArrayList<XSSFHyperlinkRecord>();
  83. }
  84. overlapping.add(record);
  85. }
  86. }
  87. return overlapping;
  88. }
  89. private void loadUrlsFromSheetRels(PackagePart sheetPart) {
  90. try {
  91. for (PackageRelationship rel : sheetPart.getRelationshipsByType(XSSFRelation.SHEET_HYPERLINKS.getRelation())) {
  92. relIdToHyperlink.put(rel.getId(), rel.getTargetURI().toString());
  93. }
  94. } catch (InvalidFormatException e) {
  95. //swallow
  96. }
  97. }
  98. private class HyperlinkSheetScraper extends XSSFBParser {
  99. private XSSFBCellRange hyperlinkCellRange = new XSSFBCellRange();
  100. private final StringBuilder xlWideStringBuffer = new StringBuilder();
  101. HyperlinkSheetScraper(InputStream is) {
  102. super(is, RECORDS);
  103. }
  104. @Override
  105. public void handleRecord(int recordType, byte[] data) throws XSSFBParseException {
  106. if (recordType != XSSFBRecordType.BrtHLink.getId()) {
  107. return;
  108. }
  109. int offset = 0;
  110. String relId = "";
  111. String location = "";
  112. String toolTip = "";
  113. String display = "";
  114. hyperlinkCellRange = XSSFBCellRange.parse(data, offset, hyperlinkCellRange);
  115. offset += XSSFBCellRange.length;
  116. xlWideStringBuffer.setLength(0);
  117. offset += XSSFBUtils.readXLNullableWideString(data, offset, xlWideStringBuffer);
  118. relId = xlWideStringBuffer.toString();
  119. xlWideStringBuffer.setLength(0);
  120. offset += XSSFBUtils.readXLWideString(data, offset, xlWideStringBuffer);
  121. location = xlWideStringBuffer.toString();
  122. xlWideStringBuffer.setLength(0);
  123. offset += XSSFBUtils.readXLWideString(data, offset, xlWideStringBuffer);
  124. toolTip = xlWideStringBuffer.toString();
  125. xlWideStringBuffer.setLength(0);
  126. offset += XSSFBUtils.readXLWideString(data, offset, xlWideStringBuffer);
  127. display = xlWideStringBuffer.toString();
  128. CellRangeAddress cellRangeAddress = new CellRangeAddress(hyperlinkCellRange.firstRow, hyperlinkCellRange.lastRow, hyperlinkCellRange.firstCol, hyperlinkCellRange.lastCol);
  129. String url = relIdToHyperlink.get(relId);
  130. if (location == null || location.length() == 0) {
  131. location = url;
  132. }
  133. hyperlinkRecords.add(
  134. new XSSFHyperlinkRecord(cellRangeAddress, relId, location, toolTip, display)
  135. );
  136. }
  137. }
  138. private static class TopLeftCellAddressComparator implements Comparator<CellAddress> {
  139. @Override
  140. public int compare(CellAddress o1, CellAddress o2) {
  141. if (o1.getRow() < o2.getRow()) {
  142. return -1;
  143. } else if (o1.getRow() > o2.getRow()) {
  144. return 1;
  145. }
  146. if (o1.getColumn() < o2.getColumn()) {
  147. return -1;
  148. } else if (o1.getColumn() > o2.getColumn()) {
  149. return 1;
  150. }
  151. return 0;
  152. }
  153. }
  154. }