Skip to content

Commit a96d0ff

Browse files
committed
Added parseHeader() method and expanded the Columns subclass to support header keyword lookup in the CSV class
1 parent 45c311a commit a96d0ff

File tree

1 file changed

+66
-4
lines changed

1 file changed

+66
-4
lines changed

src/javaxt/express/utils/CSV.java

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package javaxt.express.utils;
2+
import java.util.HashMap;
23
import java.util.ArrayList;
34

45
//******************************************************************************
@@ -15,11 +16,11 @@
1516
* found in this class:
1617
<pre>
1718
javaxt.io.File csvFile = new javaxt.io.File("/temp/employees.csv");
18-
try (java.io.BufferedReader is = csvFile.getBufferedReader("UTF-8")){
19+
try (java.io.BufferedReader br = csvFile.getBufferedReader("UTF-8")){
1920
2021
2122
//Read header
22-
String header = CSV.readLine(is);
23+
String header = CSV.readLine(br);
2324
2425
//Remove the Byte Order Mark (BOM) if there is one
2526
int bom = CSV.getByteOrderMark(header);
@@ -35,7 +36,7 @@
3536
3637
//Read rows
3738
String row;
38-
while (!(row=CSV.readLine(is)).isEmpty()){
39+
while (!(row=CSV.readLine(br)).isEmpty()){
3940
4041
4142
//Parse row
@@ -59,7 +60,6 @@ public class CSV {
5960

6061
public static final String TAB_DELIMITER = "\t";
6162
public static final String COMMA_DELIMITER = ",";
62-
//public static final String UTF8_BOM = "\uFEFF";
6363

6464

6565
//**************************************************************************
@@ -68,13 +68,18 @@ public class CSV {
6868
/** Class used to encapsulate columns in a row
6969
*/
7070
public static class Columns implements Iterable<javaxt.utils.Value> {
71+
7172
private ArrayList<javaxt.utils.Value> cols;
73+
private HashMap<String, Integer> header;
74+
7275
public Columns(){
7376
cols = new ArrayList<>();
7477
}
78+
7579
public void add(javaxt.utils.Value col){
7680
cols.add(col);
7781
}
82+
7883
public javaxt.utils.Value get(int idx){
7984
try{
8085
return cols.get(idx);
@@ -83,6 +88,25 @@ public javaxt.utils.Value get(int idx){
8388
return new javaxt.utils.Value(null);
8489
}
8590
}
91+
92+
public javaxt.utils.Value get(String key){
93+
Integer idx = header.get(key.toLowerCase());
94+
if (idx==null) return new javaxt.utils.Value(null);
95+
return get(idx);
96+
}
97+
98+
public void setHeader(Columns header){
99+
if (header==null) return;
100+
this.header = new HashMap<>();
101+
int x = 0;
102+
for (javaxt.utils.Value val : header){
103+
String str = val.toString();
104+
if (str!=null) str = str.toLowerCase();
105+
this.header.put(str, x);
106+
x++;
107+
}
108+
}
109+
86110
public int length(){
87111
return cols.size();
88112
}
@@ -262,6 +286,41 @@ public static String readLine(java.io.BufferedReader reader) throws java.io.IOEx
262286
}
263287

264288

289+
//**************************************************************************
290+
//** parseHeader
291+
//**************************************************************************
292+
/** Parses a header (e.g. first row in a file) into columns. Removes the
293+
* Byte Order Mark (BOM) as needed.
294+
*/
295+
public static Columns parseHeader(String header, String delimiter){
296+
int bom = CSV.getByteOrderMark(header);
297+
if (bom>-1) header = header.substring(bom);
298+
return CSV.getColumns(header, delimiter);
299+
}
300+
301+
302+
//**************************************************************************
303+
//** parseHeader
304+
//**************************************************************************
305+
/** Parses a header (e.g. first row in a file) into columns. Removes the
306+
* Byte Order Mark (BOM) as needed.
307+
*/
308+
public static Columns parseHeader(java.io.InputStream is, String delimiter) throws java.io.IOException {
309+
return parseHeader(CSV.readLine(is), delimiter);
310+
}
311+
312+
313+
//**************************************************************************
314+
//** parseHeader
315+
//**************************************************************************
316+
/** Parses a header (e.g. first row in a file) into columns. Removes the
317+
* Byte Order Mark (BOM) as needed.
318+
*/
319+
public static Columns parseHeader(java.io.BufferedReader reader, String delimiter) throws java.io.IOException {
320+
return parseHeader(CSV.readLine(reader), delimiter);
321+
}
322+
323+
265324
//**************************************************************************
266325
//** getByteOrderMark
267326
//**************************************************************************
@@ -273,6 +332,9 @@ public static String readLine(java.io.BufferedReader reader) throws java.io.IOEx
273332
*/
274333
public static int getByteOrderMark(String str){
275334

335+
if (str.startsWith("\uFEFF")) return 1;
336+
337+
276338
if (str.length()<2) return -1;
277339

278340
int a=-1, b=-1, c=-1, d=-1;

0 commit comments

Comments
 (0)