Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
/**
* Represents a CSV (Comma Separated Values) {@link org.apache.camel.spi.DataFormat}
*
* @version
* @version
*/
@XmlRootElement(name = "csv")
@XmlAccessorType(XmlAccessType.FIELD)
Expand All @@ -48,6 +48,8 @@ public class CsvDataFormat extends DataFormatDefinition {
private Boolean skipFirstLine;
@XmlAttribute
private Boolean lazyLoad;
@XmlAttribute
private Boolean useMaps;

public CsvDataFormat() {
super("csv");
Expand Down Expand Up @@ -111,6 +113,14 @@ public void setLazyLoad(Boolean lazyLoad) {
this.lazyLoad = lazyLoad;
}

public Boolean getUseMaps() {
return useMaps;
}

public void setUseMaps(Boolean useMaps) {
this.useMaps = useMaps;
}

@Override
protected DataFormat createDataFormat(RouteContext routeContext) {
DataFormat csvFormat = super.createDataFormat(routeContext);
Expand Down Expand Up @@ -150,5 +160,9 @@ protected void configureDataFormat(DataFormat dataFormat, CamelContext camelCont
if (lazyLoad != null) {
setProperty(camelContext, dataFormat, "lazyLoad", lazyLoad);
}

if (useMaps != null) {
setProperty(camelContext, dataFormat, "useMaps", useMaps);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* Autogeneration can be disabled. In this case, only the fields defined in
* csvConfig are written on the output.
*
* @version
* @version
*/
public class CsvDataFormat implements DataFormat {
private CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
Expand All @@ -58,6 +58,7 @@ public class CsvDataFormat implements DataFormat {
* Lazy row loading with iterator for big files.
*/
private boolean lazyLoad;
private boolean useMaps;

public void marshal(Exchange exchange, Object object, OutputStream outputStream) throws Exception {
if (delimiter != null) {
Expand Down Expand Up @@ -105,12 +106,18 @@ public Object unmarshal(Exchange exchange, InputStream inputStream) throws Excep
reader = IOHelper.buffered(new InputStreamReader(inputStream, IOHelper.getCharsetName(exchange)));
CSVParser parser = new CSVParser(reader, strategy);

if (skipFirstLine) {
// read one line ahead and skip it
parser.getLine();
CsvLineConverter<?> lineConverter;
if (useMaps) {
lineConverter = CsvLineConverters.getMapLineConverter(parser.getLine());
} else {
lineConverter = CsvLineConverters.getListConverter();
if (skipFirstLine) {
// read one line ahead and skip it
parser.getLine();
}
}

CsvIterator csvIterator = new CsvIterator(parser, reader);
@SuppressWarnings("unchecked") CsvIterator<?> csvIterator = new CsvIterator(parser, reader, lineConverter);
return lazyLoad ? csvIterator : loadAllAsList(csvIterator);
} catch (Exception e) {
error = true;
Expand All @@ -122,9 +129,9 @@ public Object unmarshal(Exchange exchange, InputStream inputStream) throws Excep
}
}

private List<List<String>> loadAllAsList(CsvIterator iter) {
private <T> List<T> loadAllAsList(CsvIterator<T> iter) {
try {
List<List<String>> list = new ArrayList<List<String>>();
List<T> list = new ArrayList<T>();
while (iter.hasNext()) {
list.add(iter.next());
}
Expand All @@ -145,7 +152,7 @@ public void setDelimiter(String delimiter) {
}
this.delimiter = delimiter;
}

public CSVConfig getConfig() {
return config;
}
Expand Down Expand Up @@ -191,6 +198,20 @@ public void setLazyLoad(boolean lazyLoad) {
this.lazyLoad = lazyLoad;
}

public boolean isUseMaps() {
return useMaps;
}

/**
* Sets whether or not the result of the unmarshalling should be a {@code java.util.Map} instead of a {@code java.util.List}. It uses the first line as a
* header line and uses it as keys of the maps.
*
* @param useMaps {@code true} in order to use {@code java.util.Map} instead of {@code java.util.List}, {@code false} otherwise.
*/
public void setUseMaps(boolean useMaps) {
this.useMaps = useMaps;
}

private synchronized void updateFieldsInConfig(Set<?> set, Exchange exchange) {
for (Object value : set) {
if (value != null) {
Expand All @@ -203,5 +224,4 @@ private synchronized void updateFieldsInConfig(Set<?> set, Exchange exchange) {
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@
import java.io.Closeable;
import java.io.IOException;
import java.io.Reader;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

import org.apache.camel.util.IOHelper;
import org.apache.commons.csv.CSVParser;

/**
*/
public class CsvIterator implements Iterator<List<String>>, Closeable {
public class CsvIterator<T> implements Iterator<T>, Closeable {

private final CSVParser parser;
private final Reader reader;
private final CsvLineConverter<T> lineConverter;
private String[] line;

public CsvIterator(CSVParser parser, Reader reader) throws IOException {
public CsvIterator(CSVParser parser, Reader reader, CsvLineConverter<T> lineConverter) throws IOException {
this.parser = parser;
this.reader = reader;
this.lineConverter = lineConverter;
line = parser.getLine();
}

Expand All @@ -48,11 +48,11 @@ public boolean hasNext() {
}

@Override
public List<String> next() {
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
List<String> result = Arrays.asList(line);
T result = lineConverter.convertLine(line);
try {
line = parser.getLine();
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.camel.dataformat.csv;

/**
* This interface helps converting a single CSV line into another representation.
*
* @param <T> Class for representing a single line
*/
public interface CsvLineConverter<T> {
/**
* Converts a single CSV line.
*
* @param line CSV line
* @return Another representation of the CSV line
*/
public T convertLine(String[] line);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.camel.dataformat.csv;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* This {@code CsvLineConverters} class provides common implementations of the {@code CsvLineConverter} interface.
*/
public final class CsvLineConverters {
/**
* Provides an implementation of {@code CsvLineConverter} that converts a line into a {@code List}.
*
* @return List-based {@code CsvLineConverter} implementation
*/
public static CsvLineConverter<List<String>> getListConverter() {
return ListLineConverter.SINGLETON;
}

/**
* Provides an implementation of {@code CsvLineConverter} that converts a line into a {@code Map}.
* <p/>
* It requires to have unique {@code headers} values as well as the same number of item in each line.
*
* @param headers Headers of the CSV file
* @return Map-based {@code CsvLineConverter} implementation
*/
public static CsvLineConverter<Map<String, String>> getMapLineConverter(String[] headers) {
return new MapLineConverter(headers);
}

private static class ListLineConverter implements CsvLineConverter<List<String>> {
public static final ListLineConverter SINGLETON = new ListLineConverter();

@Override
public List<String> convertLine(String[] line) {
return Arrays.asList(line);
}
}

private static class MapLineConverter implements CsvLineConverter<Map<String, String>> {
private final String[] headers;

private MapLineConverter(String[] headers) {
this.headers = checkHeaders(headers);
}

@Override
public Map<String, String> convertLine(String[] line) {
if (line.length != headers.length) {
throw new IllegalStateException("This line does not have the same number of items than the header");
}

Map<String, String> result = new HashMap<String, String>(line.length);
for (int i = 0; i < line.length; i++) {
result.put(headers[i], line[i]);
}
return result;
}

private static String[] checkHeaders(String[] headers) {
// Check that we have headers
if (headers == null || headers.length == 0) {
throw new IllegalArgumentException("Missing headers for the CSV parsing");
}

// Check that there is no duplicates
Set<String> headerSet = new HashSet<String>(headers.length);
Collections.addAll(headerSet, headers);
if (headerSet.size() != headers.length) {
throw new IllegalArgumentException("There are duplicate headers");
}

return headers;
}
}

private CsvLineConverters() {
// Prevent instantiation
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,27 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;

import mockit.Expectations;
import mockit.Injectable;
import org.apache.commons.csv.CSVParser;
import org.junit.Assert;
import org.junit.Test;
import mockit.Expectations;
import mockit.Injectable;

public class CsvIteratorTest {

public static final String HDD_CRASH = "HDD crash";

@Test
public void closeIfError(
@Injectable final InputStreamReader reader,
@Injectable final CSVParser parser) throws IOException {
public void closeIfError(@Injectable final InputStreamReader reader, @Injectable final CSVParser parser) throws IOException {
new Expectations() {
{
parser.getLine();
result = new String[] {"1"};
result = new String[]{"1"};

parser.getLine();
result = new String[] {"2"};
result = new String[]{"2"};

parser.getLine();
result = new IOException(HDD_CRASH);
Expand All @@ -52,7 +50,7 @@ public void closeIfError(
};

@SuppressWarnings("resource")
CsvIterator iterator = new CsvIterator(parser, reader);
CsvIterator<List<String>> iterator = new CsvIterator<List<String>>(parser, reader, CsvLineConverters.getListConverter());
Assert.assertTrue(iterator.hasNext());
Assert.assertEquals(Arrays.asList("1"), iterator.next());
Assert.assertTrue(iterator.hasNext());
Expand All @@ -75,15 +73,14 @@ public void closeIfError(
}

@Test
public void normalCycle(@Injectable final InputStreamReader reader,
@Injectable final CSVParser parser) throws IOException {
public void normalCycle(@Injectable final InputStreamReader reader, @Injectable final CSVParser parser) throws IOException {
new Expectations() {
{
parser.getLine();
result = new String[] {"1"};
result = new String[]{"1"};

parser.getLine();
result = new String[] {"2"};
result = new String[]{"2"};

parser.getLine();
result = null;
Expand All @@ -92,9 +89,9 @@ public void normalCycle(@Injectable final InputStreamReader reader,
reader.close();
}
};

@SuppressWarnings("resource")
CsvIterator iterator = new CsvIterator(parser, reader);
CsvIterator<List<String>> iterator = new CsvIterator<List<String>>(parser, reader, CsvLineConverters.getListConverter());
Assert.assertTrue(iterator.hasNext());
Assert.assertEquals(Arrays.asList("1"), iterator.next());

Expand All @@ -109,6 +106,5 @@ public void normalCycle(@Injectable final InputStreamReader reader,
} catch (NoSuchElementException e) {
// okay
}

}
}
Loading