aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/zwobble/mammoth/Mammoth.java22
-rw-r--r--src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java4
-rw-r--r--src/main/java/org/zwobble/mammoth/docx/DocxFile.java8
-rw-r--r--src/main/java/org/zwobble/mammoth/docx/ZippedDocxFile.java11
4 files changed, 35 insertions, 10 deletions
diff --git a/src/main/java/org/zwobble/mammoth/Mammoth.java b/src/main/java/org/zwobble/mammoth/Mammoth.java
index d466bed..5830c6a 100644
--- a/src/main/java/org/zwobble/mammoth/Mammoth.java
+++ b/src/main/java/org/zwobble/mammoth/Mammoth.java
@@ -8,14 +8,13 @@ import org.zwobble.mammoth.xml.XmlElement;
import java.io.File;
import java.io.IOException;
+import java.util.Optional;
import java.util.zip.ZipFile;
public class Mammoth {
public static Result<String> convertToHtml(File file) {
try (DocxFile zipFile = new ZippedDocxFile(new ZipFile(file))) {
- XmlElement documentXml = OfficeXml.parseXml(zipFile.getInputStream("word/document.xml"));
-
- Styles styles = Styles.EMPTY;
+ Styles styles = readStyles(zipFile);
Numbering numbering = Numbering.EMPTY;
Relationships relationships = Relationships.EMPTY;
ContentTypes contentTypes = ContentTypes.DEFAULT;
@@ -32,7 +31,7 @@ public class Mammoth {
fileReader), notes);
// TODO: prefix
String idPrefix = "document";
- return reader.readElement(documentXml)
+ return reader.readElement(parseOfficeXml(zipFile, "word/document.xml"))
.map(nodes -> DocumentConverter.convertToHtml(idPrefix, nodes))
.map(Html::stripEmpty)
.map(Html::collapse)
@@ -41,4 +40,19 @@ public class Mammoth {
throw new UnsupportedOperationException("Should return a result of failure");
}
}
+
+ private static Styles readStyles(DocxFile file) throws IOException {
+ return tryParseOfficeXml(file, "word/styles.xml")
+ .map(StylesXml::readStylesXmlElement)
+ .orElse(Styles.EMPTY);
+ }
+
+ private static Optional<XmlElement> tryParseOfficeXml(DocxFile zipFile, String name) throws IOException {
+ return zipFile.tryGetInputStream(name).map(OfficeXml::parseXml);
+ }
+
+ private static XmlElement parseOfficeXml(DocxFile zipFile, String name) throws IOException {
+ return tryParseOfficeXml(zipFile, name)
+ .orElseThrow(() -> new IOException("Missing entry in file: " + name));
+ }
}
diff --git a/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java b/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java
index 65cbc82..9a1f95b 100644
--- a/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java
+++ b/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java
@@ -274,7 +274,7 @@ public class BodyXmlReader {
Optional<String> title = element.getAttributeOrNone("o:title");
String relationshipId = element.getAttribute("r:id");
String imagePath = relationshipIdToDocxPath(relationshipId);
- return readImage(imagePath, title, () -> file.getInputStream(imagePath));
+ return readImage(imagePath, title, () -> DocxFile.getInputStream(file, imagePath));
}
private ReadResult readInline(XmlElement element) {
@@ -296,7 +296,7 @@ public class BodyXmlReader {
Optional<String> linkRelationshipId = blip.getAttributeOrNone("r:link");
if (embedRelationshipId.isPresent()) {
String imagePath = relationshipIdToDocxPath(embedRelationshipId.get());
- return readImage(imagePath, altText, () -> file.getInputStream(imagePath));
+ return readImage(imagePath, altText, () -> DocxFile.getInputStream(file, imagePath));
} else if (linkRelationshipId.isPresent()) {
String imagePath = relationships.findRelationshipById(linkRelationshipId.get()).getTarget();
return readImage(imagePath, altText, () -> fileReader.getInputStream(imagePath));
diff --git a/src/main/java/org/zwobble/mammoth/docx/DocxFile.java b/src/main/java/org/zwobble/mammoth/docx/DocxFile.java
index b05ee1f..0ef8c23 100644
--- a/src/main/java/org/zwobble/mammoth/docx/DocxFile.java
+++ b/src/main/java/org/zwobble/mammoth/docx/DocxFile.java
@@ -3,7 +3,13 @@ package org.zwobble.mammoth.docx;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
+import java.util.Optional;
public interface DocxFile extends Closeable {
- InputStream getInputStream(String name) throws IOException;
+ static InputStream getInputStream(DocxFile file, String name) throws IOException {
+ return file.tryGetInputStream(name)
+ .orElseThrow(() -> new IOException("Missing entry in file: " + name));
+ }
+
+ Optional<InputStream> tryGetInputStream(String name) throws IOException;
}
diff --git a/src/main/java/org/zwobble/mammoth/docx/ZippedDocxFile.java b/src/main/java/org/zwobble/mammoth/docx/ZippedDocxFile.java
index 6b8528a..c69c147 100644
--- a/src/main/java/org/zwobble/mammoth/docx/ZippedDocxFile.java
+++ b/src/main/java/org/zwobble/mammoth/docx/ZippedDocxFile.java
@@ -2,6 +2,7 @@ package org.zwobble.mammoth.docx;
import java.io.IOException;
import java.io.InputStream;
+import java.util.Optional;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@@ -13,9 +14,13 @@ public class ZippedDocxFile implements DocxFile {
}
@Override
- public InputStream getInputStream(String name) throws IOException {
- ZipEntry entry = zipFile.getEntry("word/document.xml");
- return zipFile.getInputStream(entry);
+ public Optional<InputStream> tryGetInputStream(String name) throws IOException {
+ ZipEntry entry = zipFile.getEntry(name);
+ if (entry == null) {
+ return Optional.empty();
+ } else {
+ return Optional.of(zipFile.getInputStream(entry));
+ }
}
@Override