Frames | No Frames |
1: /** 2: * ======================================== 3: * JFreeReport : a free Java report library 4: * ======================================== 5: * 6: * Project Info: http://reporting.pentaho.org/ 7: * 8: * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors. 9: * 10: * This library is free software; you can redistribute it and/or modify it under the terms 11: * of the GNU Lesser General Public License as published by the Free Software Foundation; 12: * either version 2.1 of the License, or (at your option) any later version. 13: * 14: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 15: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 16: * See the GNU Lesser General Public License for more details. 17: * 18: * You should have received a copy of the GNU Lesser General Public License along with this 19: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, 20: * Boston, MA 02111-1307, USA. 21: * 22: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 23: * in the United States and other countries.] 24: * 25: * ------------ 26: * $Id: CSVTableModelProducer.java 2725 2007-04-01 18:49:29Z taqua $ 27: * ------------ 28: * (C) Copyright 2000-2005, by Object Refinery Limited. 29: * (C) Copyright 2005-2007, by Pentaho Corporation. 30: */ 31: 32: package org.jfree.report.modules.misc.tablemodel; 33: 34: import java.io.BufferedReader; 35: import java.io.FileNotFoundException; 36: import java.io.FileReader; 37: import java.io.IOException; 38: import java.io.InputStream; 39: import java.io.InputStreamReader; 40: import java.util.ArrayList; 41: import javax.swing.table.TableModel; 42: 43: import org.jfree.report.util.CSVTokenizer; 44: 45: /** 46: * Creates a <code>TableModel</code> using a file formated in CSV for input. The 47: * separation can be what ever you want (as it is an understandable regexp). The default 48: * separator is a <code>,</code>. 49: * 50: * @author Mimil 51: */ 52: public class CSVTableModelProducer 53: { 54: private BufferedReader reader; 55: private String separator; 56: private CSVTableModel tableModel; 57: private boolean columnNameFirst; 58: 59: public CSVTableModelProducer (final InputStream in) 60: { 61: this(new BufferedReader(new InputStreamReader(in))); 62: } 63: 64: public CSVTableModelProducer (final String filename) 65: throws FileNotFoundException 66: { 67: this(new BufferedReader(new FileReader(filename))); 68: } 69: 70: public CSVTableModelProducer (final BufferedReader r) 71: { 72: if (r == null) 73: { 74: throw new NullPointerException("The input stream must not be null"); 75: } 76: this.reader = r; 77: this.separator = ","; 78: } 79: 80: public void close () 81: throws IOException 82: { 83: this.reader.close(); 84: } 85: 86: /** 87: * Parses the input and stores data in a TableModel. 88: * 89: * @see this.getTableModel() 90: */ 91: public synchronized TableModel parse () 92: throws IOException 93: { 94: if (tableModel != null) 95: { 96: return tableModel; 97: } 98: 99: 100: this.tableModel = new CSVTableModel(); 101: 102: if (this.columnNameFirst == true) 103: { //read the fisrt line 104: final String first = this.reader.readLine(); 105: 106: if (first == null) 107: { 108: // after the end of the file it makes no sense to read anything. 109: // so we can safely return .. 110: return tableModel; 111: } 112: this.tableModel.setColumnNames(splitLine(first)); 113: } 114: 115: final ArrayList data = new ArrayList(); 116: String line; 117: int maxLength = 0; 118: while ((line = this.reader.readLine()) != null) 119: { 120: final String[] o = splitLine(line); 121: if (o.length > maxLength) 122: { 123: maxLength = o.length; 124: } 125: data.add(o); 126: } 127: 128: close(); 129: 130: final Object[][] array = new Object[data.size()][]; 131: data.toArray(array); 132: this.tableModel.setData(array); 133: return tableModel; 134: } 135: 136: private String[] splitLine (final String line) 137: { 138: final ArrayList row = new ArrayList(); 139: final CSVTokenizer tokenizer = new CSVTokenizer(line, getSeparator()); 140: while (tokenizer.hasMoreElements()) 141: { 142: row.add(tokenizer.nextElement()); 143: } 144: return (String[]) row.toArray(new String[row.size()]); 145: } 146: 147: /** 148: * Returns the current separator used to parse the input. 149: * 150: * @return a regexp 151: */ 152: public String getSeparator () 153: { 154: return separator; 155: } 156: 157: /** 158: * Sets the separator for parsing the input. It can be a regexp as we use the function 159: * <code>String.split()</code>. The default separator is a <code>;</code>. 160: * 161: * @param separator a regexp 162: */ 163: public void setSeparator (final String separator) 164: { 165: this.separator = separator; 166: } 167: 168: /** 169: * Creates the corrspondant TableModel of the input. 170: * 171: * @return the new TableModel 172: */ 173: public TableModel getTableModel () 174: throws IOException 175: { 176: return this.parse(); 177: } 178: 179: /** 180: * Tells if the first line of the input was column names. 181: * 182: * @return boolean 183: */ 184: public boolean isColumnNameFirstLine () 185: { 186: return columnNameFirst; 187: } 188: 189: /** 190: * Set if the first line of the input is column names or not. 191: * 192: * @param columnNameFirst boolean 193: */ 194: public void setColumnNameFirstLine (final boolean columnNameFirst) 195: { 196: this.columnNameFirst = columnNameFirst; 197: } 198: 199: }