Skip to content

Commit

Permalink
Issue #86 - Splitting up GedcomWriter - Pretty much the rest of it
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Harrah (frizbog) committed Jul 11, 2016
1 parent 15a775f commit 32fcb0d
Show file tree
Hide file tree
Showing 20 changed files with 1,087 additions and 668 deletions.
12 changes: 9 additions & 3 deletions src/main/java/org/gedcom4j/writer/AbstractEmitter.java
Expand Up @@ -26,6 +26,7 @@
import java.util.List;

import org.gedcom4j.exception.GedcomWriterException;
import org.gedcom4j.exception.WriterCancelledException;
import org.gedcom4j.model.StringTree;
import org.gedcom4j.model.StringWithCustomTags;
import org.gedcom4j.model.SupportedVersion;
Expand Down Expand Up @@ -69,13 +70,18 @@ abstract class AbstractEmitter<T> {
* write starting at this level
* @param writeFrom
* object to write
* @throws WriterCancelledException
* if cancellation was requested during the operation
*/
protected AbstractEmitter(GedcomWriter baseWriter, int startLevel, T writeFrom) {
protected AbstractEmitter(GedcomWriter baseWriter, int startLevel, T writeFrom) throws WriterCancelledException {
this.baseWriter = baseWriter;
this.startLevel = startLevel;
this.writeFrom = writeFrom;
if (baseWriter != null) {
baseWriter.notifyConstructObserversIfNeeded();
if (baseWriter.isCancelled()) {
throw new WriterCancelledException("Construction of GEDCOM data cancelled");
}
}
}

Expand Down Expand Up @@ -144,8 +150,8 @@ protected void emitLinesOfText(int level, String xref, String startingTag, List<
* the level to write at
* @param strings
* the list of strings to write (with custom tags)
* @param tag
* the tag to use for each string value in the collection
* @param tagValue
* the string value of the tag to use for each value in the collection
* @throws GedcomWriterException
* if the data cannot be written
*/
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/gedcom4j/writer/AddressEmitter.java
Expand Up @@ -23,6 +23,7 @@
package org.gedcom4j.writer;

import org.gedcom4j.exception.GedcomWriterException;
import org.gedcom4j.exception.WriterCancelledException;
import org.gedcom4j.model.Address;

/**
Expand All @@ -41,8 +42,10 @@ class AddressEmitter extends AbstractEmitter<Address> {
* write starting at this level
* @param writeFrom
* object to write
* @throws WriterCancelledException
* if cancellation was requested during the operation
*/
AddressEmitter(GedcomWriter baseWriter, int startLevel, Address writeFrom) {
AddressEmitter(GedcomWriter baseWriter, int startLevel, Address writeFrom) throws WriterCancelledException {
super(baseWriter, startLevel, writeFrom);
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/gedcom4j/writer/ChangeDateEmitter.java
Expand Up @@ -23,6 +23,7 @@
package org.gedcom4j.writer;

import org.gedcom4j.exception.GedcomWriterException;
import org.gedcom4j.exception.WriterCancelledException;
import org.gedcom4j.model.ChangeDate;

/**
Expand All @@ -39,8 +40,10 @@ class ChangeDateEmitter extends AbstractEmitter<ChangeDate> {
* write starting at this level
* @param writeFrom
* object to write
* @throws WriterCancelledException
* if cancellation was requested during the operation
*/
ChangeDateEmitter(GedcomWriter baseWriter, int startLevel, ChangeDate writeFrom) {
ChangeDateEmitter(GedcomWriter baseWriter, int startLevel, ChangeDate writeFrom) throws WriterCancelledException {
super(baseWriter, startLevel, writeFrom);
}

Expand Down
72 changes: 72 additions & 0 deletions src/main/java/org/gedcom4j/writer/EventEmitter.java
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2009-2016 Matthew R. Harrah
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.gedcom4j.writer;

import org.gedcom4j.exception.GedcomWriterException;
import org.gedcom4j.exception.WriterCancelledException;
import org.gedcom4j.model.AbstractEvent;

/**
* An emitter for {@link AbstractEvent}s and their subclasses
*
* @author frizbog
*/
class EventEmitter extends AbstractEmitter<AbstractEvent> {

/**
* Constructor
*
* @param baseWriter
* The base Gedcom writer class this Emitter is partnering with to emit the entire file
* @param startLevel
* write starting at this level
* @param writeFrom
* object to write
* @throws WriterCancelledException
* if cancellation was requested during the operation
*/
EventEmitter(GedcomWriter baseWriter, int startLevel, AbstractEvent writeFrom) throws WriterCancelledException {
super(baseWriter, startLevel, writeFrom);
}

/**
* {@inheritDoc}
*/
@Override
protected void emit() throws GedcomWriterException {
emitTagIfValueNotNull(startLevel, "TYPE", writeFrom.getSubType());
emitTagIfValueNotNull(startLevel, "DATE", writeFrom.getDate());
new PlaceEmitter(baseWriter, startLevel, writeFrom.getPlace()).emit();
new AddressEmitter(baseWriter, startLevel, writeFrom.getAddress()).emit();
emitTagIfValueNotNull(startLevel, "AGE", writeFrom.getAge());
emitTagIfValueNotNull(startLevel, "AGNC", writeFrom.getRespAgency());
emitTagIfValueNotNull(startLevel, "CAUS", writeFrom.getCause());
emitTagIfValueNotNull(startLevel, "RELI", writeFrom.getReligiousAffiliation());
emitTagIfValueNotNull(startLevel, "RESN", writeFrom.getRestrictionNotice());
new SourceCitationEmitter(baseWriter, startLevel, writeFrom.getCitations()).emit();
new MultimediaLinksEmitter(baseWriter, startLevel, writeFrom.getMultimedia()).emit();
new NotesEmitter(baseWriter, startLevel, writeFrom.getNotes()).emit();
emitCustomTags(startLevel, writeFrom.getCustomTags());
}

}
148 changes: 148 additions & 0 deletions src/main/java/org/gedcom4j/writer/FamilyEmitter.java
@@ -0,0 +1,148 @@
/*
* Copyright (c) 2009-2016 Matthew R. Harrah
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.gedcom4j.writer;

import java.util.Collection;

import org.gedcom4j.exception.GedcomWriterException;
import org.gedcom4j.exception.WriterCancelledException;
import org.gedcom4j.model.*;

/**
* Emitter for {@link Family} objects
*
* @author frizbog
*/
class FamilyEmitter extends AbstractEmitter<Collection<Family>> {

/**
* Constructor
*
* @param baseWriter
* The base Gedcom writer class this Emitter is partnering with to emit the entire file
* @param startLevel
* write starting at this level
* @param writeFrom
* object to write
* @throws WriterCancelledException
* if cancellation was requested during the operation
*/
FamilyEmitter(GedcomWriter baseWriter, int startLevel, Collection<Family> writeFrom) throws WriterCancelledException {
super(baseWriter, startLevel, writeFrom);
}

/**
* {@inheritDoc}
*/
@Override
protected void emit() throws GedcomWriterException {
for (Family f : writeFrom) {
emitTag(0, f.getXref(), "FAM");
if (f.getEvents() != null) {
for (FamilyEvent e : f.getEvents()) {
emitFamilyEventStructure(1, e);
}
}
if (f.getHusband() != null) {
emitTagWithRequiredValue(1, "HUSB", f.getHusband().getXref());
}
if (f.getWife() != null) {
emitTagWithRequiredValue(1, "WIFE", f.getWife().getXref());
}
if (f.getChildren() != null) {
for (Individual i : f.getChildren()) {
emitTagWithRequiredValue(1, "CHIL", i.getXref());
}
}
emitTagIfValueNotNull(1, "NCHI", f.getNumChildren());
if (f.getSubmitters() != null) {
for (Submitter s : f.getSubmitters()) {
emitTagWithRequiredValue(1, "SUBM", s.getXref());
}
}
if (f.getLdsSpouseSealings() != null) {
for (LdsSpouseSealing s : f.getLdsSpouseSealings()) {
emitLdsFamilyOrdinance(1, s);
}
}
emitTagIfValueNotNull(1, "RESN", f.getRestrictionNotice());
new SourceCitationEmitter(baseWriter, 1, f.getCitations()).emit();
new MultimediaLinksEmitter(baseWriter, 1, f.getMultimedia()).emit();
new NotesEmitter(baseWriter, 1, f.getNotes()).emit();
if (f.getUserReferences() != null) {
for (UserReference u : f.getUserReferences()) {
emitTagWithRequiredValue(1, "REFN", u.getReferenceNum());
emitTagIfValueNotNull(2, "TYPE", u.getType());
}
}
emitTagIfValueNotNull(1, "RIN", f.getAutomatedRecordId());
new ChangeDateEmitter(baseWriter, 1, f.getChangeDate()).emit();
emitCustomTags(1, f.getCustomTags());
}
}

/**
* Emit a family event structure (see FAMILY_EVENT_STRUCTURE in the GEDCOM spec)
*
* @param level
* the level we're writing at
* @param e
* the event
* @throws GedcomWriterException
* if the data is malformed and cannot be written
*/
private void emitFamilyEventStructure(int level, FamilyEvent e) throws GedcomWriterException {
emitTagWithOptionalValue(level, e.getType().getTag(), e.getyNull());
new EventEmitter(baseWriter, level + 1, e).emit();
if (e.getHusbandAge() != null) {
emitTag(level + 1, "HUSB");
emitTagWithRequiredValue(level + 2, "AGE", e.getHusbandAge());
}
if (e.getWifeAge() != null) {
emitTag(level + 1, "WIFE");
emitTagWithRequiredValue(level + 2, "AGE", e.getWifeAge());
}
}

/**
* Emit the LDS spouse sealing information
*
* @param level
* the level we're writing at
* @param sealings
* the {@link LdsSpouseSealing} structure
* @throws GedcomWriterException
* if the data is malformed and cannot be written
*/
private void emitLdsFamilyOrdinance(int level, LdsSpouseSealing sealings) throws GedcomWriterException {
emitTag(level, "SLGS");
emitTagIfValueNotNull(level + 1, "STAT", sealings.getStatus());
emitTagIfValueNotNull(level + 1, "DATE", sealings.getDate());
emitTagIfValueNotNull(level + 1, "TEMP", sealings.getTemple());
emitTagIfValueNotNull(level + 1, "PLAC", sealings.getPlace());
new SourceCitationEmitter(baseWriter, level + 1, sealings.getCitations()).emit();
new NotesEmitter(baseWriter, level + 1, sealings.getNotes()).emit();
emitCustomTags(level + 1, sealings.getCustomTags());
}

}

0 comments on commit 32fcb0d

Please sign in to comment.