package org.autoplot.hapiserver; import java.io.IOException; import java.io.OutputStream; import java.text.DecimalFormat; 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.das2.util.NumberFormatUtil; 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; 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(); } } @Override public void initialize( JSONObject info, OutputStream out, QDataSet record) { unitsFormatter= new boolean[record.length()]; datumFormatter= new DatumFormatter[record.length()]; quotes= new boolean[record.length()]; units= new Units[record.length()]; for ( int i=0; i0 ) { datumFormatter[i]= getDataFormatter( dfs, u ); } else { datumFormatter[i]= u.getDatumFormatterFactory().defaultFormatter(); } unitsFormatter[i]= false; quotes[i]= false; } } if ( false ) { System.err.println("==="); for ( int i=0; i