Skip to content

Commit

Permalink
Read default for complex field checkboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Nov 30, 2024
1 parent 57a988d commit 9272cde
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import org.zwobble.mammoth.internal.documents.*;
import org.zwobble.mammoth.internal.results.InternalResult;
import org.zwobble.mammoth.internal.util.*;
import org.zwobble.mammoth.internal.xml.XmlElement;
import org.zwobble.mammoth.internal.xml.XmlElementLike;
import org.zwobble.mammoth.internal.xml.XmlElementList;
import org.zwobble.mammoth.internal.xml.XmlNode;
import org.zwobble.mammoth.internal.xml.*;

import java.util.*;
import java.util.function.Function;
Expand Down Expand Up @@ -335,10 +332,15 @@ private ReadResult readFieldChar(XmlElement element) {

private ComplexField parseCurrentInstrText(ComplexField complexField) {
String instrText = currentInstrText.toString();
return parseInstrText(instrText, complexField);

XmlElementLike fldChar = complexField instanceof BeginComplexField
? ((BeginComplexField) complexField).fldChar
: NullXmlElement.INSTANCE;

return parseInstrText(instrText, fldChar);
}

private ComplexField parseInstrText(String instrText, ComplexField complexField) {
private ComplexField parseInstrText(String instrText, XmlElementLike fldChar) {
Pattern externalLinkPattern = Pattern.compile("\\s*HYPERLINK \"(.*)\"");
Matcher externalLinkMatcher = externalLinkPattern.matcher(instrText);
if (externalLinkMatcher.lookingAt()) {
Expand All @@ -356,7 +358,13 @@ private ComplexField parseInstrText(String instrText, ComplexField complexField)
Pattern checkboxPattern = Pattern.compile("\\s*FORMCHECKBOX\\s*");
Matcher checkboxMatcher = checkboxPattern.matcher(instrText);
if (checkboxMatcher.lookingAt()) {
return ComplexField.checkbox(false);
XmlElementLike checkboxElement = fldChar
.findChildOrEmpty("w:ffData")
.findChildOrEmpty("w:checkBox");

boolean checked = readBooleanElement(checkboxElement, "w:default");

return ComplexField.checkbox(checked);
}

return ComplexField.UNKNOWN;
Expand Down
74 changes: 74 additions & 0 deletions src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,80 @@ public void complexFieldCheckboxWithSeparateIsRead() {
))
)));
}

@Test
public void complexFieldCheckboxWithoutDefaultNorCheckedIsUnchecked() {
XmlElement element = complexFieldCheckboxParagraph(list(
element("w:checkBox")
));

DocumentElement paragraph = readSuccess(bodyReader(), element);

assertThat(paragraph, isParagraph(hasChildren(
isEmptyRun(),
isEmptyRun(),
isRun(hasChildren(
isCheckbox(false)
))
)));
}

@Test
public void complexFieldCheckboxWithDefault0AndWithoutCheckedIsUnchecked() {
XmlElement element = complexFieldCheckboxParagraph(list(
element("w:checkBox", list(
element("w:default", map("w:val", "0"))
))
));

DocumentElement paragraph = readSuccess(bodyReader(), element);

assertThat(paragraph, isParagraph(hasChildren(
isEmptyRun(),
isEmptyRun(),
isRun(hasChildren(
isCheckbox(false)
))
)));
}

@Test
public void complexFieldCheckboxWithDefault1AndWithoutCheckedIsChecked() {
XmlElement element = complexFieldCheckboxParagraph(list(
element("w:checkBox", list(
element("w:default", map("w:val", "1"))
))
));

DocumentElement paragraph = readSuccess(bodyReader(), element);

assertThat(paragraph, isParagraph(hasChildren(
isEmptyRun(),
isEmptyRun(),
isRun(hasChildren(
isCheckbox(true)
))
)));
}

private XmlElement complexFieldCheckboxParagraph(List<XmlNode> ffDataChildren) {
return element("w:p", list(
element("w:r", list(
element("w:fldChar", map("w:fldCharType", "begin"), list(
element("w:ffData", ffDataChildren)
))
)),
element("w:instrText", list(
textXml(" FORMCHECKBOX ")
)),
element("w:r", list(
element("w:fldChar", map("w:fldCharType", "separate"))
)),
element("w:r", list(
element("w:fldChar", map("w:fldCharType", "end"))
))
));
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ static Matcher<DocumentElement> isHyperlink(Matcher<? super Hyperlink>... matche
return cast(Hyperlink.class, allOf(matchers));
}

@SafeVarargs
static Matcher<DocumentElement> isCheckbox(Matcher<? super Checkbox>... matchers) {
return cast(Checkbox.class, allOf(matchers));
static Matcher<DocumentElement> isCheckbox() {
return instanceOf(Checkbox.class);
}

static Matcher<DocumentElement> isCheckbox(boolean checked) {
return new DeepReflectionMatcher<>(new Checkbox(checked));
}

static Matcher<Hyperlink> hasHref(String href) {
Expand Down

0 comments on commit 9272cde

Please sign in to comment.