aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java')
-rw-r--r--src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java39
1 files changed, 36 insertions, 3 deletions
diff --git a/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java b/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java
index 4a0a169..4ac44d2 100644
--- a/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java
+++ b/src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java
@@ -3,9 +3,11 @@ package org.zwobble.mammoth.docx;
import org.zwobble.mammoth.documents.*;
import org.zwobble.mammoth.results.Result;
import org.zwobble.mammoth.results.Warning;
+import org.zwobble.mammoth.util.InputStreamSupplier;
import org.zwobble.mammoth.util.MammothOptionals;
import org.zwobble.mammoth.xml.XmlElement;
import org.zwobble.mammoth.xml.XmlElementLike;
+import org.zwobble.mammoth.xml.XmlElementList;
import org.zwobble.mammoth.xml.XmlNode;
import java.util.Optional;
@@ -76,6 +78,9 @@ public class BodyXmlReader {
case "v:imagedata":
return readImagedata(element);
+ case "wp:inline":
+ return readInline(element);
+
case "w:ins":
case "w:smartTag":
case "w:drawing":
@@ -245,13 +250,41 @@ public class BodyXmlReader {
private ReadResult readImagedata(XmlElement element) {
Optional<String> title = element.getAttributeOrNone("o:title");
String relationshipId = element.getAttribute("r:id");
- Relationship relationship = relationships.findRelationshipById(relationshipId);
- String imagePath = "word/" + trimLeft(relationship.getTarget(), '/');
+ String imagePath = relationshipIdToDocxPath(relationshipId);
+ return readImage(imagePath, title, () -> file.getInputStream(imagePath));
+ }
+
+ private ReadResult readInline(XmlElement element) {
+ Optional<String> altText = element.findChildOrEmpty("wp:docPr").getAttributeOrNone("descr");
+ XmlElementList blips = element.findChildren("a:graphic")
+ .findChildren("a:graphicData")
+ .findChildren("pic:pic")
+ .findChildren("pic:blipFill")
+ .findChildren("a:blip");
+ return readBlips(blips, altText);
+ }
+
+ private ReadResult readBlips(XmlElementList blips, Optional<String> altText) {
+ return ReadResult.concat(transform(blips, blip -> readBlip(blip, altText)));
+ }
+
+ private ReadResult readBlip(XmlElement blip, Optional<String> altText) {
+ String relationshipId = blip.getAttribute("r:embed");
+ String imagePath = relationshipIdToDocxPath(relationshipId);
+ return readImage(imagePath, altText, () -> file.getInputStream(imagePath));
+ }
+
+ private ReadResult readImage(String imagePath, Optional<String> altText, InputStreamSupplier open) {
Optional<String> contentType = contentTypes.findContentType(imagePath);
- Image image = new Image(title, contentType, () -> file.getInputStream(imagePath));
+ Image image = new Image(altText, contentType, open);
return success(image);
}
+ private String relationshipIdToDocxPath(String relationshipId) {
+ Relationship relationship = relationships.findRelationshipById(relationshipId);
+ return "word/" + trimLeft(relationship.getTarget(), '/');
+ }
+
private Optional<String> readVal(XmlElementLike element, String name) {
return element.findChildOrEmpty(name).getAttributeOrNone("w:val");
}