diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-02-13 23:02:55 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-02-13 23:02:55 +0000 |
| commit | ddde81cf806a5534a01483b563651bd84cf7452d (patch) | |
| tree | 228d3debc22faf3ec1d18cb026300e9ded565ada /src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java | |
| parent | 26ac5524f3667d44bb53fd19d8a770d900847210 (diff) | |
Read embedded blips in wp:inline
Diffstat (limited to 'src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java')
| -rw-r--r-- | src/main/java/org/zwobble/mammoth/docx/BodyXmlReader.java | 39 |
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"); } |
