package org.autoplot.hapiserver; import java.io.IOException; import java.io.OutputStream; import java.text.ParseException; import java.util.logging.Level; import java.util.logging.Logger; import org.das2.datum.Datum; import org.das2.datum.Units; import org.das2.datum.UnitsUtil; import org.das2.datum.format.DatumFormatter; import org.das2.datum.format.EnumerationDatumFormatter; import org.das2.datum.format.FormatStringFormatter; import org.das2.datum.format.TimeDatumFormatter; import org.das2.qds.DDataSet; import org.json.JSONObject; import org.das2.qds.DataSetUtil; import org.das2.qds.QDataSet; import org.das2.qds.SparseDataSetBuilder; import org.das2.qds.WritableDataSet; import org.json.JSONArray; import org.json.JSONException; /** * Comma Separated Value (CSV) formatter * @author jbf */ public class CsvDataFormatter implements DataFormatter { private static final Logger logger= Logger.getLogger("hapi.csv"); boolean[] unitsFormatter; DatumFormatter[] datumFormatter; boolean[] quotes; String[] fill; Units[] units; /** * @see AsciiTableDataSourceFormat#getDataFormatter * @param df format, such as %.2f or %d * @param u the units to fall back on. * @return the DatumFormatter. */ private DatumFormatter getDataFormatter( String df, Units u ) { try { if ( !df.contains("%") ) df= "%"+df; //TODO: would be nice if we could verify formatter. I had %f5.2 instead of %5.2f and it wasn't telling me. return new FormatStringFormatter( df, false ); } catch ( RuntimeException ex ) { logger.log( Level.SEVERE, ex.getMessage(), ex); return u.getDatumFormatterFactory().defaultFormatter(); } } /** * return the parameter number for the column. * @param col * @return */ int columnMap( int col ) { return col; } @Override public void initialize( JSONObject info, OutputStream out, QDataSet record) { try { unitsFormatter= new boolean[record.length()]; datumFormatter= new DatumFormatter[record.length()]; quotes= new boolean[record.length()]; units= new Units[record.length()]; fill= new String[record.length()]; int[] lens= Util.getNumberOfElements(info); JSONArray parameters= info.getJSONArray("parameters"); JSONObject parameter= parameters.getJSONObject(0); int iparam=0; int iele=0; for ( int i=0; i0 ) { datumFormatter[i]= getDataFormatter( dfs, u ); } else { datumFormatter[i]= u.getDatumFormatterFactory().defaultFormatter(); } unitsFormatter[i]= false; quotes[i]= false; fill[i]= parameter.getString("fill"); } iele++; if ( iele==lens[iparam] ) { iparam++; iele=0; if ( iparam==parameters.length() ) { if ( i+1!=record.length() ) { throw new IllegalStateException("things have gone wrong"); } } else { parameter= parameters.getJSONObject(iparam); } } } // if ( false ) { // System.err.println("==="); // for ( int i=0; i