aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-02-20 15:55:36 +0000
committerMichael Williamson <mike@zwobble.org>2016-02-20 15:55:36 +0000
commit9f91b3bb403c0cda34fa91bdbf8cb5ea22cb7525 (patch)
tree18c3e91cca011b83c08018956aaac98940d90b77
parentf61e177abb96034353572e5c32df0a8ae671a4d5 (diff)
Add paragraph matchers for numbering
-rw-r--r--src/main/java/org/zwobble/mammoth/documents/NumberingLevel.java4
-rw-r--r--src/main/java/org/zwobble/mammoth/styles/ParagraphMatcher.java28
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/documents/DocumentElementMakers.java3
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java1
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/styles/ParagraphMatcherTests.java38
5 files changed, 67 insertions, 7 deletions
diff --git a/src/main/java/org/zwobble/mammoth/documents/NumberingLevel.java b/src/main/java/org/zwobble/mammoth/documents/NumberingLevel.java
index cdbb0c4..9baa3fc 100644
--- a/src/main/java/org/zwobble/mammoth/documents/NumberingLevel.java
+++ b/src/main/java/org/zwobble/mammoth/documents/NumberingLevel.java
@@ -5,6 +5,10 @@ public class NumberingLevel {
return new NumberingLevel(levelIndex, true);
}
+ public static NumberingLevel unordered(String levelIndex) {
+ return new NumberingLevel(levelIndex, false);
+ }
+
private final String levelIndex;
private final boolean isOrdered;
diff --git a/src/main/java/org/zwobble/mammoth/styles/ParagraphMatcher.java b/src/main/java/org/zwobble/mammoth/styles/ParagraphMatcher.java
index 3a6432d..5ce1512 100644
--- a/src/main/java/org/zwobble/mammoth/styles/ParagraphMatcher.java
+++ b/src/main/java/org/zwobble/mammoth/styles/ParagraphMatcher.java
@@ -1,5 +1,6 @@
package org.zwobble.mammoth.styles;
+import org.zwobble.mammoth.documents.NumberingLevel;
import org.zwobble.mammoth.documents.Paragraph;
import org.zwobble.mammoth.documents.Style;
@@ -7,26 +8,36 @@ import java.util.Optional;
import java.util.function.BiPredicate;
public class ParagraphMatcher {
- public static final ParagraphMatcher ANY = new ParagraphMatcher(Optional.empty(), Optional.empty());
+ public static final ParagraphMatcher ANY = new ParagraphMatcher(Optional.empty(), Optional.empty(), Optional.empty());
public static ParagraphMatcher styleId(String styleId) {
- return new ParagraphMatcher(Optional.of(styleId), Optional.empty());
+ return new ParagraphMatcher(Optional.of(styleId), Optional.empty(), Optional.empty());
}
public static ParagraphMatcher styleName(String styleName) {
- return new ParagraphMatcher(Optional.empty(), Optional.of(styleName));
+ return new ParagraphMatcher(Optional.empty(), Optional.of(styleName), Optional.empty());
+ }
+
+ public static ParagraphMatcher orderedList(String level) {
+ return new ParagraphMatcher(Optional.empty(), Optional.empty(), Optional.of(NumberingLevel.ordered(level)));
+ }
+
+ public static ParagraphMatcher unorderedList(String level) {
+ return new ParagraphMatcher(Optional.empty(), Optional.empty(), Optional.of(NumberingLevel.unordered(level)));
}
private final Optional<String> styleId;
private final Optional<String> styleName;
+ private final Optional<NumberingLevel> numbering;
- public ParagraphMatcher(Optional<String> styleId, Optional<String> styleName) {
+ public ParagraphMatcher(Optional<String> styleId, Optional<String> styleName, Optional<NumberingLevel> numbering) {
this.styleId = styleId;
this.styleName = styleName;
+ this.numbering = numbering;
}
public boolean matches(Paragraph paragraph) {
- return matchesStyleId(paragraph) && matchesStyleName(paragraph);
+ return matchesStyleId(paragraph) && matchesStyleName(paragraph) && matchesNumbering(paragraph);
}
private boolean matchesStyleId(Paragraph paragraph) {
@@ -37,7 +48,12 @@ public class ParagraphMatcher {
return matches(styleName, paragraph.getStyle().flatMap(Style::getName), String::equalsIgnoreCase);
}
- private boolean matches(Optional<String> required, Optional<String> actual, BiPredicate<String, String> areEqual) {
+ private boolean matchesNumbering(Paragraph paragraph) {
+ return matches(numbering, paragraph.getNumbering(), (first, second) ->
+ first.isOrdered() == second.isOrdered() && first.getLevelIndex().equalsIgnoreCase(second.getLevelIndex()));
+ }
+
+ private <T> boolean matches(Optional<T> required, Optional<T> actual, BiPredicate<T, T> areEqual) {
return required
.map(requiredValue -> actual.map(actualValue -> areEqual.test(requiredValue, actualValue)).orElse(false))
.orElse(true);
diff --git a/src/test/java/org/zwobble/mammoth/tests/documents/DocumentElementMakers.java b/src/test/java/org/zwobble/mammoth/tests/documents/DocumentElementMakers.java
index 1091ab8..a80e2ea 100644
--- a/src/test/java/org/zwobble/mammoth/tests/documents/DocumentElementMakers.java
+++ b/src/test/java/org/zwobble/mammoth/tests/documents/DocumentElementMakers.java
@@ -15,6 +15,7 @@ import static org.zwobble.mammoth.util.MammothLists.list;
public class DocumentElementMakers {
public static final Property<HasChildren, Optional<Style>> STYLE = newProperty();
+ public static final Property<HasChildren, Optional<NumberingLevel>> NUMBERING = newProperty();
public static final Property<HasChildren, Boolean> BOLD = newProperty();
public static final Property<HasChildren, Boolean> ITALIC = newProperty();
public static final Property<HasChildren, Boolean> UNDERLINE = newProperty();
@@ -25,7 +26,7 @@ public class DocumentElementMakers {
public static final Instantiator<Paragraph> PARAGRAPH =
propertyLookup -> new Paragraph(
propertyLookup.valueOf(STYLE, Optional.empty()),
- Optional.empty(),
+ propertyLookup.valueOf(NUMBERING, Optional.empty()),
propertyLookup.valueOf(CHILDREN, list()));
public static final Instantiator<Run> RUN =
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 517773c..2c1d505 100644
--- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java
+++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java
@@ -33,6 +33,7 @@ import static org.zwobble.mammoth.results.Warning.warning;
import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals;
import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.*;
import static org.zwobble.mammoth.tests.docx.BodyXmlReaderMakers.*;
+import static org.zwobble.mammoth.tests.docx.BodyXmlReaderMakers.NUMBERING;
import static org.zwobble.mammoth.util.MammothLists.list;
import static org.zwobble.mammoth.util.MammothMaps.map;
import static org.zwobble.mammoth.xml.XmlNodes.element;
diff --git a/src/test/java/org/zwobble/mammoth/tests/styles/ParagraphMatcherTests.java b/src/test/java/org/zwobble/mammoth/tests/styles/ParagraphMatcherTests.java
index fdaa075..436dc62 100644
--- a/src/test/java/org/zwobble/mammoth/tests/styles/ParagraphMatcherTests.java
+++ b/src/test/java/org/zwobble/mammoth/tests/styles/ParagraphMatcherTests.java
@@ -1,6 +1,7 @@
package org.zwobble.mammoth.tests.styles;
import org.junit.Test;
+import org.zwobble.mammoth.documents.NumberingLevel;
import org.zwobble.mammoth.documents.Paragraph;
import org.zwobble.mammoth.documents.Style;
import org.zwobble.mammoth.styles.ParagraphMatcher;
@@ -12,6 +13,7 @@ import static com.natpryce.makeiteasy.MakeItEasy.make;
import static com.natpryce.makeiteasy.MakeItEasy.with;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.NUMBERING;
import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.PARAGRAPH;
import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.STYLE;
@@ -70,4 +72,40 @@ public class ParagraphMatcherTests {
with(STYLE, Optional.of(new Style("TipsParagraph", Optional.of("Tips Paragraph"))))));
assertTrue(matcher.matches(paragraphWithCorrectStyleName));
}
+
+ @Test
+ public void matcherWithOrderedListOnlyMatchesParagraphsWithOrderedListAtThatLeve() {
+ ParagraphMatcher matcher = ParagraphMatcher.orderedList("4");
+ assertFalse(matcher.matches(make(a(PARAGRAPH))));
+
+ Paragraph paragraphWithCorrectNumbering = make(a(PARAGRAPH,
+ with(NUMBERING, Optional.of(NumberingLevel.ordered("4")))));
+ assertTrue(matcher.matches(paragraphWithCorrectNumbering));
+
+ Paragraph paragraphWithIncorrectLevel = make(a(PARAGRAPH,
+ with(NUMBERING, Optional.of(NumberingLevel.ordered("3")))));
+ assertFalse(matcher.matches(paragraphWithIncorrectLevel));
+
+ Paragraph paragraphWithIncorrectOrdering = make(a(PARAGRAPH,
+ with(NUMBERING, Optional.of(NumberingLevel.unordered("4")))));
+ assertFalse(matcher.matches(paragraphWithIncorrectOrdering));
+ }
+
+ @Test
+ public void matcherWithUnorderedListOnlyMatchesParagraphsWithUnrderedListAtThatLeve() {
+ ParagraphMatcher matcher = ParagraphMatcher.unorderedList("4");
+ assertFalse(matcher.matches(make(a(PARAGRAPH))));
+
+ Paragraph paragraphWithCorrectNumbering = make(a(PARAGRAPH,
+ with(NUMBERING, Optional.of(NumberingLevel.unordered("4")))));
+ assertTrue(matcher.matches(paragraphWithCorrectNumbering));
+
+ Paragraph paragraphWithIncorrectLevel = make(a(PARAGRAPH,
+ with(NUMBERING, Optional.of(NumberingLevel.unordered("3")))));
+ assertFalse(matcher.matches(paragraphWithIncorrectLevel));
+
+ Paragraph paragraphWithIncorrectOrdering = make(a(PARAGRAPH,
+ with(NUMBERING, Optional.of(NumberingLevel.ordered("4")))));
+ assertFalse(matcher.matches(paragraphWithIncorrectOrdering));
+ }
}