metridoc.plugins.impl.iterators.CsvIterator.groovy Maven / Gradle / Ivy
/*
* Copyright 2010 Trustees of the University of Pennsylvania Licensed under the
* Educational Community 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.osedu.org/licenses/ECL-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 metridoc.plugins.impl.iterators
import metridoc.plugins.Plugin
import metridoc.plugins.iterators.FileIteratorCreator
import org.apache.commons.csv.CSVParser
import org.apache.commons.csv.CSVStrategy
/**
* Created by IntelliJ IDEA.
* User: tbarker
* Date: 11/28/11
* Time: 3:37 PM
*
* Handles iterating over csv files
* The allowed parameters (which can be retrieved by calling {@link CsvIterator#getParameters} must be a property in
* {@link CSVStrategy}, otherwise an exception will occur.
*
*
*/
@Plugin(category = "grid", name = "csv")
class CsvIterator extends FileIteratorCreator {
CSVParser csvParser
@Override
Iterator doCreate(InputStream inputStream) {
def reader = new InputStreamReader(inputStream)
def csvStrategy = CSVStrategy.DEFAULT_STRATEGY
csvStrategy.setEscape('\\' as char)
if (getParameters()) {
csvStrategy = new CSVStrategy(getParameters())
}
def csvParser = new CSVParser(reader, csvStrategy)
return new CsvIterator(csvParser: csvParser)
}
@Override
List doNext() {
def next = csvParser.getLine()
next == null ? null : Arrays.asList(next)
}
}