Skip to content

Commit

Permalink
Merge pull request #62 from profeg/60-ParseCRLF
Browse files Browse the repository at this point in the history
#60 issue resolving
  • Loading branch information
lvca committed Apr 23, 2015
2 parents a345513 + 110a89e commit 960f5dd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,24 @@ protected OExtractedItem fetchNext() throws IOException {
if (!bReader.ready())
return null;

final String line = bReader.readLine();
final String line = getCheckedString();

if( line == null || line.isEmpty())
return null;

return new OExtractedItem(current++, line);
}

protected String getCheckedString() throws IOException {
StringBuilder sbLine = new StringBuilder();
boolean isOpenQuote = false;
do {
if (isOpenQuote) sbLine.append("\r\n");
isOpenQuote = false;
sbLine.append(bReader.readLine());
if ("null".equals(sbLine.toString())) return null;
for (char c : sbLine.toString().toCharArray()) if ('"' == c) isOpenQuote = !isOpenQuote;
} while (isOpenQuote);
return sbLine.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -312,17 +312,16 @@ public void testEndingSpaceInFieldName() {
assertEquals(new Integer(1), (Integer) doc.field("num "));
}

// @Test
// Temporary disabled on April 13th 2015 by Luca
// public void testCRLFIWithinQuotes() {
// String cfgJson = "{source: { content: { value: 'id ,text ,num \r\n1,\"my test\r\n text\",1\r\n'} }, extractor : { row : {} }, transformers : [{ csv : {} }], loader : { test: {} } }";
// process(cfgJson);
// List<ODocument> res = getResult();
// ODocument doc = res.get(0);
// assertEquals(new Integer(1), (Integer) doc.field("id "));
// assertEquals("my test\r\n text", (String) doc.field("text "));
// assertEquals(new Integer(1), (Integer) doc.field("num "));
// }
@Test
public void testCRLFIWithinQuotes() {
String cfgJson = "{source: { content: { value: 'id ,text ,num \r\n1,\"my test\r\n text\",1\r\n'} }, extractor : { row : {} }, transformers : [{ csv : {} }], loader : { test: {} } }";
process(cfgJson);
List<ODocument> res = getResult();
ODocument doc = res.get(0);
assertEquals(new Integer(1), (Integer) doc.field("id "));
assertEquals("my test\r\n text", (String) doc.field("text "));
assertEquals(new Integer(1), (Integer) doc.field("num "));
}

@Test
public void testEscapingDoubleQuotes() {
Expand Down

0 comments on commit 960f5dd

Please sign in to comment.