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 String[] readHeaders() throws IOException, FHIRException { 067 cols = parseLine(); 068 return cols; 069 } 070 071 public boolean line() throws IOException, FHIRException { 072 if (ready()) { 073 cells = parseLine(); 074 return true; 075 } else 076 return false; 077 } 078 079 public boolean has(String name) { 080 for (int i = 0; i < cols.length; i++) { 081 if (name.equals(cols[i].trim())) 082 return cell(name) != null; 083 } 084 return false; 085 } 086 087 public String cell(String name) { 088 int index = -1; 089 for (int i = 0; i < cols.length; i++) { 090 if (name.equals(cols[i].trim())) { 091 index = i; 092 break; 093 } 094 } 095 if (index == -1) 096 throw new FHIRException("no cell "+name+" in "+cols); 097 String s = cells.length > index ? cells[index] : null; 098 if (Utilities.noString(s)) 099 return null; 100 if (s.startsWith("\"") && s.endsWith("\"")) { 101 return s.substring(1, s.length()-2); 102 } else { 103 return s; 104 } 105 } 106 107 protected boolean parseBoolean(String column) { 108 if (column == null) 109 return false; 110 else if (column.equalsIgnoreCase("y") || column.equalsIgnoreCase("yes") || column.equalsIgnoreCase("true") || column.equalsIgnoreCase("1")) 111 return true; 112 else 113 return false; 114 } 115 116 protected static String getColumn(String[] titles, String[] values, String column) { 117 int c = -1; 118 // String s = ""; 119 for (int i = 0; i < titles.length; i++) { 120 // s = s + ","+titles[i]; 121 if (titles[i].equalsIgnoreCase(column)) 122 c = i; 123 } 124 if (c == -1) 125 return ""; // throw new Exception("unable to find column "+column+" in "+s.substring(1)); 126 else if (values.length <= c) 127 return ""; 128 else 129 return values[c]; 130 } 131 132 133 /** 134 * Split one line in a CSV file into its cells. Comma's appearing in double quoted strings will 135 * not be seen as a separator. 136 * @return 137 * @throws IOException 138 * @throws FHIRException 139 * @ 140 */ 141 public String[] parseLine() throws IOException, FHIRException { 142 List<String> res = new ArrayList<String>(); 143 StringBuilder b = new StringBuilder(); 144 boolean inQuote = false; 145 146 while (more() && !finished(inQuote, res.size())) { 147 char c = peek(); 148 next(); 149 if (c == '"' && doingQuotes) { 150 if (ready() && peek() == '"') { 151 b.append(c); 152 next(); 153 } else { 154 inQuote = !inQuote; 155 } 156 } 157 else if (!inQuote && c == delimiter ) { 158 res.add(b.toString().trim()); 159 b = new StringBuilder(); 160 } 161 else 162 b.append(c); 163 } 164 res.add(b.toString().trim()); 165 while (ready() && (peek() == '\r' || peek() == '\n')) { 166 next(); 167 } 168 169 String[] r = new String[] {}; 170 r = res.toArray(r); 171 return r; 172 } 173 174 private boolean more() throws IOException { 175 return state == 1 || ready(); 176 } 177 178 private boolean finished(boolean inQuote, int size) throws FHIRException, IOException { 179 if (multiline && cols != null) { 180 return size == cols.length || (size == cols.length - 1 && !(inQuote || (peek() != '\r' && peek() != '\n'))); 181 } else { 182 return !(inQuote || (peek() != '\r' && peek() != '\n')); 183 } 184 } 185 186 private int state = 0; 187 private char pc; 188 189 private char peek() throws FHIRException, IOException 190 { 191 if (state == 0) 192 next(); 193 if (state == 1) 194 return pc; 195 else 196 throw new FHIRException("read past end of source"); 197 } 198 199 private void next() throws FHIRException, IOException 200 { 201 if (state == 2) 202 throw new FHIRException("read past end of source"); 203 state = 1; 204 int i = read(); 205 if (i == -1) 206 state = 2; 207 else 208 pc = (char) i; 209 } 210 211 212 public void checkColumn(int i, String name, String desc) throws FHIRException { 213 if (cols.length < i) 214 throw new FHIRException("Error parsing "+desc+": expected column "+name+" at col "+i+" but only found "+cols.length+" cols"); 215 if (!cols[i-1].equals(name)) 216 throw new FHIRException("Error parsing "+desc+": expected column "+name+" at col "+i+" but found '"+cols[i-1]+"'"); 217 } 218 219 220 public String value(int i) { 221 if (i > cells.length) 222 return null; 223 if (Utilities.noString(cells[i-1])) 224 return null; 225 return cells[i-1]; 226 } 227 228 public char getDelimiter() { 229 return delimiter; 230 } 231 232 public void setDelimiter(char delimiter) { 233 this.delimiter = delimiter; 234 } 235 236 public boolean isMultiline() { 237 return multiline; 238 } 239 240 public void setMultiline(boolean multiline) { 241 this.multiline = multiline; 242 } 243 244 public boolean isDoingQuotes() { 245 return doingQuotes; 246 } 247 248 public void setDoingQuotes(boolean doingQuotes) { 249 this.doingQuotes = doingQuotes; 250 } 251 252 public String[] getCells() { 253 return cells; 254 } 255 256 257}