View Javadoc
1   /*
2    * Copyright 2008, 2009, 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.OctaveCell;
26  import eu.simuline.octave.type.OctaveObject;
27  import eu.simuline.octave.type.OctaveStruct;
28  
29  import static eu.simuline.octave.io.impl.StructWriter.LENGTH;
30  import static eu.simuline.octave.io.impl.StructWriter.NAME;
31  import static eu.simuline.octave.io.impl.StructWriter.TYPE_CELL;
32  
33  /**
34   * The reader for the octave type "struct" 
35   * reading an {@link OctaveStruct} from a {@link BufferedReader}. 
36   * Reads the format written by {@link StructWriter}
37   */
38  public final class StructReader extends OctaveDataReader {
39      private static final String N_DIMS2 = "# ndims: 2";
40      private static final String V_DIMS2 = " 1 1";
41  
42      private static final CellReaderder.html#CellReader">CellReader CELL_READER = new CellReader();
43  
44      @Override
45      public String octaveType() {
46          return "struct";
47      }
48  
49      @Override
50      @SuppressWarnings("PMD.NPathComplexity")
51      public OctaveStruct read(final BufferedReader reader) {
52          String line = OctaveIO.readerReadLine(reader);
53          // In octave 3.6 dimension of the scalar is also written now 
54  	// **** this i cannot see in Writer 
55          if (line != null && line.startsWith("# ndims:")) {
56              if (!N_DIMS2.equals(line)) {
57                  throw new OctaveParseException
58  		    ("JavaOctave does not support matrix structs, read '" + 
59  		     line + "'");
60              }
61  
62  	    // 1 1
63              line = OctaveIO.readerReadLine(reader);
64              if (!V_DIMS2.equals(line)) {
65                  throw new OctaveParseException
66  		    ("JavaOctave does not support matrix structs, read '" + 
67  		     line + "'");
68              }
69              line = OctaveIO.readerReadLine(reader);
70          }
71  
72          // # length: 4
73  //        String line = OctaveIO.readerReadLine(reader);
74          if (line == null || !line.startsWith(LENGTH)) {
75              throw new OctaveParseException
76  		("Expected <" + LENGTH + "> got <" + line + ">. ");
77          }
78          final int length = Integer.parseInt(line.substring(LENGTH.length()));
79  	// only used during conversion
80  
81          final Map<String, OctaveObject> data = 
82  	    new HashMap<String, OctaveObject>();
83  
84          for (int i = 0; i < length; i++) {
85              // # name: elemmatrix
86  	    // Work around differences in number of line feeds 
87  	    // in octave 3.4 and 3.6: 
88  	    // keep reading until line is non-empty
89              do {
90                  line = OctaveIO.readerReadLine(reader);
91              } while ("".equals(line));
92              if (!line.startsWith(NAME)) {
93                  throw new OctaveParseException
94  		    ("Expected <" + NAME + "> got <" + line + ">. ");
95              }
96              final String subname = line.substring(NAME.length());
97  
98  
99  
100 
101             line = OctaveIO.readerReadLine(reader);
102             if (!line.equals(TYPE_CELL)) {
103                 throw new OctaveParseException
104 		    ("Expected '" + TYPE_CELL + "' got '" + line + "'");
105             }
106 
107             final OctaveCell cell = CELL_READER.read(reader);
108             if (cell.getSize(1) != 1 || cell.getSize(2) != 1) {
109                 throw new OctaveParseException
110 		    ("JavaOctave does not support matrix structs, size="
111 		     + cell.getSize(1) + " " + cell.getSize(2) + "...");
112 	    }
113 
114             // data...
115 	    final OctaveObject value = cell.get(1, 1);
116 	    data.put(subname, value);
117         } // for 
118 
119         return new OctaveStruct(data);
120     }
121 
122 }