From 26af8fca2838549a185345458bbdb24db8a62d9b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 31 Dec 2015 21:05:46 +0000 Subject: Add reading of styles XML --- .../java/org/zwobble/mammoth/docx/StylesXml.java | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/main/java/org/zwobble/mammoth/docx/StylesXml.java (limited to 'src/main/java/org/zwobble/mammoth/docx/StylesXml.java') diff --git a/src/main/java/org/zwobble/mammoth/docx/StylesXml.java b/src/main/java/org/zwobble/mammoth/docx/StylesXml.java new file mode 100644 index 0000000..18061d1 --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/docx/StylesXml.java @@ -0,0 +1,31 @@ +package org.zwobble.mammoth.docx; + +import org.zwobble.mammoth.xml.XmlElement; +import org.zwobble.mammoth.xml.XmlElementList; + +import java.util.Map; + +import static com.google.common.collect.Iterables.filter; +import static com.google.common.collect.Maps.immutableEntry; +import static org.zwobble.mammoth.util.MammothMaps.toMap; + +public class StylesXml { + public static Styles readStylesXmlElement(XmlElement element) { + XmlElementList styleElements = element.findChildren("w:style"); + return new Styles( + readStyles(styleElements, "paragraph"), + readStyles(styleElements, "character")); + } + + private static Map readStyles(XmlElementList styleElements, String styleType) { + return toMap( + filter(styleElements, styleElement -> styleElement.getAttribute("w:type").equals(styleType)), + StylesXml::readStyle); + } + + private static Map.Entry readStyle(XmlElement element) { + return immutableEntry( + element.getAttribute("w:styleId"), + new Style(element.findChildOrEmpty("w:name").getAttributeOrNone("w:val"))); + } +} -- cgit v1.2.3