Skip to content
Snippets Groups Projects
Commit c5a01900 authored by kaloyane's avatar kaloyane
Browse files

[LMS-2660] fixed

SVN: 23841
parent 80b1ca1a
No related branches found
No related tags found
No related merge requests found
...@@ -21,6 +21,8 @@ import java.util.Iterator; ...@@ -21,6 +21,8 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import org.apache.commons.lang.StringUtils;
import ch.systemsx.cisd.common.parser.filter.ILineFilter; import ch.systemsx.cisd.common.parser.filter.ILineFilter;
/** /**
...@@ -73,17 +75,21 @@ public class DefaultParser<E, T> implements IParser<E, T> ...@@ -73,17 +75,21 @@ public class DefaultParser<E, T> implements IParser<E, T>
{ {
E object = null; E object = null;
String[] tokens = parseLine(number, nextLine, headerLength); String[] tokens = parseLine(number, nextLine, headerLength);
try // skip empty lines
{ if (false == areAllTokensBlank(tokens))
object = createObject(tokens);
} catch (final ParserException parserException)
{ {
throw new ParsingException(parserException, tokens, number); try
} {
// Skip null values object = createObject(tokens);
if (null != object) } catch (final ParserException parserException)
{ {
elements.add(object); throw new ParsingException(parserException, tokens, number);
}
// Skip null values
if (null != object)
{
elements.add(object);
}
} }
} }
} }
...@@ -91,6 +97,18 @@ public class DefaultParser<E, T> implements IParser<E, T> ...@@ -91,6 +97,18 @@ public class DefaultParser<E, T> implements IParser<E, T>
return elements; return elements;
} }
private boolean areAllTokensBlank(String[] tokens)
{
for (String token : tokens)
{
if (false == StringUtils.isBlank(token))
{
return false;
}
}
return true;
}
public final Iterator<E> parseIteratively(final Iterator<ILine<T>> lineIterator, public final Iterator<E> parseIteratively(final Iterator<ILine<T>> lineIterator,
final ILineFilter lineFilter, final int headerLength) throws ParsingException final ILineFilter lineFilter, final int headerLength) throws ParsingException
{ {
......
...@@ -47,6 +47,10 @@ public final class DefaultParserTest ...@@ -47,6 +47,10 @@ public final class DefaultParserTest
"# This is a comment", "firstName\tlastName\taddress\tcity", "# This is a comment", "firstName\tlastName\taddress\tcity",
"\tDarwin\tHumboldt Ave. 1865", "Albert\tEinstein"); "\tDarwin\tHumboldt Ave. 1865", "Albert\tEinstein");
private final static List<String> textWithEmptyLines = Arrays.asList("", "# This is a comment",
"firstName\tlastName\taddress\tcity", "\tDarwin\tHumboldt Ave. 1865", " \t \t \t ",
"Albert\tEinstein");
private final static int HEADER_LENGTH = 4; private final static int HEADER_LENGTH = 4;
private final static IParser<String[], String> createParser() private final static IParser<String[], String> createParser()
...@@ -130,6 +134,18 @@ public final class DefaultParserTest ...@@ -130,6 +134,18 @@ public final class DefaultParserTest
assertEquals("", result.get(2)[3]); assertEquals("", result.get(2)[3]);
} }
@Test
public final void testParseEmptyLinesIgnored()
{
final IParser<String[], String> parser = createParser();
final List<String[]> result =
parser.parse(createLineIterator(textWithEmptyLines), new HeaderLineFilter(),
HEADER_LENGTH);
assertEquals(3, result.size());
assertEquals("Darwin", result.get(1)[1]);
assertEquals("Albert", result.get(2)[0]);
}
@Test @Test
public final void testParseWithColumnSizeMismatching() public final void testParseWithColumnSizeMismatching()
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment