001 /****************************************************************************** 002 * Copyright (C) MActor Developers. All rights reserved. * 003 * ---------------------------------------------------------------------------* 004 * This file is part of MActor. * 005 * * 006 * MActor is free software; you can redistribute it and/or modify * 007 * it under the terms of the GNU General Public License as published by * 008 * the Free Software Foundation; either version 2 of the License, or * 009 * (at your option) any later version. * 010 * * 011 * MActor is distributed in the hope that it will be useful, * 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 014 * GNU General Public License for more details. * 015 * * 016 * You should have received a copy of the GNU General Public License * 017 * along with MActor; if not, write to the Free Software * 018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * 019 ******************************************************************************/ 020 package org.mactor.framework.data; 021 022 import java.io.File; 023 import java.io.FileReader; 024 import java.io.IOException; 025 import java.io.LineNumberReader; 026 027 import org.mactor.framework.MactorException; 028 import org.mactor.framework.spec.ProjectContext; 029 030 public class CsvDataProvider implements DataProvider { 031 private String dataSourceConnectionSpec; 032 CsvDataProvider(String dataSourceConnectionSpec) { 033 this.dataSourceConnectionSpec = dataSourceConnectionSpec; 034 } 035 public DataTable loadData() throws MactorException { 036 File source = ProjectContext.getGlobalInstance().getAbsolutePath(dataSourceConnectionSpec); 037 if (!source.exists()) 038 throw new MactorException("The specifed file does not exist '" + source.getAbsolutePath() + "'"); 039 try { 040 LineNumberReader fr = null; 041 try { 042 fr = new LineNumberReader(new FileReader(source)); 043 String header = fr.readLine(); 044 if (header == null) 045 throw new MactorException("Invalid file format. The file '" + source.getAbsolutePath() + "' is empty"); 046 String[] cols = header.split(";"); 047 if (cols == null || cols.length == 0) 048 throw new MactorException("Invalid file format. The file '" + source.getAbsolutePath() + "' does not contains any columns (colums must be seperated by the ';' character)"); 049 DataTable table = new DataTable(); 050 for (String col : cols) 051 table.addColumn(col.trim()); 052 int colCount = cols.length; 053 for (;;) { 054 String line = fr.readLine(); 055 if (line == null) 056 break; 057 line = line.trim(); 058 if (line.length() == 0) 059 continue; 060 if (line.startsWith("#")) // skip comments 061 continue; 062 String[] cells = line.split(";"); 063 int count = cells == null ? 0 : cells.length; 064 if (count != colCount) 065 throw new MactorException("Invalid file format. The file '" + source.getAbsolutePath() + "' contains an invalid number of columns on line '" + fr.getLineNumber() 066 + "'. Expected " + colCount + "' columns, but was '" + count + "'"); 067 table.addRow(cells); 068 } 069 return table; 070 } finally { 071 if (fr != null) 072 fr.close(); 073 } 074 } catch (IOException ioe) { 075 throw new MactorException("Failed to read the file '" + source.getAbsolutePath() + "'. Error:" + ioe.getMessage(), ioe); 076 } 077 } 078 }