frizbog / gedcom4j

Java library for reading/writing genealogy files in GEDCOM format
http://gedcom4j.org
53 stars 36 forks source link

Extend GedcomParser to accept String from GEDCOM file #182

Open whatis777 opened 7 years ago

whatis777 commented 7 years ago

Currently only a file or an input stream is accepted in the GedcomParser.load() method. If no GEDCOM file is available but only its content as String representation, then one cannot use the load() method directly.

If the string is wrapped by a BufferedInputStream (see code below) then the String seems to be invalid and cannot be used (see error message below).

Code: BufferedInputStream stream = new BufferedInputStream(new ByteArrayInputStream(gedcomDataAsString.getBytes(StandardCharsets.UTF_8))); gp.load(stream);

Resulting error message: Caused by: java.io.IOException: Does not appear to be a valid gedcom file - doesn't begin with a zero or newline in any supported encoding, and does not begin with a BOM marker for UTF-8 encoding. at org.gedcom4j.io.reader.GedcomFileReader.getEncodingSpecificReader(GedcomFileReader.java:246) at org.gedcom4j.io.reader.GedcomFileReader.(GedcomFileReader.java:104) at org.gedcom4j.parser.GedcomParser.load(GedcomParser.java:313)

It would be very useful to have a load() method that can deal with a GEDCOM string.

whatis777 commented 7 years ago

PS: I solved my problem by passing an InputStream instead of the String representation. This preserves the content form being encoded...