diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-01-03 11:53:34 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-01-03 11:53:34 +0000 |
| commit | 8733a942ec5e53a1f4d5ac78ffaaf8badfdee2cf (patch) | |
| tree | 4bad0320d2f462d83ed0e50a9431ad149dee2433 /src | |
| parent | df0e65ca725de5fb471abdd7ff8621904e41a209 (diff) | |
Add reading of notes XML
Diffstat (limited to 'src')
3 files changed, 108 insertions, 0 deletions
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<DocumentElement> body; + + public Note(String id, List<DocumentElement> 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<Note> readElement(XmlElement element) { + Iterable<XmlElement> 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<Note> 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<Note> notes = reader.readElement(element); + + assertThat(notes, deepEquals(ImmutableList.of())); + } +} |
