1 32 33 package it.businesslogic.ireport.connection; 34 35 import it.businesslogic.ireport.JRField; 36 import java.io.File ; 37 import java.io.FileNotFoundException ; 38 import java.io.FileReader ; 39 import java.io.IOException ; 40 import java.io.InputStream ; 41 import java.io.InputStreamReader ; 42 import java.io.Reader ; 43 import java.math.BigDecimal ; 44 import java.text.DateFormat ; 45 import java.text.SimpleDateFormat ; 46 import java.util.HashMap ; 47 import java.util.Vector ; 48 import net.sf.jasperreports.engine.JRException; 49 import net.sf.jasperreports.engine.JRRuntimeException; 50 51 56 public class JRCsvDataSourceInspector { 57 58 private DateFormat dateFormat = new SimpleDateFormat (); 59 private char fieldDelimiter = ','; 60 private String recordDelimiter = "\n"; 61 private HashMap columnNames = new HashMap (); 62 63 private Vector fields; 64 private Reader reader; 65 private char buffer[] = new char[1024]; 66 private int position; 67 private int bufSize; 68 69 72 public JRCsvDataSourceInspector(InputStream stream) 73 { 74 this(new InputStreamReader (stream)); 75 } 76 77 78 82 public JRCsvDataSourceInspector(File file) throws FileNotFoundException 83 { 84 this(new FileReader (file)); 85 } 86 87 88 92 public JRCsvDataSourceInspector(Reader reader) 93 { 94 this.reader = reader; 95 } 96 97 98 101 private boolean parseRow() throws IOException 102 { 103 int pos = 0; 104 int startFieldPos = 0; 105 boolean insideQuotes = false; 106 boolean hadQuotes = false; 107 boolean misplacedQuote = false; 108 char c; 109 fields = new Vector (); 110 111 String row = getRow(); 112 if (row == null || row.length() == 0) 113 return false; 114 115 while (pos < row.length()) { 116 c = row.charAt(pos); 117 118 if (c == '"') { 119 if (!insideQuotes) { 121 if (!hadQuotes) { 122 insideQuotes = true; 123 hadQuotes = true; 124 } 125 else misplacedQuote = true; 127 } 128 else { 131 if (pos+1 < row.length() && row.charAt(pos+1) == '"') 132 pos++; 133 else 134 insideQuotes = false; 135 } 136 } 137 if (c == fieldDelimiter && !insideQuotes) { 139 String field = row.substring(startFieldPos, pos); 140 if (misplacedQuote) { 142 misplacedQuote = false; 143 hadQuotes = false; 144 field = ""; 145 } 146 else if (hadQuotes) { 148 field = field.trim(); 149 if (field.startsWith("\"") && field.endsWith("\"")) { 150 field = field.substring(1, field.length() - 1); 151 field = replaceAll(field, "\"\"", "\""); 152 } 153 else 154 field = ""; 155 hadQuotes = false; 156 } 157 158 fields.add(field); 159 startFieldPos = pos + 1; 160 } 161 162 pos++; 163 if ((pos == row.length()) && insideQuotes) { 166 row = row + recordDelimiter + getRow(); 167 } 168 } 169 170 String field = row.substring(startFieldPos, pos); 172 if (field == null || field.length() == 0) 173 return true; 174 175 if (misplacedQuote) 176 field = ""; 177 else if (hadQuotes) { 178 field = field.trim(); 179 if (field.startsWith("\"") && field.endsWith("\"")) { 180 field = field.substring(1, field.length() - 1); 181 field = replaceAll(field, "\"\"", "\""); 182 } 183 else 184 field = ""; 185 } 186 fields.add(field); 187 188 return true; 189 } 190 191 192 195 private String getRow() throws IOException 196 { 197 StringBuffer row = new StringBuffer (); 198 char c; 199 200 while (true) { 201 try { 202 c = getChar(); 203 204 if (c == recordDelimiter.charAt(0)) { 206 int i; 207 char[] temp = new char[recordDelimiter.length()]; 208 temp[0] = c; 209 boolean isDelimiter = true; 210 for (i = 1; i < recordDelimiter.length() && isDelimiter; i++) { 212 temp[i] = getChar(); 213 if (temp[i] != recordDelimiter.charAt(i)) 214 isDelimiter = false; 215 } 216 217 if (isDelimiter) 218 return row.toString(); 219 220 row.append(temp, 0, i); 221 } 222 223 row.append(c); 224 } catch (JRException e) { 225 return row.toString(); 226 } 227 228 } } 230 231 232 237 private char getChar() throws IOException , JRException 238 { 239 if (position + 1 > bufSize) { 241 bufSize = reader.read(buffer); 242 position = 0; 243 if (bufSize == -1) 244 throw new JRException("No more chars"); 245 } 246 247 return buffer[position++]; 248 } 249 250 251 254 public DateFormat getDateFormat() 255 { 256 return dateFormat; 257 } 258 259 260 263 public void setDateFormat(DateFormat dateFormat) 264 { 265 this.dateFormat = dateFormat; 266 } 267 268 269 272 public char getFieldDelimiter() 273 { 274 return fieldDelimiter; 275 } 276 277 278 283 public void setFieldDelimiter(char fieldDelimiter) 284 { 285 this.fieldDelimiter = fieldDelimiter; 286 } 287 288 289 292 public String getRecordDelimiter() 293 { 294 return recordDelimiter; 295 } 296 297 298 302 public void setRecordDelimiter(String recordDelimiter) 303 { 304 this.recordDelimiter = recordDelimiter; 305 } 306 307 308 311 public void setColumnNames(String [] columnNames) 312 { 313 this.columnNames.clear(); 314 for (int i = 0; i < columnNames.length; i++) 315 this.columnNames.put(columnNames[i], new Integer (i)); 316 } 317 318 public Vector getColumnNames() throws java.io.IOException 319 { 320 parseRow(); 321 return fields; 322 } 323 324 private String replaceAll(String string, String substring, String replacement) 325 { 326 StringBuffer result = new StringBuffer (); 327 int index = string.indexOf(substring); 328 int oldIndex = 0; 329 while (index >= 0) { 330 result.append(string.substring(oldIndex, index)); 331 result.append(replacement); 332 index += substring.length(); 333 oldIndex = index; 334 335 index = string.indexOf(substring, index); 336 } 337 338 if (oldIndex < string.length()) 339 result.append(string.substring(oldIndex, string.length())); 340 341 return result.toString(); 342 } 343 344 345 } 346 | Popular Tags |