source: trunk/src/org/expeditee/io/ExaReader.java@ 919

Last change on this file since 919 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

File size: 8.8 KB
Line 
1/**
2 * ExaReader.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.io;
20
21import java.io.BufferedReader;
22import java.io.FileInputStream;
23import java.io.FileReader;
24import java.io.IOException;
25import java.io.InputStreamReader;
26import java.io.Reader;
27import java.util.HashMap;
28
29import org.expeditee.gui.AttributeUtils;
30import org.expeditee.gui.AttributeUtils.Attribute;
31import org.expeditee.gui.Frame;
32import org.expeditee.items.Constraint;
33import org.expeditee.items.Item;
34import org.expeditee.items.Line;
35
36/**
37 * Experimental format which is designed to be more readable
38 * NON FUNCTIONAL DUE TO CHANGES TO AttributeUtils
39 *
40 * @author jts21
41 *
42 */
43public class ExaReader implements FrameReader {
44
45 public static final String EXTENTION = ".exa";
46
47 private String _frameName;
48 private HashMap<Integer, Item> _linePoints = new HashMap<Integer, Item>();
49 private Frame _frame;
50 private BufferedReader _reader;
51
52 public ExaReader(String frameName) {
53 _frameName = frameName;
54 }
55
56 public Frame readFrame(String fullPath) throws IOException {
57 Reader in = new InputStreamReader(new FileInputStream(fullPath), "UTF-8");
58 return readFrame(new BufferedReader(in));
59 }
60
61 /**
62 * Reads a file with the given name from disk.
63 *
64 * @param frameName
65 * the name of the Frame to read in from a file.
66 * @return A new Frame object that contains any Items described in the file.
67 * @throws IOException
68 * Any exceptions occured by the BufferedReader.
69 */
70 public Frame readFrame(BufferedReader reader) throws IOException {
71
72 AttributeUtils.ensureReady();
73
74 _reader = reader;
75
76 _frame = new Frame();
77 // Framename must be set before setting the frame number
78 _frame.setName(_frameName);
79
80 // Read frame properties
81 processHeader();
82 // Read items
83 processItems();
84 // Read lines
85 processLines();
86 // Read constraints
87 processConstraints();
88
89 _reader.close();
90 _frame.setChanged(false);
91
92 return _frame;
93 }
94
95 /**
96 * Splits a line at the first space
97 *
98 */
99 private static String[] getAttributeValue(String line) {
100 int firstSpace = line.indexOf(' ');
101 // attribute/value are separated by a space, so there must always be a space
102 if(firstSpace <= 0) {
103 System.err.println("Invalid line '" + line + "'");
104 return null;
105 }
106 String attribute = line.substring(0, firstSpace).trim();
107 String value = line.substring(firstSpace).trim();
108
109 // System.out.println(attribute + " : " + value);
110
111 return new String[] { attribute, value };
112 }
113
114 /**
115 * Load the frame properties based on the list in AttributeUtils
116 *
117 */
118 private void processHeader() throws IOException {
119
120 String line = "";
121
122 // sections are separated by an empty newline
123 while(_reader.ready() && (line = _reader.readLine()) != null && !line.equals("")) {
124
125 String[] av = getAttributeValue(line);
126 if(av == null) {
127 continue;
128 }
129 // TODO: NON FUNCTIONAL DUE TO CHANGES TO AttributeUtils
130// Attribute attribute = AttributeUtils._FrameAttrib.get(av[0].toLowerCase());
131// if(attribute == null || attribute.saveSetter == null) {
132// System.err.println("Attribute '" + attribute + "' is not supported");
133// continue;
134// }
135// Object[] vals = Conversion.Convert(attribute.saveSetter, av[1]);
136// try {
137// attribute.saveSetter.invoke(_frame, vals);
138// } catch(Exception e) {
139// System.err.println("Error running method: " + attribute.saveSetter.getName());
140// e.printStackTrace();
141// }
142
143 }
144 }
145
146 /**
147 * Load the items and their properties based on the list in AttributeUtils
148 *
149 */
150 private void processItems() throws IOException {
151
152 String line = "";
153 Item item = null;
154
155 // sections are separated by an empty newline
156 while(_reader.ready() && (line = _reader.readLine()) != null && !line.equals("")) {
157
158 String[] av = getAttributeValue(line);
159 if(av == null) {
160 continue;
161 }
162 if(av[0].equalsIgnoreCase("Item")) {
163 // System.out.println("Found next item");
164 // System.out.println(item);
165 String[] type_id = getAttributeValue(av[1]);
166 if(type_id == null) {
167 throw new IOException("Invalid id at line '" + line +"'");
168 }
169 // if we fail to load an item, then item will be set to null,
170 // and it's properties will be ignored
171 // (i.e. we just skip to the next item)
172 try {
173 // refresh the last item loaded
174 if(item != null) {
175 item.updatePolygon();
176 item.invalidateAll();
177 }
178 item = (Item) Class.forName(type_id[0]).getConstructor(int.class).newInstance(Integer.parseInt(type_id[1]));
179 _linePoints.put(item.getID(), item);
180 _frame.addItem(item);
181 } catch(ReflectiveOperationException e) {
182 item = null;
183 }
184 } else if(item != null) {
185 // TODO: NON FUNCTIONAL DUE TO CHANGES TO AttributeUtils
186// Attribute attribute = AttributeUtils._Attrib.get(av[0].toLowerCase());
187// if(attribute == null || attribute.saveSetter == null) {
188// System.err.println("Attribute '" + av[0] + "' is not supported");
189// continue;
190// }
191// Object[] vals = Conversion.Convert(attribute.saveSetter, av[1]);
192// try {
193// attribute.saveSetter.invoke(item, vals);
194// } catch(Exception e) {
195// System.err.println("Error running method: " + attribute.saveSetter.getName());
196// e.printStackTrace();
197// }
198 }
199 }
200 // refresh the last item loaded
201 if(item != null) {
202 item.updatePolygon();
203 item.invalidateAll();
204 }
205 }
206
207 /**
208 * Load the lines
209 * This one's pretty hardcoded due to the Lines requiring their endpoints in the constructor
210 * Could possibly change that in the future
211 *
212 */
213 private void processLines() throws IOException {
214
215 String line = "";
216 int startID = -1;
217 int endID = -1;
218 int ID = -1;
219
220 while(_reader.ready() && (line = _reader.readLine()) != null && !line.equals("")) {
221
222 String[] av = getAttributeValue(line);
223 if(av == null) {
224 continue;
225 }
226 if(av[0].equalsIgnoreCase("Line")) {
227 if(ID > 0 && startID > 0 && endID > 0) {
228 _frame.addItem(new Line(_linePoints.get(startID), _linePoints.get(endID), ID));
229 }
230 ID = Integer.parseInt(av[1]);
231 startID = -1;
232 endID = -1;
233 } else if(av[0].equalsIgnoreCase("Start")) {
234 startID = Integer.parseInt(av[1]);
235 } else if(av[0].equalsIgnoreCase("End")) {
236 endID = Integer.parseInt(av[1]);
237 }
238 }
239 if(ID > 0 && startID > 0 && endID > 0) {
240 _frame.addItem(new Line(_linePoints.get(startID), _linePoints.get(endID), ID));
241 }
242 }
243
244 /**
245 * Load the constraints
246 * As with the lines, this is pretty hardcoded
247 */
248 private void processConstraints() throws IOException {
249
250 String line = "";
251 int type = -1;
252 int startID = -1;
253 int endID = -1;
254 int ID = -1;
255
256 while(_reader.ready() && (line = _reader.readLine()) != null && !line.equals("")) {
257
258 String[] av = getAttributeValue(line);
259 if(av == null) {
260 continue;
261 }
262 if(av[0].equalsIgnoreCase("Constraint")) {
263
264 if(type > 0 && ID > 0 && startID > 0 && endID > 0) {
265 new Constraint(_linePoints.get(startID), _linePoints.get(endID), ID, type);
266 }
267
268 String[] type_id = getAttributeValue(av[1]);
269 if(type_id == null) {
270 throw new IOException("Invalid id at line '" + line +"'");
271 }
272 type = Integer.parseInt(type_id[0]);
273 ID = Integer.parseInt(type_id[1]);
274 startID = -1;
275 endID = -1;
276 } else if(av[0].equalsIgnoreCase("Start")) {
277 startID = Integer.parseInt(av[1]);
278 } else if(av[0].equalsIgnoreCase("End")) {
279 endID = Integer.parseInt(av[1]);
280 }
281 }
282 if(type > 0 && ID > 0 && startID > 0 && endID > 0) {
283 new Constraint(_linePoints.get(startID), _linePoints.get(endID), ID, type);
284 }
285 }
286
287 public static int getVersion(String fullpath) {
288 BufferedReader reader = null;
289 try {
290 reader = new BufferedReader(new FileReader(fullpath));
291 String line = "";
292 // First read the header lines until we get the version number
293 while(reader.ready() && (line = reader.readLine()) != null && !line.equals("")) {
294
295 String[] av = getAttributeValue(line);
296 if(av == null) {
297 continue;
298 }
299 if(av[0].equalsIgnoreCase("Version")) {
300 reader.close();
301 return Integer.parseInt(av[1]);
302 }
303 }
304 } catch (Exception e) {
305 }
306 if(reader != null) {
307 try {
308 reader.close();
309 } catch (IOException e) {
310 }
311 }
312 return -1;
313 }
314
315}
Note: See TracBrowser for help on using the repository browser.