Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

remove all gedcom-lib stuff

  • Loading branch information...
commit 64f7730a197b0171568dd44cc645373faeab9f56 1 parent 76269c1
Christopher Mosher authored
Showing with 9 additions and 4,563 deletions.
  1. +5 −25 pom.xml
  2. +0 −238 src/main/java/nu/mine/mosher/collection/TreeNode.java
  3. +0 −228 src/main/java/nu/mine/mosher/gedcom/Gedcom.java
  4. +0 −78 src/main/java/nu/mine/mosher/gedcom/GedcomConcatenator.java
  5. +0 −229 src/main/java/nu/mine/mosher/gedcom/GedcomLine.java
  6. +0 −203 src/main/java/nu/mine/mosher/gedcom/GedcomParser.java
  7. +0 −95 src/main/java/nu/mine/mosher/gedcom/GedcomTag.java
  8. +0 −123 src/main/java/nu/mine/mosher/gedcom/GedcomTree.java
  9. +0 −158 src/main/java/nu/mine/mosher/gedcom/date/DatePeriod.java
  10. +0 −257 src/main/java/nu/mine/mosher/gedcom/date/DateRange.java
  11. +0 −378 src/main/java/nu/mine/mosher/gedcom/date/YMD.java
  12. +0 −38 src/main/java/nu/mine/mosher/gedcom/exception/GedcomParseException.java
  13. +0 −17 src/main/java/nu/mine/mosher/gedcom/exception/IllegalLevel.java
  14. +0 −16 src/main/java/nu/mine/mosher/gedcom/exception/InvalidID.java
  15. +0 −15 src/main/java/nu/mine/mosher/gedcom/exception/InvalidLevel.java
  16. +0 −17 src/main/java/nu/mine/mosher/gedcom/exception/MissingTag.java
  17. +4 −3 src/main/java/nu/mine/mosher/gedcom/servlet/GedcomServlet.java
  18. +0 −520 src/main/java/nu/mine/mosher/gedcom/servlet/Loader.java
  19. +0 −66 src/main/java/nu/mine/mosher/gedcom/servlet/struct/Event.java
  20. +0 −70 src/main/java/nu/mine/mosher/gedcom/servlet/struct/EventNames.java
  21. +0 −63 src/main/java/nu/mine/mosher/gedcom/servlet/struct/FamilyEvent.java
  22. +0 −33 src/main/java/nu/mine/mosher/gedcom/servlet/struct/GedcomFile.java
  23. +0 −72 src/main/java/nu/mine/mosher/gedcom/servlet/struct/Partnership.java
  24. +0 −391 src/main/java/nu/mine/mosher/gedcom/servlet/struct/Person.java
  25. +0 −109 src/main/java/nu/mine/mosher/gedcom/servlet/struct/Source.java
  26. +0 −151 src/main/java/nu/mine/mosher/time/Time.java
  27. +0 −407 src/main/javacc/GedcomDateValueParser.jj
  28. +0 −215 src/test/java/nu/mine/mosher/collection/TreeNodeTest.java
  29. +0 −100 src/test/java/nu/mine/mosher/gedcom/date/YMDTest.java
  30. +0 −173 src/test/java/nu/mine/mosher/gedcom/date/parser/GedcomDateValueParserTest.java
  31. +0 −75 src/test/java/nu/mine/mosher/time/TimeTest.java
View
30 pom.xml
@@ -35,19 +35,6 @@
<finalName>genealogy</finalName>
<plugins>
<plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>javacc-maven-plugin</artifactId>
- <version>2.6</version>
- <executions>
- <execution>
- <phase>generate-sources</phase>
- <goals>
- <goal>javacc</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
@@ -122,6 +109,11 @@
</build>
<dependencies>
<dependency>
+ <groupId>nu.mine.mosher.gedcom</groupId>
+ <artifactId>gedcom-lib</artifactId>
+ <version>[1.0.0-SNAPSHOT,)</version>
+ </dependency>
+ <dependency>
<groupId>net.sourceforge.templat</groupId>
<artifactId>templat</artifactId>
<version>[1.0,)</version>
@@ -132,17 +124,5 @@
<version>6.0</version>
<scope>provided</scope>
</dependency>
- <dependency>
- <groupId>org.hamcrest</groupId>
- <artifactId>hamcrest-library</artifactId>
- <version>1.2.1</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.10</version>
- <scope>test</scope>
- </dependency>
</dependencies>
</project>
View
238 src/main/java/nu/mine/mosher/collection/TreeNode.java
@@ -1,238 +0,0 @@
-package nu.mine.mosher.collection;
-
-
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-
-
-/**
- * Represents a node in a tree, and the (sub-)tree rooted at that node.
- * @author Chris Mosher
- * @param <T> type of object in each node
- */
-public class TreeNode<T> implements Iterable<TreeNode<T>>
-{
- private T object;
-
- private TreeNode<T> parent;
- private final List<TreeNode<T>> children = new ArrayList<>();
-
- /**
- * Constructs a node, with no children, and no wrapped object.
- */
- public TreeNode()
- {
- this(null);
- }
-
- /**
- * Constructs a node, with no children, and the given wrapped object.
- * @param object the object that this node will wrap
- */
- public TreeNode(final T object)
- {
- this.object = object;
- }
-
- /**
- * Returns the object this node wraps, as passed in to the constructor.
- * Returns <code>null</code> if this node does not wrap an object.
- * @return the wrapped object
- */
- public T getObject()
- {
- return this.object;
- }
-
- /**
- * Sets (or resets) the object this node wraps.
- * @param object the object that this node will wrap
- */
- public void setObject(final T object)
- {
- this.object = object;
- }
-
- /**
- * Gets the (immediate) children of this node, as an iterator.
- * @return iterator of immediate children
- */
- public Iterator<TreeNode<T>> children()
- {
- return this.children.iterator();
- }
-
- /**
- * Gets the count of (immediate) children of this node.
- * @return the count of immediate children
- */
- public int getChildCount()
- {
- return this.children.size();
- }
-
- /**
- * Adds a child to this node. The given child will be removed from any
- * parent it may have.
- * @param child the child to add
- */
- public void addChild(final TreeNode<T> child)
- {
- if (child.parent != null)
- {
- child.removeFromParent();
- }
-
- this.children.add(child);
- child.parent = this;
- }
-
- /**
- * Indicates the attempt to remove a child of a node, when that child is not
- * actually a child of the node.
- * @author Chris Mosher
- */
- public static class NotChild extends Exception
- {
- private NotChild(final TreeNode<?> node, final TreeNode<?> child)
- {
- super("given TreeNode (" + child + ") is not a child of this TreeNode (" + node + ")");
- }
- }
-
- /**
- * Removes the given child from this node.
- * @param child the child to remove
- * @throws NotChild if this given <code>child</code> is not a child of this node
- */
- @SuppressWarnings("synthetic-access")
- public void removeChild(final TreeNode<T> child) throws NotChild
- {
- if (child.parent != this)
- {
- throw new NotChild(this, child);
- }
-
- for (final Iterator<TreeNode<T>> i = children(); i.hasNext();)
- {
- final TreeNode<T> childN = i.next();
- if (childN == child)
- {
- i.remove();
- child.parent = null;
- }
- }
- }
-
- /**
- * Gets this node's parent, if any.
- * @return this node's parent, or <code>null</code> if this node has no
- * parent.
- */
- public TreeNode<T> parent()
- {
- return this.parent;
- }
-
- /**
- * Removes this node from any parent it may have. If this node does not have
- * a parent, then this method just returns without doing anything.
- */
- public void removeFromParent()
- {
- if (this.parent == null)
- {
- return;
- }
-
- try
- {
- this.parent.removeChild(this);
- }
- catch (final NotChild e)
- {
- throw new IllegalStateException(e);
- }
- }
-
- /**
- * Returns what this node's wrapped object's <code>toString</code> method
- * returns, or "[null]" if this node does not have a parent
- * @return same as wrapped object's <code>toString</code>, or "[null]"
- */
- @Override
- public String toString()
- {
- final StringBuilder sb = new StringBuilder();
- try
- {
- appendStringShallow(sb);
- }
- catch (final IOException e)
- {
- throw new IllegalStateException(e);
- }
- return sb.toString();
- }
-
- /**
- * Builds an outline-style string representation of the tree rooted at this
- * node, and appends it to the given <code>Appendable</code>
- * @param appendTo <code>Appendable</code> to append the tree to
- * @throws IOException if an I/O exception occurs while appending to
- * <code>appendTo</code>
- */
- public void appendStringDeep(final Appendable appendTo) throws IOException
- {
- appendStringDeep(appendTo, 0);
- }
-
- private void appendStringDeep(final Appendable appendTo, final int level) throws IOException
- {
- for (int i = 0; i < level; ++i)
- {
- appendTo.append(" ");
- }
-
- appendStringShallow(appendTo);
- appendTo.append("\n");
-
- for (final TreeNode<T> child : this.children)
- {
- child.appendStringDeep(appendTo, level + 1);
- }
- }
-
- /**
- * Builds the same string as {@link TreeNode#toString}, and appends it to
- * the given <code>Appendable</code>
- * @param appendTo <code>Appendable</code> to append the string to
- * @throws IOException if an I/O exception occurs while appending to
- * <code>appendTo</code>
- */
- public void appendStringShallow(final Appendable appendTo) throws IOException
- {
- if (this.object != null)
- {
- appendTo.append(this.object.toString());
- }
- else
- {
- appendTo.append("[null]");
- }
- }
-
- /**
- * Same as {@link TreeNode#children()}
- * @return iterator of immediate children
- */
- @Override
- public Iterator<TreeNode<T>> iterator()
- {
- return children();
- }
-}
View
228 src/main/java/nu/mine/mosher/gedcom/Gedcom.java
@@ -1,228 +0,0 @@
-package nu.mine.mosher.gedcom;
-
-
-
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
-
-import nu.mine.mosher.gedcom.exception.InvalidLevel;
-
-
-
-/**
- * Handles reading in a GEDCOM file and parsing into an internal representation.
- * Still a work-in-progress.
- * @author Chris Mosher
- */
-public final class Gedcom
-{
- private Gedcom()
- {
- throw new UnsupportedOperationException();
- }
-
- public static void main(String[] rArg) throws InvalidLevel, IOException
- {
- if (rArg.length != 1)
- {
- throw new IllegalArgumentException("usage: java Gedcom gedcomfile");
- }
-
- GedcomTree gt = parseFile(new File(rArg[0]));
- System.out.println(gt.toString());
- }
-
- public static GedcomTree parseFile(File in) throws IOException,
- UnsupportedEncodingException, FileNotFoundException, InvalidLevel
- {
- String charset = getCharset(in);
- return readTree(in, charset);
- }
-
- protected static GedcomTree readTree(File fileIn, String charset)
- throws UnsupportedEncodingException, FileNotFoundException,
- InvalidLevel
- {
- BufferedReader in = null;
- try
- {
- in = new BufferedReader(new InputStreamReader(new FileInputStream(
- fileIn), charset));
- return readTree(in);
- }
- finally
- {
- if (in != null)
- {
- try
- {
- in.close();
- }
- catch (Throwable ignore)
- {
- ignore.printStackTrace();
- }
- }
- }
- }
-
- public static GedcomTree readTree(final BufferedReader reader)
- throws InvalidLevel
- {
- final GedcomParser parser = new GedcomParser(reader);
-
- final GedcomTree tree = new GedcomTree();
- for (final GedcomLine line : parser)
- {
- tree.appendLine(line);
- }
-
- final GedcomConcatenator concat = new GedcomConcatenator(tree);
- concat.concatenate();
-
- return tree;
- }
-
- public static String getCharset(final File f) throws IOException
- {
- InputStream in = null;
- try
- {
- in = new FileInputStream(f);
- return guessGedcomCharset(in);
- }
- finally
- {
- if (in != null)
- {
- try
- {
- in.close();
- }
- catch (Throwable ignore)
- {
- ignore.printStackTrace();
- }
- }
- }
- }
-
- public static String guessGedcomCharset(final InputStream in)
- throws IOException
- {
- // read first four bytes of input stream
- int b0 = in.read();
- if (b0 == -1)
- return "";
- int b1 = in.read();
- if (b1 == -1)
- return "";
- int b2 = in.read();
- if (b2 == -1)
- return "";
- int b3 = in.read();
- if (b3 == -1)
- return "";
-
- // build a word from the first two bytes,
- // assuming little-endian byte order
- int w0 = 0;
- w0 |= b1;
- w0 <<= 8;
- w0 |= b0;
-
- // build a longword from the first four bytes,
- // assuming little-endian byte order
- int i0 = 0;
- i0 |= b3;
- i0 <<= 8;
- i0 |= b2;
- i0 <<= 8;
- i0 |= b1;
- i0 <<= 8;
- i0 |= b0;
-
- if (i0 == 0x0000feff || i0 == 0x00000030)
- {
- return "UTF-32";
- }
-
- if (i0 == 0xfffe0000 || i0 == 0x30000000)
- {
- return "UTF-32";
- }
-
- if (w0 == 0x0000feff || w0 == 0x00000030)
- {
- return "UTF-16";
- }
-
- if (w0 == 0x0000fffe || w0 == 0x00003000)
- {
- return "UTF-16";
- }
-
- if (b0 == 0x000000ef && b1 == 0x000000bb && b2 == 0x000000bf)
- {
- return "UTF-8";
- }
-
- BufferedReader bin = new BufferedReader(new InputStreamReader(in,
- "US-ASCII"));
- bin.readLine(); // ignore rest of header line
-
- String s = bin.readLine();
- while (s != null && s.length() > 0 && s.charAt(0) != '0')
- {
- if (s.startsWith("1 CHAR"))
- {
- s = s.toUpperCase();
- if (s.indexOf("WIN", 6) >= 0)
- {
- return "windows-1252";
- }
- if (s.indexOf("ANSI", 6) >= 0)
- {
- return "windows-1252";
- }
- if (s.indexOf("UTF-8", 6) >= 0)
- {
- return "UTF-8";
- }
- if (s.indexOf("DOS", 6) >= 0)
- {
- return "Cp850";
- }
- if (s.indexOf("PC", 6) >= 0)
- {
- return "Cp850";
- }
- if (s.indexOf("OEM", 6) >= 0)
- {
- return "Cp850";
- }
- if (s.indexOf("ASCII", 6) >= 0)
- {
- return "windows-1252";
- }
- if (s.indexOf("MAC", 6) >= 0)
- {
- return "MacRoman";
- }
- if (s.indexOf("ANSEL", 6) >= 0)
- {
- return "x-gedcom-ansel";
- }
- }
- s = bin.readLine();
- }
-
- return "US-ASCII";
- }
-}
View
78 src/main/java/nu/mine/mosher/gedcom/GedcomConcatenator.java
@@ -1,78 +0,0 @@
-package nu.mine.mosher.gedcom;
-
-
-
-import java.util.ArrayList;
-import java.util.List;
-
-import nu.mine.mosher.collection.TreeNode;
-
-
-
-/**
- * Handles CONT and CONC tags in a given <code>GedcomTree</code> by appending
- * their values to the previous <code>GedcomLine</code>.
- * @author Chris Mosher
- */
-class GedcomConcatenator
-{
- private final GedcomTree tree;
-
- /**
- * @param tree
- */
- public GedcomConcatenator(final GedcomTree tree)
- {
- this.tree = tree;
- }
-
- /**
- *
- */
- public void concatenate()
- {
- concatenateHelper(this.tree.getRoot());
- }
-
- private static void concatenateHelper(final TreeNode<GedcomLine> nodeParent)
- {
- final List<TreeNode<GedcomLine>> rToBeRemoved = new ArrayList<>();
-
- for (final TreeNode<GedcomLine> nodeChild : nodeParent)
- {
- // TODO: remove recursion
- concatenateHelper(nodeChild);
-
- final GedcomLine lineChild = nodeChild.getObject();
-
- final GedcomTag tag = lineChild.getTag();
-
- switch (tag)
- {
- case CONT:
- {
- nodeParent.setObject(nodeParent.getObject().contValue(
- lineChild.getValue()));
- rToBeRemoved.add(nodeChild);
- }
- break;
-
- case CONC:
- {
- nodeParent.setObject(nodeParent.getObject().concValue(
- lineChild.getValue()));
- rToBeRemoved.add(nodeChild);
- }
- break;
-
- default:
- // we don't do anything with tags other than CONT or CONC
- }
- }
-
- for (final TreeNode<GedcomLine> nodeRemove : rToBeRemoved)
- {
- nodeRemove.removeFromParent();
- }
- }
-}
View
229 src/main/java/nu/mine/mosher/gedcom/GedcomLine.java
@@ -1,229 +0,0 @@
-package nu.mine.mosher.gedcom;
-
-
-
-/**
- * Represents one GEDCOM entry (usually one line). Objects of this class are
- * immutable.
- * @author Chris Mosher
- */
-public class GedcomLine
-{
- private final int level;
- private final String id;
- private final String tagString;
- private final String value;
- private final String pointer;
- private final GedcomTag tag;
-
- /**
- * Initializes a <code>GedcomLine</code>.
- * @param level
- * @param id
- * @param tag
- * @param value
- */
- GedcomLine(final int level, final String id, final String tag,
- final String value)
- {
- this.level = level;
- this.id = getPointer(id);
- this.tagString = tag;
- final String v = getPointer(value);
- if (v.length() > 0)
- {
- this.pointer = v;
- this.value = "";
- }
- else
- {
- this.value = replaceAts(value);
- this.pointer = "";
- }
-
- this.tag = parseTag();
- }
-
- private GedcomLine(final String id, final int level, final String pointer,
- final GedcomTag tag, final String tagString, final String value)
- {
- this.id = id;
- this.level = level;
- this.pointer = pointer;
- this.tag = tag;
- this.tagString = tagString;
- this.value = value;
- }
-
- private GedcomTag parseTag()
- {
- try
- {
- return GedcomTag.valueOf(this.tagString);
- }
- catch (final IllegalArgumentException e)
- {
- return GedcomTag.UNKNOWN;
- }
- }
-
- private static String getPointer(final String s)
- {
- if (!s.startsWith("@") || !s.endsWith("@") || s.length() < 3)
- {
- return "";
- }
-
- final String pointer = s.substring(1, s.length() - 1);
- if (pointer.indexOf('@') >= 0)
- {
- return "";
- }
- return pointer;
- }
-
- private static String replaceAts(final String s)
- {
- return s.replaceAll("@@", "@");
- }
-
- @Override
- public String toString()
- {
- final StringBuilder sb = new StringBuilder(256);
- if (hasID())
- {
- sb.append(this.id);
- sb.append(": ");
- }
- sb.append(this.tagString);
- sb.append(" ");
- if (isPointer())
- {
- sb.append("--> ");
- sb.append(this.pointer);
- }
- else
- {
- appendFilteredValue(this.value, sb);
- }
- return sb.toString();
- }
-
- public void dump(final StringBuilder appendTo)
- {
- appendTo.append(this.level);
- appendTo.append(",");
- if (hasID())
- {
- appendTo.append("id=");
- appendTo.append(this.id);
- appendTo.append(",");
- }
- appendTo.append("tag=");
- appendTo.append(this.tagString);
- appendTo.append(",");
- if (isPointer())
- {
- appendTo.append("pointer=");
- appendTo.append(this.pointer);
- }
- else
- {
- appendTo.append("value=\"");
- appendFilteredValue(this.value, appendTo);
- appendTo.append("\"");
- }
- }
-
- private static void appendFilteredValue(final String value,
- final StringBuilder appendTo)
- {
- appendTo.append(value.replaceAll("\n", "[NEWLINE]"));
- }
-
- /**
- * @return if this line has an ID
- */
- public boolean hasID()
- {
- return this.id.length() > 0;
- }
-
- /**
- * @return if this line has a pointer
- */
- public boolean isPointer()
- {
- return this.pointer.length() > 0;
- }
-
- /**
- * @return the ID for this line
- */
- public String getID()
- {
- return this.id;
- }
-
- /**
- * @return the level number of this line
- */
- public int getLevel()
- {
- return this.level;
- }
-
- /**
- * @return the pointer value, if any, in this line
- */
- public String getPointer()
- {
- return this.pointer;
- }
-
- /**
- * @return the GEDCOM tag on this line
- */
- public GedcomTag getTag()
- {
- return this.tag;
- }
-
- public String getTagString()
- {
- return this.tagString;
- }
-
- /**
- * @return the actual value of this line
- */
- public String getValue()
- {
- return this.value;
- }
-
- /**
- * Handles CONT tags by appending the given string to the value of this
- * line, and returning a new <code>GedcomLine</code>.
- * @param sContinuedLine
- * @return new <code>GedcomLine</code>
- */
- GedcomLine contValue(final String sContinuedLine)
- {
- return new GedcomLine(this.id, this.level, this.pointer, this.tag,
- this.tagString, this.value + "\n" + sContinuedLine);
- }
-
- /**
- * Handles CONC tags by appending the given string to the value of this
- * line, and returning a new <code>GedcomLine</code>.
- * @param sConcatenatedLine
- * @return new <code>GedcomLine</code>
- */
- GedcomLine concValue(final String sConcatenatedLine)
- {
- return new GedcomLine(this.id, this.level, this.pointer, this.tag,
- this.tagString, this.value + sConcatenatedLine);
- }
-}
View
203 src/main/java/nu/mine/mosher/gedcom/GedcomParser.java
@@ -1,203 +0,0 @@
-package nu.mine.mosher.gedcom;
-
-
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.util.Iterator;
-import java.util.NoSuchElementException;
-import java.util.StringTokenizer;
-
-import nu.mine.mosher.gedcom.exception.GedcomParseException;
-import nu.mine.mosher.gedcom.exception.IllegalLevel;
-import nu.mine.mosher.gedcom.exception.InvalidID;
-import nu.mine.mosher.gedcom.exception.MissingTag;
-
-
-
-/**
- * A tool to parse a GEDCOM document. Given a <code>Reader</code> containing a
- * GEDCOM transmission, this parser returns a series of <code>GedcomLine</code>
- * objects representing the lines of the transmission.
- * @author Chris Mosher
- */
-class GedcomParser implements Iterable<GedcomLine>
-{
- private final BufferedReader in;
-
- /**
- * Initializes the <code>GedcomParser</code> to read lines from the given
- * GEDCOM transmission.
- * @param in the GEDCOM transmission to read from
- */
- public GedcomParser(final BufferedReader in)
- {
- this.in = in;
- }
-
- @Override
- public Iterator<GedcomLine> iterator()
- {
- return new Iter();
- }
-
- private class Iter implements Iterator<GedcomLine>
- {
- private GedcomLine lineNext;
- private GedcomParseException exception;
-
- @SuppressWarnings("synthetic-access")
- private void prepareNext()
- {
- try
- {
- this.lineNext = nextLine();
- this.exception = null;
- }
- catch (final GedcomParseException e)
- {
- this.lineNext = null;
- this.exception = e;
- }
- }
-
- private void checkNext() throws NoSuchElementException
- {
- if (this.lineNext == null)
- {
- final NoSuchElementException noElement = new NoSuchElementException();
- noElement.initCause(this.exception);
- throw noElement;
- }
- }
-
- /**
- *
- */
- public Iter()
- {
- prepareNext();
- }
-
- @Override
- public boolean hasNext()
- {
- return this.lineNext != null;
- }
-
- @Override
- public GedcomLine next() throws NoSuchElementException
- {
- checkNext();
- final GedcomLine returned = this.lineNext;
- prepareNext();
-
- return returned;
- }
-
- @Override
- public void remove() throws UnsupportedOperationException
- {
- throw new UnsupportedOperationException();
- }
- }
-
- /**
- * Parses the next line from this parser's GEDCOM transmission.
- * @return a (new) <code>GedcomLine</code> object representing the next
- * GEDCOM line read from the transmission. Returns <code>null</code>
- * at the end of the transmission.
- * @throws GedcomParseException
- */
- private GedcomLine nextLine() throws GedcomParseException
- {
- final String sLine = getNextNonblankLine();
- if (sLine == null)
- {
- return null;
- }
-
- return parseLine(sLine);
- }
-
- private String getNextNonblankLine() throws GedcomParseException
- {
- try
- {
- String s = this.in.readLine();
- while (s != null && s.trim().length() == 0)
- {
- s = this.in.readLine();
- }
- return s;
- }
- catch (final IOException e)
- {
- throw new GedcomParseException("Error reading from input source.",
- e);
- }
- }
-
- private static GedcomLine parseLine(final String sLine)
- throws IllegalLevel, MissingTag, InvalidID
- {
- final StringTokenizer st = new StringTokenizer(sLine);
- if (!st.hasMoreTokens())
- {
- // should never happen, because lines with only white-space
- // are skipped in the read loop above.
- throw new IllegalLevel(sLine, new GedcomLine(-1, "", "", ""));
- }
-
- final String sLevel = st.nextToken();
- int level = -1;
- try
- {
- level = Integer.parseInt(sLevel);
- }
- catch (final NumberFormatException e)
- {
- level = -1;
- }
-
- if (!st.hasMoreTokens()) // missing tag
- {
- throw new MissingTag(sLine, new GedcomLine(level, "", "", ""));
- }
- final String sID, sTag;
- final String sIDorTag = st.nextToken();
- if (sIDorTag.startsWith("@"))
- {
- sID = sIDorTag;
- if (!st.hasMoreTokens()) // missing tag
- {
- throw new MissingTag(sLine, new GedcomLine(level, sID, "", ""));
- }
- sTag = st.nextToken();
- }
- else
- {
- sID = "";
- sTag = sIDorTag;
- }
-
- String sValue = "";
- if (st.hasMoreTokens())
- {
- sValue = st.nextToken("\0"); // rest of line
- sValue = sValue.substring(1); // skip one space after tag
- }
-
- if (level < 0 || 99 < level)
- {
- throw new IllegalLevel(sLine, new GedcomLine(level, sID, sTag,
- sValue));
- }
- if (level > 0 && sID.length() > 0)
- {
- throw new InvalidID(sLine, new GedcomLine(level, sID, sTag, sValue));
- }
-
- return new GedcomLine(level, sID, sTag, sValue);
- }
-}
View
95 src/main/java/nu/mine/mosher/gedcom/GedcomTag.java
@@ -1,95 +0,0 @@
-/*
- * Created on Aug 7, 2005
- */
-package nu.mine.mosher.gedcom;
-
-
-
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Set;
-
-
-
-/**
- * TODO
- * @author Chris Mosher
- */
-public enum GedcomTag
-{
- UNKNOWN, ABBR, ADDR, ADR1, ADR2, ADOP, AFN, AGE, AGNC, ALIA, ANCE, ANCI, ANUL, ASSO, AUTH, BAPL, BAPM, BARM, BASM, BIRT, BLES, BLOB, BURI, CALN, CAST, CAUS, CENS, CHAN, CHAR, CHIL, CHR, CHRA, CITY, CONC, CONF, CONL, CONT, COPR, CORP, CREM, CTRY, DATA, DATE, DEAT, DESC, DESI, DEST, DIV, DIVF, DSCR, EDUC, EMIG, ENDL, ENGA, EVEN, FAM, FAMC, FAMF, FAMS, FCOM, FILE, FORM, GEDC, GIVN, GRAD, HEAD, HUSB, IDNO, IMMI, INDI, LANG, LEGA, MARB, MARC, MARL, MARR, MARS, MEDI, NAME, NATI, NATU, NCHI, NICK, NMR, NOTE, NPFX, NSFX, OBJE, OCCU, ORDI, ORDN, PAGE, PEDI, PHON, PLAC, POST, PROB, PROP, PUBL, QUAY, REFN, RELA, RELI, REPO, RESI, RESN, RETI, RFN, RIN, ROLE, SEX, SLGC, SLGS, SOUR, SPFX, SSN, STAE, STAT, SUBM, SUBN, SURN, TEMP, TEXT, TIME, TITL, TRLR, TYPE, VERS, WIFE, WILL, _UID, ;
-
- public static final Set<GedcomTag> setIndividualEvent;
- static
- {
- final Set<GedcomTag> set = new HashSet<>();
-
- set.add(GedcomTag.BIRT);
- set.add(GedcomTag.CHR);
- set.add(GedcomTag.DEAT);
- set.add(GedcomTag.BURI);
- set.add(GedcomTag.CREM);
- set.add(GedcomTag.ADOP);
- set.add(GedcomTag.BAPM);
- set.add(GedcomTag.BARM);
- set.add(GedcomTag.BASM);
- set.add(GedcomTag.BLES);
- set.add(GedcomTag.CHRA);
- set.add(GedcomTag.CONF);
- set.add(GedcomTag.FCOM);
- set.add(GedcomTag.ORDN);
- set.add(GedcomTag.NATU);
- set.add(GedcomTag.EMIG);
- set.add(GedcomTag.IMMI);
- set.add(GedcomTag.CENS);
- set.add(GedcomTag.PROB);
- set.add(GedcomTag.WILL);
- set.add(GedcomTag.GRAD);
- set.add(GedcomTag.RETI);
- set.add(GedcomTag.EVEN);
-
- setIndividualEvent = Collections.<GedcomTag> unmodifiableSet(set);
- }
-
- public static final Set<GedcomTag> setIndividualAttribute;
- static
- {
- final Set<GedcomTag> set = new HashSet<>();
-
- set.add(GedcomTag.CAST);
- set.add(GedcomTag.DSCR);
- set.add(GedcomTag.EDUC);
- set.add(GedcomTag.IDNO);
- set.add(GedcomTag.NATI);
- set.add(GedcomTag.NCHI);
- set.add(GedcomTag.NMR);
- set.add(GedcomTag.OCCU);
- set.add(GedcomTag.PROP);
- set.add(GedcomTag.RELI);
- set.add(GedcomTag.RESI);
- set.add(GedcomTag.SSN);
- set.add(GedcomTag.TITL);
-
- setIndividualAttribute = Collections.<GedcomTag> unmodifiableSet(set);
- }
-
- public static final Set<GedcomTag> setFamilyEvent;
- static
- {
- final Set<GedcomTag> set = new HashSet<>();
-
- set.add(GedcomTag.ANUL);
- set.add(GedcomTag.CENS);
- set.add(GedcomTag.DIV);
- set.add(GedcomTag.DIVF);
- set.add(GedcomTag.ENGA);
- set.add(GedcomTag.MARR);
- set.add(GedcomTag.MARB);
- set.add(GedcomTag.MARC);
- set.add(GedcomTag.MARL);
- set.add(GedcomTag.MARS);
- set.add(GedcomTag.EVEN);
-
- setFamilyEvent = Collections.<GedcomTag> unmodifiableSet(set);
- }
-}
View
123 src/main/java/nu/mine/mosher/gedcom/GedcomTree.java
@@ -1,123 +0,0 @@
-package nu.mine.mosher.gedcom;
-
-
-
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-
-import nu.mine.mosher.collection.TreeNode;
-import nu.mine.mosher.gedcom.exception.InvalidLevel;
-
-
-
-/**
- * Represents a GEDCOM document. A GEDCOM document is a tree structure or
- * <code>GedcomLine</code> objects.
- * @author Chris Mosher
- */
-public class GedcomTree
-{
- private final TreeNode<GedcomLine> root;
- private final Map<String, TreeNode<GedcomLine>> mapIDtoNode = new HashMap<>();
-
- private int prevLevel;
- private TreeNode<GedcomLine> prevNode;
-
- /**
- * Initializes a new <code>GedcomTree</code>.
- */
- public GedcomTree()
- {
- this.root = new TreeNode<>();
- this.prevNode = this.root;
- this.prevLevel = -1;
- }
-
- /**
- * Appends a <code>GedcomLine</code> to this tree. This method must be
- * called in the same sequence that GEDCOM lines appear in the file.
- * @param line GEDCOM line to be appended to this tree.
- * @throws InvalidLevel if the <code>line</code>'s level is invalid (that
- * is, in the wrong sequence to be correct within the context of
- * the lines added to this tree so far)
- */
- void appendLine(final GedcomLine line) throws InvalidLevel
- {
- final int cPops = this.prevLevel + 1 - line.getLevel();
- if (cPops < 0)
- {
- throw new InvalidLevel(line);
- }
-
- TreeNode<GedcomLine> parent = this.prevNode;
- for (int i = 0; i < cPops; ++i)
- {
- parent = parent.parent();
- }
-
- this.prevLevel = line.getLevel();
- this.prevNode = new TreeNode<>();
- this.prevNode.setObject(line);
- parent.addChild(this.prevNode);
-
- if (line.hasID())
- {
- this.mapIDtoNode.put(line.getID(), this.prevNode);
- }
- }
-
- /**
- * Gets the node in this <code>GedcomTree</code> with the given ID.
- * @param id ID of the GEDCOM node to look up
- * @return the node with the given ID.
- */
- public TreeNode<GedcomLine> getNode(final String id)
- {
- return this.mapIDtoNode.get(id);
- }
-
- /**
- * Returns a string representation of this tree. The string returned is
- * intended for debugging purposes, not for any kind of persistence.
- * @return string representation of this tree
- */
- @Override
- public String toString()
- {
- final StringBuilder sb = new StringBuilder(1024);
-
- try
- {
- this.root.appendStringDeep(sb);
-
- sb.append("--------map-of-IDs-to-Nodes--------\n");
- for (final Map.Entry<String, TreeNode<GedcomLine>> entry : this.mapIDtoNode.entrySet())
- {
- sb.append(entry.getKey());
- sb.append(" --> ");
- entry.getValue().appendStringShallow(sb);
- sb.append("\n");
- }
- }
- catch (final IOException e)
- {
- /*
- * StringBuffer does not throw IOException, so this should never
- * happen.
- */
- throw new IllegalStateException(e);
- }
-
- return sb.toString();
- }
-
- /**
- * Gets the root of this tree.
- * @return root node
- */
- public TreeNode<GedcomLine> getRoot()
- {
- return this.root;
- }
-}
View
158 src/main/java/nu/mine/mosher/gedcom/date/DatePeriod.java
@@ -1,158 +0,0 @@
-/*
- * Created on Apr 23, 2005
- */
-package nu.mine.mosher.gedcom.date;
-
-
-
-/**
- * Represents a period of time, specified by a starting and ending date, either
- * of which could be a range of possible dates.
- * @author Chris Mosher
- */
-public class DatePeriod implements Comparable<DatePeriod>
-{
- private final DateRange dateStart;
- private final DateRange dateEnd;
-
- /**
- * Degenerate (but also nominal) case of a period lasting just one day.
- * @param date the single date
- */
- public DatePeriod(final DateRange date)
- {
- this(date, date);
- }
-
- /**
- * Period of time lasting from <code>dateStart</code> to
- * <code>dateEnd</code>, inclusive. Either argument could be
- * <code>null</code>, indicating "unknown."
- * @param dateStart start date (range) or <code>null</code>
- * @param dateEnd end date (range) or <code>null</code>
- */
- public DatePeriod(final DateRange dateStart, final DateRange dateEnd)
- {
- if (dateStart == null)
- {
- this.dateStart = DateRange.UNKNOWN;
- }
- else
- {
- this.dateStart = dateStart;
- }
- if (dateEnd == null)
- {
- this.dateEnd = DateRange.UNKNOWN;
- }
- else
- {
- this.dateEnd = dateEnd;
- }
- }
-
- /**
- * @return the start date
- */
- public DateRange getStartDate()
- {
- return this.dateStart;
- }
-
- /**
- * @return the end date
- */
- public DateRange getEndDate()
- {
- return this.dateEnd;
- }
-
- /**
- * Checks if this period is just one date (that is, start equals end).
- * @return if start equals end
- */
- public boolean isSingle()
- {
- return this.dateStart.equals(this.dateEnd);
- }
-
- /**
- * Checks if this period has the same start and end date (ranges) as the
- * given <code>DatePeriod</code>.
- * @return if this period is the same as the given period
- */
- @Override
- public boolean equals(final Object object)
- {
- if (!(object instanceof DatePeriod))
- {
- return false;
- }
- final DatePeriod that = (DatePeriod) object;
- return this.dateStart.equals(that.dateStart)
- && this.dateEnd.equals(that.dateEnd);
- }
-
- /**
- * Gets a hash code for this period.
- */
- @Override
- public int hashCode()
- {
- return this.dateStart.hashCode() ^ this.dateEnd.hashCode();
- }
-
- /**
- * Returns a string representation of this period, intended for display to
- * the end user (not persistence).
- * @return display string
- */
- @Override
- public String toString()
- {
- if (isSingle())
- {
- return this.dateStart.toString();
- }
- final StringBuilder sb = new StringBuilder(32);
- sb.append(this.dateStart.toString());
- sb.append("-");
- sb.append(this.dateEnd.toString());
- return sb.toString();
- }
-
- /**
- * Compares two periods; for sorting purposes.
- * @param that period to compare to (can be <code>null</code>)
- * @return -1, 0, or +1, for less, equal, or greater
- */
- @Override
- public int compareTo(final DatePeriod that)
- {
- if (that == null)
- {
- return -1;
- }
- int d = 0;
- if (d == 0)
- {
- d = this.dateStart.compareTo(that.dateStart);
- }
- if (d == 0 || this.dateStart.equals(DateRange.UNKNOWN))
- {
- d = this.dateEnd.compareTo(that.dateEnd);
- }
- return d;
- }
-
- /**
- * Checks to see if this DatePeriod overlaps the given DatePeriod.
- * @param periodTarget period to compare to
- * @return true if they overlap
- */
- public boolean overlaps(final DatePeriod periodTarget)
- {
- return this.dateStart.compareTo(periodTarget.dateEnd) <= 0
- && periodTarget.dateStart.compareTo(this.dateEnd) <= 0;
- }
-}
View
257 src/main/java/nu/mine/mosher/gedcom/date/DateRange.java
@@ -1,257 +0,0 @@
-package nu.mine.mosher.gedcom.date;
-
-
-
-import java.util.Date;
-
-import nu.mine.mosher.time.Time;
-
-
-
-/**
- * Represents an date that could fall sometime within a given range. For
- * example, "some date between 1840 and 1846 inclusive," would be represented as
- * a a range with earliest year 1840 and latest year 1846. A range of "unknown"
- * is represented by a full range (earliest = Jan. 1, 9999 BC, and latest = Dec.
- * 31, 9999 AD). Date is "exact" if and only if earliest.equals(latest).
- * @author Chris Mosher
- */
-public class DateRange implements Comparable<DateRange>
-{
- /*
- * YMD always represent Gregorian calendar. Range of possible dates is given
- * by earliest thru latest (inclusive). Date is exact if and only if
- * earliest.equals(latest).
- */
- private final YMD earliest;
- private final YMD latest;
-
- private transient final int hash;
- private transient final Time approx;
- private transient final boolean exact;
-
- /**
- * Represents an unknown date (Jan. 1, 9999 BC to Dec. 31, 9999 AD)
- */
- public static final DateRange UNKNOWN = new DateRange(null);
-
- /**
- * Indicates that the (supposed) earliest and latest dates were given
- * incorrectly, such that latest is less than earliest.
- * @author Chris Mosher
- */
- public static class DatesOutOfOrder extends Exception
- {
- private DatesOutOfOrder(final YMD earliestGreater, final YMD latestLesser)
- {
- super("Latest date (" + latestLesser + ") is less than earliest date (" + earliestGreater + ").");
- }
- }
-
- /**
- * Initializes an exact date.
- * @param ymdExact the exact date (earliest = latest = ymdExact)
- */
- public DateRange(final YMD ymdExact)
- {
- if (ymdExact != null)
- {
- this.earliest = this.latest = ymdExact;
- this.exact = true;
- }
- else
- {
- this.earliest = YMD.getMinimum();
- this.latest = YMD.getMaximum();
- this.exact = false;
- }
- this.approx = calcApprox();
- this.hash = calcHash();
- }
-
- /**
- * Initializes a date range.
- * @param earliest earliest possible date
- * @param latest latest possible date
- * @throws DatesOutOfOrder if <code>latest</code> is less than
- * <code>earliest</code>
- */
- @SuppressWarnings("synthetic-access")
- public DateRange(final YMD earliest, final YMD latest) throws DatesOutOfOrder
- {
- if (earliest != null)
- {
- this.earliest = earliest;
- }
- else
- {
- this.earliest = YMD.getMinimum();
- }
-
- if (latest != null)
- {
- this.latest = latest;
- }
- else
- {
- this.latest = YMD.getMaximum();
- }
-
- if (this.latest.compareTo(this.earliest) < 0)
- {
- throw new DatesOutOfOrder(this.earliest, this.latest);
- }
-
- this.approx = calcApprox();
- this.hash = calcHash();
- this.exact = this.earliest.equals(this.latest);
- }
-
- /**
- * @return earliest possible date
- */
- public YMD getEarliest()
- {
- return this.earliest;
- }
-
- /**
- * @return latest possible date
- */
- public YMD getLatest()
- {
- return this.latest;
- }
-
- /**
- * @return if this represents an exact date
- */
- public boolean isExact()
- {
- return this.exact;
- }
-
- /**
- * @return an approximation of this range
- */
- public Time getApproxDay()
- {
- return this.approx;
- }
-
- /**
- * Compares this range to another object to see if it has the same value
- * (both earliest and latest dates)
- * @return true if object is a DateRange that is equal
- */
- @Override
- public boolean equals(final Object object)
- {
- if (!(object instanceof DateRange))
- {
- return false;
- }
-
- final DateRange that = (DateRange) object;
-
- return this.earliest.equals(that.earliest)
- && this.latest.equals(that.latest);
- }
-
- /**
- * Returns a hash code for this object.
- * @return hash code
- */
- @Override
- public int hashCode()
- {
- return this.hash;
- }
-
- /**
- * Returns a string representation of this range, indended for display to
- * the end user, and not intended for persistence.
- * @return displayable string
- */
- @Override
- public String toString()
- {
- final StringBuilder sb = new StringBuilder();
-
- if (isExact())
- {
- sb.append(this.earliest.toString());
- }
- else if (equals(UNKNOWN))
- {
- sb.append("[unknown]");
- }
- else
- {
- sb.append(this.earliest.toString());
- sb.append("?");
- sb.append(this.latest.toString());
- }
- return sb.toString();
- }
-
- /**
- * Compares this range to the given range. Not consistent with equals.
- * @param that range to compare to
- * @return -1, 0, or +1; for less, equal, or greater
- */
- @Override
- public int compareTo(final DateRange that)
- {
- return this.approx.compareTo(that.approx);
- }
-
- private Time calcApprox()
- {
- if (this.earliest.equals(YMD.getMinimum()) && this.latest.equals(YMD.getMaximum()))
- {
- return new Time(new Date(0));
- }
- if (this.earliest.equals(YMD.getMinimum()))
- {
- return this.latest.getApproxTime();
- }
- if (this.latest.equals(YMD.getMaximum()))
- {
- return this.earliest.getApproxTime();
- }
-
- /* optimization: don't bother making a new Time object if exact */
- if (this.exact)
- {
- return this.earliest.getApproxTime();
- }
-
- /* @formatter:off */
- return
- new Time(
- new Date(
- (
- this.earliest.getApproxTime().asDate().getTime()
- +
- this.latest.getApproxTime().asDate().getTime()
- )
- /
- 2
- )
- );
- /* @formatter:on */
- }
-
- private int calcHash()
- {
- int h = 17;
-
- h *= 37;
- h += this.earliest.hashCode();
- h *= 37;
- h += this.latest.hashCode();
-
- return h;
- }
-}
View
378 src/main/java/nu/mine/mosher/gedcom/date/YMD.java
@@ -1,378 +0,0 @@
-package nu.mine.mosher.gedcom.date;
-
-
-
-import java.util.Calendar;
-import java.util.Date;
-import java.util.GregorianCalendar;
-
-import nu.mine.mosher.time.Time;
-
-
-
-/**
- * Represents a date, specified as a year, month, and day, allowing for some
- * values to be unknown. An unknown day or month is specified as zero. Objects
- * of this class are immutable and thread-safe. Gregorian calendar is always
- * assumed; this class does not do any converting of dates between different
- * calendars.
- * @author Chris Mosher
- */
-public class YMD implements Comparable<YMD>
-{
- /**
- * One-based year. Negative year means B.C.; positive means A.D. Must be
- * -9999 to -1, or 1 to 9999.
- */
- private final int year;
-
- /**
- * month (1 = January) (0 = unknown)
- */
- private final int month;
-
- /**
- * day of month (1-31) (0 = unknown)
- */
- private final int day;
-
- /**
- * Indicates what the preferred display calendar is. true==Julian,
- * false==Gregorian Note that this indicates only how to display the
- * date(s), not how they are stored. Dates are always stored using the
- * Gregorian calendar. Further, it is only a preference, and therefore the
- * value may be ignored.
- */
- private final boolean julian;
-
- /**
- * Indicates the date is an approximation.
- */
- private final boolean circa;
-
- private transient final int hash;
- private transient final Time approx;
-
- /**
- * Initializes this YMD with the given year, and an unknown month and day.
- * @param year the year (-9999 to -1, or 1 to 9999)
- */
- public YMD(final int year)
- {
- this(year, 0);
- }
-
- /**
- * Initializes this YMD with the given year and month, and an unknown day.
- * @param year the year (-9999 to -1, or 1 to 9999)
- * @param month the month (1=Jan. to 12=Dec.) or 0 meaning "unknown"
- */
- public YMD(final int year, final int month)
- {
- this(year, month, 0, false, false);
- }
-
- /**
- * Initializes this YMD with the given year, month and day.
- * @param year the year (-9999 to -1, or 1 to 9999)
- * @param month the month (1=Jan. to 12=Dec.) or 0 meaning "unknown"
- * @param day the day within the month (1-31) or 0 meaning "unknown"
- */
- public YMD(final int year, final int month, final int day)
- {
- this(year, month, day, false, false);
- }
-
- /**
- * Initializes this YMD with the given year, month and day, and the given
- * "circa" (true or false, to indicate approximation or not).
- * @param year the year (-9999 to -1, or 1 to 9999)
- * @param month the month (1=Jan. to 12=Dec.) or 0 meaning "unknown"
- * @param day the day within the month (1-31) or 0 meaning "unknown"
- * @param circa true if this date is an approximation
- */
- public YMD(final int year, final int month, final int day, final boolean circa)
- {
- this(year, month, day, circa, false);
- }
-
- /**
- * Initializes this YMD with the given year, month and day, and the given
- * "circa" (true or false, to indicate approximation or not).
- * @param year the year (-9999 to -1, or 1 to 9999)
- * @param month the month (1=Jan. to 12=Dec.) or 0 meaning "unknown"
- * @param day the day within the month (1-31) or 0 meaning "unknown"
- * @param circa true if this date is an approximation
- * @param julian true if this date is preferred to be shown in the Julian
- * calendar (it does not indicate that the given year, month, and
- * date are Julian--they are Gregorian regardless)
- */
- public YMD(final int year, final int month, final int day, final boolean circa, final boolean julian)
- {
- this.year = year;
- if (this.year <= -10000 || this.year == 0 || +10000 <= this.year)
- {
- throw new IllegalStateException("Invalid year: " + this.year);
- }
- this.month = month;
- if (this.month < 0 || 12 < this.month)
- {
- throw new IllegalStateException("Invalid month: " + this.month);
- }
- this.day = day;
- if (this.day < 0 || 31 < this.day)
- {
- throw new IllegalStateException("Invalid day: " + this.day);
- }
- this.circa = circa;
- this.julian = julian;
-
- this.approx = calcApprox();
- this.hash = calcHash();
- }
-
- /**
- * Initializes this YMD with the year, month and day taken from the given
- * {@link Time}.
- * @param time <code>Time</code> to get the year, month, and day of (must be
- * Gregorian)
- */
- public YMD(final Time time)
- {
- final GregorianCalendar cal = new GregorianCalendar();
- cal.setGregorianChange(new Date(Long.MIN_VALUE));
- cal.setTime(time.asDate());
- this.year = cal.get(Calendar.YEAR);
- this.month = cal.get(Calendar.MONTH) + 1;
- this.day = cal.get(Calendar.DAY_OF_MONTH);
- this.circa = false;
- this.julian = false;
- this.approx = calcApprox();
- this.hash = calcHash();
- }
-
- /**
- * @return the day, or zero if unknown
- */
- public int getDay()
- {
- return this.day;
- }
-
- /**
- * @return the month (1 means January), or zero if unknown
- */
- public int getMonth()
- {
- return this.month;
- }
-
- /**
- * @return the year, or zero if unknown. (negative means BC)
- */
- public int getYear()
- {
- return this.year;
- }
-
- /**
- * Returns if this date is preferred to be shown using the Julian calendar.
- * @return true if should be (converted to and) displayed in Julian
- */
- public boolean isJulian()
- {
- return this.julian;
- }
-
- /**
- * Returns if this date is an approximation.
- * @return if this date is an approximation
- */
- public boolean isCirca()
- {
- return this.circa;
- }
-
- /**
- * Gets the exact <code>Time</code> represented by this <code>YMD</code>,
- * assuming it is exact. Throws otherwise.
- * @return the <code>Time</code> representing this exact <code>YMD</code>
- * (at noon, local time).
- * @throws IllegalStateException if this <code>YMD</code> if any of year,
- * month, or day are zero
- */
- public Time getExactTime()
- {
- if (!isExact())
- {
- throw new IllegalStateException();
- }
-
- return this.approx;
- }
-
- /**
- * Gets a <code>Time</code> that can be used as an approximation of this
- * <code>YMD</code> for computation purposes. Never display this value to
- * the user!
- * @return an approximate <code>Time</code> for this <code>YMD</code>
- */
- public Time getApproxTime()
- {
- return this.approx;
- }
-
- /**
- * Gets if this <code>YMD</code> is exact.
- * @return <code>true</code> if exact
- */
- public boolean isExact()
- {
- return valid(this.month) && valid(this.day) && !this.circa;
- }
-
- /**
- * Returns a new <code>YMD</code> representing January 1, 9999 BC.
- * @return Jan. 1, 9999 BC
- */
- public static YMD getMinimum()
- {
- return new YMD(-9999, 1, 1);
- }
-
- /**
- * Returns a new <code>YMD</code> representing December 31, AD 9999.
- * @return Dec. 31, AD 9999
- */
- public static YMD getMaximum()
- {
- return new YMD(9999, 12, 31);
- }
-
- /**
- * Compares this date to the given object. Note that two "unknown" values
- * (zero) are still considered equal.
- * @param object the object to compare this object to
- * @return true if the given object is a <code>YMD</code> with the same
- * values for year, month, and day.
- */
- @Override
- public boolean equals(final Object object)
- {
- if (!(object instanceof YMD))
- {
- return false;
- }
-
- final YMD that = (YMD) object;
- return this.year == that.year && this.month == that.month
- && this.day == that.day;
- }
-
- /**
- * Gets the hash code for this object.
- * @return the hash code
- */
- @Override
- public int hashCode()
- {
- return this.hash;
- }
-
- /**
- * Formats this object into a string, intended to be shown to the end user.
- * @return string for display
- */
- @Override
- public String toString()
- {
- final StringBuilder sb = new StringBuilder();
- if (this.circa)
- {
- sb.append("c. ");
- }
-
- /* this check is an optimization */
- if (this.year == 9999 || this.year == -9999)
- {
- if (equals(YMD.getMaximum()))
- {
- return "[after]";
- }
- if (equals(YMD.getMinimum()))
- {
- return "[before]";
- }
- }
- if (this.year < 0)
- {
- sb.append('-');
- }
- sb.append(String.format("%04d",
- Integer.valueOf(Math.abs(this.year))));
- if (this.month > 0)
- {
- sb.append(String.format("-%02d", Integer.valueOf(this.month)));
- if (this.day > 0)
- {
- sb.append(String.format("-%02d", Integer.valueOf(this.day)));
- }
- }
-
- return sb.toString();
- }
-
- /**
- * Compares this date to the given date. Not consistent with
- * <code>equals</code>. Uses heuristics to provide nice sequencing, intended
- * for display to the end-user.
- * @return -1, 0, or 1, for less, equal, or greater
- */
- @Override
- public int compareTo(final YMD that)
- {
- return this.approx.compareTo(that.approx);
- }
-
- private static boolean valid(final int i)
- {
- return i != 0;
- }
-
- private Time calcApprox()
- {
- int m = this.month;
- int d = this.day;
-
- // if month and day are missing, assume mid-year (July 3).
- if (m == 0 && d == 0)
- {
- m = 7;
- d = 3;
- }
- // if just day is missing, assume mid-month (the 15th).
- else if (d == 0)
- {
- d = 15;
- }
-
- return createTime(this.year, m, d);
- }
-
- private static Time createTime(final int year, final int month,
- final int day)
- {
- final GregorianCalendar cal = new GregorianCalendar();
- cal.setGregorianChange(new Date(Long.MIN_VALUE));
-
- cal.set(year, month - 1, day, 12, 0, 0);
- cal.set(Calendar.MILLISECOND, 0);
-
- return new Time(cal.getTime());
- }
-
- private int calcHash()
- {
- return this.approx.hashCode();
- }
-}
View
38 src/main/java/nu/mine/mosher/gedcom/exception/GedcomParseException.java
@@ -1,38 +0,0 @@
-package nu.mine.mosher.gedcom.exception;
-
-
-
-import nu.mine.mosher.gedcom.GedcomLine;
-
-
-
-public class GedcomParseException extends Exception
-{
- private final String rawGedcomLine;
- private final GedcomLine parsedGedcomLine;
-
- public GedcomParseException(final String message,
- final String rawGedcomLine, final GedcomLine parsedGedcomLine)
- {
- super(message);
- this.rawGedcomLine = rawGedcomLine;
- this.parsedGedcomLine = parsedGedcomLine;
- }
-
- public GedcomParseException(final String message, final Throwable cause)
- {
- super(message, cause);
- this.rawGedcomLine = "";
- this.parsedGedcomLine = null;
- }
-
- public String getRawGedcomLine()
- {
- return this.rawGedcomLine;
- }
-
- public GedcomLine getParsedGedcomLine()
- {
- return this.parsedGedcomLine;
- }
-}
View
17 src/main/java/nu/mine/mosher/gedcom/exception/IllegalLevel.java
@@ -1,17 +0,0 @@
-package nu.mine.mosher.gedcom.exception;
-
-
-
-import nu.mine.mosher.gedcom.GedcomLine;
-
-
-
-public class IllegalLevel extends GedcomParseException
-{
- public IllegalLevel(final String rawGedcomLine,
- final GedcomLine parsedGedcomLine)
- {
- super("GEDCOM line has an invalid level number", rawGedcomLine,
- parsedGedcomLine);
- }
-}
View
16 src/main/java/nu/mine/mosher/gedcom/exception/InvalidID.java
@@ -1,16 +0,0 @@
-package nu.mine.mosher.gedcom.exception;
-
-
-
-import nu.mine.mosher.gedcom.GedcomLine;
-
-
-
-public class InvalidID extends GedcomParseException
-{
- public InvalidID(final String rawGedcomLine,
- final GedcomLine parsedGedcomLine)
- {
- super("GEDCOM line has an invalid ID", rawGedcomLine, parsedGedcomLine);
- }
-}
View
15 src/main/java/nu/mine/mosher/gedcom/exception/InvalidLevel.java
@@ -1,15 +0,0 @@
-package nu.mine.mosher.gedcom.exception;
-
-
-
-import nu.mine.mosher.gedcom.GedcomLine;
-
-
-
-public class InvalidLevel extends Exception
-{
- public InvalidLevel(final GedcomLine line)
- {
- super("GEDCOM line has invalid level number at line: " + line);
- }
-}
View
17 src/main/java/nu/mine/mosher/gedcom/exception/MissingTag.java
@@ -1,17 +0,0 @@
-package nu.mine.mosher.gedcom.exception;
-
-
-
-import nu.mine.mosher.gedcom.GedcomLine;
-
-
-
-public class MissingTag extends GedcomParseException
-{
- public MissingTag(final String rawGedcomLine,
- final GedcomLine parsedGedcomLine)
- {
- super("GEDCOM line does not have any tag", rawGedcomLine,
- parsedGedcomLine);
- }
-}
View
7 src/main/java/nu/mine/mosher/gedcom/servlet/GedcomServlet.java
@@ -32,9 +32,10 @@
import nu.mine.mosher.gedcom.Gedcom;
import nu.mine.mosher.gedcom.GedcomTree;
import nu.mine.mosher.gedcom.exception.InvalidLevel;
-import nu.mine.mosher.gedcom.servlet.struct.GedcomFile;
-import nu.mine.mosher.gedcom.servlet.struct.Person;
-import nu.mine.mosher.gedcom.servlet.struct.Source;
+import nu.mine.mosher.gedcom.model.GedcomFile;
+import nu.mine.mosher.gedcom.model.Loader;
+import nu.mine.mosher.gedcom.model.Person;
+import nu.mine.mosher.gedcom.model.Source;
/**
* @author Chris Mosher
View
520 src/main/java/nu/mine/mosher/gedcom/servlet/Loader.java
@@ -1,520 +0,0 @@
-package nu.mine.mosher.gedcom.servlet;
-import java.io.StringReader;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-import java.util.UUID;
-
-import nu.mine.mosher.gedcom.GedcomLine;
-import nu.mine.mosher.gedcom.GedcomTag;
-import nu.mine.mosher.gedcom.GedcomTree;
-import nu.mine.mosher.gedcom.date.parser.GedcomDateValueParser;
-import nu.mine.mosher.gedcom.date.parser.ParseException;
-import nu.mine.mosher.gedcom.servlet.struct.Event;
-import nu.mine.mosher.gedcom.servlet.struct.EventNames;
-import nu.mine.mosher.gedcom.servlet.struct.Partnership;
-import nu.mine.mosher.gedcom.servlet.struct.Person;
-import nu.mine.mosher.gedcom.servlet.struct.Source;
-import nu.mine.mosher.gedcom.date.DatePeriod;
-import nu.mine.mosher.gedcom.date.DateRange.DatesOutOfOrder;
-import nu.mine.mosher.time.Time;
-import nu.mine.mosher.collection.TreeNode;
-
-/**
- * Parses the given <code>GedcomTree</code> into <code>Person</code> objects.
- *
- * <p>Created on 2006-10-09.</p>
- * @author Chris Mosher
- */
-public class Loader
-{
- private final GedcomTree gedcom;
- private final String name;
-
- private final Map<UUID,Person> mapUUIDtoPerson = new HashMap<UUID,Person>();
- private final Map<UUID,Source> mapUUIDtoSource = new HashMap<UUID,Source>();
-
- private Person first;
- private String description;
-
-
-
- public Loader(final GedcomTree gedcom, final String filename)
- {
- this.gedcom = gedcom;
- this.name = filename;
- }
-
-
-
- public void parse()
- {
- final TreeNode<GedcomLine> root = this.gedcom.getRoot();
-
- final Collection<TreeNode<GedcomLine>> rNodeTop = new ArrayList<TreeNode<GedcomLine>>();
- getChildren(root,rNodeTop);
-
- for (final TreeNode<GedcomLine> nodeTop : rNodeTop)
- {
- final GedcomLine lineTop = nodeTop.getObject();
- final GedcomTag tagTop = lineTop.getTag();
-
- if (tagTop.equals(GedcomTag.HEAD))
- {
- this.description = parseHead(nodeTop);
- break;
- }
- }
-
- final Map<String,Person> mapIDtoPerson = new HashMap<String,Person>();
-
- for (final TreeNode<GedcomLine> nodeTop : rNodeTop)
- {
- final GedcomLine lineTop = nodeTop.getObject();
- final GedcomTag tagTop = lineTop.getTag();
-
- if (tagTop.equals(GedcomTag.INDI))
- {
- final Person person = parseIndividual(nodeTop);
- mapIDtoPerson.put(person.getID(),person);
- storeInUuidMap(person);
- if (this.first == null)
- {
- this.first = person;
- }
- }
- }
-
- for (final TreeNode<GedcomLine> nodeTop : rNodeTop)
- {
- final GedcomLine lineTop = nodeTop.getObject();
- final GedcomTag tagTop = lineTop.getTag();
-
- if (tagTop.equals(GedcomTag.FAM))
- {
- parseFamily(nodeTop,mapIDtoPerson);
- }
- }
-
- for (final Person person: mapIDtoPerson.values())
- {
- person.initKeyDates();
- }
- }
-
- public String getName()
- {
- return this.name;
- }
-
- public String getDescription()
- {
- return this.description;
- }
-
- public Person getFirstPerson()
- {
- return this.first;
- }
-
- public Person lookUpPerson(final UUID uuid)
- {
- return this.mapUUIDtoPerson.get(uuid);
- }
-
- public Source lookUpSource(final UUID uuid)
- {
- return this.mapUUIDtoSource.get(uuid);
- }
-
-
- public void appendAllUuids(final Set<UUID> appendTo)
- {
- appendTo.addAll(this.mapUUIDtoPerson.keySet());
- }
-
- private void storeInUuidMap(final Person person) {
- final UUID uuid = person.getUuid();
- if (uuid == null)
- {
- return;
- }
- final Person existing = this.mapUUIDtoPerson.get(uuid);
- if (existing != null)
- {
- System.err.println("Duplicate INDI _UID value: "+uuid);
- return;
- }
- this.mapUUIDtoPerson.put(uuid, person);
- }
-
- private void storeInUuidMap(final Source source) {
- final UUID uuid = source.getUuid();
- if (uuid == null)
- {
- return;
- }
- final Source existing = this.mapUUIDtoSource.get(uuid);
- if (existing != null)
- {
- return;
- }
- this.mapUUIDtoSource.put(uuid, source);
- }
-
-
-
- private static void getChildren(TreeNode<GedcomLine> root, Collection<TreeNode<GedcomLine>> rNodeTop)
- {
- for (final TreeNode<GedcomLine> child : root)
- {
- rNodeTop.add(child);
- }
- }
-
- private static String parseHead(final TreeNode<GedcomLine> head)
- {
- final Collection<TreeNode<GedcomLine>> rNode = new ArrayList<TreeNode<GedcomLine>>();
- getChildren(head,rNode);
-
- for (final TreeNode<GedcomLine> node : rNode)
- {
- final GedcomLine line = node.getObject();
- final GedcomTag tag = line.getTag();
- if (tag.equals(GedcomTag.NOTE))
- {
- return line.getValue();
- }
- }
- return "";
- }
-
- private Person parseIndividual(final TreeNode<GedcomLine> nodeIndi)
- {
- String name = "[unknown]";
- UUID uuid = null;
- final ArrayList<Event> rEvent = new ArrayList<Event>();
- boolean isPrivate = false;
-
- final Collection<TreeNode<GedcomLine>> rNode = new ArrayList<TreeNode<GedcomLine>>();
- getChildren(nodeIndi,rNode);
-
- for (final TreeNode<GedcomLine> node : rNode)
- {
- final GedcomLine line = node.getObject();
- final GedcomTag tag = line.getTag();
- if (tag.equals(GedcomTag.NAME))
- {
- name = parseName(node);
- }
- else if (tag.equals(GedcomTag._UID))
- {
- try
- {
- uuid = parseUuid(node);
- }
- catch (final Throwable e)
- {
- System.err.println("Error while parsing inidividual \""+name+"\"");
- e.printStackTrace();
- uuid = null;
- }
- }
- else if (GedcomTag.setIndividualEvent.contains(tag) || GedcomTag.setIndividualAttribute.contains(tag))
- {
- final Event event = parseEvent(node);
- rEvent.add(event);
- if (tag.equals(GedcomTag.BIRT))
- {
- isPrivate = calculatePrivacy(event);
- }
- }
- }
-
- return new Person(nodeIndi.getObject().getID(),name,rEvent,new ArrayList<Partnership>(),isPrivate,uuid);
- }
-
- private static UUID parseUuid(final TreeNode<GedcomLine> nodeUuid) {
- final String rawUuid = nodeUuid.getObject().getValue();
- return UUID.fromString(rawUuid);
- }
-
- private void parseFamily(final TreeNode<GedcomLine> nodeFam, final Map<String,Person> mapIDtoPerson)
- {
- Person husb = null;
- Person wife = null;
- final ArrayList<Person> rChild = new ArrayList<Person>();
- final ArrayList<Event> rEvent = new ArrayList<Event>();
-
- final Collection<TreeNode<GedcomLine>> rNode = new ArrayList<TreeNode<GedcomLine>>();
- getChildren(nodeFam,rNode);
-
- for (final TreeNode<GedcomLine> node : rNode)
- {
- final GedcomLine line = node.getObject();
- final GedcomTag tag = line.getTag();
-
- if (tag.equals(GedcomTag.HUSB))
- {
- husb = lookUpPerson(line.getPointer(),mapIDtoPerson);
- }
- else if (tag.equals(GedcomTag.WIFE))
- {
- wife = lookUpPerson(line.getPointer(),mapIDtoPerson);
- }
- else if (tag.equals(GedcomTag.CHIL))
- {
- final Person child = lookUpPerson(line.getPointer(),mapIDtoPerson);
- rChild.add(child);
- }
- else if (GedcomTag.setFamilyEvent.contains(tag))
- {
- final Event event = parseEvent(node);
- rEvent.add(event);
- }
- }
- buildFamily(husb,wife,rChild,rEvent);
- }
-
- private static Person lookUpPerson(final String id, final Map<String,Person> mapIDtoPerson)
- {
- return mapIDtoPerson.get(id);
- }
-
- private static String parseName(final TreeNode<GedcomLine> nodeName)
- {
- return nodeName.getObject().getValue();
- }
-
- private Event parseEvent(final TreeNode<GedcomLine> nodeEvent)
- {
- final String whichEvent = getEventName(nodeEvent);
-
- final Collection<TreeNode<GedcomLine>> rNode = new ArrayList<TreeNode<GedcomLine>>();
- getChildren(nodeEvent,rNode);
-
- DatePeriod date = null;
- String place = "";
- String note = "";
- Source source = null;
-
- for (final TreeNode<GedcomLine> node : rNode)
- {
- final GedcomLine line = node.getObject();
- final GedcomTag tag = line.getTag();
- if (tag.equals(GedcomTag.DATE))
- {
- final String sDate = line.getValue();
- final GedcomDateValueParser parser = new GedcomDateValueParser(new StringReader(sDate));
- try
- {
- date = parser.parse();
- }
- catch (final ParseException | DatesOutOfOrder e)
- {
- System.err.println("Error while parsing \""+sDate+"\"");
- e.printStackTrace();
- date = null;
- }
- }
- else if (tag.equals(GedcomTag.PLAC))
- {
- place = line.getValue();
- }
- else if (tag.equals(GedcomTag.NOTE))
- {
- note = parseNote(node);
- }
- else if (tag.equals(GedcomTag.SOUR))
- {
- source = parseSource(node);
- if (source != null)
- {
- storeInUuidMap(source);
- }
- }
- }
- // TODO handle case of date == null (see grojs for example)
- return new Event(whichEvent,date,HtmlUtil.escapeHtml(place),HtmlUtil.escapeHtml(note),source);
- }
-
- private Source parseSource(final TreeNode<GedcomLine> node)
- {
- final String pointingText = getSourcePtText(node);
-
- final String id = node.getObject().getPointer();
- final TreeNode<GedcomLine> nodeSource = this.gedcom.getNode(id);
- if (nodeSource == null)
- {
- return null;
- }
-
- final Collection<TreeNode<GedcomLine>> rNode = new ArrayList<TreeNode<GedcomLine>>();
- getChildren(nodeSource,rNode);
-
- String author = "";
- String title = "";
- String publication = "";
- String text = "";
- UUID uuid = null;
- for (final TreeNode<GedcomLine> n : rNode)
- {
- final GedcomLine line = n.getObject();
- final GedcomTag tag = line.getTag();
- if (tag.equals(GedcomTag.AUTH))
- {
- author = line.getValue();
- }
- else if (tag.equals(GedcomTag.TITL))
- {
- title = line.getValue();
- }
- else if (tag.equals(GedcomTag.PUBL))
- {
- publication = line.getValue();
- }
- else if (tag.equals(GedcomTag.TEXT))
- {
- text = line.getValue();
- }
- else if (tag.equals(GedcomTag._UID))
- {
- try
- {
- uuid = parseUuid(n);
- }
- catch (final Throwable e)
- {
- System.err.println("Error while parsing source \""+title+"\"");
- e.printStackTrace();
- uuid = null;
- }
- }
- }
- return new Source(HtmlUtil.escapeHtml(author),HtmlUtil.escapeHtml(title),HtmlUtil.escapeHtml(publication),HtmlUtil.smartEscapeHtml(pointingText+text),uuid);
- }
-
- private static String getSourcePtText(TreeNode<GedcomLine> node)
- {
- final Collection<TreeNode<GedcomLine>> rNode = new ArrayList<TreeNode<GedcomLine>>();
- getChildren(node,rNode);
- for (final TreeNode<GedcomLine> n : rNode)