diff options
Diffstat (limited to 'src/main')
6 files changed, 67 insertions, 15 deletions
diff --git a/src/main/java/org/zwobble/mammoth/Mammoth.java b/src/main/java/org/zwobble/mammoth/Mammoth.java index 26db504..2201bd2 100644 --- a/src/main/java/org/zwobble/mammoth/Mammoth.java +++ b/src/main/java/org/zwobble/mammoth/Mammoth.java @@ -1,5 +1,6 @@ package org.zwobble.mammoth; +import org.zwobble.mammoth.documents.Notes; import org.zwobble.mammoth.docx.*; import org.zwobble.mammoth.html.Html; import org.zwobble.mammoth.results.Result; @@ -7,7 +8,6 @@ import org.zwobble.mammoth.xml.XmlElement; import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.util.zip.ZipFile; public class Mammoth { @@ -19,11 +19,9 @@ public class Mammoth { Numbering numbering = Numbering.EMPTY; Relationships relationships = Relationships.EMPTY; ContentTypes contentTypes = ContentTypes.DEFAULT; - FileReader fileReader = new FileReader() { - @Override - public InputStream getInputStream(String uri) throws IOException { - throw new UnsupportedOperationException(); - } + Notes notes = Notes.EMPTY; + FileReader fileReader = uri -> { + throw new UnsupportedOperationException(); }; DocumentXmlReader reader = new DocumentXmlReader(new BodyXmlReader( styles, @@ -31,7 +29,7 @@ public class Mammoth { relationships, contentTypes, zipFile, - fileReader)); + fileReader), notes); // TODO: prefix String idPrefix = "document"; return reader.readElement(documentXml) diff --git a/src/main/java/org/zwobble/mammoth/documents/Document.java b/src/main/java/org/zwobble/mammoth/documents/Document.java index 4d91925..c3a14d3 100644 --- a/src/main/java/org/zwobble/mammoth/documents/Document.java +++ b/src/main/java/org/zwobble/mammoth/documents/Document.java @@ -4,12 +4,18 @@ import java.util.List; public class Document implements HasChildren { private final List<DocumentElement> children; + private final Notes notes; - public Document(List<DocumentElement> children) { + public Document(List<DocumentElement> children, Notes notes) { this.children = children; + this.notes = notes; } public List<DocumentElement> getChildren() { return children; } + + public Notes getNotes() { + return notes; + } } diff --git a/src/main/java/org/zwobble/mammoth/documents/Note.java b/src/main/java/org/zwobble/mammoth/documents/Note.java index 1a0d3c0..da6f68f 100644 --- a/src/main/java/org/zwobble/mammoth/documents/Note.java +++ b/src/main/java/org/zwobble/mammoth/documents/Note.java @@ -3,11 +3,21 @@ package org.zwobble.mammoth.documents; import java.util.List; public class Note { + private final NoteType noteType; private final String id; private final List<DocumentElement> body; - public Note(String id, List<DocumentElement> body) { + public Note(NoteType noteType, String id, List<DocumentElement> body) { + this.noteType = noteType; this.id = id; this.body = body; } + + public NoteType getNoteType() { + return noteType; + } + + public String getId() { + return id; + } } diff --git a/src/main/java/org/zwobble/mammoth/documents/Notes.java b/src/main/java/org/zwobble/mammoth/documents/Notes.java new file mode 100644 index 0000000..2959bb0 --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/documents/Notes.java @@ -0,0 +1,27 @@ +package org.zwobble.mammoth.documents; + +import com.google.common.collect.Maps; +import com.google.common.collect.Multimaps; + +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import static org.zwobble.mammoth.util.MammothLists.list; + +public class Notes { + public final static Notes EMPTY = new Notes(list()); + + private final Map<NoteType, Map<String, Note>> notes; + + public Notes(List<Note> notes) { + this.notes = Maps.transformValues( + Multimaps.index(notes, Note::getNoteType).asMap(), + notesOfType -> Maps.uniqueIndex(notesOfType, Note::getId)); + } + + public Optional<Note> findNote(NoteType noteType, String noteId) { + return Optional.ofNullable(notes.get(noteType)) + .flatMap(notesOfType -> Optional.ofNullable(notesOfType.get(noteId))); + } +} diff --git a/src/main/java/org/zwobble/mammoth/docx/DocumentXmlReader.java b/src/main/java/org/zwobble/mammoth/docx/DocumentXmlReader.java index 2d7bec0..5e94e2f 100644 --- a/src/main/java/org/zwobble/mammoth/docx/DocumentXmlReader.java +++ b/src/main/java/org/zwobble/mammoth/docx/DocumentXmlReader.java @@ -1,20 +1,23 @@ package org.zwobble.mammoth.docx; -import org.zwobble.mammoth.results.Result; import org.zwobble.mammoth.documents.Document; +import org.zwobble.mammoth.documents.Notes; +import org.zwobble.mammoth.results.Result; import org.zwobble.mammoth.xml.XmlElement; public class DocumentXmlReader { private final BodyXmlReader bodyReader; + private final Notes notes; - public DocumentXmlReader(BodyXmlReader bodyReader) { + public DocumentXmlReader(BodyXmlReader bodyReader, Notes notes) { this.bodyReader = bodyReader; + this.notes = notes; } public Result<Document> readElement(XmlElement element) { XmlElement body = element.findChild("w:body"); return bodyReader.readElements(body.children()) .toResult() - .map(Document::new); + .map(children -> new Document(children, notes)); } } diff --git a/src/main/java/org/zwobble/mammoth/docx/NotesXmlReader.java b/src/main/java/org/zwobble/mammoth/docx/NotesXmlReader.java index 0838076..726c476 100644 --- a/src/main/java/org/zwobble/mammoth/docx/NotesXmlReader.java +++ b/src/main/java/org/zwobble/mammoth/docx/NotesXmlReader.java @@ -1,5 +1,6 @@ package org.zwobble.mammoth.docx; +import org.zwobble.mammoth.documents.NoteType; import org.zwobble.mammoth.results.Result; import org.zwobble.mammoth.documents.Note; import org.zwobble.mammoth.xml.XmlElement; @@ -10,16 +11,22 @@ import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.transform; public class NotesXmlReader { + public static NotesXmlReader footnote(BodyXmlReader bodyReader) { + return new NotesXmlReader(bodyReader, "footnote", NoteType.FOOTNOTE); + } + private final BodyXmlReader bodyReader; - private final String noteType; + private final String tagName; + private final NoteType noteType; - public NotesXmlReader(BodyXmlReader bodyReader, String noteType) { + private NotesXmlReader(BodyXmlReader bodyReader, String tagName, NoteType noteType) { this.bodyReader = bodyReader; + this.tagName = tagName; this.noteType = noteType; } public Result<List<Note>> readElement(XmlElement element) { - Iterable<XmlElement> elements = filter(element.findChildren("w:" + noteType), this::isNoteElement); + Iterable<XmlElement> elements = filter(element.findChildren("w:" + tagName), this::isNoteElement); return Result.concat(transform(elements, this::readNoteElement)); } @@ -37,6 +44,7 @@ public class NotesXmlReader { return bodyReader.readElements(element.children()) .toResult() .map(children -> new Note( + noteType, element.getAttribute("w:id"), children)); } |
