1 package org.simantics.simulation.export;
4 import java.io.IOException;
5 import java.text.DecimalFormatSymbols;
6 import java.util.Collections;
8 import java.util.Locale;
10 import org.eclipse.core.runtime.preferences.InstanceScope;
11 import org.eclipse.jface.preference.IPreferenceStore;
12 import org.eclipse.ui.preferences.ScopedPreferenceStore;
13 import org.osgi.service.prefs.Preferences;
14 import org.simantics.databoard.Accessors;
15 import org.simantics.databoard.Bindings;
16 import org.simantics.databoard.Datatypes;
17 import org.simantics.databoard.accessor.RecordAccessor;
18 import org.simantics.databoard.accessor.error.AccessorConstructionException;
19 import org.simantics.databoard.accessor.error.AccessorException;
20 import org.simantics.databoard.accessor.reference.ChildReference;
21 import org.simantics.databoard.binding.mutable.Variant;
22 import org.simantics.databoard.type.Datatype;
23 import org.simantics.databoard.type.DoubleType;
24 import org.simantics.databoard.type.RecordType;
25 import org.simantics.export.core.ExportContext;
26 import org.simantics.export.core.error.ExportException;
27 import org.simantics.export.core.intf.FormatClass;
28 import org.simantics.export.core.manager.Content;
29 import org.simantics.history.HistoryException;
30 import org.simantics.history.csv.ColumnSeparator;
31 import org.simantics.history.csv.DecimalSeparator;
34 * This class represents the CSV (Comma Separated Value) file format.
36 * @author toni.kalajainen@semantum.fi
38 public class CSVFormat implements FormatClass {
41 public static ChildReference P_CSV_COLUMN_SEPARATOR = ChildReference.parsePath("Comma Separated Value (CSV)/Column Separator");
42 public static ChildReference P_CSV_DECIMAL_SEPARATOR = ChildReference.parsePath("Comma Separated Value (CSV)/Decimal Separator");
43 public static ChildReference P_CSV_TIME_STEP = ChildReference.parsePath("Comma Separated Value (CSV)/Time Step");
45 static RecordType options;
46 static RecordType csvOptions;
49 Datatype second = new DoubleType("s");
51 csvOptions = new RecordType();
52 csvOptions.addComponent("Time Step", second);
53 csvOptions.addComponent("Column Separator", Datatypes.STRING);
54 csvOptions.addComponent("Decimal Separator", Datatypes.STRING);
56 options = new RecordType();
57 options.addComponent("Comma Separated Value (CSV)", csvOptions);
61 public RecordType options(ExportContext context)
62 throws ExportException {
67 public List<String> validate(ExportContext context, Variant options) throws ExportException {
68 return Collections.emptyList();
73 public void fillDefaultPrefs( ExportContext ctx, Variant options ) throws ExportException {
74 // 1. Figure out suitable default values
75 IPreferenceStore csvnode = new ScopedPreferenceStore( InstanceScope.INSTANCE, CSVPreferences.P_NODE );
77 Double timeStep = CSVPreferences.DEFAULT_CSV_TIME_STEP;
78 String decimalSeparator = ".";
79 String columnSeparator = "\t";
81 Locale locale = Locale.getDefault();
82 DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance( locale );
83 decimalSeparator = symbols.getDecimalSeparator()+"";
84 columnSeparator = decimalSeparator.equals(",")?"\\t":",";
86 if (csvnode != null) {
87 if ( csvnode.contains(CSVPreferences.P_CSV_TIME_STEP) ) timeStep = csvnode.getDouble(CSVPreferences.P_CSV_TIME_STEP);
88 if ( csvnode.contains(CSVPreferences.P_CSV_DECIMAL_SEPARATOR) ) decimalSeparator = csvnode.getString(CSVPreferences.P_CSV_DECIMAL_SEPARATOR);
89 if ( csvnode.contains(CSVPreferences.P_CSV_COLUMN_SEPARATOR) ) columnSeparator = csvnode.getString(CSVPreferences.P_CSV_COLUMN_SEPARATOR);
92 // 2. Write default values
94 RecordAccessor ra = Accessors.getAccessor(options);
95 ra.setValue(P_CSV_COLUMN_SEPARATOR, Bindings.STRING, columnSeparator);
96 ra.setValue(P_CSV_DECIMAL_SEPARATOR, Bindings.STRING, decimalSeparator);
97 ra.setValue(P_CSV_TIME_STEP, Bindings.DOUBLE, timeStep);
99 } catch (AccessorConstructionException e) {
100 throw new ExportException(e);
101 } catch (AccessorException e) {
102 throw new ExportException(e);
107 public void savePref(Variant options, Preferences contentScopeNode, Preferences workbenchScopeNode) throws ExportException {
109 RecordAccessor ra = Accessors.getAccessor(options);
111 String columnSeparator = (String) ra.getValue( P_CSV_COLUMN_SEPARATOR, Bindings.STRING );
112 if ( columnSeparator != null ) workbenchScopeNode.put(CSVPreferences.P_CSV_COLUMN_SEPARATOR, columnSeparator);
114 String decimalSeparator = (String) ra.getValue(P_CSV_DECIMAL_SEPARATOR, Bindings.STRING );
115 if ( decimalSeparator != null ) workbenchScopeNode.put(CSVPreferences.P_CSV_DECIMAL_SEPARATOR, decimalSeparator);
117 Double timeStep = (Double) ra.getValue(P_CSV_TIME_STEP, Bindings.DOUBLE );
118 if ( timeStep != null ) contentScopeNode.putDouble(CSVPreferences.P_CSV_TIME_STEP, timeStep);
120 } catch (AccessorConstructionException e) {
121 throw new ExportException(e);
122 } catch (AccessorException e) {
123 throw new ExportException(e);
128 public void loadPref(Variant options, Preferences contentScopeNode, Preferences workbenchScopeNode) throws ExportException {
130 RecordAccessor ra = Accessors.getAccessor(options);
132 String columnSeparator = workbenchScopeNode.get(CSVPreferences.P_CSV_COLUMN_SEPARATOR, null);
133 if ( columnSeparator != null ) ra.setValue(P_CSV_COLUMN_SEPARATOR, Bindings.STRING, columnSeparator );
135 String decimalSeparator = workbenchScopeNode.get(CSVPreferences.P_CSV_DECIMAL_SEPARATOR, null);
136 if ( decimalSeparator != null ) ra.setValue(P_CSV_DECIMAL_SEPARATOR, Bindings.STRING, decimalSeparator );
138 Double timeStep = contentScopeNode.getDouble(CSVPreferences.P_CSV_TIME_STEP, 0);
139 if ( timeStep != null ) ra.setValue(P_CSV_TIME_STEP, Bindings.DOUBLE, timeStep );
141 } catch (AccessorConstructionException e) {
142 throw new ExportException(e);
143 } catch (AccessorException e) {
144 throw new ExportException(e);
149 public Object createFile(ExportContext context, File outputFile, Variant options) throws ExportException {
150 CSVWriter writer = new CSVWriter();
151 writer.file = outputFile;
154 /// Read configurations
155 RecordAccessor ra = Accessors.getAccessor(options);
158 Double startTime = (Double) ra.getValue( ExperimentExportClass.P_EXPERIMENT_START, Bindings.DOUBLE );
159 Double endTime = (Double) ra.getValue( ExperimentExportClass.P_EXPERIMENT_END, Bindings.DOUBLE );
160 writer.setTimeRange(startTime, endTime);
163 String columnSeparator = (String) ra.getValue( CSVFormat.P_CSV_COLUMN_SEPARATOR, Bindings.STRING );
164 writer.setColumnSeparator(ColumnSeparator.fromPreference(columnSeparator));
166 String decimalSeparator = (String) ra.getValue( CSVFormat.P_CSV_DECIMAL_SEPARATOR, Bindings.STRING );
167 writer.setDecimalSeparator(DecimalSeparator.fromPreference(decimalSeparator));
169 Double timeStep = (Double) ra.getValue( CSVFormat.P_CSV_TIME_STEP, Bindings.DOUBLE );
170 writer.setTimeStep(timeStep);
172 } catch (AccessorConstructionException e) {
173 throw new ExportException(e);
174 } catch (AccessorException e) {
175 throw new ExportException(e);
182 public Object openFile(ExportContext context, File inputFile, Variant options) throws ExportException {
183 throw new ExportException("Not implemented");
187 public void closeFile(ExportContext context, Object handle_) throws ExportException {
188 CSVWriter handle = (CSVWriter) handle_;
191 } catch (IOException e) {
192 throw new ExportException(e);
193 } catch (HistoryException e) {
194 throw new ExportException(e);
199 public void addAttachment(ExportContext context, Object handle, List<Content> attachments) throws ExportException {
200 throw new ExportException( "Cannot add attachments to a CSV file." );