aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2024-02-18 11:11:55 +0000
committerMichael Williamson <mike@zwobble.org>2024-02-18 11:11:55 +0000
commitcb8360ca25dd74e3afa82fb5626df932f71013e8 (patch)
tree46c2a5d34120ac8ed4e22d956dc72dbb2e6ea67e
parent33359a6d13376f64e8cd3e41f185fc6d23fb8524 (diff)
Throw error when failing to find the body element in a document
-rw-r--r--NEWS2
-rw-r--r--src/main/java/org/zwobble/mammoth/internal/docx/DocumentXmlReader.java11
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/docx/DocumentXmlTests.java37
3 files changed, 38 insertions, 12 deletions
diff --git a/NEWS b/NEWS
index 9977f85..a5855c9 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,8 @@
* Support attributes in HTML paths in style mappings.
+* Throw error when failing to find the body element in a document.
+
# 1.6.0
* Support merged paragraphs when revisions are tracked.
diff --git a/src/main/java/org/zwobble/mammoth/internal/docx/DocumentXmlReader.java b/src/main/java/org/zwobble/mammoth/internal/docx/DocumentXmlReader.java
index 25855f3..e2db2e7 100644
--- a/src/main/java/org/zwobble/mammoth/internal/docx/DocumentXmlReader.java
+++ b/src/main/java/org/zwobble/mammoth/internal/docx/DocumentXmlReader.java
@@ -5,9 +5,9 @@ import org.zwobble.mammoth.internal.documents.Document;
import org.zwobble.mammoth.internal.documents.Notes;
import org.zwobble.mammoth.internal.results.InternalResult;
import org.zwobble.mammoth.internal.xml.XmlElement;
-import org.zwobble.mammoth.internal.xml.XmlElementLike;
import java.util.List;
+import java.util.Optional;
public class DocumentXmlReader {
private final BodyXmlReader bodyReader;
@@ -21,8 +21,13 @@ public class DocumentXmlReader {
}
public InternalResult<Document> readElement(XmlElement element) {
- XmlElementLike body = element.findChildOrEmpty("w:body");
- return bodyReader.readElements(body.getChildren())
+ Optional<XmlElement> body = element.findChild("w:body");
+
+ if (!body.isPresent()) {
+ throw new IllegalArgumentException("Could not find the body element: are you sure this is a docx file?");
+ }
+
+ return bodyReader.readElements(body.get().getChildren())
.toResult()
.map(children -> new Document(children, notes, comments));
}
diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/DocumentXmlTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/DocumentXmlTests.java
index 3aa6a62..92674d0 100644
--- a/src/test/java/org/zwobble/mammoth/tests/docx/DocumentXmlTests.java
+++ b/src/test/java/org/zwobble/mammoth/tests/docx/DocumentXmlTests.java
@@ -11,6 +11,7 @@ import org.zwobble.mammoth.internal.xml.XmlElement;
import org.zwobble.mammoth.internal.xml.XmlNodes;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
import static org.zwobble.mammoth.internal.util.Lists.list;
import static org.zwobble.mammoth.internal.xml.XmlNodes.element;
import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals;
@@ -19,22 +20,21 @@ import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.document
import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.paragraphWithText;
import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.withChildren;
import static org.zwobble.mammoth.tests.docx.BodyXmlReaderMakers.bodyReader;
+import static org.zwobble.mammoth.tests.util.MammothAsserts.assertThrows;
public class DocumentXmlTests {
-
@Test
- public void canReadTextWithinDocument() {
- XmlElement documentElement = element("w:document", list(
- element("w:body", list(
- element("w:p", list(
- element("w:r", list(
- element("w:t", list(
- XmlNodes.text("Hello!")))))))))));
+ public void whenBodyElementIsPresentThenBodyIsRead() {
+ XmlElement textXml = element("w:t", list(XmlNodes.text("Hello!")));
+ XmlElement runXml = element("w:r", list(textXml));
+ XmlElement paragraphXml = element("w:p", list(runXml));
+ XmlElement bodyXml = element("w:body", list(paragraphXml));
+ XmlElement documentXml = element("w:document", list(bodyXml));
DocumentXmlReader reader = new DocumentXmlReader(bodyReader(), Notes.EMPTY, list());
assertThat(
- reader.readElement(documentElement),
+ reader.readElement(documentXml),
isInternalSuccess(document(
withChildren(paragraphWithText("Hello!"))
))
@@ -42,6 +42,25 @@ public class DocumentXmlTests {
}
@Test
+ public void whenBodyElementIsNotPresentThenErrorIsThrown() {
+ XmlElement paragraphXml = element("w:p");
+ XmlElement bodyXml = element("w:body2", list(paragraphXml));
+ XmlElement documentXml = element("w:document", list(bodyXml));
+
+ DocumentXmlReader reader = new DocumentXmlReader(bodyReader(), Notes.EMPTY, list());
+
+ IllegalArgumentException exception = assertThrows(
+ IllegalArgumentException.class,
+ () -> reader.readElement(documentXml)
+ );
+
+ assertThat(
+ exception.getMessage(),
+ equalTo("Could not find the body element: are you sure this is a docx file?")
+ );
+ }
+
+ @Test
public void notesOfDocumentAreIncludedInDocument() {
Note note = new Note(NoteType.FOOTNOTE, "4", list(paragraphWithText("Hello")));
Notes notes = new Notes(list(note));