From 8733a942ec5e53a1f4d5ac78ffaaf8badfdee2cf Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 3 Jan 2016 11:53:34 +0000 Subject: Add reading of notes XML --- .../java/org/zwobble/mammoth/documents/Note.java | 13 ++++++ .../org/zwobble/mammoth/docx/NotesXmlReader.java | 41 ++++++++++++++++ .../mammoth/tests/docx/NotesXmlReaderTests.java | 54 ++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 src/main/java/org/zwobble/mammoth/documents/Note.java create mode 100644 src/main/java/org/zwobble/mammoth/docx/NotesXmlReader.java create mode 100644 src/test/java/org/zwobble/mammoth/tests/docx/NotesXmlReaderTests.java (limited to 'src') diff --git a/src/main/java/org/zwobble/mammoth/documents/Note.java b/src/main/java/org/zwobble/mammoth/documents/Note.java new file mode 100644 index 0000000..1a0d3c0 --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/documents/Note.java @@ -0,0 +1,13 @@ +package org.zwobble.mammoth.documents; + +import java.util.List; + +public class Note { + private final String id; + private final List body; + + public Note(String id, List body) { + this.id = id; + this.body = body; + } +} diff --git a/src/main/java/org/zwobble/mammoth/docx/NotesXmlReader.java b/src/main/java/org/zwobble/mammoth/docx/NotesXmlReader.java new file mode 100644 index 0000000..06d17ca --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/docx/NotesXmlReader.java @@ -0,0 +1,41 @@ +package org.zwobble.mammoth.docx; + +import com.google.common.collect.ImmutableList; +import org.zwobble.mammoth.documents.Note; +import org.zwobble.mammoth.xml.XmlElement; + +import java.util.List; + +import static com.google.common.collect.Iterables.filter; +import static com.google.common.collect.Iterables.transform; + +public class NotesXmlReader { + private final BodyXmlReader bodyReader; + private final String noteType; + + public NotesXmlReader(BodyXmlReader bodyReader, String noteType) { + this.bodyReader = bodyReader; + this.noteType = noteType; + } + + public List readElement(XmlElement element) { + Iterable elements = filter(element.findChildren("w:" + noteType), this::isNoteElement); + return ImmutableList.copyOf(transform(elements, this::readNoteElement)); + } + + private boolean isNoteElement(XmlElement element) { + return element.getAttributeOrNone("w:type") + .map(type -> !isSeparatorType(type)) + .orElse(true); + } + + private boolean isSeparatorType(String type) { + return type.equals("continuationSeparator") || type.equals("separator"); + } + + private Note readNoteElement(XmlElement element) { + return new Note( + element.getAttribute("w:id"), + bodyReader.readElements(element.children())); + } +} diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/NotesXmlReaderTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/NotesXmlReaderTests.java new file mode 100644 index 0000000..e1f65ab --- /dev/null +++ b/src/test/java/org/zwobble/mammoth/tests/docx/NotesXmlReaderTests.java @@ -0,0 +1,54 @@ +package org.zwobble.mammoth.tests.docx; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import org.junit.Test; +import org.zwobble.mammoth.documents.Note; +import org.zwobble.mammoth.documents.Paragraph; +import org.zwobble.mammoth.docx.BodyXmlReader; +import org.zwobble.mammoth.docx.NotesXmlReader; +import org.zwobble.mammoth.xml.XmlElement; + +import java.util.List; +import java.util.Optional; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; +import static org.zwobble.mammoth.xml.XmlNodes.element; + +public class NotesXmlReaderTests { + @Test + public void idAndBodyOfFootnoteAreRead() { + XmlElement element = element("w:footnotes", ImmutableList.of( + element("w:footnote", ImmutableMap.of("w:id", "1"), ImmutableList.of( + element("w:p"))))); + + NotesXmlReader reader = new NotesXmlReader(new BodyXmlReader(null), "footnote"); + List notes = reader.readElement(element); + + assertThat( + notes, + deepEquals(ImmutableList.of(new Note("1", ImmutableList.of(new Paragraph(Optional.empty(), ImmutableList.of())))))); + } + + @Test + public void continuationSeparatorIsIgnored() { + assertFootnoteTypeIsIgnored("continuationSeparator"); + } + + @Test + public void separatorIsIgnored() { + assertFootnoteTypeIsIgnored("separator"); + } + + private void assertFootnoteTypeIsIgnored(String noteType) { + XmlElement element = element("w:footnotes", ImmutableList.of( + element("w:footnote", ImmutableMap.of("w:id", "1", "w:type", noteType), ImmutableList.of( + element("w:p"))))); + + NotesXmlReader reader = new NotesXmlReader(new BodyXmlReader(null), "footnote"); + List notes = reader.readElement(element); + + assertThat(notes, deepEquals(ImmutableList.of())); + } +} -- cgit v1.2.3