View Javadoc
1   /*
2    * Copyright 2012 Ange Optimization ApS
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package eu.simuline.octave.io.impl;
17  
18  import java.io.BufferedReader;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import eu.simuline.octave.exception.OctaveParseException;
23  import eu.simuline.octave.io.OctaveIO;
24  import eu.simuline.octave.io.spi.OctaveDataReader;
25  import eu.simuline.octave.type.OctaveObject;
26  import eu.simuline.octave.type.OctaveStruct;
27  
28  /**
29   * The reader for the octave type "scalar struct" 
30   * (which is an encoding similar to "struct" 
31   * introduced in octave 3.6, optimized to the 1x1 struct) 
32   * reading an {@link OctaveObject} from a {@link BufferedReader}.
33   */
34  public final class ScalarStructReader extends OctaveDataReader {
35      private static final String NAME = "# name: ";
36      private static final String LENGTH = "# length: ";
37      private static final String N_DIMS2 = "# ndims: 2";
38      private static final String V_DIMS2 = " 1 1";
39  
40      @Override
41      public String octaveType() {
42          return "scalar struct";
43      }
44  
45      @Override
46      public OctaveStruct read(final BufferedReader reader) {
47  	// **** this i cannot see in Writer 
48          // # ndims: 2
49          String line = OctaveIO.readerReadLine(reader);
50          if (!N_DIMS2.equals(line)) {
51              throw new OctaveParseException
52  		("JavaOctave does not support matrix structs, read=" + line);
53          }
54  
55          // 1 1
56          line = OctaveIO.readerReadLine(reader);
57          if (!V_DIMS2.equals(line)) {
58              throw new OctaveParseException
59  		("JavaOctave does not support matrix structs, read=" + line);
60          }
61  
62          // # length: 4
63          line = OctaveIO.readerReadLine(reader);
64          if (line == null || !line.startsWith(LENGTH)) {
65              throw new OctaveParseException
66  		("Expected <" + LENGTH + "> got <" + line + ">. ");
67         }
68          final int length = Integer.parseInt(line.substring(LENGTH.length()));
69  	// only used during conversion
70  
71          final Map<String, OctaveObject> data =
72  	    new HashMap<String, OctaveObject>();
73  
74          for (int i = 0; i < length; i++) {
75              // # name: elemmatrix
76  	    // Work around differences in number of line feeds 
77  	    // in octave 3.4 and 3.6
78  	    // keep reading until line is non-empty
79              do {
80                  line = OctaveIO.readerReadLine(reader);
81              } while ("".equals(line));
82              if (!line.startsWith(NAME)) {
83                  throw new OctaveParseException
84  		    ("Expected <" + NAME + "> got <" + line + ">. ");
85              }
86              final String subname = line.substring(NAME.length());
87  
88  
89  
90  
91              // data...
92              final OctaveObject value = OctaveIO.read(reader);
93              data.put(subname, value);
94          } // for
95  
96          return new OctaveStruct(data);
97      }
98  
99  }