~ubuntu-branches/ubuntu/raring/scilab/raring-proposed

« back to all changes in this revision

Viewing changes to modules/xcos/src/java/org/scilab/modules/xcos/io/scicos/H5RWHandler.java

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2012-08-30 14:42:38 UTC
  • mfrom: (1.4.7)
  • Revision ID: package-import@ubuntu.com-20120830144238-c1y2og7dbm7m9nig
Tags: 5.4.0-beta-3-1~exp1
* New upstream release
* Update the scirenderer dep
* Get ride of libjhdf5-java dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3
 
 * Copyright (C) 2010 - DIGITEO - Clement DAVID
4
 
 *
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
10
 
 *
11
 
 */
12
 
 
13
 
package org.scilab.modules.xcos.io.scicos;
14
 
 
15
 
import java.io.File;
16
 
import java.util.LinkedHashMap;
17
 
import java.util.Map;
18
 
import java.util.logging.Level;
19
 
import java.util.logging.Logger;
20
 
 
21
 
import ncsa.hdf.hdf5lib.exceptions.HDF5Exception;
22
 
 
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;
34
 
 
35
 
/**
36
 
 * Implement useful methods to easily import or export Scicos data.
37
 
 */
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());
43
 
 
44
 
    private final String h5File;
45
 
 
46
 
    /**
47
 
     * Constructor a new instance with a file.
48
 
     *
49
 
     * @param hdf5file
50
 
     *            the file path.
51
 
     */
52
 
    public H5RWHandler(File hdf5file) {
53
 
        h5File = hdf5file.getAbsolutePath();
54
 
    }
55
 
 
56
 
    /**
57
 
     * Construct a new instance with the file path.
58
 
     *
59
 
     * @param hdf5file
60
 
     *            the file path.
61
 
     */
62
 
    public H5RWHandler(String hdf5file) {
63
 
        h5File = hdf5file;
64
 
    }
65
 
 
66
 
    /*
67
 
     * Read methods
68
 
     */
69
 
 
70
 
    /*
71
 
     * (non-Javadoc)
72
 
     *
73
 
     * @see org.scilab.modules.xcos.io.scicos.RWHandler#readBlock()
74
 
     */
75
 
    @Override
76
 
    public BasicBlock readBlock() throws ScicosFormatException {
77
 
        return readBlock(null);
78
 
    }
79
 
 
80
 
    /*
81
 
     * (non-Javadoc)
82
 
     *
83
 
     * @see
84
 
     * org.scilab.modules.xcos.io.scicos.RWHandler#readBlock(org.scilab.modules
85
 
     * .xcos.block.BasicBlock)
86
 
     */
87
 
    @Override
88
 
    public BasicBlock readBlock(BasicBlock into) throws ScicosFormatException {
89
 
        final ScilabMList data = new ScilabMList();
90
 
        final BlockElement element = new BlockElement(null);
91
 
        BasicBlock instance;
92
 
 
93
 
        if (LOG.isLoggable(Level.FINEST)) {
94
 
            LOG.finest("Reading block from " + h5File);
95
 
        }
96
 
 
97
 
        try {
98
 
            int fileId = H5Read.openFile(h5File);
99
 
            H5Read.readDataFromFile(fileId, data);
100
 
            H5Read.closeFile(fileId);
101
 
 
102
 
            instance = element.decode(data, into);
103
 
            StyleMap style = new StyleMap(instance.getInterfaceFunctionName());
104
 
            style.putAll(instance.getStyle());
105
 
 
106
 
            instance.setStyle(style.toString());
107
 
 
108
 
        } catch (HDF5Exception e) {
109
 
            LOG.severe(e.toString());
110
 
            instance = null;
111
 
        }
112
 
 
113
 
        if (LOG.isLoggable(Level.FINEST)) {
114
 
            LOG.finest("End of reading block from " + h5File);
115
 
        }
116
 
 
117
 
        return instance;
118
 
    }
119
 
 
120
 
    /*
121
 
     * (non-Javadoc)
122
 
     *
123
 
     * @see org.scilab.modules.xcos.io.scicos.RWHandler#readContext()
124
 
     */
125
 
    @Override
126
 
    public Map<String, String> readContext() {
127
 
        final ScilabList list = new ScilabList();
128
 
        final Map<String, String> result = new LinkedHashMap<String, String>();
129
 
 
130
 
        if (LOG.isLoggable(Level.FINEST)) {
131
 
            LOG.finest("Reading context from " + h5File);
132
 
        }
133
 
 
134
 
        try {
135
 
            int handle = H5Read.openFile(h5File);
136
 
            if (handle >= 0) {
137
 
                H5Read.readDataFromFile(handle, list);
138
 
            }
139
 
        } catch (HDF5Exception e) {
140
 
            LOG.severe(e.toString());
141
 
            return result;
142
 
        }
143
 
 
144
 
        // We are starting at 2 because a struct is composed of
145
 
        // - the fields names (ScilabString)
146
 
        // - the dimension
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();
151
 
 
152
 
            result.put(key, value);
153
 
        }
154
 
 
155
 
        if (LOG.isLoggable(Level.FINEST)) {
156
 
            LOG.finest("End of reading context from " + h5File);
157
 
        }
158
 
 
159
 
        return result;
160
 
    }
161
 
 
162
 
    /*
163
 
     * (non-Javadoc)
164
 
     *
165
 
     * @see org.scilab.modules.xcos.io.scicos.RWHandler#readDiagram()
166
 
     */
167
 
    @Override
168
 
    public XcosDiagram readDiagram() throws VersionMismatchException {
169
 
        return readDiagram(null);
170
 
    }
171
 
 
172
 
    /*
173
 
     * (non-Javadoc)
174
 
     *
175
 
     * @see
176
 
     * org.scilab.modules.xcos.io.scicos.RWHandler#readDiagram(org.scilab.modules
177
 
     * .xcos.graph.XcosDiagram)
178
 
     */
179
 
    @Override
180
 
    public XcosDiagram readDiagram(XcosDiagram instance) {
181
 
        final ScilabMList data = new ScilabMList();
182
 
        final DiagramElement element = new DiagramElement();
183
 
 
184
 
        XcosDiagram diagram;
185
 
        if (instance == null) {
186
 
            diagram = new XcosDiagram();
187
 
        } else {
188
 
            diagram = instance;
189
 
        }
190
 
 
191
 
        diagram.getModel().beginUpdate();
192
 
 
193
 
        if (LOG.isLoggable(Level.FINEST)) {
194
 
            LOG.finest("Reading diagram from " + h5File);
195
 
        }
196
 
 
197
 
        try {
198
 
            int fileId = H5Read.openFile(h5File);
199
 
 
200
 
            H5Read.readDataFromFile(fileId, data);
201
 
            H5Read.closeFile(fileId);
202
 
            element.decode(data, diagram);
203
 
        } catch (ScicosFormatException e) {
204
 
            if (e instanceof VersionMismatchException) {
205
 
                /*
206
 
                 * On version mismatch alert the user but the current instance
207
 
                 * contains the partially decoded data so continue.
208
 
                 */
209
 
                diagram.error(XcosMessages.UNKNOW_VERSION + ((VersionMismatchException) e).getWrongVersion() + "\n" + XcosMessages.TRY_TO_CONTINUE);
210
 
            } else {
211
 
                // rethrow
212
 
                throw new RuntimeException(e);
213
 
            }
214
 
        } catch (HDF5Exception e) {
215
 
            throw new RuntimeException(e);
216
 
        } catch (Throwable t) {
217
 
            t.printStackTrace();
218
 
        } finally {
219
 
            diagram.getModel().endUpdate();
220
 
        }
221
 
 
222
 
        if (LOG.isLoggable(Level.FINEST)) {
223
 
            LOG.finest("End of reading diagram from " + h5File);
224
 
        }
225
 
 
226
 
        return diagram;
227
 
    }
228
 
 
229
 
    /*
230
 
     * Write methods
231
 
     */
232
 
 
233
 
    /*
234
 
     * (non-Javadoc)
235
 
     *
236
 
     * @see
237
 
     * org.scilab.modules.xcos.io.scicos.RWHandler#writeBlock(org.scilab.modules
238
 
     * .xcos.block.BasicBlock)
239
 
     */
240
 
    @Override
241
 
    @Deprecated
242
 
    public void writeBlock(BasicBlock block) {
243
 
        final BlockElement element = new BlockElement(null);
244
 
        final ScilabType data = element.encode(block, null);
245
 
 
246
 
        if (LOG.isLoggable(Level.FINEST)) {
247
 
            LOG.finest("Writing block to " + h5File);
248
 
        }
249
 
 
250
 
        try {
251
 
            int fileId = H5Write.createFile(h5File);
252
 
 
253
 
            H5Write.writeInDataSet(fileId, SCS_M, data);
254
 
 
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());
264
 
        }
265
 
 
266
 
        if (LOG.isLoggable(Level.FINEST)) {
267
 
            LOG.finest("End of writing block to " + h5File);
268
 
        }
269
 
    }
270
 
 
271
 
    /*
272
 
     * (non-Javadoc)
273
 
     *
274
 
     * @see
275
 
     * org.scilab.modules.xcos.io.scicos.RWHandler#writeContext(java.lang.String
276
 
     * [])
277
 
     */
278
 
    @Override
279
 
    @Deprecated
280
 
    public void writeContext(String[] context) {
281
 
        final ScilabString string = new ScilabString(context);
282
 
 
283
 
        if (LOG.isLoggable(Level.FINEST)) {
284
 
            LOG.finest("Writing context to " + h5File);
285
 
        }
286
 
 
287
 
        try {
288
 
            int fileId = H5Write.createFile(h5File);
289
 
 
290
 
            H5Write.writeInDataSet(fileId, CONTEXT, string);
291
 
 
292
 
            H5Write.closeFile(fileId);
293
 
        } catch (HDF5Exception e) {
294
 
            LOG.severe(e.toString());
295
 
        }
296
 
 
297
 
        if (LOG.isLoggable(Level.FINEST)) {
298
 
            LOG.finest("End of writing context to " + h5File);
299
 
        }
300
 
    }
301
 
 
302
 
    /*
303
 
     * (non-Javadoc)
304
 
     *
305
 
     * @see
306
 
     * org.scilab.modules.xcos.io.scicos.RWHandler#writeDiagram(org.scilab.modules
307
 
     * .xcos.graph.XcosDiagram)
308
 
     */
309
 
    @Override
310
 
    @Deprecated
311
 
    public void writeDiagram(XcosDiagram diagram) {
312
 
        final DiagramElement element = new DiagramElement();
313
 
        final ScilabType data = element.encode(diagram, null);
314
 
 
315
 
        if (LOG.isLoggable(Level.FINEST)) {
316
 
            LOG.finest("Writing diagram to " + h5File);
317
 
        }
318
 
 
319
 
        try {
320
 
            int fileId = H5Write.createFile(h5File);
321
 
 
322
 
            H5Write.writeInDataSet(fileId, SCS_M, data);
323
 
 
324
 
            H5Write.closeFile(fileId);
325
 
        } catch (HDF5Exception e) {
326
 
            // important error which need a backtrace.
327
 
            LOG.severe(e.toString());
328
 
            e.printStackTrace();
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());
335
 
        }
336
 
 
337
 
        if (LOG.isLoggable(Level.FINEST)) {
338
 
            LOG.finest("End of writing diagram to " + h5File);
339
 
        }
340
 
    }
341
 
}
342
 
// CSON: ClassDataAbstractionCoupling