1 |
package dk.daoas.adressevedligehold; |
package dk.daoas.adressevedligehold; |
2 |
|
|
3 |
import java.io.BufferedReader; |
import java.io.BufferedReader; |
4 |
|
import java.io.IOException; |
5 |
import java.io.InputStream; |
import java.io.InputStream; |
6 |
import java.io.InputStreamReader; |
import java.io.InputStreamReader; |
7 |
|
import java.nio.charset.Charset; |
8 |
|
import java.util.List; |
9 |
|
|
10 |
import org.apache.commons.fileupload.FileItem; |
import org.apache.commons.fileupload.FileItem; |
11 |
|
|
12 |
|
import com.google.common.base.Splitter; |
13 |
|
|
14 |
import dk.daoas.adressevedligehold.util.DeduplicateHelper; |
import dk.daoas.adressevedligehold.util.DeduplicateHelper; |
15 |
|
|
16 |
public abstract class AbstractAddressSource implements AddressSource { |
public abstract class AbstractAddressSource implements AddressSource { |
52 |
System.out.println("Error on closing " + e.getMessage() ); |
System.out.println("Error on closing " + e.getMessage() ); |
53 |
} |
} |
54 |
} |
} |
55 |
|
|
56 |
|
protected void validatNoHeaderLine(int exptectedFieldCount, char seperator) throws IOException { |
57 |
|
try ( |
58 |
|
InputStream is1 = file.getInputStream(); |
59 |
|
InputStreamReader isr1 = new InputStreamReader(is1, Charset.forName("ISO-8859-1") ); |
60 |
|
BufferedReader br1 = new BufferedReader(isr1) |
61 |
|
) { |
62 |
|
String line = br1.readLine(); |
63 |
|
|
64 |
|
if (line == null) { |
65 |
|
throw new IOException("Can't read 1st line - is file empty?"); |
66 |
|
} |
67 |
|
|
68 |
|
List<String> parts = Splitter.on(seperator).splitToList(line); |
69 |
|
if (parts.size() != exptectedFieldCount) { |
70 |
|
throw new IOException("Not enough fields in CSV file. Found " + parts.size() + ", expected " + exptectedFieldCount); |
71 |
|
} |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
|
is = file.getInputStream(); |
76 |
|
isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") ); |
77 |
|
br = new BufferedReader(isr); |
78 |
|
} |
79 |
|
|
80 |
|
protected void validateWithHeader(int expectedFieldCount, char seperator) throws IOException { |
81 |
|
try { |
82 |
|
is = file.getInputStream(); |
83 |
|
isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") ); |
84 |
|
br = new BufferedReader(isr); |
85 |
|
|
86 |
|
String line = br.readLine(); |
87 |
|
if (line == null) { |
88 |
|
throw new IOException("Can't read 1st line - is file empty?"); |
89 |
|
} |
90 |
|
|
91 |
|
List<String> parts = Splitter.on(seperator).splitToList(line); |
92 |
|
if (parts.size() != expectedFieldCount) { |
93 |
|
throw new IOException("Not enough fields in CSV file. Found " + parts.size() + ", expected " + expectedFieldCount); |
94 |
|
} |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
} catch (Exception e) { |
99 |
|
try { |
100 |
|
br.close(); |
101 |
|
isr.close(); |
102 |
|
is.close(); |
103 |
|
} catch (Exception e2) { |
104 |
|
System.out.println("Error cleaning up resources"); |
105 |
|
} |
106 |
|
|
107 |
|
throw e; // Re-throw |
108 |
|
} |
109 |
|
} |
110 |
} |
} |