001: /**
002: * ========================================
003: * JFreeReport : a free Java report library
004: * ========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * $Id: CSVTableModelProducer.java 3048 2007-07-28 18:02:42Z tmorgner $
027: * ------------
028: * (C) Copyright 2000-2005, by Object Refinery Limited.
029: * (C) Copyright 2005-2007, by Pentaho Corporation.
030: */package org.jfree.report.modules.misc.tablemodel;
031:
032: import java.io.BufferedReader;
033: import java.io.FileNotFoundException;
034: import java.io.FileReader;
035: import java.io.IOException;
036: import java.io.InputStream;
037: import java.io.InputStreamReader;
038: import java.util.ArrayList;
039: import javax.swing.table.TableModel;
040:
041: import org.jfree.report.util.CSVTokenizer;
042:
043: /**
044: * Creates a <code>TableModel</code> using a file formated in CSV for input. The
045: * separation can be what ever you want (as it is an understandable regexp). The default
046: * separator is a <code>,</code>.
047: *
048: * @author Mimil
049: */
050: public class CSVTableModelProducer {
051: private BufferedReader reader;
052: private String separator;
053: private CSVTableModel tableModel;
054: private boolean columnNameFirst;
055:
056: public CSVTableModelProducer(final InputStream in) {
057: this (new BufferedReader(new InputStreamReader(in)));
058: }
059:
060: public CSVTableModelProducer(final String filename)
061: throws FileNotFoundException {
062: this (new BufferedReader(new FileReader(filename)));
063: }
064:
065: public CSVTableModelProducer(final BufferedReader r) {
066: if (r == null) {
067: throw new NullPointerException(
068: "The input stream must not be null");
069: }
070: this .reader = r;
071: this .separator = ",";
072: }
073:
074: public void close() throws IOException {
075: this .reader.close();
076: }
077:
078: /**
079: * Parses the input and stores data in a TableModel.
080: *
081: * @see this.getTableModel()
082: */
083: public synchronized TableModel parse() throws IOException {
084: if (tableModel != null) {
085: return tableModel;
086: }
087:
088: this .tableModel = new CSVTableModel();
089:
090: if (this .columnNameFirst == true) { //read the fisrt line
091: final String first = this .reader.readLine();
092:
093: if (first == null) {
094: // after the end of the file it makes no sense to read anything.
095: // so we can safely return ..
096: return tableModel;
097: }
098: this .tableModel.setColumnNames(splitLine(first));
099: }
100:
101: final ArrayList data = new ArrayList();
102: String line;
103: int maxLength = 0;
104: while ((line = this .reader.readLine()) != null) {
105: final String[] o = splitLine(line);
106: if (o.length > maxLength) {
107: maxLength = o.length;
108: }
109: data.add(o);
110: }
111:
112: close();
113:
114: final Object[][] array = new Object[data.size()][];
115: data.toArray(array);
116: this .tableModel.setData(array);
117: return tableModel;
118: }
119:
120: private String[] splitLine(final String line) {
121: final ArrayList row = new ArrayList();
122: final CSVTokenizer tokenizer = new CSVTokenizer(line,
123: getSeparator());
124: while (tokenizer.hasMoreElements()) {
125: row.add(tokenizer.nextElement());
126: }
127: return (String[]) row.toArray(new String[row.size()]);
128: }
129:
130: /**
131: * Returns the current separator used to parse the input.
132: *
133: * @return a regexp
134: */
135: public String getSeparator() {
136: return separator;
137: }
138:
139: /**
140: * Sets the separator for parsing the input. It can be a regexp as we use the function
141: * <code>String.split()</code>. The default separator is a <code>;</code>.
142: *
143: * @param separator a regexp
144: */
145: public void setSeparator(final String separator) {
146: this .separator = separator;
147: }
148:
149: /**
150: * Creates the corrspondant TableModel of the input.
151: *
152: * @return the new TableModel
153: */
154: public TableModel getTableModel() throws IOException {
155: return this .parse();
156: }
157:
158: /**
159: * Tells if the first line of the input was column names.
160: *
161: * @return boolean
162: */
163: public boolean isColumnNameFirstLine() {
164: return columnNameFirst;
165: }
166:
167: /**
168: * Set if the first line of the input is column names or not.
169: *
170: * @param columnNameFirst boolean
171: */
172: public void setColumnNameFirstLine(final boolean columnNameFirst) {
173: this.columnNameFirst = columnNameFirst;
174: }
175:
176: }
|