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.io.UnsupportedEncodingException;
038import java.util.ArrayList;
039import java.util.List;
040
041import org.hl7.fhir.exceptions.FHIRException;
042
043/**
044 * Baseclass for readers that read data from files in comma separated file format
045 * @author Ewout
046 *
047 */
048public class CSVReader extends InputStreamReader {
049        
050        public CSVReader(InputStream in) throws FHIRException, IOException {
051                super(in, "UTF-8");
052                checkBOM();
053        }
054
055        private void checkBOM() throws FHIRException, IOException {
056    if (peek() == '\uFEFF')
057      next();
058    
059  }
060
061  private String[] cols;
062  private String[] cells;
063  
064        public void readHeaders() throws IOException, FHIRException {
065    cols = parseLine();  
066        }
067        
068
069  public boolean line() throws IOException, FHIRException {
070    if (ready()) {
071      cells = parseLine();
072      return true;
073    }  else
074      return false;
075  }
076
077  public boolean has(String name) {
078    for (int i = 0; i < cols.length; i++) {
079      if (name.equals(cols[i].trim()))
080        return cell(name) != null;
081    }
082    return false;
083  }
084  
085  public String cell(String name) {
086    int index = -1;
087    for (int i = 0; i < cols.length; i++) {
088      if (name.equals(cols[i].trim()))
089        index = i;
090    }
091    if (index == -1)
092      throw new FHIRException("no cell "+name);
093    String s = cells.length >= index ? cells[index] : null;
094    if (Utilities.noString(s))
095      return null;
096    if (s.startsWith("\"") && s.endsWith("\"")) {
097      return s.substring(1, s.length()-2);     
098    } else {
099      return s;
100    }
101  }
102    
103        protected boolean parseBoolean(String column) {
104                if (column == null)
105                        return false;
106                else if (column.equalsIgnoreCase("y") || column.equalsIgnoreCase("yes") || column.equalsIgnoreCase("true") || column.equalsIgnoreCase("1"))
107                        return true;
108                else
109                        return false;
110        }
111
112        protected static String getColumn(String[] titles, String[] values, String column)  {
113                int c = -1;
114        //      String s = "";
115                for (int i = 0; i < titles.length; i++) {
116                //      s = s + ","+titles[i];
117                        if (titles[i].equalsIgnoreCase(column))
118                                c = i;
119                }
120                if (c == -1)
121                        return ""; // throw new Exception("unable to find column "+column+" in "+s.substring(1));
122                else if (values.length <= c)
123                        return "";
124                else
125                        return values[c];
126        }
127
128        
129        /**
130         * Split one line in a CSV file into its rows. Comma's appearing in double quoted strings will
131         * not be seen as a separator.
132         * @return
133         * @throws IOException 
134         * @throws FHIRException 
135         * @
136         */
137        public String[] parseLine() throws IOException, FHIRException  {
138                List<String> res = new ArrayList<String>();
139                StringBuilder b = new StringBuilder();
140                boolean inQuote = false;
141
142                while (ready() && (inQuote || (peek() != '\r' && peek() != '\n'))) {
143                        char c = peek();
144                        next();
145                        if (c == '"') {
146                                if (ready() && peek() == '"') {
147                b.append(c);
148          next();
149                                } else {
150                            inQuote = !inQuote;
151                                }
152                        }
153                        else if (!inQuote && c == ',') {
154                                res.add(b.toString().trim());
155                                b = new StringBuilder();
156                        }
157                        else 
158                                b.append(c);
159                }
160                res.add(b.toString().trim());
161                while (ready() && (peek() == '\r' || peek() == '\n')) {
162                        next();
163                }
164                
165                String[] r = new String[] {};
166                r = res.toArray(r);
167                return r;
168                
169        }
170
171        private int state = 0;
172        private char pc;
173        
174        private char peek() throws FHIRException, IOException 
175        {
176          if (state == 0)
177                  next();
178          if (state == 1)
179                  return pc;
180          else
181                  throw new FHIRException("read past end of source");
182        }
183        
184        private void next() throws FHIRException, IOException 
185        {
186                  if (state == 2)
187                          throw new FHIRException("read past end of source");
188          state = 1;
189                  int i = read();
190                  if (i == -1)
191                          state = 2;
192                  else 
193                          pc = (char) i;
194        }
195
196
197  public void checkColumn(int i, String name, String desc) throws FHIRException {
198    if (cols.length < i)
199      throw new FHIRException("Error parsing "+desc+": expected column "+name+" at col "+i+" but only found "+cols.length+" cols");
200    if (!cols[i-1].equals(name))
201      throw new FHIRException("Error parsing "+desc+": expected column "+name+" at col "+i+" but found '"+cols[i-1]+"'");
202  }
203
204
205  public String value(int i) {
206    if (i > cells.length)
207      return null;
208    if (Utilities.noString(cells[i-1]))
209      return null;
210    return cells[i-1];
211  }
212
213
214}