001: package jimm.datavision.source.charsep;
002:
003: import jimm.datavision.*;
004: import jimm.datavision.source.*;
005: import jimm.util.XMLWriter;
006: import java.io.*;
007: import java.util.*;
008:
009: /**
010: * A data source for files whose lines are rows and columns are separated
011: * by a character.
012: *
013: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
014: */
015: public class CharSepSource extends DataSource {
016:
017: protected static final char DEFAULT_SEP_CHAR = ',';
018:
019: protected ArrayList columns;
020: protected char sepChar;
021: protected BufferedReader reader;
022: protected String sourceFilePath;
023:
024: public CharSepSource(Report report) {
025: super (report, new CharSepQuery(report));
026: columns = new ArrayList();
027: sepChar = DEFAULT_SEP_CHAR;
028: }
029:
030: public boolean canJoinTables() {
031: return true;
032: }
033:
034: public boolean isSQLGenerated() {
035: return false;
036: }
037:
038: public boolean isConnectionEditable() {
039: return false;
040: }
041:
042: public boolean areRecordsSelectable() {
043: return true;
044: }
045:
046: public boolean areRecordsSortable() {
047: return false;
048: }
049:
050: public boolean canGroupRecords() {
051: return false;
052: }
053:
054: public boolean usesSourceFile() {
055: return true;
056: }
057:
058: public boolean needsSourceFile() {
059: return reader == null;
060: }
061:
062: public boolean alreadyUsedSourceFile() {
063: return sourceFilePath != null && reader == null;
064: }
065:
066: public void setSourceFile(String filePath)
067: throws FileNotFoundException {
068: sourceFilePath = filePath;
069: reuseSourceFile();
070: }
071:
072: public void reuseSourceFile() throws FileNotFoundException {
073: setInput(new FileReader(sourceFilePath));
074: }
075:
076: public void setInput(Reader reader) {
077: if (reader instanceof BufferedReader)
078: this .reader = (BufferedReader) reader;
079: else
080: this .reader = new BufferedReader(reader);
081: }
082:
083: public void setInput(InputStreamReader inputStreamReader) {
084: reader = new BufferedReader(inputStreamReader);
085: }
086:
087: public void setInput(String fileName) throws FileNotFoundException {
088: setSourceFile(fileName);
089: }
090:
091: public char getSepChar() {
092: return sepChar;
093: }
094:
095: public void setSepChar(char c) {
096: sepChar = c;
097: }
098:
099: /**
100: * This override not only remembers the column but also hands it to the
101: * query for cacheing.
102: *
103: * @param col a column
104: */
105: public void addColumn(Column col) {
106: columns.add(col);
107: ((CharSepQuery) query).addColumn(col);
108: }
109:
110: /**
111: * Given an id (a column name), returns the column that has that id. If no
112: * column with the specified id exists, returns <code>null</code>. Uses
113: * <code>Table.findColumn</code>.
114: *
115: * @param id a column id
116: * @return a column, or <code>null</code> if no column with the specified
117: * id exists
118: * @see Table#findColumn
119: */
120: public Column findColumn(Object id) {
121: for (Iterator iter = columns.iterator(); iter.hasNext();) {
122: Column col = (Column) iter.next();
123: if (col.getId().equals(id))
124: return col;
125: }
126: return null;
127: }
128:
129: public int indexOfSelectable(Selectable sel) {
130: return columns.indexOf(sel);
131: }
132:
133: public Iterator tables() {
134: return null;
135: }
136:
137: public Iterator tablesUsedInReport() {
138: return null;
139: }
140:
141: public Iterator columns() {
142: return columns.iterator();
143: }
144:
145: public DataCursor execute() {
146: return new CharSepRow(this , query);
147: }
148:
149: BufferedReader getReader() {
150: return reader;
151: }
152:
153: void closeReader() {
154: try {
155: if (reader != null)
156: reader.close();
157: } catch (IOException ioe) {
158: ErrorHandler.error(ioe);
159: } finally {
160: reader = null;
161: }
162: }
163:
164: /**
165: * Writes this database and all its tables as an XML tag.
166: *
167: * @param out a writer that knows how to write XML
168: */
169: protected void doWriteXML(XMLWriter out) {
170: out.startElement("charsep");
171: out.attr("sepchar", sepChar);
172: if (metadataURL != null)
173: out.textElement("metadata-url", metadataURL);
174: else
175: for (Iterator iter = columns.iterator(); iter.hasNext();)
176: ((Column) iter.next()).writeXML(out);
177: out.endElement();
178: }
179:
180: }
|