1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.any23.extractor.csv; |
19 | |
|
20 | |
import org.apache.any23.configuration.DefaultConfiguration; |
21 | |
import org.apache.commons.csv.CSVParser; |
22 | |
import org.apache.commons.csv.CSVStrategy; |
23 | |
|
24 | |
import java.io.IOException; |
25 | |
import java.io.InputStream; |
26 | |
import java.io.InputStreamReader; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | public class CSVReaderBuilder { |
36 | |
|
37 | |
private static final String DEFAULT_FIELD_DELIMITER = ","; |
38 | |
|
39 | |
private static final String DEFAULT_COMMENT_DELIMITER = "#"; |
40 | |
|
41 | |
public static final char NULL_CHAR = ' '; |
42 | |
|
43 | 0 | private static final char[] popularDelimiters = {'\t', '|', ',', ';'}; |
44 | |
|
45 | 0 | private static DefaultConfiguration defaultConfiguration = |
46 | |
DefaultConfiguration.singleton(); |
47 | |
|
48 | |
private static final CSVStrategy[] strategies; |
49 | |
|
50 | |
static { |
51 | 0 | strategies = new CSVStrategy[ popularDelimiters.length + 1 ]; |
52 | 0 | strategies[0] = CSVStrategy.DEFAULT_STRATEGY; |
53 | 0 | int index = 1; |
54 | 0 | for(char dlmt : popularDelimiters) { |
55 | 0 | strategies[index++] = getCsvStrategy(dlmt, NULL_CHAR); |
56 | |
} |
57 | 0 | } |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public static CSVParser build(InputStream is) throws IOException { |
68 | 0 | CSVStrategy bestStrategy = getBestStrategy(is); |
69 | 0 | if(bestStrategy == null) bestStrategy = getCSVStrategyFromConfiguration(); |
70 | 0 | return new CSVParser( new InputStreamReader(is), bestStrategy ); |
71 | |
} |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public static boolean isCSV(InputStream is) throws IOException { |
81 | 0 | return getBestStrategy(is) != null; |
82 | |
} |
83 | |
|
84 | |
private static CSVStrategy getBestStrategy(InputStream is) throws IOException { |
85 | 0 | for( CSVStrategy strategy : strategies ) { |
86 | 0 | if( testStrategy(is, strategy) ) { |
87 | 0 | return strategy; |
88 | |
} |
89 | |
} |
90 | 0 | return null; |
91 | |
} |
92 | |
|
93 | |
private static CSVStrategy getCsvStrategy(char delimiter, char comment) { |
94 | 0 | return new CSVStrategy(delimiter, '\'', comment); |
95 | |
} |
96 | |
|
97 | |
private static CSVStrategy getCSVStrategyFromConfiguration() { |
98 | 0 | char fieldDelimiter = getCharValueFromConfiguration( |
99 | |
"any23.extraction.csv.field", |
100 | |
DEFAULT_FIELD_DELIMITER |
101 | |
); |
102 | 0 | char commentDelimiter = getCharValueFromConfiguration( |
103 | |
"any23.extraction.csv.comment", |
104 | |
DEFAULT_COMMENT_DELIMITER |
105 | |
); |
106 | 0 | return new CSVStrategy(fieldDelimiter, '\'', commentDelimiter); |
107 | |
} |
108 | |
|
109 | |
private static char getCharValueFromConfiguration(String property, String defaultValue) { |
110 | 0 | String delimiter = defaultConfiguration.getProperty( |
111 | |
property, |
112 | |
defaultValue |
113 | |
); |
114 | 0 | if (delimiter.length() != 1 || delimiter.equals("")) { |
115 | 0 | throw new RuntimeException(property + " value must be a single character"); |
116 | |
} |
117 | 0 | return delimiter.charAt(0); |
118 | |
} |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
private static boolean testStrategy(InputStream is, CSVStrategy strategy) throws IOException { |
131 | 0 | final int MIN_COLUMNS = 2; |
132 | |
|
133 | 0 | is.mark(Integer.MAX_VALUE); |
134 | |
try { |
135 | 0 | final CSVParser parser = new CSVParser(new InputStreamReader(is), strategy); |
136 | 0 | int linesToCheck = 5; |
137 | 0 | int headerColumnCount = -1; |
138 | 0 | while (linesToCheck > 0) { |
139 | |
String[] row; |
140 | 0 | row = parser.getLine(); |
141 | 0 | if (row == null) { |
142 | 0 | break; |
143 | |
} |
144 | 0 | if (row.length < MIN_COLUMNS) { |
145 | 0 | return false; |
146 | |
} |
147 | 0 | if (headerColumnCount == -1) { |
148 | 0 | headerColumnCount = row.length; |
149 | |
} else { |
150 | 0 | if (row.length < headerColumnCount) { |
151 | 0 | return false; |
152 | 0 | } else if (row.length - 1 > headerColumnCount) { |
153 | 0 | return false; |
154 | |
} |
155 | |
} |
156 | 0 | linesToCheck--; |
157 | 0 | } |
158 | 0 | return true; |
159 | |
} finally { |
160 | 0 | is.reset(); |
161 | |
} |
162 | |
} |
163 | |
|
164 | |
|
165 | |
} |