Skip to content
Snippets Groups Projects
Commit 4e520211 authored by ribeaudc's avatar ribeaudc
Browse files

LMS-2

Finishing first implementation of the parser.

SVN: 126
parent ae8d3346
No related branches found
No related tags found
No related merge requests found
/*
* 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;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
*
* @author Christian Ribeaud
*/
public class DefaultPropertyMapper implements IPropertyMapper
{
private final Map<String, Property> properties;
DefaultPropertyMapper(List<Property> properties) {
this.properties = listToMap(properties);
}
private final static Map<String, Property> listToMap(List<Property> properties)
{
Map<String, Property> map = new HashMap<String, Property>(properties.size());
for (Property property : properties)
{
map.put(property.name, property);
}
return map;
}
///////////////////////////////////////////////////////
// IPropertyMapper
///////////////////////////////////////////////////////
public Property getProperty(String name)
{
return properties.get(name);
}
}
......@@ -57,6 +57,9 @@ public class DefaultReaderParser<E> implements IReaderParser<E>
private IPropertyMapperFactory mapperFactory;
// TODO Christian: refactor this!!!
private boolean propertyMapperSet = false;
public DefaultReaderParser()
{
this(new DefaultLineTokenizer());
......@@ -111,10 +114,11 @@ public class DefaultReaderParser<E> implements IReaderParser<E>
{
for (int lineNumber = 0; (line = bufferedReader.readLine()) != null; lineNumber++)
{
if (mapperFactory != null && mapperFactory.getHeaderLine() > -1)
if (mapperFactory != null && mapperFactory.getHeaderLine() > -1 && propertyMapperSet == false)
{
String[] tokens = parseLine(lineNumber, line);
factory.setPropertyMapper(mapperFactory.createPropertyMapper(tokens));
propertyMapperSet = true;
continue;
}
if (lineFilter.acceptLine(line))
......
......@@ -19,8 +19,6 @@ package ch.systemsx.cisd.common.parser;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
/**
*
*
......@@ -28,11 +26,9 @@ import org.apache.commons.lang.StringUtils;
*/
public class HeaderFilePropertyMapper implements IPropertyMapper
{
public final static char NAME_FORMAT_SEPARATOR = ',';
private final Map<String, Property> properties;
HeaderFilePropertyMapper(String[] headerTokens) {
public HeaderFilePropertyMapper(String[] headerTokens) {
this.properties = tokensToMap(headerTokens);
}
......@@ -42,13 +38,7 @@ public class HeaderFilePropertyMapper implements IPropertyMapper
for (int i = 0; i < tokens.length; i++)
{
String token = tokens[i];
String[] split = StringUtils.split(token, NAME_FORMAT_SEPARATOR);
String format = null;
if (split.length > 1)
{
format = split[1];
}
map.put(token, new Property(i, split[0], format));
map.put(token, new Property(i, token));
}
return map;
}
......
......@@ -37,20 +37,16 @@ public interface IPropertyMapper
*
* @author Christian Ribeaud
*/
// TODO Christian: we should maybe add a type...
public final static class Property {
public final int column;
public final String name;
public final String format;
protected Property(final int column, final String name, final String format)
protected Property(final int column, final String name)
{
this.column = column;
this.name = name;
this.format = format;
}
///////////////////////////////////////////////////////
......
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