1 /*
2 * Copyright 2013 Mark Rogers.
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
17 package com.opencsv.bean;
18
19 /**
20 * Filters allow lines of input to be ignored before a bean is created.
21 * <p>Using a filter means you are looking at the data from the input after it
22 * has been parsed, but before a bean has been created and populated.</p>
23 * <p>Filters <em>must</em> be thread-safe.</p>
24 *
25 * Where possible use the BeanVerifier as you have the ability to to check specific
26 * fields in the object. If you know the order of the data OR your checks are checking
27 * something other than the content/structure/format of the data (IE filter out any line
28 * that does not have 10 columns) then use the CsvToBeanFilter.
29 *
30 * <p>Here's an example showing how to use {@link CsvToBean} that removes empty lines.
31 * Since the parser returns an array with a single empty string for a blank line
32 * that is what it is checking.</p>
33 *
34 * <pre>
35 * {@code
36 * private class EmptyLineFilter implements CsvToBeanFilter {
37 *
38 * private final MappingStrategy strategy;
39 *
40 * public EmptyLineFilter(MappingStrategy strategy) {
41 * this.strategy = strategy;
42 * }
43 *
44 * public boolean allowLine(String[] line) {
45 * boolean blankLine = line.length == 1 && line[0].isEmpty();
46 * return !blankLine;
47 * }
48 *
49 * }
50 *
51 * public List<Feature> parseCsv(InputStreamReader streamReader) {
52 * HeaderColumnNameTranslateMappingStrategy<Feature> strategy = new HeaderColumnNameTranslateMappingStrategy();
53 * Map<String, String> columnMap = new HashMap();
54 * columnMap.put("FEATURE_NAME", "name");
55 * columnMap.put("STATE", "state");
56 * strategy.setColumnMapping(columnMap);
57 * strategy.setType(Feature.class);
58 * CSVReader reader = new CSVReader(streamReader);
59 * CsvToBeanFilter filter = new EmptyLineFilter(strategy);
60 * return new CsvToBean().parse(strategy, reader, filter);
61 * }
62 * }
63 * </pre>
64 *
65 * @see BeanVerifier
66 *
67 */
68
69 public interface CsvToBeanFilter {
70
71 /**
72 * Determines if a line from the CSV file will be included in the
73 * output of {@link CsvToBean}.
74 *
75 * @param line A line of data from the CSV file
76 * @return True if the line is to be included in the output. Otherwise,
77 * false.
78 */
79 boolean allowLine(String[] line);
80
81 }