From 9d5da9d5339055198aac8d0d41c3d33d97344ebd Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 21 Feb 2016 09:15:50 +0000 Subject: Handle errors when reading images --- .../java/org/zwobble/mammoth/DocumentConverter.java | 4 ++-- .../java/org/zwobble/mammoth/docx/DocumentReader.java | 8 +++++++- .../java/org/zwobble/mammoth/tests/MammothTests.java | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/zwobble/mammoth/DocumentConverter.java b/src/main/java/org/zwobble/mammoth/DocumentConverter.java index 5cf8c9d..154b878 100644 --- a/src/main/java/org/zwobble/mammoth/DocumentConverter.java +++ b/src/main/java/org/zwobble/mammoth/DocumentConverter.java @@ -217,8 +217,8 @@ public class DocumentConverter { return list(Html.selfClosingElement("img", attributes.build())); } catch (IOException exception) { - // TODO: return a result with a warning - throw new RuntimeException(exception); + warnings.add(warning(exception.getMessage())); + return MammothLists.list(); } }) .orElse(list()); diff --git a/src/main/java/org/zwobble/mammoth/docx/DocumentReader.java b/src/main/java/org/zwobble/mammoth/docx/DocumentReader.java index 8fe0919..284dd89 100644 --- a/src/main/java/org/zwobble/mammoth/docx/DocumentReader.java +++ b/src/main/java/org/zwobble/mammoth/docx/DocumentReader.java @@ -22,7 +22,13 @@ public class DocumentReader { Styles styles = readStyles(zipFile); Numbering numbering = readNumbering(zipFile); ContentTypes contentTypes = readContentTypes(zipFile); - FileReader fileReader = uri -> path.get().toUri().resolve(uri).toURL().openStream(); + FileReader fileReader = uri -> { + try { + return path.get().toUri().resolve(uri).toURL().openStream(); + } catch (IOException exception) { + throw new IOException("could not open external image '" + uri + "': " + exception.getMessage()); + } + }; BodyReaders bodyReaders = name -> { Relationships relationships = readRelationships(zipFile, name); return new BodyXmlReader(styles, numbering, relationships, contentTypes, zipFile, fileReader); diff --git a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java index e778630..603d39a 100644 --- a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java @@ -9,6 +9,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertThat; import static org.zwobble.mammoth.results.Result.success; import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; @@ -84,6 +85,23 @@ public class MammothTests { } } + @Test + public void warnIfImagesStoredOutsideOfDocumentAreNotFound() throws IOException { + Path tempDirectory = Files.createTempDirectory("mammoth-"); + try { + Path documentPath = tempDirectory.resolve("external-picture.docx"); + Files.copy(TestData.file("external-picture.docx").toPath(), documentPath); + assertThat( + Mammoth.convertToHtml(documentPath.toFile()), + allOf( + hasProperty("value", equalTo("")), + hasProperty("warnings", contains( + hasProperty("message", startsWith("could not open external image 'tiny-picture.png'")))))); + } finally { + tempDirectory.toFile().delete(); + } + } + @Test public void contentTypesAreRead() { assertThat( -- cgit v1.2.3