View Javadoc
1   /*
2    * Copyright 2008, 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  
20  import eu.simuline.octave.exception.OctaveParseException;
21  import eu.simuline.octave.io.OctaveIO;
22  import eu.simuline.octave.io.spi.OctaveDataReader;
23  import eu.simuline.octave.type.OctaveCell;
24  import eu.simuline.octave.type.OctaveObject;
25  
26  //import static eu.simuline.octave.io.impl.CellWriter.TYPE_CELL;
27  import static eu.simuline.octave.io.impl.CellWriter.NROWS;
28  import static eu.simuline.octave.io.impl.CellWriter.NCOLUMNS;
29  
30  /**
31   * The reader for the octave type "cell" 
32   * reading in an {@link OctaveCell} from a {@link BufferedReader}. 
33   */
34  public final class CellReader extends OctaveDataReader {
35  
36      @Override
37      public String octaveType() {
38          return "cell";
39      }
40  
41      @Override
42      public OctaveCell read(final BufferedReader reader) {
43          String line = OctaveIO.readerReadLine(reader);
44          String token = NROWS;
45  
46          if (!line.startsWith(token)) {
47              throw new OctaveParseException
48  		("Expected <" + token + ">, but got <" + line + ">");
49          }
50          final int nrows = Integer.parseInt(line.substring(token.length()));
51  
52          line = OctaveIO.readerReadLine(reader);
53          token = NCOLUMNS;
54          if (!line.startsWith(token)) {
55              throw new OctaveParseException
56  		("Expected <" + token + ">, but got <" + line + ">");
57          }
58          final int ncols = Integer.parseInt(line.substring(token.length()));
59  
60          final OctaveCelll.html#OctaveCell">OctaveCell octaveCell = new OctaveCell(nrows, ncols);
61  
62          for (int col = 1; col <= ncols; col++) {
63              for (int row = 1; row <= nrows; row++) {
64  		// Work around differences in number of line feeds 
65  		// in octave 3.4 and 3.6: 
66  		// keep reading until line is non-empty
67  		do {
68                      line = OctaveIO.readerReadLine(reader);
69                  } while ("".equals(line));
70  		token = "# name: <cell-element>";
71  		if (!line.equals(token)) {
72                      throw new OctaveParseException
73  			("Expected <" + token + ">, but got <" + line + ">");
74                  }
75                  final OctaveObject octaveType = OctaveIO.read(reader);
76                  octaveCell.set(octaveType, row, col);
77              }
78              line = OctaveIO.readerReadLine(reader);
79              token = "";
80              if (line == null || !line.equals(token)) {
81                  throw new OctaveParseException
82  		    ("Expected <" + token + ">, but got <" + line + ">");
83              }
84          }
85  
86          return octaveCell;
87      }
88  
89  }