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.

XSLFHyperlink.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.xslf.usermodel;
  16. import java.net.URI;
  17. import org.apache.poi.common.usermodel.HyperlinkType;
  18. import org.apache.poi.openxml4j.opc.PackagePart;
  19. import org.apache.poi.openxml4j.opc.PackagePartName;
  20. import org.apache.poi.openxml4j.opc.PackageRelationship;
  21. import org.apache.poi.openxml4j.opc.TargetMode;
  22. import org.apache.poi.sl.usermodel.Hyperlink;
  23. import org.apache.poi.sl.usermodel.Slide;
  24. import org.apache.poi.util.Internal;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTHyperlink;
  26. public class XSLFHyperlink implements Hyperlink<XSLFShape,XSLFTextParagraph> {
  27. final XSLFSheet _sheet;
  28. final CTHyperlink _link;
  29. XSLFHyperlink(CTHyperlink link, XSLFSheet sheet){
  30. _sheet = sheet;
  31. _link = link;
  32. }
  33. @Internal
  34. public CTHyperlink getXmlObject(){
  35. return _link;
  36. }
  37. @Override
  38. public void setAddress(String address) {
  39. linkToUrl(address);
  40. }
  41. @Override
  42. public String getAddress() {
  43. String id = _link.getId();
  44. if (id == null || "".equals(id)) {
  45. return _link.getAction();
  46. }
  47. URI targetURI = _sheet.getPackagePart().getRelationship(id).getTargetURI();
  48. return targetURI.toASCIIString();
  49. }
  50. @Override
  51. public String getLabel() {
  52. return _link.getTooltip();
  53. }
  54. @Override
  55. public void setLabel(String label) {
  56. _link.setTooltip(label);
  57. }
  58. /* (non-Javadoc)
  59. * @deprecated POI 3.15. Use {@link #getTypeEnum()} instead.
  60. * Will return a HyperlinkType enum in the future
  61. */
  62. @Override
  63. public int getType() {
  64. return getTypeEnum().getCode();
  65. }
  66. @Override
  67. public HyperlinkType getTypeEnum() {
  68. String action = _link.getAction();
  69. if (action == null) {
  70. action = "";
  71. }
  72. if (action.equals("ppaction://hlinksldjump") || action.startsWith("ppaction://hlinkshowjump")) {
  73. return HyperlinkType.DOCUMENT;
  74. }
  75. String address = getAddress();
  76. if (address == null) {
  77. address = "";
  78. }
  79. if (address.startsWith("mailto:")) {
  80. return HyperlinkType.EMAIL;
  81. } else {
  82. return HyperlinkType.URL;
  83. }
  84. }
  85. @Override
  86. public void linkToEmail(String emailAddress) {
  87. linkToExternal("mailto:"+emailAddress);
  88. setLabel(emailAddress);
  89. }
  90. @Override
  91. public void linkToUrl(String url) {
  92. linkToExternal(url);
  93. setLabel(url);
  94. }
  95. private void linkToExternal(String url) {
  96. PackagePart thisPP = _sheet.getPackagePart();
  97. if (_link.isSetId() && !_link.getId().isEmpty()) {
  98. thisPP.removeRelationship(_link.getId());
  99. }
  100. PackageRelationship rel = thisPP.addExternalRelationship(url, XSLFRelation.HYPERLINK.getRelation());
  101. _link.setId(rel.getId());
  102. if (_link.isSetAction()) {
  103. _link.unsetAction();
  104. }
  105. }
  106. @Override
  107. public void linkToSlide(Slide<XSLFShape,XSLFTextParagraph> slide) {
  108. PackagePart thisPP = _sheet.getPackagePart();
  109. PackagePartName otherPPN = ((XSLFSheet)slide).getPackagePart().getPartName();
  110. if (_link.isSetId() && !_link.getId().isEmpty()) {
  111. thisPP.removeRelationship(_link.getId());
  112. }
  113. PackageRelationship rel =
  114. thisPP.addRelationship(otherPPN, TargetMode.INTERNAL, XSLFRelation.SLIDE.getRelation());
  115. _link.setId(rel.getId());
  116. _link.setAction("ppaction://hlinksldjump");
  117. }
  118. @Override
  119. public void linkToNextSlide() {
  120. linkToRelativeSlide("nextslide");
  121. }
  122. @Override
  123. public void linkToPreviousSlide() {
  124. linkToRelativeSlide("previousslide");
  125. }
  126. @Override
  127. public void linkToFirstSlide() {
  128. linkToRelativeSlide("firstslide");
  129. }
  130. @Override
  131. public void linkToLastSlide() {
  132. linkToRelativeSlide("lastslide");
  133. }
  134. private void linkToRelativeSlide(String jump) {
  135. PackagePart thisPP = _sheet.getPackagePart();
  136. if (_link.isSetId() && !_link.getId().isEmpty()) {
  137. thisPP.removeRelationship(_link.getId());
  138. }
  139. _link.setId("");
  140. _link.setAction("ppaction://hlinkshowjump?jump="+jump);
  141. }
  142. }