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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2868 by torben, Thu Jan 28 16:12:28 2016 UTC revision 2870 by torben, Thu Jan 28 16:24:53 2016 UTC
# Line 1  Line 1 
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 {
# Line 47  public abstract class AbstractAddressSou Line 52  public abstract class AbstractAddressSou
52                          System.out.println("Error on closing " + e.getMessage() );                          System.out.println("Error on closing " + e.getMessage() );
53                  }                        }      
54          }          }
55            /**
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            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            
88            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  }  }

Legend:
Removed from v.2868  
changed lines
  Added in v.2870

  ViewVC Help
Powered by ViewVC 1.1.20