/*
 *  Copyright 2013 Eric F. Savage, code@efsavage.com
 *
 *   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 com.ajah.csv;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import au.com.bytecode.opencsv.CSVReader;

/**
 * Provides a way to easily get data from a CSV file.
 * 
 * @author <a href="http://efsavage.com">Eric F. Savage</a>, <a
 *         href="mailto:code@efsavage.com">code@efsavage.com</a>.
 */
public class CsvReader implements AutoCloseable, Iterable<CsvRow>, Iterator<CsvRow> {

	CSVReader reader = null;
	List<String> fieldNames = new ArrayList<>();
	String[] next;

	/**
	 * Creates a reader from a file reference. The file will remain open as long
	 * as this reader is open. The first line of the file must contain the
	 * column headings.
	 * 
	 * @param file
	 *            The file to open and read from.
	 * @throws IOException
	 *             If the file could not be opened.
	 */
	public CsvReader(final File file) throws IOException {
		this.reader = new CSVReader(new FileReader(file));
		String[] headers = this.reader.readNext();
		for (String header : headers) {
			if (this.fieldNames.contains(header)) {
				throw new IllegalArgumentException("Field " + header + " is defined twice");
			}
			this.fieldNames.add(header);
		}
		this.next = this.reader.readNext();
	}

	@Override
	public void close() throws IOException {
		if (this.reader != null) {
			this.reader.close();
		}
	}

	/**
	 * This is here so we can use it in enhanced for loops.
	 * 
	 * @see java.lang.Iterable#iterator()
	 */
	@Override
	public Iterator<CsvRow> iterator() {
		return this;
	}

	@Override
	public boolean hasNext() {
		return this.next != null;
	}

	@Override
	public CsvRow next() {
		CsvRow row = new CsvRow(this.fieldNames, this.next);
		try {
			this.next = this.reader.readNext();
		} catch (IOException e) {
			throw new IllegalArgumentException(e);
		}
		return row;
	}

	/**
	 * Unsupported.
	 */
	@Override
	public void remove() {
		throw new UnsupportedOperationException();
	}

}

@@ -1712,17 +1712,20 @@
 (String 
-h
+rawH
 eader : 
@@ -1735,16 +1735,53 @@
 ders) %7B%0A
+%09%09%09String header = rawHeader.trim();%0A
 %09%09%09if (t
@@ -1930,16 +1930,23 @@
 d(header
+.trim()
 );%0A%09%09%7D%0A%09

Trim header fields