/* * __ .__ .__ ._____. * _/ |_ _______ __|__| ____ | | |__\_ |__ ______ * \ __\/ _ \ \/ / |/ ___\| | | || __ \ / ___/ * | | ( <_> > <| \ \___| |_| || \_\ \\___ \ * |__| \____/__/\_ \__|\___ >____/__||___ /____ > * \/ \/ \/ \/ * * Copyright (c) 2006-2011 Karsten Schmidt * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * http://creativecommons.org/licenses/LGPL/2.1/ * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ package toxi.data.csv; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; /** * This class allows the user to refer to CSV fields/columns via freely chosen * IDs. This is useful in cases where the actual CSV column name is very long or * the order of fields can be varying (e.g. when consuming CSV data from * different sources). The class constructor expects a {@link HashMap} to * establish the mapping between the custom keys and the actual CSV field names. * The following example demonstrates this usage: * *
* HashMap<String,String> keys=new HashMap<String,String>(); * * // creates a new alias "name" for the actual CSV field name: "What is your name?" * keys.put("name","What is your name?"); * keys.put("phone","What is your phone number?"); * keys.put("tue","Times available on Tuesday"); * * keys.put("id","Actual CSV field name"); * * CSVFieldMapper mapper=new CSVFieldMapper(keys); * ... ** * This mapper instance is then passed to a {@link CSVParser} and used to help * parsing & validating the data. When a row has been successfully parsed, * the {@link CSVParser} emits an event to allow clients to further process this * data, e.g. by bundling it into custom data types: * *
* public void csvNewItemParsed(String[] fields, CSVFieldMapper map) { * Person p = new Person(); * // use the mapper to get the field for the custom field ID "name" * p.setName(map.get("name",fields)); * p.setPhone(map.get("phone",fields)); * ... * } **/ public class CSVFieldMapper { /** * */ public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); /** * */ protected HashMap