diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-01-02 22:32:32 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-01-02 22:32:32 +0000 |
| commit | 98a94a032f08e9f1d0ad6a07ea45ef539dd8eac9 (patch) | |
| tree | c399c299fba1b7865a75f5455ef44a144e0eb0b8 | |
| parent | 871df6bd1a1f7c45e750a49589c7a195c3506205 (diff) | |
Use Style class in Paragraph
8 files changed, 48 insertions, 39 deletions
diff --git a/src/main/java/org/zwobble/mammoth/documents/ParagraphElement.java b/src/main/java/org/zwobble/mammoth/documents/ParagraphElement.java index dcf8dad..22d9306 100644 --- a/src/main/java/org/zwobble/mammoth/documents/ParagraphElement.java +++ b/src/main/java/org/zwobble/mammoth/documents/ParagraphElement.java @@ -4,16 +4,16 @@ import java.util.List; import java.util.Optional; public class ParagraphElement implements DocumentElement, HasChildren { - private final Optional<String> styleId; + private final Optional<Style> style; private final List<DocumentElement> children; - public ParagraphElement(Optional<String> styleId, List<DocumentElement> children) { - this.styleId = styleId; + public ParagraphElement(Optional<Style> style, List<DocumentElement> children) { + this.style = style; this.children = children; } - public Optional<String> getStyleId() { - return styleId; + public Optional<Style> getStyle() { + return style; } public List<DocumentElement> getChildren() { diff --git a/src/main/java/org/zwobble/mammoth/documents/Style.java b/src/main/java/org/zwobble/mammoth/documents/Style.java new file mode 100644 index 0000000..82a25de --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/documents/Style.java @@ -0,0 +1,21 @@ +package org.zwobble.mammoth.documents; + +import java.util.Optional; + +public class Style { + private final String styleId; + private final Optional<String> name; + + public Style(String styleId, Optional<String> name) { + this.styleId = styleId; + this.name = name; + } + + public String getStyleId() { + return styleId; + } + + public Optional<String> getName() { + return name; + } +} diff --git a/src/main/java/org/zwobble/mammoth/docx/BodyXml.java b/src/main/java/org/zwobble/mammoth/docx/BodyXml.java index 8aba487..df98fbc 100644 --- a/src/main/java/org/zwobble/mammoth/docx/BodyXml.java +++ b/src/main/java/org/zwobble/mammoth/docx/BodyXml.java @@ -1,10 +1,7 @@ package org.zwobble.mammoth.docx; import com.google.common.collect.ImmutableList; -import org.zwobble.mammoth.documents.DocumentElement; -import org.zwobble.mammoth.documents.ParagraphElement; -import org.zwobble.mammoth.documents.RunElement; -import org.zwobble.mammoth.documents.TextElement; +import org.zwobble.mammoth.documents.*; import org.zwobble.mammoth.xml.XmlElement; import org.zwobble.mammoth.xml.XmlElementLike; import org.zwobble.mammoth.xml.XmlNode; @@ -39,12 +36,13 @@ public class BodyXml { } private static ParagraphElement readParagraph(XmlElement element) { - return new ParagraphElement(readParagraphStyleId(element), readElements(element.children())); + return new ParagraphElement(readParagraphStyle(element), readElements(element.children())); } - private static Optional<String> readParagraphStyleId(XmlElement paragraph) { + private static Optional<Style> readParagraphStyle(XmlElement paragraph) { XmlElementLike properties = paragraph.findChildOrEmpty("w:pPr"); - return properties.findChildOrEmpty("w:pStyle").getAttributeOrNone("w:val"); + return properties.findChildOrEmpty("w:pStyle").getAttributeOrNone("w:val") + .map(styleId -> new Style(styleId, Optional.empty())); } private static List<DocumentElement> readElements(Iterable<XmlNode> nodes) { diff --git a/src/main/java/org/zwobble/mammoth/docx/Style.java b/src/main/java/org/zwobble/mammoth/docx/Style.java deleted file mode 100644 index 551a93f..0000000 --- a/src/main/java/org/zwobble/mammoth/docx/Style.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.zwobble.mammoth.docx; - -import java.util.Optional; - -public class Style { - private final Optional<String> name; - - public Style(Optional<String> name) { - this.name = name; - } - - public Optional<String> getName() { - return name; - } -} diff --git a/src/main/java/org/zwobble/mammoth/docx/Styles.java b/src/main/java/org/zwobble/mammoth/docx/Styles.java index 89098de..d2311fe 100644 --- a/src/main/java/org/zwobble/mammoth/docx/Styles.java +++ b/src/main/java/org/zwobble/mammoth/docx/Styles.java @@ -1,5 +1,7 @@ package org.zwobble.mammoth.docx; +import org.zwobble.mammoth.documents.Style; + import java.util.Map; import java.util.Optional; diff --git a/src/main/java/org/zwobble/mammoth/docx/StylesXml.java b/src/main/java/org/zwobble/mammoth/docx/StylesXml.java index 18061d1..f7a955d 100644 --- a/src/main/java/org/zwobble/mammoth/docx/StylesXml.java +++ b/src/main/java/org/zwobble/mammoth/docx/StylesXml.java @@ -1,9 +1,11 @@ package org.zwobble.mammoth.docx; +import org.zwobble.mammoth.documents.Style; import org.zwobble.mammoth.xml.XmlElement; import org.zwobble.mammoth.xml.XmlElementList; import java.util.Map; +import java.util.Optional; import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Maps.immutableEntry; @@ -24,8 +26,8 @@ public class StylesXml { } private static Map.Entry<String, Style> readStyle(XmlElement element) { - return immutableEntry( - element.getAttribute("w:styleId"), - new Style(element.findChildOrEmpty("w:name").getAttributeOrNone("w:val"))); + String styleId = element.getAttribute("w:styleId"); + Optional<String> styleName = element.findChildOrEmpty("w:name").getAttributeOrNone("w:val"); + return immutableEntry(styleId, new Style(styleId, styleName)); } } diff --git a/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java b/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java index 54b74ee..2668811 100644 --- a/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java +++ b/src/test/java/org/zwobble/mammoth/tests/DeepReflectionMatcher.java @@ -18,6 +18,10 @@ import static com.google.common.collect.Iterables.transform; import static java.util.Arrays.asList; public class DeepReflectionMatcher<T> extends TypeSafeDiagnosingMatcher<T> { + public static <T> Matcher<T> deepEquals(T value) { + return new DeepReflectionMatcher<>(value); + } + private final T expected; public DeepReflectionMatcher(T expected) { diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java index b0152a5..8c37ea9 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java @@ -4,10 +4,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import org.hamcrest.Matcher; import org.junit.Test; -import org.zwobble.mammoth.documents.DocumentElement; -import org.zwobble.mammoth.documents.ParagraphElement; -import org.zwobble.mammoth.documents.RunElement; -import org.zwobble.mammoth.documents.TextElement; +import org.zwobble.mammoth.documents.*; import org.zwobble.mammoth.tests.DeepReflectionMatcher; import org.zwobble.mammoth.xml.XmlElement; import org.zwobble.mammoth.xml.XmlNode; @@ -18,9 +15,9 @@ import java.util.Optional; import static java.util.Arrays.asList; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasProperty; import static org.zwobble.mammoth.docx.BodyXml.readBodyXmlElement; +import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; import static org.zwobble.mammoth.xml.XmlNodes.element; public class BodyXmlTests { @@ -51,7 +48,7 @@ public class BodyXmlTests { XmlElement element = paragraphXml(); assertThat( readBodyXmlElement(element), - hasStyleId(Optional.empty())); + hasStyle(Optional.empty())); } @Test @@ -61,7 +58,7 @@ public class BodyXmlTests { element("w:pStyle", ImmutableMap.of("w:val", "Heading1")))))); assertThat( readBodyXmlElement(element), - hasStyleId(Optional.of("Heading1"))); + hasStyle(Optional.of(new Style("Heading1", Optional.empty())))); } private XmlElement paragraphXml() { @@ -92,8 +89,8 @@ public class BodyXmlTests { return new DeepReflectionMatcher<>(new TextElement(value)); } - private Matcher<? super DocumentElement> hasStyleId(Optional<String> expected) { - return hasProperty("styleId", equalTo(expected)); + private Matcher<? super DocumentElement> hasStyle(Optional<Style> expected) { + return hasProperty("style", deepEquals(expected)); } private ParagraphElement paragraph(DocumentElement... children) { |
