Skip to content

Commit

Permalink
Issue #106 - GedcomFileReader doesn't return ArrayLists of Strings
Browse files Browse the repository at this point in the history
And now GedcomFileReader no longer offers an option to read the whole
file into an ArrayList of strings - if you want that, build your own
ArrayList.
  • Loading branch information
Matt Harrah (frizbog) committed Jul 3, 2016
1 parent d88a00f commit ddaff5a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 44 deletions.
30 changes: 0 additions & 30 deletions src/main/java/org/gedcom4j/io/reader/GedcomFileReader.java
Expand Up @@ -22,7 +22,6 @@
package org.gedcom4j.io.reader;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import org.gedcom4j.exception.GedcomParserException;
Expand Down Expand Up @@ -96,35 +95,6 @@ public GedcomFileReader(GedcomParser parser, BufferedInputStream bufferedInputSt
encodingSpecificReader = getEncodingSpecificReader();
}

/**
* Get the gedcom file as a list of string
*
* @return a <code>List</code> of <code>String</code> objects, each of which represents a line of the input file
* represented by the byte stream
* @throws IOException
* if there is a problem reading the data
* @throws GedcomParserException
* if the file load was cancelled or was not well formed
*/
public List<String> getLines() throws IOException, GedcomParserException {
List<String> result = new ArrayList<String>();
String s = nextLine();

// Strip off Byte Order Mark if needed
if (s != null && s.length() > 0 && s.charAt(0) == ((char) 0xFEFF)) {
s = s.substring(1);
}

while (s != null) {
if (s.length() > 0) {
result.add(s);
}
s = nextLine();
}
encodingSpecificReader.cleanUp();
return result;
}

/**
* Get the next line of the file.
*
Expand Down
44 changes: 33 additions & 11 deletions src/test/java/org/gedcom4j/io/reader/GedcomFileReaderTest.java
Expand Up @@ -26,6 +26,7 @@
import static org.junit.Assert.assertNotNull;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import org.gedcom4j.exception.GedcomParserException;
Expand Down Expand Up @@ -59,7 +60,7 @@ public void testAnselCrlf() throws IOException, GedcomParserException {
try {
s = new BufferedInputStream(new ByteArrayInputStream(anselData));
GedcomFileReader gr = new GedcomFileReader(new GedcomParser(), s);
List<String> lines = gr.getLines();
List<String> lines = getLines(gr);
assertNotNull(lines);
assertFalse(lines.isEmpty());
assertEquals(2, lines.size());
Expand Down Expand Up @@ -93,7 +94,7 @@ public void testAnselCrOnly() throws IOException, GedcomParserException {
s = new BufferedInputStream(new ByteArrayInputStream(anselData));
GedcomFileReader gr = new GedcomFileReader(new GedcomParser(), s);
;
List<String> lines = gr.getLines();
List<String> lines = getLines(gr);
assertNotNull(lines);
assertFalse(lines.isEmpty());
assertEquals(2, lines.size());
Expand Down Expand Up @@ -158,7 +159,7 @@ public void testAnselLfOnly() throws IOException, GedcomParserException {
s = new BufferedInputStream(new ByteArrayInputStream(anselData));
GedcomFileReader gr = new GedcomFileReader(new GedcomParser(), s);
;
List<String> lines = gr.getLines();
List<String> lines = getLines(gr);
assertNotNull(lines);
assertFalse(lines.isEmpty());
assertEquals(2, lines.size());
Expand Down Expand Up @@ -211,7 +212,7 @@ public void testUnicodeBigEndianWithCrlf() throws IOException, GedcomParserExcep
s = new BufferedInputStream(new ByteArrayInputStream(unicodeData));
GedcomFileReader gr = new GedcomFileReader(new GedcomParser(), s);
;
List<String> lines = gr.getLines();
List<String> lines = getLines(gr);
assertNotNull(lines);
assertFalse(lines.isEmpty());
assertEquals(2, lines.size());
Expand Down Expand Up @@ -244,7 +245,7 @@ public void testUnicodeBigEndianWithCrOnly() throws IOException, GedcomParserExc
s = new BufferedInputStream(new ByteArrayInputStream(unicodeData));
GedcomFileReader gr = new GedcomFileReader(new GedcomParser(), s);
;
List<String> lines = gr.getLines();
List<String> lines = getLines(gr);
assertNotNull(lines);
assertFalse(lines.isEmpty());
assertEquals(2, lines.size());
Expand Down Expand Up @@ -277,7 +278,7 @@ public void testUnicodeBigEndianWithLfOnly() throws IOException, GedcomParserExc
s = new BufferedInputStream(new ByteArrayInputStream(unicodeData));
GedcomFileReader gr = new GedcomFileReader(new GedcomParser(), s);
;
List<String> lines = gr.getLines();
List<String> lines = getLines(gr);
assertNotNull(lines);
assertFalse(lines.isEmpty());
assertEquals(2, lines.size());
Expand Down Expand Up @@ -310,7 +311,7 @@ public void testUnicodeLittleEndianWithCrlf() throws IOException, GedcomParserEx
s = new BufferedInputStream(new ByteArrayInputStream(unicodeData));
GedcomFileReader gr = new GedcomFileReader(new GedcomParser(), s);
;
List<String> lines = gr.getLines();
List<String> lines = getLines(gr);
assertNotNull(lines);
assertFalse(lines.isEmpty());
assertEquals(2, lines.size());
Expand Down Expand Up @@ -343,7 +344,7 @@ public void testUnicodeLittleEndianWithCrOnly() throws IOException, GedcomParser
s = new BufferedInputStream(new ByteArrayInputStream(unicodeData));
GedcomFileReader gr = new GedcomFileReader(new GedcomParser(), s);
;
List<String> lines = gr.getLines();
List<String> lines = getLines(gr);
assertNotNull(lines);
assertFalse(lines.isEmpty());
assertEquals(2, lines.size());
Expand Down Expand Up @@ -376,7 +377,7 @@ public void testUnicodeLittleEndianWithLfOnly() throws IOException, GedcomParser
s = new BufferedInputStream(new ByteArrayInputStream(unicodeData));
GedcomFileReader gr = new GedcomFileReader(new GedcomParser(), s);
;
List<String> lines = gr.getLines();
List<String> lines = getLines(gr);
assertNotNull(lines);
assertFalse(lines.isEmpty());
assertEquals(2, lines.size());
Expand Down Expand Up @@ -434,13 +435,34 @@ public void testUtf8CrNoBOM() throws IOException, GedcomParserException {
* @throws IOException
* if the data can't be read
* @throws GedcomParserException
* if the file load was cancelled
* if the file cannot be parsed or the load was cancelled
*/
@Test
public void testUtf8LfNoBOM() throws IOException, GedcomParserException {
testUtf8File("sample/utf8_lf_nobom.ged");
}

/**
* Get all the lines from the file as an arraylist of string
*
* @param gr
* the {@link GedcomFileReader}
* @return all the lines of the file
* @throws IOException
* if the file cannot be read
* @throws GedcomParserException
* if the file cannot be parsed or the load was cancelled
*/
private List<String> getLines(GedcomFileReader gr) throws IOException, GedcomParserException {
ArrayList<String> result = new ArrayList<String>();
String s = gr.nextLine();
while (s != null) {
result.add(s);
s = gr.nextLine();
}
return result;
}

/**
* Test that a UTF file was read and character decoded correctly
*
Expand All @@ -460,7 +482,7 @@ private void testUtf8File(String fileName) throws IOException, FileNotFoundExcep
fileInputStream = new FileInputStream(fileName);
bufferedInputStream = new BufferedInputStream(fileInputStream);
GedcomFileReader gr = new GedcomFileReader(new GedcomParser(), bufferedInputStream);
List<String> lines = gr.getLines();
List<String> lines = getLines(gr);
assertNotNull(lines);
assertEquals(77, lines.size());
assertEquals("2 VERS 5.5.1", lines.get(6));
Expand Down
31 changes: 28 additions & 3 deletions src/test/java/org/gedcom4j/writer/GedcomWriterTest.java
Expand Up @@ -108,7 +108,12 @@ public GedcomWriterTest() throws IOException, GedcomParserException, GedcomWrite
try {
byteStream = new FileInputStream(tempFile);
GedcomFileReader gfr = new GedcomFileReader(new GedcomParser(), new BufferedInputStream(byteStream));
readbackLines = gfr.getLines();
readbackLines = new ArrayList<String>();
String s = gfr.nextLine();
while (s != null) {
readbackLines.add(s);
s = gfr.nextLine();
}
} finally {
if (byteStream != null) {
byteStream.close();
Expand Down Expand Up @@ -427,8 +432,28 @@ private void assertLineSequence(String failureMessage, List<String> lookIn, Stri
* if the file load was cancelled or was malformed
*/
private List<String> readBack(File fileToRead) throws IOException, GedcomParserException {
GedcomFileReader gfr = new GedcomFileReader(new GedcomParser(), new BufferedInputStream(new FileInputStream(fileToRead)));
return gfr.getLines();
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(fileToRead);
bis = new BufferedInputStream(fis);
GedcomFileReader gfr = new GedcomFileReader(new GedcomParser(), bis);
List<String> result = new ArrayList<String>();
String s = gfr.nextLine();
while (s != null) {
result.add(s);
s = gfr.nextLine();
}

return result;
} finally {
if (bis != null) {
bis.close();
}
if (fis != null) {
fis.close();
}
}
}

}

0 comments on commit ddaff5a

Please sign in to comment.