Skip to content
Snippets Groups Projects
Commit 591bffd0 authored by ribeaudc's avatar ribeaudc
Browse files

change: - 'ParserUtilities.getFirstAcceptedLine' renamed to...

change: - 'ParserUtilities.getFirstAcceptedLine' renamed to 'ParserUtilities.tryGetFirstAcceptedLine'.
add: - 'ParserUtilities.tryGetFirstAcceptedLine' for 'Reader'.
- 'NonEmptyLineFilter'.

SVN: 7932
parent dc985690
No related branches found
No related tags found
No related merge requests found
......@@ -18,13 +18,13 @@ package ch.systemsx.cisd.common.parser;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.apache.log4j.Logger;
import ch.systemsx.cisd.common.logging.LogCategory;
import ch.systemsx.cisd.common.logging.LogFactory;
import ch.systemsx.cisd.common.exceptions.WrappedIOException;
import ch.systemsx.cisd.common.parser.filter.AlwaysAcceptLineFilter;
import ch.systemsx.cisd.common.parser.filter.ILineFilter;
......@@ -35,15 +35,58 @@ import ch.systemsx.cisd.common.parser.filter.ILineFilter;
*/
public final class ParserUtilities
{
private static final Logger machineLog =
LogFactory.getLogger(LogCategory.MACHINE, ParserUtilities.class);
private ParserUtilities()
{
// Can not be instantiated.
}
private final static Line tryFirstAcceptedLine(final ILineFilter filter,
final LineIterator lineIterator)
{
for (int line = 0; lineIterator.hasNext(); line++)
{
final String nextLine = lineIterator.nextLine();
if (filter.acceptLine(nextLine, line))
{
return new Line(line, nextLine);
}
}
return null;
}
private final static ILineFilter getLineFilter(final ILineFilter lineFilter)
{
return lineFilter == null ? AlwaysAcceptLineFilter.INSTANCE : lineFilter;
}
/**
* Returns the first <code>Line</code> that is not filtered out by given
* <code>ILineFilter</code>.
* <p>
* You should not call this method if given <var>file</var> does not exist.
* </p>
*
* @param lineFilter could be <code>null</code>. In this case, the
* {@link AlwaysAcceptLineFilter} implementation will be used.
* @param reader the reader that is going to be analyzed. Can not be <code>null</code>.
* @return <code>null</code> if all lines have been filtered out.
*/
public final static Line tryGetFirstAcceptedLine(final Reader reader,
final ILineFilter lineFilter)
{
assert reader != null : "Unspecified reader.";
final ILineFilter filter = getLineFilter(lineFilter);
LineIterator lineIterator = null;
try
{
lineIterator = IOUtils.lineIterator(reader);
return tryFirstAcceptedLine(filter, lineIterator);
} finally
{
LineIterator.closeQuietly(lineIterator);
}
}
/**
* Returns the first <code>Line</code> that is not filtered out by given
* <code>ILineFilter</code>.
......@@ -57,34 +100,21 @@ public final class ParserUtilities
* exists.
* @return <code>null</code> if all lines have been filtered out.
*/
public final static Line getFirstAcceptedLine(final File file, final ILineFilter lineFilter)
public final static Line tryGetFirstAcceptedLine(final File file, final ILineFilter lineFilter)
{
assert file != null && file.exists() : "Given file must not be null and must exist.";
final ILineFilter filter =
lineFilter == null ? AlwaysAcceptLineFilter.INSTANCE : lineFilter;
final ILineFilter filter = getLineFilter(lineFilter);
LineIterator lineIterator = null;
try
{
lineIterator = FileUtils.lineIterator(file);
for (int line = 0; lineIterator.hasNext(); line++)
{
String nextLine = lineIterator.nextLine();
if (filter.acceptLine(nextLine, line))
{
return new Line(line, nextLine);
}
}
} catch (IOException ex)
return tryFirstAcceptedLine(filter, lineIterator);
} catch (final IOException ex)
{
machineLog
.error("An I/O exception has occurred while reading file '" + file + "'.", ex);
throw new WrappedIOException(ex);
} finally
{
LineIterator.closeQuietly(lineIterator);
}
return null;
}
}
......@@ -38,6 +38,7 @@ public final class ExcludeEmptyAndCommentLineFilter implements ILineFilter
public final boolean acceptLine(final String line, final int lineNumber)
{
assert line != null : "Unspecified line";
final String trimmed = line.trim();
return trimmed.length() > 0 && trimmed.startsWith("#") == false;
}
......
......@@ -29,5 +29,5 @@ public interface ILineFilter
*
* @param line the line read from the <code>Reader</code>. Can not be <code>null</code>.
*/
public boolean acceptLine(String line, int lineNumber);
public boolean acceptLine(final String line, final int lineNumber);
}
/*
* Copyright 2007 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.systemsx.cisd.common.parser.filter;
import org.apache.commons.lang.StringUtils;
/**
* A default <code>LineFilter</code> implementation that excludes empty lines.
*
* @author Christian Ribeaud
*/
public final class NonEmptyLineFilter implements ILineFilter
{
public final static ILineFilter INSTANCE = new NonEmptyLineFilter();
private NonEmptyLineFilter()
{
}
//
// ILineFilter
//
public final boolean acceptLine(final String line, final int lineNumber)
{
return StringUtils.isNotBlank(line);
}
}
\ No newline at end of file
......@@ -68,8 +68,7 @@ public final class ParserUtilitiesTest
boolean exceptionThrown = false;
try
{
ParserUtilities.getFirstAcceptedLine(null, null);
ParserUtilities.tryGetFirstAcceptedLine((File) null, null);
} catch (AssertionError e)
{
exceptionThrown = true;
......@@ -80,7 +79,7 @@ public final class ParserUtilitiesTest
assert file.exists() == false;
try
{
ParserUtilities.getFirstAcceptedLine(file, null);
ParserUtilities.tryGetFirstAcceptedLine(file, null);
} catch (AssertionError e)
{
......@@ -96,7 +95,7 @@ public final class ParserUtilitiesTest
{ StringUtils.EMPTY, "non-empty line", StringUtils.EMPTY, "hello" };
File file = new File(workingDirectory, "test.txt");
FileUtils.writeLines(file, Arrays.asList(lines));
Line line = ParserUtilities.getFirstAcceptedLine(file, null);
Line line = ParserUtilities.tryGetFirstAcceptedLine(file, null);
assertEquals(StringUtils.EMPTY, line.getText());
assertEquals(0, line.getNumber());
assert file.delete();
......@@ -110,7 +109,7 @@ public final class ParserUtilitiesTest
File file = new File(workingDirectory, "test.txt");
FileUtils.writeLines(file, Arrays.asList(lines));
Line line =
ParserUtilities.getFirstAcceptedLine(file,
ParserUtilities.tryGetFirstAcceptedLine(file,
ExcludeEmptyAndCommentLineFilter.INSTANCE);
assertEquals("hello", line.getText());
assertEquals(3, line.getNumber());
......
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