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