From 2e819654db985bf8a7e7ed76e0a43dc2f46eec8d Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 23 Jan 2017 20:37:05 +0000 Subject: Handle base-relative target URIs --- NEWS | 3 +++ .../mammoth/internal/docx/BodyXmlReader.java | 4 ++-- .../org/zwobble/mammoth/internal/docx/Uris.java | 14 ++++++++++++ .../org/zwobble/mammoth/tests/MammothTests.java | 9 +++++++- .../java/org/zwobble/mammoth/tests/docx/Uris.java | 25 +++++++++++++++++++++ .../tiny-picture-target-base-relative.docx | Bin 0 -> 10881 bytes 6 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/zwobble/mammoth/internal/docx/Uris.java create mode 100644 src/test/java/org/zwobble/mammoth/tests/docx/Uris.java create mode 100644 src/test/resources/test-data/tiny-picture-target-base-relative.docx diff --git a/NEWS b/NEWS index 2fea155..1c21ca1 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,9 @@ * Ignore bold, italic, underline and strikethrough elements that have a value of false or 0. +* Handle absolute paths within zip files. This should fix an issue where some + images within a document couldn't be found. + # 0.0.4 * Add support for comments. diff --git a/src/main/java/org/zwobble/mammoth/internal/docx/BodyXmlReader.java b/src/main/java/org/zwobble/mammoth/internal/docx/BodyXmlReader.java index ec82930..4cbdda7 100644 --- a/src/main/java/org/zwobble/mammoth/internal/docx/BodyXmlReader.java +++ b/src/main/java/org/zwobble/mammoth/internal/docx/BodyXmlReader.java @@ -18,12 +18,12 @@ import java.util.function.Function; import static org.zwobble.mammoth.internal.docx.ReadResult.EMPTY_SUCCESS; import static org.zwobble.mammoth.internal.docx.ReadResult.success; +import static org.zwobble.mammoth.internal.docx.Uris.uriToZipEntryName; import static org.zwobble.mammoth.internal.util.Iterables.lazyFilter; import static org.zwobble.mammoth.internal.util.Lists.list; import static org.zwobble.mammoth.internal.util.Maps.entry; import static org.zwobble.mammoth.internal.util.Maps.lookup; import static org.zwobble.mammoth.internal.util.Sets.set; -import static org.zwobble.mammoth.internal.util.Strings.trimLeft; public class BodyXmlReader { private static final Set IMAGE_TYPES_SUPPORTED_BY_BROWSERS = set( @@ -456,7 +456,7 @@ public class BodyXmlReader { private String relationshipIdToDocxPath(String relationshipId) { Relationship relationship = relationships.findRelationshipById(relationshipId); - return "word/" + trimLeft(relationship.getTarget(), '/'); + return uriToZipEntryName("word", relationship.getTarget()); } private Optional readVal(XmlElementLike element, String name) { diff --git a/src/main/java/org/zwobble/mammoth/internal/docx/Uris.java b/src/main/java/org/zwobble/mammoth/internal/docx/Uris.java new file mode 100644 index 0000000..51dab9c --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/internal/docx/Uris.java @@ -0,0 +1,14 @@ +package org.zwobble.mammoth.internal.docx; + +public class Uris { + private Uris() { + } + + public static String uriToZipEntryName(String base, String uri) { + if (uri.startsWith("/")) { + return uri.substring(1); + } else { + return base + "/" + uri; + } + } +} diff --git a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java index d7a2b66..ac81c76 100644 --- a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java @@ -69,12 +69,19 @@ public class MammothTests { } @Test - public void inlineImagesAreIncludedInOutput() throws IOException { + public void inlineImagesReferencedByPathRelativeToPartAreIncludedInOutput() throws IOException { assertThat( convertToHtml("tiny-picture.docx"), isSuccess("

")); } + @Test + public void inlineImagesReferencedByPathRelativeToBaseAreIncludedInOutput() throws IOException { + assertThat( + convertToHtml("tiny-picture-target-base-relative.docx"), + isSuccess("

")); + } + @Test public void imagesStoredOutsideOfDocumentAreIncludedInOutput() throws IOException { Path tempDirectory = Files.createTempDirectory("mammoth-"); diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/Uris.java b/src/test/java/org/zwobble/mammoth/tests/docx/Uris.java new file mode 100644 index 0000000..36d4bcb --- /dev/null +++ b/src/test/java/org/zwobble/mammoth/tests/docx/Uris.java @@ -0,0 +1,25 @@ +package org.zwobble.mammoth.tests.docx; + +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.zwobble.mammoth.internal.docx.Uris.uriToZipEntryName; + +public class Uris { + @Test + public void whenPathDoesNotHaveLeadingSlashThenPathIsResolvedRelativeToBase() { + assertThat( + uriToZipEntryName("one/two", "three/four"), + equalTo("one/two/three/four") + ); + } + + @Test + public void whenPathHasLeadingSlashThenBaseIsIgnored() { + assertThat( + uriToZipEntryName("one/two", "/three/four"), + equalTo("three/four") + ); + } +} diff --git a/src/test/resources/test-data/tiny-picture-target-base-relative.docx b/src/test/resources/test-data/tiny-picture-target-base-relative.docx new file mode 100644 index 0000000..1d61988 Binary files /dev/null and b/src/test/resources/test-data/tiny-picture-target-base-relative.docx differ -- cgit v1.2.3