| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package eu.simuline.octave.io; |
| 17 | |
|
| 18 | |
import static eu.simuline.octave.io.OctaveIO.readerReadLine; |
| 19 | |
|
| 20 | |
import java.io.BufferedReader; |
| 21 | |
import java.io.Reader; |
| 22 | |
import java.util.Map; |
| 23 | |
|
| 24 | |
import eu.simuline.octave.exception.OctaveParseException; |
| 25 | |
import eu.simuline.octave.exec.ReadFunctor; |
| 26 | |
import eu.simuline.octave.type.OctaveObject; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
final class DataReadFunctor implements ReadFunctor { |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
private final String name; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
private OctaveObject data; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | 180 | DataReadFunctor(final String name) { |
| 51 | 180 | this.name = name; |
| 52 | 180 | } |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
@Override |
| 58 | |
public void doReads(final Reader reader) { |
| 59 | 180 | final BufferedReader bufferedReader = new BufferedReader(reader); |
| 60 | 180 | final String createByOctaveLine = readerReadLine(bufferedReader); |
| 61 | 180 | if (createByOctaveLine == null || |
| 62 | 180 | !createByOctaveLine.startsWith("# Created by Octave")) { |
| 63 | 0 | throw new OctaveParseException |
| 64 | |
("Not created by Octave?: '" + createByOctaveLine + "'"); |
| 65 | |
} |
| 66 | 180 | final Map<String, OctaveObject> map = |
| 67 | 180 | OctaveIO.readWithName(bufferedReader); |
| 68 | 176 | if (map.size() != 1) { |
| 69 | 0 | throw new OctaveParseException |
| 70 | |
("Expected map of size 1 but got " + map + "'"); |
| 71 | |
} |
| 72 | 176 | if (!map.containsKey(this.name)) { |
| 73 | 0 | throw new OctaveParseException |
| 74 | |
("Expected variable named '" + this.name + |
| 75 | |
"' but got '" + map + "'"); |
| 76 | |
} |
| 77 | 176 | this.data = map.get(this.name); |
| 78 | 176 | } |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
public OctaveObject getData() { |
| 84 | 176 | return this.data; |
| 85 | |
} |
| 86 | |
|
| 87 | |
} |