2
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3
* Copyright (C) 2010 - DIGITEO - Clement DAVID
5
* This file must be used under the terms of the CeCILL.
6
* This source file is licensed as described in the file COPYING, which
7
* you should have received as part of this distribution. The terms
8
* are also available at
9
* http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13
package org.scilab.modules.xcos.io.scicos;
16
import java.util.LinkedHashMap;
18
import java.util.logging.Level;
19
import java.util.logging.Logger;
21
import ncsa.hdf.hdf5lib.exceptions.HDF5Exception;
23
import org.scilab.modules.graph.utils.StyleMap;
24
import org.scilab.modules.hdf5.read.H5Read;
25
import org.scilab.modules.hdf5.write.H5Write;
26
import org.scilab.modules.types.ScilabList;
27
import org.scilab.modules.types.ScilabMList;
28
import org.scilab.modules.types.ScilabString;
29
import org.scilab.modules.types.ScilabType;
30
import org.scilab.modules.xcos.block.BasicBlock;
31
import org.scilab.modules.xcos.graph.XcosDiagram;
32
import org.scilab.modules.xcos.io.scicos.ScicosFormatException.VersionMismatchException;
33
import org.scilab.modules.xcos.utils.XcosMessages;
36
* Implement useful methods to easily import or export Scicos data.
38
// CSOFF: ClassDataAbstractionCoupling
39
public class H5RWHandler implements Handler {
40
private static final String CONTEXT = "context";
41
private static final String SCS_M = "scs_m";
42
private static final Logger LOG = Logger.getLogger(H5RWHandler.class.getName());
44
private final String h5File;
47
* Constructor a new instance with a file.
52
public H5RWHandler(File hdf5file) {
53
h5File = hdf5file.getAbsolutePath();
57
* Construct a new instance with the file path.
62
public H5RWHandler(String hdf5file) {
73
* @see org.scilab.modules.xcos.io.scicos.RWHandler#readBlock()
76
public BasicBlock readBlock() throws ScicosFormatException {
77
return readBlock(null);
84
* org.scilab.modules.xcos.io.scicos.RWHandler#readBlock(org.scilab.modules
85
* .xcos.block.BasicBlock)
88
public BasicBlock readBlock(BasicBlock into) throws ScicosFormatException {
89
final ScilabMList data = new ScilabMList();
90
final BlockElement element = new BlockElement(null);
93
if (LOG.isLoggable(Level.FINEST)) {
94
LOG.finest("Reading block from " + h5File);
98
int fileId = H5Read.openFile(h5File);
99
H5Read.readDataFromFile(fileId, data);
100
H5Read.closeFile(fileId);
102
instance = element.decode(data, into);
103
StyleMap style = new StyleMap(instance.getInterfaceFunctionName());
104
style.putAll(instance.getStyle());
106
instance.setStyle(style.toString());
108
} catch (HDF5Exception e) {
109
LOG.severe(e.toString());
113
if (LOG.isLoggable(Level.FINEST)) {
114
LOG.finest("End of reading block from " + h5File);
123
* @see org.scilab.modules.xcos.io.scicos.RWHandler#readContext()
126
public Map<String, String> readContext() {
127
final ScilabList list = new ScilabList();
128
final Map<String, String> result = new LinkedHashMap<String, String>();
130
if (LOG.isLoggable(Level.FINEST)) {
131
LOG.finest("Reading context from " + h5File);
135
int handle = H5Read.openFile(h5File);
137
H5Read.readDataFromFile(handle, list);
139
} catch (HDF5Exception e) {
140
LOG.severe(e.toString());
144
// We are starting at 2 because a struct is composed of
145
// - the fields names (ScilabString)
147
// - variables values...
148
for (int index = 2; index < list.size(); index++) {
149
String key = ((ScilabString) list.get(0)).getData()[0][index];
150
String value = list.get(index).toString();
152
result.put(key, value);
155
if (LOG.isLoggable(Level.FINEST)) {
156
LOG.finest("End of reading context from " + h5File);
165
* @see org.scilab.modules.xcos.io.scicos.RWHandler#readDiagram()
168
public XcosDiagram readDiagram() throws VersionMismatchException {
169
return readDiagram(null);
176
* org.scilab.modules.xcos.io.scicos.RWHandler#readDiagram(org.scilab.modules
177
* .xcos.graph.XcosDiagram)
180
public XcosDiagram readDiagram(XcosDiagram instance) {
181
final ScilabMList data = new ScilabMList();
182
final DiagramElement element = new DiagramElement();
185
if (instance == null) {
186
diagram = new XcosDiagram();
191
diagram.getModel().beginUpdate();
193
if (LOG.isLoggable(Level.FINEST)) {
194
LOG.finest("Reading diagram from " + h5File);
198
int fileId = H5Read.openFile(h5File);
200
H5Read.readDataFromFile(fileId, data);
201
H5Read.closeFile(fileId);
202
element.decode(data, diagram);
203
} catch (ScicosFormatException e) {
204
if (e instanceof VersionMismatchException) {
206
* On version mismatch alert the user but the current instance
207
* contains the partially decoded data so continue.
209
diagram.error(XcosMessages.UNKNOW_VERSION + ((VersionMismatchException) e).getWrongVersion() + "\n" + XcosMessages.TRY_TO_CONTINUE);
212
throw new RuntimeException(e);
214
} catch (HDF5Exception e) {
215
throw new RuntimeException(e);
216
} catch (Throwable t) {
219
diagram.getModel().endUpdate();
222
if (LOG.isLoggable(Level.FINEST)) {
223
LOG.finest("End of reading diagram from " + h5File);
237
* org.scilab.modules.xcos.io.scicos.RWHandler#writeBlock(org.scilab.modules
238
* .xcos.block.BasicBlock)
242
public void writeBlock(BasicBlock block) {
243
final BlockElement element = new BlockElement(null);
244
final ScilabType data = element.encode(block, null);
246
if (LOG.isLoggable(Level.FINEST)) {
247
LOG.finest("Writing block to " + h5File);
251
int fileId = H5Write.createFile(h5File);
253
H5Write.writeInDataSet(fileId, SCS_M, data);
255
H5Write.closeFile(fileId);
256
} catch (HDF5Exception e) {
257
LOG.severe(e.toString());
258
} catch (java.lang.NullPointerException e) {
259
LOG.severe(e.toString());
260
LOG.warning(data.toString());
261
} catch (java.lang.IndexOutOfBoundsException e) {
262
LOG.severe(e.toString());
263
LOG.warning(data.toString());
266
if (LOG.isLoggable(Level.FINEST)) {
267
LOG.finest("End of writing block to " + h5File);
275
* org.scilab.modules.xcos.io.scicos.RWHandler#writeContext(java.lang.String
280
public void writeContext(String[] context) {
281
final ScilabString string = new ScilabString(context);
283
if (LOG.isLoggable(Level.FINEST)) {
284
LOG.finest("Writing context to " + h5File);
288
int fileId = H5Write.createFile(h5File);
290
H5Write.writeInDataSet(fileId, CONTEXT, string);
292
H5Write.closeFile(fileId);
293
} catch (HDF5Exception e) {
294
LOG.severe(e.toString());
297
if (LOG.isLoggable(Level.FINEST)) {
298
LOG.finest("End of writing context to " + h5File);
306
* org.scilab.modules.xcos.io.scicos.RWHandler#writeDiagram(org.scilab.modules
307
* .xcos.graph.XcosDiagram)
311
public void writeDiagram(XcosDiagram diagram) {
312
final DiagramElement element = new DiagramElement();
313
final ScilabType data = element.encode(diagram, null);
315
if (LOG.isLoggable(Level.FINEST)) {
316
LOG.finest("Writing diagram to " + h5File);
320
int fileId = H5Write.createFile(h5File);
322
H5Write.writeInDataSet(fileId, SCS_M, data);
324
H5Write.closeFile(fileId);
325
} catch (HDF5Exception e) {
326
// important error which need a backtrace.
327
LOG.severe(e.toString());
329
} catch (java.lang.NullPointerException e) {
330
LOG.severe(e.toString());
331
LOG.warning(data.toString());
332
} catch (java.lang.IndexOutOfBoundsException e) {
333
LOG.severe(e.toString());
334
LOG.warning(data.toString());
337
if (LOG.isLoggable(Level.FINEST)) {
338
LOG.finest("End of writing diagram to " + h5File);
342
// CSON: ClassDataAbstractionCoupling