aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--fop-core/src/main/java/org/apache/fop/render/intermediate/extensions/URIAction.java22
-rw-r--r--fop-core/src/test/java/org/apache/fop/render/intermediate/URIActionTestCase.java8
2 files changed, 29 insertions, 1 deletions
diff --git a/fop-core/src/main/java/org/apache/fop/render/intermediate/extensions/URIAction.java b/fop-core/src/main/java/org/apache/fop/render/intermediate/extensions/URIAction.java
index 9a4960bb9..db920cc7c 100644
--- a/fop-core/src/main/java/org/apache/fop/render/intermediate/extensions/URIAction.java
+++ b/fop-core/src/main/java/org/apache/fop/render/intermediate/extensions/URIAction.java
@@ -19,6 +19,10 @@
package org.apache.fop.render.intermediate.extensions;
+import java.nio.charset.StandardCharsets;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
@@ -47,7 +51,23 @@ public class URIAction extends AbstractAction implements DocumentNavigationExten
this.uri = uri;
this.newWindow = newWindow;
this.altText = altText;
- setID(getIDPrefix() + (uri + newWindow).hashCode());
+ setID(createID(getIDPrefix(), uri + newWindow));
+ }
+
+ private String createID(String idPrefix, String url) {
+ try {
+ MessageDigest md = MessageDigest.getInstance("MD5");
+ byte[] thedigest = md.digest(url.getBytes(StandardCharsets.UTF_8));
+
+ StringBuilder hex = new StringBuilder();
+ for (byte b : thedigest) {
+ hex.append(String.format("%02x", b));
+ }
+
+ return idPrefix + hex;
+ } catch (NoSuchAlgorithmException e) {
+ throw new RuntimeException("MD5 algorithm not found", e);
+ }
}
/**
diff --git a/fop-core/src/test/java/org/apache/fop/render/intermediate/URIActionTestCase.java b/fop-core/src/test/java/org/apache/fop/render/intermediate/URIActionTestCase.java
index 42675f8a0..44eff68a5 100644
--- a/fop-core/src/test/java/org/apache/fop/render/intermediate/URIActionTestCase.java
+++ b/fop-core/src/test/java/org/apache/fop/render/intermediate/URIActionTestCase.java
@@ -32,4 +32,12 @@ public class URIActionTestCase {
Assert.assertEquals(action.getID(), action2.getID());
Assert.assertFalse(action.getID().equals(action3.getID()));
}
+
+ @Test
+ public void testIdEdgeCase() {
+ URIAction action = new URIAction("19", true, null);
+ URIAction action2 = new URIAction("0X", true, null);
+ Assert.assertNotEquals("We can't use the hashcode for the ids as some strings have the same hashcode",
+ action.getID(), action2.getID());
+ }
}