4
4
package edu .caltech .ipac .firefly .server .util .ipactable ;
5
5
6
6
import edu .caltech .ipac .astro .FITSTableReader ;
7
+ import edu .caltech .ipac .firefly .data .table .TableMeta ;
7
8
import edu .caltech .ipac .firefly .server .db .spring .mapper .DataGroupUtil ;
8
9
import edu .caltech .ipac .firefly .server .query .TemplateGenerator ;
9
10
import edu .caltech .ipac .firefly .server .util .DsvToDataGroup ;
@@ -27,12 +28,6 @@ public class DataGroupReader {
27
28
public static final String LINE_SEP = System .getProperty ("line.separator" );
28
29
private static final Logger .LoggerImpl logger = Logger .getLogger ();
29
30
30
- public static enum Format { TSV (CSVFormat .TDF ), CSV (CSVFormat .DEFAULT ), IPACTABLE (), UNKNOWN (), FIXEDTARGETS (), FITS (), JSON ();
31
- CSVFormat type ;
32
- Format () {}
33
- Format (CSVFormat type ) {this .type = type ;}
34
- }
35
-
36
31
public static DataGroup readAnyFormat (File inf ) throws IOException {
37
32
return readAnyFormat (inf , 0 );
38
33
}
@@ -62,10 +57,10 @@ public static DataGroup readAnyFormat(File inf, int tableIndex) throws IOExcepti
62
57
throw new IOException ("Unsupported format, file:" + inf );
63
58
}
64
59
}
65
-
60
+
66
61
public static DataGroup read (File inf , String ... onlyColumns ) throws IOException {
67
62
return read (inf , false , onlyColumns );
68
-
63
+
69
64
}
70
65
71
66
public static DataGroup read (File inf , boolean readAsString , String ... onlyColumns ) throws IOException {
@@ -77,78 +72,15 @@ public static DataGroup read(File inf, boolean isFixedLength, boolean readAsStri
77
72
}
78
73
79
74
public static DataGroup read (File inf , boolean isFixedLength , boolean readAsString , boolean saveFormattedData , String ... onlyColumns ) throws IOException {
80
-
81
75
TableDef tableDef = IpacTableUtil .getMetaInfo (inf );
82
- List <DataGroup .Attribute > attributes = tableDef .getAllAttributes ();
83
- List <DataType > cols = tableDef .getCols ();
84
-
85
- if (readAsString ) {
86
- for (DataType dt : cols ) {
87
- dt .setDataType (String .class );
88
- }
89
- }
90
-
91
- DataGroup headers = new DataGroup (null , cols );
92
- DataGroup data = null ;
93
- boolean isSelectedColumns = onlyColumns != null && onlyColumns .length > 0 ;
94
-
95
- if (isSelectedColumns ) {
96
- List <DataType > selCols = new ArrayList <DataType >();
97
- for (String c : onlyColumns ) {
98
- DataType dt = headers .getDataDefintion (c );
99
- if (dt != null ) {
100
- try {
101
- selCols .add ((DataType ) dt .clone ());
102
- } catch (CloneNotSupportedException e ) {} // shouldn't happen
103
- }
104
- }
105
- data = new DataGroup (null , selCols );
106
- } else {
107
- data = headers ;
108
- }
109
-
110
- data .setAttributes (attributes );
111
-
112
- String line = null ;
113
- int lineNum = 0 ;
114
-
115
- BufferedReader reader = new BufferedReader (new FileReader (inf ), IpacTableUtil .FILE_IO_BUFFER_SIZE );
116
- try {
117
- line = reader .readLine ();
118
- lineNum ++;
119
- while (line != null ) {
120
- DataObject row = IpacTableUtil .parseRow (headers , line , isFixedLength , saveFormattedData );
121
- if (row != null ) {
122
- if (isSelectedColumns ) {
123
- DataObject arow = new DataObject (data );
124
- for (DataType dt : data .getDataDefinitions ()) {
125
- arow .setDataElement (dt , row .getDataElement (dt .getKeyName ()));
126
- if (dt .getFormatInfo ().isDefault ()) {
127
- dt .getFormatInfo ().setDataFormat (
128
- headers .getDataDefintion (dt .getKeyName ()).getFormatInfo ().getDataFormatStr ());
129
- }
130
- }
131
- data .add (arow );
132
- } else {
133
- data .add (row );
134
- }
135
- }
136
- line = reader .readLine ();
137
- lineNum ++;
138
- }
139
- } catch (Exception e ) {
140
- String msg = e .getMessage ()+"<br>on line " +lineNum +": " + line ;
141
- if (msg .length ()>128 ) msg = msg .substring (0 ,128 )+"..." ;
142
- logger .error (e , "on line " +lineNum +": " + line );
143
- throw new IOException (msg );
144
- } finally {
145
- reader .close ();
146
- }
76
+ BufferedReader bufferedReader = new BufferedReader (new FileReader (inf ), IpacTableUtil .FILE_IO_BUFFER_SIZE );
77
+ return doRead (bufferedReader , tableDef , isFixedLength , readAsString , saveFormattedData , onlyColumns );
78
+ }
147
79
148
- if (! saveFormattedData ) {
149
- data . shrinkToFitData ( );
150
- }
151
- return data ;
80
+ public static DataGroup read ( Reader reader , boolean isFixedLength , boolean readAsString , boolean saveFormattedData , String ... onlyColumns ) throws IOException {
81
+ BufferedReader bufferedReader = new BufferedReader ( reader , IpacTableUtil . FILE_IO_BUFFER_SIZE );
82
+ TableDef tableDef = IpacTableUtil . getMetaInfo ( bufferedReader );
83
+ return doRead ( bufferedReader , tableDef , isFixedLength , readAsString , saveFormattedData , onlyColumns ) ;
152
84
}
153
85
154
86
public static DataGroup getEnumValues (File inf , int cutoffPoint ) throws IOException {
@@ -236,7 +168,7 @@ public static Format guessFormat(File inf) throws IOException {
236
168
}
237
169
238
170
int readAhead = 10 ;
239
-
171
+
240
172
int row = 0 ;
241
173
BufferedReader reader = new BufferedReader (new FileReader (inf ), IpacTableUtil .FILE_IO_BUFFER_SIZE );
242
174
try {
@@ -297,11 +229,91 @@ public static Format guessFormat(File inf) throws IOException {
297
229
298
230
}
299
231
232
+ private static DataGroup doRead (BufferedReader bufferedReader , TableDef tableDef , boolean isFixedLength , boolean readAsString , boolean saveFormattedData , String ... onlyColumns ) throws IOException {
233
+
234
+ List <DataGroup .Attribute > attributes = tableDef .getAllAttributes ();
235
+ List <DataType > cols = tableDef .getCols ();
236
+
237
+ if (readAsString ) {
238
+ for (DataType dt : cols ) {
239
+ dt .setDataType (String .class );
240
+ }
241
+ }
242
+
243
+ DataGroup inData = new DataGroup (null , cols );
244
+ DataGroup outData = null ;
245
+ boolean isSelectedColumns = onlyColumns != null && onlyColumns .length > 0 ;
246
+
247
+ if (isSelectedColumns ) {
248
+ List <DataType > selCols = new ArrayList <DataType >();
249
+ for (String c : onlyColumns ) {
250
+ DataType dt = inData .getDataDefintion (c );
251
+ if (dt != null ) {
252
+ try {
253
+ selCols .add ((DataType ) dt .clone ());
254
+ } catch (CloneNotSupportedException e ) {} // shouldn't happen
255
+ }
256
+ }
257
+ outData = new DataGroup (null , selCols );
258
+ } else {
259
+ outData = inData ;
260
+ }
261
+
262
+ outData .setAttributes (attributes );
263
+
264
+ String line = null ;
265
+ int lineNum = tableDef .getExtras () == null ? 0 : tableDef .getExtras ().getKey ();
266
+
267
+ try {
268
+ line = tableDef .getExtras () == null ? bufferedReader .readLine () : tableDef .getExtras ().getValue ();
269
+ lineNum ++;
270
+ while (line != null ) {
271
+ DataObject row = IpacTableUtil .parseRow (inData , line , isFixedLength , saveFormattedData );
272
+ if (row != null ) {
273
+ if (isSelectedColumns ) {
274
+ DataObject arow = new DataObject (outData );
275
+ for (DataType dt : outData .getDataDefinitions ()) {
276
+ arow .setDataElement (dt , row .getDataElement (dt .getKeyName ()));
277
+ if (dt .getFormatInfo ().isDefault ()) {
278
+ dt .getFormatInfo ().setDataFormat (
279
+ inData .getDataDefintion (dt .getKeyName ()).getFormatInfo ().getDataFormatStr ());
280
+ }
281
+ }
282
+ outData .add (arow );
283
+ } else {
284
+ outData .add (row );
285
+ }
286
+ }
287
+ line = bufferedReader .readLine ();
288
+ lineNum ++;
289
+ }
290
+ } catch (Exception e ) {
291
+ String msg = e .getMessage ()+"<br>on line " +lineNum +": " + line ;
292
+ if (msg .length ()>128 ) msg = msg .substring (0 ,128 )+"..." ;
293
+ logger .error (e , "on line " +lineNum +": " + line );
294
+ throw new IOException (msg );
295
+ } finally {
296
+ bufferedReader .close ();
297
+ }
298
+
299
+ if (!saveFormattedData ) {
300
+ outData .shrinkToFitData ();
301
+ }
302
+ return outData ;
303
+
304
+ }
305
+
300
306
301
307
302
308
//====================================================================
303
309
//
304
310
//====================================================================
305
311
312
+ public static enum Format { TSV (CSVFormat .TDF ), CSV (CSVFormat .DEFAULT ), IPACTABLE (), UNKNOWN (), FIXEDTARGETS (), FITS (), JSON ();
313
+ CSVFormat type ;
314
+ Format () {}
315
+ Format (CSVFormat type ) {this .type = type ;}
316
+ }
317
+
306
318
}
307
319
0 commit comments