1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package eu.simuline.octave.io;
17
18 import java.io.IOException;
19 import java.io.Writer;
20 import java.util.Map;
21
22 import eu.simuline.octave.exception.OctaveIOException;
23 import eu.simuline.octave.exec.WriteFunctor;
24 import eu.simuline.octave.type.OctaveObject;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29
30
31
32
33
34
35 final class DataWriteFunctor implements WriteFunctor {
36
37 private static final String MSG_IOE_UNEXP = "Unexpected IOException";
38
39 private static final Log LOG = LogFactory.getLog(DataWriteFunctor.class);
40
41
42
43
44 private final Map<String, OctaveObject> varName2Value;
45
46
47
48
49
50 DataWriteFunctor(final Map<String, OctaveObject> varName2Value) {
51 this.varName2Value = varName2Value;
52 }
53
54
55
56
57
58
59
60
61 public void doWrites(final Writer writer) {
62 try {
63
64 LOG.trace("write: 'load(\"-text\", \"-\")' " +
65 "to start read data from input mode");
66 writer.write("load(\"-text\", \"-\")\n");
67
68 for (final Map.Entry<String, OctaveObject> entry
69 : this.varName2Value.entrySet()) {
70
71 final String name = entry.getKey();
72 final OctaveObject value = entry.getValue();
73 if (LOG.isTraceEnabled()) {
74 LOG.trace("write: variable '" + name +
75 "', value=<<<" + value + ">>>");
76 }
77 OctaveIO.write(writer, name, value);
78 }
79
80 LOG.trace("write: '# name:' to exit octave from read data mode");
81 writer.write("# name: \n");
82 writer.flush();
83 } catch (final IOException e) {
84
85 LOG.debug(MSG_IOE_UNEXP, e);
86 throw new OctaveIOException(MSG_IOE_UNEXP, e);
87 }
88 }
89
90 }