KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > source > charsep > CharSepRow


1 package jimm.datavision.source.charsep;
2 import jimm.datavision.Formula;
3 import jimm.datavision.ErrorHandler;
4 import jimm.datavision.source.DataCursor;
5 import jimm.datavision.source.Column;
6 import jimm.datavision.source.Query;
7 import java.util.*;
8 import java.io.IOException JavaDoc;
9 import java.text.SimpleDateFormat JavaDoc;
10 import java.text.ParseException JavaDoc;
11
12 /**
13  * A concrete subclass of <code>DataCursor</code> that wraps a delimited file parser.
14  *
15  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
16  */

17 public class CharSepRow extends DataCursor {
18
19 protected CharSepSource source;
20 protected Query query;
21 protected Formula whereClauseFormula;
22 protected boolean noMoreData;
23 protected DelimParser parser;
24 protected HashMap dateParsers;
25 protected boolean dateParseErrorReported;
26
27 CharSepRow(CharSepSource source, Query query) {
28     this.source = source;
29     this.query = query;
30     this.query.findSelectablesUsed(); // Needed so we can find columns later
31

32     String JavaDoc script = query.getWhereClause();
33     if (script != null && script.length() > 0)
34     whereClauseFormula = new Formula(null, source.getReport(), "", script);
35 }
36
37 /**
38  * Returns the next row of data. If there is a where clause, use that to
39  * determine which rows we accept or reject.
40  */

41 public List readRowData() {
42     if (noMoreData)
43     return null;
44
45     List data = null;
46
47     boolean acceptRow;
48     do {
49     data = retrieveNextRow();
50     if (whereClauseFormula != null && data != null) {
51         // Run the Ruby script and retrive its boolean value. If true,
52
// accept the line. Else, reject it and move on to the next
53
// line.
54

55         // First, save the existing current row and make the new data
56
// the current row. We need to do this because the formula we
57
// are about to evaluate may make use of data in the new
58
// current row.
59
List origCurrRowData = currRowData;
60         currRowData = data; // Need data available to formula
61

62         // Evaulate the script and retrieve a boolean.
63
Object JavaDoc obj = whereClauseFormula.eval();
64         acceptRow = ((Boolean JavaDoc)obj).booleanValue();
65
66         // Replace the original current row data (acutally the previous
67
// row, but we don't care).
68
currRowData = origCurrRowData;
69     }
70     else
71         acceptRow = true;
72     } while (!acceptRow);
73
74     return data;
75 }
76
77 /**
78  * Retrieve the next row of data and return it as a list of column values.
79  *
80  * @return a list of column values
81  */

82 protected List retrieveNextRow() {
83     if (parser == null)
84     parser = new DelimParser(source.getReader(), source.getSepChar());
85
86     List data = null;
87     try {
88     data = parser.parse();
89     }
90     catch (IOException JavaDoc ioe) {
91     ErrorHandler.error(ioe);
92     noMoreData = true;
93     return null;
94     }
95     if (data == null) {
96     noMoreData = true;
97     return null;
98     }
99
100     int numColumnsInData = data.size();
101     int i = 0;
102     for (Iterator iter = source.columns(); iter.hasNext(); ++i) {
103     Column col = (Column)iter.next();
104
105     if (i >= numColumnsInData) {
106         data.add(null);
107         continue;
108     }
109
110     if (col.isNumeric()) {
111         String JavaDoc str = data.get(i).toString();
112         if (str == null || str.length() == 0)
113         data.set(i, new Integer JavaDoc(0));
114         else if (str.indexOf('.') == -1)
115         data.set(i, new Integer JavaDoc(str));
116         else
117         data.set(i, new Double JavaDoc(str));
118     }
119     else if (col.isDate())
120         data.set(i, parseDate(col, data.get(i).toString()));
121     // else, it's a string; there is nothing to modify
122
}
123
124     return data;
125 }
126
127 protected Date parseDate(Column col, String JavaDoc dateString) {
128     String JavaDoc formatString = col.getDateParseFormat();
129
130     // Find existing parser, if any
131
if (dateParsers == null)
132     dateParsers = new HashMap();
133     SimpleDateFormat JavaDoc parser = (SimpleDateFormat JavaDoc)dateParsers.get(formatString);
134
135     if (parser == null) {
136     parser = new SimpleDateFormat JavaDoc(formatString);
137     dateParsers.put(formatString, parser);
138     }
139
140     try {
141     return parser.parse(dateString);
142     }
143     catch (ParseException JavaDoc ex) {
144     if (!dateParseErrorReported) {
145         ErrorHandler.error("Parse format string = " + formatString, ex);
146         dateParseErrorReported = true;
147     }
148     return null;
149     }
150 }
151
152 public void close() {
153     source.closeReader();
154 }
155
156 }
157
Popular Tags