View Javadoc
1   /*
2    * Copyright 2007 Kasper B. Graversen
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.supercsv.io;
17  
18  import java.io.IOException;
19  import java.io.Reader;
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.supercsv.cellprocessor.ift.CellProcessor;
26  import org.supercsv.prefs.CsvPreference;
27  import org.supercsv.util.Util;
28  
29  /**
30   * CsvMapReader reads each CSV row into a Map with the column name as the map key, and the column value as the map
31   * value.
32   * 
33   * @author Kasper B. Graversen
34   * @author James Bassett
35   */
36  public class CsvMapReader extends AbstractCsvReader implements ICsvMapReader {
37  	
38  	/**
39  	 * Constructs a new <tt>CsvMapReader</tt> with the supplied Reader and CSV preferences. Note that the
40  	 * <tt>reader</tt> will be wrapped in a <tt>BufferedReader</tt> before accessed.
41  	 * 
42  	 * @param reader
43  	 *            the reader
44  	 * @param preferences
45  	 *            the CSV preferences
46  	 * @throws NullPointerException
47  	 *             if reader or preferences are null
48  	 */
49  	public CsvMapReader(final Reader reader, final CsvPreference preferences) {
50  		super(reader, preferences);
51  	}
52  	
53  	/**
54  	 * Constructs a new <tt>CsvMapReader</tt> with the supplied (custom) Tokenizer and CSV preferences. The tokenizer
55  	 * should be set up with the Reader (CSV input) and CsvPreference beforehand.
56  	 * 
57  	 * @param tokenizer
58  	 *            the tokenizer
59  	 * @param preferences
60  	 *            the CSV preferences
61  	 * @throws NullPointerException
62  	 *             if tokenizer or preferences are null
63  	 */
64  	public CsvMapReader(final ITokenizer tokenizer, final CsvPreference preferences) {
65  		super(tokenizer, preferences);
66  	}
67  	
68  	/**
69  	 * {@inheritDoc}
70  	 */
71  	public Map<String, String> read(final String... nameMapping) throws IOException {
72  		
73  		if( nameMapping == null ) {
74  			throw new NullPointerException("nameMapping should not be null");
75  		}
76  		
77  		if( readRow() ) {
78  			final Map<String, String> destination = new HashMap<String, String>();
79  			Util.filterListToMap(destination, nameMapping, getColumns());
80  			return destination;
81  		}
82  		
83  		return null; // EOF
84  	}
85  	
86  	/**
87  	 * {@inheritDoc}
88  	 */
89  	public Map<String, Object> read(final String[] nameMapping, final CellProcessor[] processors) throws IOException {
90  		
91  		if( nameMapping == null ) {
92  			throw new NullPointerException("nameMapping should not be null");
93  		} else if( processors == null ) {
94  			throw new NullPointerException("processors should not be null");
95  		}
96  		
97  		if( readRow() ) {
98  			// process the columns
99  			final List<Object> processedColumns = executeProcessors(new ArrayList<Object>(getColumns().size()),
100 				processors);
101 			
102 			// convert the List to a Map
103 			final Map<String, Object> destination = new HashMap<String, Object>(processedColumns.size());
104 			Util.filterListToMap((Map<String, Object>) destination, nameMapping, (List<Object>) processedColumns);
105 			return destination;
106 		}
107 		
108 		return null; // EOF
109 	}
110 }