/[projects]/dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/AbstractAddressSource.java
ViewVC logotype

Annotation of /dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/AbstractAddressSource.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2870 - (hide annotations) (download)
Thu Jan 28 16:24:53 2016 UTC (8 years, 4 months ago) by torben
File size: 3265 byte(s)
Comment
1 torben 2868 package dk.daoas.adressevedligehold;
2    
3     import java.io.BufferedReader;
4 torben 2869 import java.io.IOException;
5 torben 2868 import java.io.InputStream;
6     import java.io.InputStreamReader;
7 torben 2869 import java.nio.charset.Charset;
8     import java.util.List;
9 torben 2868
10     import org.apache.commons.fileupload.FileItem;
11    
12 torben 2869 import com.google.common.base.Splitter;
13    
14 torben 2868 import dk.daoas.adressevedligehold.util.DeduplicateHelper;
15    
16     public abstract class AbstractAddressSource implements AddressSource {
17    
18     protected DeduplicateHelper<String> dirigeringsCache = new DeduplicateHelper<String>();
19    
20     protected FileItem file;
21    
22     protected InputStream is;
23     protected InputStreamReader isr;
24     protected BufferedReader br;
25    
26     protected int lineCount = 0;
27    
28     public AbstractAddressSource(FileItem file) {
29     this.file = file;
30     }
31    
32     @Override
33     public String getFilename() {
34     // TODO Auto-generated method stub
35     return file.getName();
36     }
37    
38     @Override //AutoCloseable
39     public void close() throws Exception {
40     System.out.println("Closing BK after lines " + lineCount);
41     try {
42     if (br != null)
43     br.close();
44     if (isr != null)
45     isr.close();
46     if (is != null)
47     is.close();
48    
49     file.delete();
50    
51     } catch (Exception e) {
52     System.out.println("Error on closing " + e.getMessage() );
53     }
54     }
55 torben 2870 /**
56     * Reads the first line, validates that there are the expected number of fields, and then
57     * re-initilze the inputstream - this is for file formats that does not use a header line.
58     *
59     * @param exptectedFieldCount
60     * @param seperator
61     * @throws IOException
62     */
63 torben 2869 protected void validatNoHeaderLine(int exptectedFieldCount, char seperator) throws IOException {
64     try (
65     InputStream is1 = file.getInputStream();
66     InputStreamReader isr1 = new InputStreamReader(is1, Charset.forName("ISO-8859-1") );
67     BufferedReader br1 = new BufferedReader(isr1)
68     ) {
69     String line = br1.readLine();
70    
71     if (line == null) {
72     throw new IOException("Can't read 1st line - is file empty?");
73     }
74    
75     List<String> parts = Splitter.on(seperator).splitToList(line);
76     if (parts.size() != exptectedFieldCount) {
77     throw new IOException("Not enough fields in CSV file. Found " + parts.size() + ", expected " + exptectedFieldCount);
78     }
79     }
80    
81    
82     is = file.getInputStream();
83     isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") );
84     br = new BufferedReader(isr);
85     }
86    
87 torben 2870
88 torben 2869 protected void validateWithHeader(int expectedFieldCount, char seperator) throws IOException {
89     try {
90     is = file.getInputStream();
91     isr = new InputStreamReader(is, Charset.forName("ISO-8859-1") );
92     br = new BufferedReader(isr);
93    
94     String line = br.readLine();
95     if (line == null) {
96     throw new IOException("Can't read 1st line - is file empty?");
97     }
98    
99     List<String> parts = Splitter.on(seperator).splitToList(line);
100     if (parts.size() != expectedFieldCount) {
101     throw new IOException("Not enough fields in CSV file. Found " + parts.size() + ", expected " + expectedFieldCount);
102     }
103    
104    
105    
106     } catch (Exception e) {
107     try {
108     br.close();
109     isr.close();
110     is.close();
111     } catch (Exception e2) {
112     System.out.println("Error cleaning up resources");
113     }
114    
115     throw e; // Re-throw
116     }
117     }
118 torben 2868 }

  ViewVC Help
Powered by ViewVC 1.1.20