aboutsummaryrefslogtreecommitdiffstats
path: root/poi-ooxml/src/main/java/org/apache/poi/ooxml
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2024-04-19 09:23:54 +0000
committerPJ Fanning <fanningpj@apache.org>2024-04-19 09:23:54 +0000
commiteebb3717e04341aa041be14f8322ac4dd835c286 (patch)
tree1cfb0694ca5988f941017b733ec3de9b3834db5e /poi-ooxml/src/main/java/org/apache/poi/ooxml
parentd525d1a5b106ed01b7036dcd4a30d34c5a919809 (diff)
downloadpoi-eebb3717e04341aa041be14f8322ac4dd835c286.tar.gz
poi-eebb3717e04341aa041be14f8322ac4dd835c286.zip
[bug-63189] support hyperlink relationships. Thanks to Ohyoung Kwon. This closes #617
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1917134 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi-ooxml/src/main/java/org/apache/poi/ooxml')
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/ooxml/HyperlinkRelationship.java44
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java90
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/ooxml/ReferenceRelationship.java79
3 files changed, 184 insertions, 29 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/ooxml/HyperlinkRelationship.java b/poi-ooxml/src/main/java/org/apache/poi/ooxml/HyperlinkRelationship.java
new file mode 100644
index 0000000000..e9649b3d52
--- /dev/null
+++ b/poi-ooxml/src/main/java/org/apache/poi/ooxml/HyperlinkRelationship.java
@@ -0,0 +1,44 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.poi.ooxml;
+
+import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
+
+import java.net.URI;
+
+/**
+ * Represents a hyperlink relationship.
+ *
+ * @since POI 5.2.6
+ */
+public class HyperlinkRelationship extends ReferenceRelationship {
+ /**
+ * Initializes a new instance of the HyperlinkRelationship.
+ *
+ * @param hyperlinkUri The target uri of the hyperlink relationship.
+ * @param isExternal Is the URI external.
+ * @param id The relationship ID.
+ */
+ protected HyperlinkRelationship(POIXMLDocumentPart container, URI hyperlinkUri, boolean isExternal, String id) {
+ super(container, hyperlinkUri, isExternal, PackageRelationshipTypes.HYPERLINK_PART, id);
+ }
+
+ @Override
+ public String getRelationshipType() {
+ return PackageRelationshipTypes.HYPERLINK_PART;
+ }
+}
diff --git a/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java b/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java
index c83ccfcaaa..c7c920f4eb 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java
@@ -23,6 +23,7 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Set;
import org.apache.logging.log4j.LogManager;
@@ -38,7 +39,6 @@ import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.opc.TargetMode;
-import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Internal;
import org.apache.poi.xddf.usermodel.chart.XDDFChart;
import org.apache.poi.xssf.usermodel.XSSFRelation;
@@ -59,6 +59,7 @@ public class POIXMLDocumentPart {
private PackagePart packagePart;
private POIXMLDocumentPart parent;
private final Map<String, RelationPart> relations = new LinkedHashMap<>();
+ private final Map<String, ReferenceRelationship> referenceRelationships = new LinkedHashMap<>();
private boolean isCommitted = false;
/**
@@ -640,38 +641,42 @@ public class POIXMLDocumentPart {
// scan breadth-first, so parent-relations are hopefully the shallowest element
for (PackageRelationship rel : rels) {
- if (rel.getTargetMode() == TargetMode.INTERNAL) {
- URI uri = rel.getTargetURI();
-
- // check for internal references (e.g. '#Sheet1!A1')
- PackagePartName relName;
- if (uri.getRawFragment() != null) {
- relName = PackagingURIHelper.createPartName(uri.getPath());
- } else {
- relName = PackagingURIHelper.createPartName(uri);
- }
+ if (Objects.equals(rel.getRelationshipType(), HyperlinkRelationship.HYPERLINK_REL_TYPE)) {
+ referenceRelationships.put(rel.getId(), new HyperlinkRelationship(this, rel.getTargetURI(), rel.getTargetMode() == TargetMode.EXTERNAL, rel.getId()));
+ } else {
+ if (rel.getTargetMode() == TargetMode.INTERNAL) {
+ URI uri = rel.getTargetURI();
+
+ // check for internal references (e.g. '#Sheet1!A1')
+ PackagePartName relName;
+ if (uri.getRawFragment() != null) {
+ relName = PackagingURIHelper.createPartName(uri.getPath());
+ } else {
+ relName = PackagingURIHelper.createPartName(uri);
+ }
- final PackagePart p = packagePart.getPackage().getPart(relName);
- if (p == null) {
- LOG.atError().log("Skipped invalid entry {}", rel.getTargetURI());
- continue;
- }
+ final PackagePart p = packagePart.getPackage().getPart(relName);
+ if (p == null) {
+ LOG.atError().log("Skipped invalid entry {}", rel.getTargetURI());
+ continue;
+ }
- POIXMLDocumentPart childPart = context.get(p);
- if (childPart == null) {
- childPart = factory.createDocumentPart(this, p);
- //here we are checking if part if embedded and excel then set it to chart class
- //so that at the time to writing we can also write updated embedded part
- if (this instanceof XDDFChart && childPart instanceof XSSFWorkbook) {
- ((XDDFChart) this).setWorkbook((XSSFWorkbook) childPart);
+ POIXMLDocumentPart childPart = context.get(p);
+ if (childPart == null) {
+ childPart = factory.createDocumentPart(this, p);
+ //here we are checking if part if embedded and excel then set it to chart class
+ //so that at the time to writing we can also write updated embedded part
+ if (this instanceof XDDFChart && childPart instanceof XSSFWorkbook) {
+ ((XDDFChart) this).setWorkbook((XSSFWorkbook) childPart);
+ }
+ childPart.parent = this;
+ // already add child to context, so other children can reference it
+ context.put(p, childPart);
+ readLater.add(childPart);
}
- childPart.parent = this;
- // already add child to context, so other children can reference it
- context.put(p, childPart);
- readLater.add(childPart);
- }
- addRelation(rel, childPart);
+ addRelation(rel, childPart);
+ }
}
}
@@ -767,4 +772,31 @@ public class POIXMLDocumentPart {
throw new POIXMLException("OOXML file structure broken/invalid", e);
}
}
+
+ public boolean removeReferenceRelationship(String relId) {
+ ReferenceRelationship existing = referenceRelationships.remove(relId);
+ if (existing != null) {
+ packagePart.removeRelationship(relId);
+ return true;
+ }
+
+ return false;
+ }
+
+ public ReferenceRelationship getReferenceRelationship(String relId) {
+ return referenceRelationships.get(relId);
+ }
+
+ public HyperlinkRelationship createHyperlink(URI uri, boolean isExternal, String relId) {
+ PackageRelationship pr = packagePart.addRelationship(uri, isExternal ? TargetMode.EXTERNAL : TargetMode.INTERNAL,
+ HyperlinkRelationship.HYPERLINK_REL_TYPE, relId);
+ HyperlinkRelationship hyperlink = new HyperlinkRelationship(this, uri, isExternal, relId);
+ referenceRelationships.put(relId, hyperlink);
+ return hyperlink;
+ }
+
+ public List<ReferenceRelationship> getReferenceRelationships() {
+ List<ReferenceRelationship> list = new ArrayList<>(referenceRelationships.values());
+ return Collections.unmodifiableList(list);
+ }
}
diff --git a/poi-ooxml/src/main/java/org/apache/poi/ooxml/ReferenceRelationship.java b/poi-ooxml/src/main/java/org/apache/poi/ooxml/ReferenceRelationship.java
new file mode 100644
index 0000000000..12dce49502
--- /dev/null
+++ b/poi-ooxml/src/main/java/org/apache/poi/ooxml/ReferenceRelationship.java
@@ -0,0 +1,79 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.poi.ooxml;
+
+import org.apache.poi.openxml4j.opc.PackageRelationship;
+import org.apache.poi.openxml4j.opc.TargetMode;
+
+import java.net.URI;
+
+/**
+ * Defines a reference relationship. A reference relationship can be internal or external.
+ *
+ * @since POI 5.2.6
+ */
+public abstract class ReferenceRelationship {
+ private POIXMLDocumentPart container;
+ private final String relationshipType;
+ private final boolean external;
+ private final String id;
+ private final URI uri;
+
+ protected ReferenceRelationship(POIXMLDocumentPart container, PackageRelationship packageRelationship) {
+ if (packageRelationship == null) {
+ throw new IllegalArgumentException("packageRelationship");
+ }
+
+ this.container = container;
+ this.relationshipType = packageRelationship.getRelationshipType();
+ this.uri = packageRelationship.getTargetURI();
+ this.external = packageRelationship.getTargetMode() == TargetMode.EXTERNAL;
+ this.id = packageRelationship.getId();
+ }
+
+ protected ReferenceRelationship(POIXMLDocumentPart container, URI targetUri, boolean isExternal, String relationshipType, String id) {
+ if (targetUri == null) {
+ throw new IllegalArgumentException("targetUri");
+ }
+
+ this.container = container;
+ this.relationshipType = relationshipType;
+ this.uri = targetUri;
+ this.id = id;
+ this.external = isExternal;
+ }
+
+ public POIXMLDocumentPart getContainer() {
+ return container;
+ }
+
+ public String getRelationshipType() {
+ return relationshipType;
+ }
+
+ public boolean isExternal() {
+ return external;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public URI getUri() {
+ return uri;
+ }
+}