1 package prefuse.data.io; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.util.ArrayList ; 6 7 import prefuse.data.Table; 8 import prefuse.data.parser.DataParseException; 9 import prefuse.data.parser.DataParser; 10 import prefuse.data.parser.ParserFactory; 11 import prefuse.data.parser.TypeInferencer; 12 import prefuse.util.collections.ByteArrayList; 13 import prefuse.util.io.IOLib; 14 15 21 public abstract class AbstractTextTableReader extends AbstractTableReader { 22 23 private ParserFactory m_pfactory; 24 private boolean m_hasHeader; 25 26 29 public AbstractTextTableReader() { 30 this(ParserFactory.getDefaultFactory()); 31 } 32 33 38 public AbstractTextTableReader(ParserFactory parserFactory) { 39 m_pfactory = parserFactory; 40 m_hasHeader = true; 41 } 42 43 48 public void setHasHeader(boolean hasHeaderRow) { 49 m_hasHeader = hasHeaderRow; 50 } 51 52 55 public Table readTable(InputStream is) throws DataIOException { 56 57 ByteArrayList buf = null; 61 if ( is.markSupported() ) { 62 is.mark(Integer.MAX_VALUE); 64 } else { 65 try { 67 buf = IOLib.readAsBytes(is); 68 } catch ( IOException ioe ) { 69 throw new DataIOException(ioe); 70 } 71 is = buf.getAsInputStream(); 73 } 74 75 final TypeInferencer di = new TypeInferencer(m_pfactory); 76 final ArrayList headers = new ArrayList (); 77 final int[] dim = new int[] { 0, 0 }; 78 79 TableReadListener scanner = new TableReadListener() { 80 int prevLine = -1; 81 public void readValue(int line, int col, String value) 82 throws DataParseException 83 { 84 if ( line > 1 || !m_hasHeader ) { 86 di.sample(col-1, value); 87 88 if ( line != prevLine ) { 90 prevLine = line; 91 dim[0]++; 92 } 93 } else if ( line == 1 && m_hasHeader ) { 94 headers.add(value); 95 } 96 97 if ( col > dim[1] ) 99 dim[1] = col; 100 } 101 }; 102 103 try { 105 read(is, scanner); 106 } catch ( IOException ioe ) { 107 throw new DataIOException(ioe); 108 } catch ( DataParseException de ) { 109 } 111 112 int nrows = dim[0]; 114 int ncols = dim[1]; 115 final Table table = new Table(nrows, ncols); 116 117 for ( int i=0; i < ncols; ++i ) { 119 String header; 120 if ( m_hasHeader ) { 121 header = (String )headers.get(i); 122 } else { 123 header = getDefaultHeader(i); 124 } 125 table.addColumn(header, di.getType(i)); 126 table.getColumn(i).setParser(di.getParser(i)); 127 } 128 129 dim[0] = dim[1] = -1; 131 132 TableReadListener parser = new TableReadListener() { 133 int prevLine = -1; 134 public void readValue(int line, int col, String value) 135 throws DataParseException 136 { 137 if ( line == 1 && m_hasHeader ) 139 return; 140 if ( line != prevLine ) { 141 prevLine = line; 142 ++dim[0]; 143 } 144 dim[1] = col-1; 145 146 DataParser dp = di.getParser(dim[1]); 153 table.set(dim[0], dim[1], dp.parse(value)); 154 } 155 }; 156 157 try { 159 if ( is.markSupported() ) { 161 is.reset(); 162 } else { 163 is = buf.getAsInputStream(); 164 } 165 read(is, parser); 167 } catch ( IOException ioe ) { 168 throw new DataIOException(ioe); 169 } catch ( DataParseException de ) { 170 throw new DataIOException("Parse exception for column " 171 + '\"' + dim[1] + '\"' + " at row: " + dim[0], de); 172 } 173 174 return table; 175 } 176 177 183 public static String getDefaultHeader(int idx) { 184 if ( idx == 0 ) return "A"; 185 int len = ((int)(Math.log(idx) / Math.log(26))) + 1; 186 char[] h = new char[len]; 187 int p = len; 188 189 h[--p] = (char)('A'+(idx%26)); 190 idx = idx / 26; 191 192 while ( idx > 26 ) { 193 h[--p] = (char)('A'+(idx%26)); 194 idx = idx/26; 195 } 196 if ( idx > 0 ) { 197 h[--p] = (char)('A'+((idx-1)%26)); 198 } 199 200 return new String (h, p, len); 201 } 202 203 211 protected abstract void read(InputStream is, TableReadListener trl) 212 throws IOException , DataParseException; 213 214 } | Popular Tags |