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 ; 9 import java.text.SimpleDateFormat ; 10 import java.text.ParseException ; 11 12 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(); 32 String script = query.getWhereClause(); 33 if (script != null && script.length() > 0) 34 whereClauseFormula = new Formula(null, source.getReport(), "", script); 35 } 36 37 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 55 List origCurrRowData = currRowData; 60 currRowData = data; 62 Object obj = whereClauseFormula.eval(); 64 acceptRow = ((Boolean )obj).booleanValue(); 65 66 currRowData = origCurrRowData; 69 } 70 else 71 acceptRow = true; 72 } while (!acceptRow); 73 74 return data; 75 } 76 77 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 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 str = data.get(i).toString(); 112 if (str == null || str.length() == 0) 113 data.set(i, new Integer (0)); 114 else if (str.indexOf('.') == -1) 115 data.set(i, new Integer (str)); 116 else 117 data.set(i, new Double (str)); 118 } 119 else if (col.isDate()) 120 data.set(i, parseDate(col, data.get(i).toString())); 121 } 123 124 return data; 125 } 126 127 protected Date parseDate(Column col, String dateString) { 128 String formatString = col.getDateParseFormat(); 129 130 if (dateParsers == null) 132 dateParsers = new HashMap(); 133 SimpleDateFormat parser = (SimpleDateFormat )dateParsers.get(formatString); 134 135 if (parser == null) { 136 parser = new SimpleDateFormat (formatString); 137 dateParsers.put(formatString, parser); 138 } 139 140 try { 141 return parser.parse(dateString); 142 } 143 catch (ParseException 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 |