aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-02-21 09:15:50 +0000
committerMichael Williamson <mike@zwobble.org>2016-02-21 09:15:50 +0000
commit9d5da9d5339055198aac8d0d41c3d33d97344ebd (patch)
tree6a34e31b8df70a654dd4dee1091b617ee2842b4c
parentcfabbcdab4fbd8401569ec5e6e88a6bca913b502 (diff)
Handle errors when reading images
-rw-r--r--src/main/java/org/zwobble/mammoth/DocumentConverter.java4
-rw-r--r--src/main/java/org/zwobble/mammoth/docx/DocumentReader.java8
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/MammothTests.java18
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.<HtmlNode>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;
@@ -85,6 +86,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(
convertToHtml("tiny-picture-custom-content-type.docx"),