001package org.hl7.fhir.utilities;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032
033
034import java.io.IOException;
035import java.io.InputStream;
036import java.io.InputStreamReader;
037import java.util.ArrayList;
038import java.util.List;
039
040import org.hl7.fhir.exceptions.FHIRException;
041
042/**
043 * Baseclass for readers that read data from files in comma separated file format
044 * @author Ewout
045 *
046 */
047public class CSVReader extends InputStreamReader {
048        
049        public CSVReader(InputStream in) throws FHIRException, IOException {
050                super(in, "UTF-8");
051                checkBOM();
052        }
053
054        private void checkBOM() throws FHIRException, IOException {
055    if (peek() == '\uFEFF')
056      next();
057    
058  }
059
060  private String[] cols;
061  private String[] cells;
062  private char delimiter = ',';
063  private boolean multiline;
064  private boolean doingQuotes = true;
065  
066        public void readHeaders() throws IOException, FHIRException {
067    cols = parseLine();  
068        }
069
070  public boolean line() throws IOException, FHIRException {
071    if (ready()) {
072      cells = parseLine();
073      return true;
074    }  else
075      return false;
076  }
077
078  public boolean has(String name) {
079    for (int i = 0; i < cols.length; i++) {
080      if (name.equals(cols[i].trim()))
081        return cell(name) != null;
082    }
083    return false;
084  }
085  
086  public String cell(String name) {
087    int index = -1;
088    for (int i = 0; i < cols.length; i++) {
089      if (name.equals(cols[i].trim())) {
090        index = i;
091        break;
092      }
093    }
094    if (index == -1)
095      throw new FHIRException("no cell "+name+" in "+cols);
096    String s = cells.length > index ? cells[index] : null;
097    if (Utilities.noString(s))
098      return null;
099    if (s.startsWith("\"") && s.endsWith("\"")) {
100      return s.substring(1, s.length()-2);     
101    } else {
102      return s;
103    }
104  }
105    
106        protected boolean parseBoolean(String column) {
107                if (column == null)
108                        return false;
109                else if (column.equalsIgnoreCase("y") || column.equalsIgnoreCase("yes") || column.equalsIgnoreCase("true") || column.equalsIgnoreCase("1"))
110                        return true;
111                else
112                        return false;
113        }
114
115        protected static String getColumn(String[] titles, String[] values, String column)  {
116                int c = -1;
117        //      String s = "";
118                for (int i = 0; i < titles.length; i++) {
119                //      s = s + ","+titles[i];
120                        if (titles[i].equalsIgnoreCase(column))
121                                c = i;
122                }
123                if (c == -1)
124                        return ""; // throw new Exception("unable to find column "+column+" in "+s.substring(1));
125                else if (values.length <= c)
126                        return "";
127                else
128                        return values[c];
129        }
130
131        
132        /**
133         * Split one line in a CSV file into its cells. Comma's appearing in double quoted strings will
134         * not be seen as a separator.
135         * @return
136         * @throws IOException 
137         * @throws FHIRException 
138         * @
139         */
140        public String[] parseLine() throws IOException, FHIRException  {
141                List<String> res = new ArrayList<String>();
142                StringBuilder b = new StringBuilder();
143                boolean inQuote = false;
144
145                while (more() && !finished(inQuote, res.size())) {
146                        char c = peek();
147                        next();
148                        if (c == '"' && doingQuotes) {
149                                if (ready() && peek() == '"') {
150                b.append(c);
151          next();
152                                } else {
153                            inQuote = !inQuote;
154                                }
155                        }
156                        else if (!inQuote && c == delimiter ) {
157                                res.add(b.toString().trim());
158                                b = new StringBuilder();
159                        }
160                        else 
161                                b.append(c);
162                }
163                res.add(b.toString().trim());
164                while (ready() && (peek() == '\r' || peek() == '\n')) {
165                        next();
166                }
167                
168                String[] r = new String[] {};
169                r = res.toArray(r);
170                return r;
171        }
172
173        private boolean more() throws IOException {
174    return state == 1 || ready();
175  }
176
177  private boolean finished(boolean inQuote, int size) throws FHIRException, IOException {
178          if (multiline && cols != null) {
179            return size == cols.length || (size == cols.length - 1 && !(inQuote || (peek() != '\r' && peek() != '\n')));
180          } else {
181            return !(inQuote || (peek() != '\r' && peek() != '\n'));
182          }
183  }
184
185  private int state = 0;
186        private char pc;
187        
188        private char peek() throws FHIRException, IOException 
189        {
190          if (state == 0)
191                  next();
192          if (state == 1)
193                  return pc;
194          else
195                  throw new FHIRException("read past end of source");
196        }
197        
198        private void next() throws FHIRException, IOException 
199        {
200                  if (state == 2)
201                          throw new FHIRException("read past end of source");
202          state = 1;
203                  int i = read();
204                  if (i == -1)
205                          state = 2;
206                  else 
207                          pc = (char) i;
208        }
209
210
211  public void checkColumn(int i, String name, String desc) throws FHIRException {
212    if (cols.length < i)
213      throw new FHIRException("Error parsing "+desc+": expected column "+name+" at col "+i+" but only found "+cols.length+" cols");
214    if (!cols[i-1].equals(name))
215      throw new FHIRException("Error parsing "+desc+": expected column "+name+" at col "+i+" but found '"+cols[i-1]+"'");
216  }
217
218
219  public String value(int i) {
220    if (i > cells.length)
221      return null;
222    if (Utilities.noString(cells[i-1]))
223      return null;
224    return cells[i-1];
225  }
226
227  public char getDelimiter() {
228    return delimiter;
229  }
230
231  public void setDelimiter(char delimiter) {
232    this.delimiter = delimiter;
233  }
234
235  public boolean isMultiline() {
236    return multiline;
237  }
238
239  public void setMultiline(boolean multiline) {
240    this.multiline = multiline;
241  }
242
243  public boolean isDoingQuotes() {
244    return doingQuotes;
245  }
246
247  public void setDoingQuotes(boolean doingQuotes) {
248    this.doingQuotes = doingQuotes;
249  }
250
251
252}