aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-02-16 22:01:46 +0000
committerMichael Williamson <mike@zwobble.org>2016-02-16 22:01:46 +0000
commit6181e50cc197f5f9a71374efe256410e1eceffe3 (patch)
tree04178188820ed3b5865dfee48fdf92d159960e2f /src
parent97d89445e8a75c3e1dc596878f1a9a944f9317d5 (diff)
Wire up style reading
Diffstat (limited to 'src')
-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
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/MammothTests.java5
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlReaderMakers.java3
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/docx/InMemoryDocxFile.java6
7 files changed, 42 insertions, 17 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
diff --git a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java
index 2fd6ba0..7e326f6 100644
--- a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java
+++ b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java
@@ -6,7 +6,6 @@ import org.zwobble.mammoth.results.Result;
import java.io.File;
-import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.zwobble.mammoth.results.Result.success;
import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals;
@@ -15,8 +14,8 @@ public class MammothTests {
@Test
public void emptyParagraphsAreIgnoredByDefault() {
assertThat(
- convertToHtml("empty.docx").getValue(),
- is(""));
+ convertToHtml("empty.docx"),
+ deepEquals(success("")));
}
@Test
diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlReaderMakers.java b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlReaderMakers.java
index f19f505..bb77b01 100644
--- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlReaderMakers.java
+++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlReaderMakers.java
@@ -6,6 +6,7 @@ import org.zwobble.mammoth.docx.*;
import java.io.IOException;
import java.io.InputStream;
+import java.util.Optional;
import static com.natpryce.makeiteasy.Property.newProperty;
@@ -25,7 +26,7 @@ public class BodyXmlReaderMakers {
propertyLookup.valueOf(CONTENT_TYPES, ContentTypes.DEFAULT),
propertyLookup.valueOf(DOCX_FILE, new DocxFile() {
@Override
- public InputStream getInputStream(String name) throws IOException {
+ public Optional<InputStream> tryGetInputStream(String name) throws IOException {
throw new UnsupportedOperationException();
}
diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/InMemoryDocxFile.java b/src/test/java/org/zwobble/mammoth/tests/docx/InMemoryDocxFile.java
index 2ed5e27..4aef42e 100644
--- a/src/test/java/org/zwobble/mammoth/tests/docx/InMemoryDocxFile.java
+++ b/src/test/java/org/zwobble/mammoth/tests/docx/InMemoryDocxFile.java
@@ -17,9 +17,9 @@ public class InMemoryDocxFile implements DocxFile {
}
@Override
- public InputStream getInputStream(String name) throws IOException {
- String value = Optional.ofNullable(entries.get(name)).get();
- return new ByteArrayInputStream(value.getBytes(StandardCharsets.UTF_8));
+ public Optional<InputStream> tryGetInputStream(String name) throws IOException {
+ return Optional.ofNullable(entries.get(name))
+ .map(value -> new ByteArrayInputStream(value.getBytes(StandardCharsets.UTF_8)));
}
@Override