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

change:

- Header tokens uniqueness should be checked outside of the 'HeaderFilePropertyMapper' constructor.
add:
- Add method 'notUnique' in 'TabFileLoader'.

SVN: 2781
parent 6fee1540
No related branches found
No related tags found
No related merge requests found
...@@ -32,7 +32,7 @@ public final class HeaderFilePropertyMapper implements IAliasPropertyMapper ...@@ -32,7 +32,7 @@ public final class HeaderFilePropertyMapper implements IAliasPropertyMapper
private final Map<String, String> aliases; private final Map<String, String> aliases;
public HeaderFilePropertyMapper(final String[] headerTokens) public HeaderFilePropertyMapper(final String[] headerTokens) throws IllegalArgumentException
{ {
assert headerTokens != null; assert headerTokens != null;
aliases = new HashMap<String, String>(); aliases = new HashMap<String, String>();
...@@ -47,12 +47,7 @@ public final class HeaderFilePropertyMapper implements IAliasPropertyMapper ...@@ -47,12 +47,7 @@ public final class HeaderFilePropertyMapper implements IAliasPropertyMapper
final String token = tokens[i]; final String token = tokens[i];
if (token != null) if (token != null)
{ {
String key = token.toLowerCase(); map.put(token.toLowerCase(), new MappedProperty(i, token));
if (map.containsKey(key))
{
throw new IllegalArgumentException("Duplicated column name '" + key + "'.");
}
map.put(key, new MappedProperty(i, token));
} }
} }
return map; return map;
......
...@@ -19,7 +19,9 @@ package ch.systemsx.cisd.common.parser; ...@@ -19,7 +19,9 @@ package ch.systemsx.cisd.common.parser;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
...@@ -53,7 +55,7 @@ public class TabFileLoader<T> ...@@ -53,7 +55,7 @@ public class TabFileLoader<T>
* names is missing, or a parsing error occurs. * names is missing, or a parsing error occurs.
* @throws EnvironmentFailureException if a IOException occured. * @throws EnvironmentFailureException if a IOException occured.
*/ */
public final List<T> load(final File file) public final List<T> load(final File file) throws UserFailureException
{ {
// Just check whether the file exists. Lets <code>FileUtils</code> do the rest. // Just check whether the file exists. Lets <code>FileUtils</code> do the rest.
if (file.exists() == false) if (file.exists() == false)
...@@ -68,7 +70,9 @@ public class TabFileLoader<T> ...@@ -68,7 +70,9 @@ public class TabFileLoader<T>
throw new UserFailureException("No header line found in file '" + file.getAbsolutePath() + "'."); throw new UserFailureException("No header line found in file '" + file.getAbsolutePath() + "'.");
} }
final HeaderLineFilter lineFilter = new HeaderLineFilter(headerLine.number); final HeaderLineFilter lineFilter = new HeaderLineFilter(headerLine.number);
final IAliasPropertyMapper propertyMapper = new HeaderFilePropertyMapper(StringUtils.split(headerLine.text)); final String[] tokens = StringUtils.split(headerLine.text, "\t");
notUnique(tokens);
final IAliasPropertyMapper propertyMapper = new HeaderFilePropertyMapper(tokens);
parser.setObjectFactory(factory.createFactory(propertyMapper)); parser.setObjectFactory(factory.createFactory(propertyMapper));
FileReader reader = null; FileReader reader = null;
try try
...@@ -88,4 +92,25 @@ public class TabFileLoader<T> ...@@ -88,4 +92,25 @@ public class TabFileLoader<T>
} }
} }
/**
* Checks given <var>tokens</var> whether there is no duplicate.
* <p>
* Note that the search is case-insensitive.
* </p>
*
* @throws UserFailureException if there is at least one duplicate in the given <var>tokens</var>.
*/
private final static void notUnique(final String[] tokens)
{
assert tokens != null : "Given tokens can not be null.";
final Set<String> unique = new HashSet<String>();
for (final String token : tokens)
{
if (unique.add(token.toLowerCase()) == false)
{
throw UserFailureException.fromTemplate("Duplicated column name '%s'.", token);
}
}
}
} }
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