source: trunk/src/org/expeditee/io/ExpReader.java@ 457

Last change on this file since 457 was 427, checked in by ra33, 15 years ago
File size: 7.4 KB
Line 
1package org.expeditee.io;
2
3import java.io.BufferedReader;
4import java.io.FileReader;
5import java.io.IOException;
6import java.lang.reflect.Method;
7import java.util.HashMap;
8
9import org.expeditee.gui.Frame;
10import org.expeditee.items.Constraint;
11import org.expeditee.items.Dot;
12import org.expeditee.items.Item;
13import org.expeditee.items.Line;
14import org.expeditee.items.Text;
15import org.expeditee.stats.SessionStats;
16
17/**
18 * Reads in Exp format files and constructs the Frame and Item objects they
19 * contain.
20 *
21 * @author mrww1
22 *
23 */
24public class ExpReader extends FrameReader {
25
26 public static final String EXTENTION = ".exp";
27
28 private BufferedReader _reader = null;
29
30 private String _frameName;
31
32 /**
33 * Does nothing, location must be set before use
34 */
35 public ExpReader(String frameName) {
36 super();
37
38 _frameName = frameName;
39 }
40
41 /**
42 * Determines whether a string begins with tag.
43 *
44 * @param s
45 * a line of text
46 * @return true if s begins with a tag
47 */
48 private static boolean isValidLine(String s) {
49 return s.length() >= 2 && s.charAt(1) == ' '
50 && Character.isLetter(s.charAt(0));
51 }
52
53 /**
54 * Reads a file with the given name from disk.
55 *
56 * @param frameName
57 * the name of the Frame to read in from a file.
58 * @return A new Frame object that contains any Items described in the file.
59 * @throws IOException
60 * Any exceptions occured by the BufferedReader.
61 */
62 public Frame readFrame(BufferedReader reader) throws IOException {
63 _reader = reader;
64 String next = "";
65 Frame newFrame = new Frame();
66
67 try {
68 // Framename must be set before setting the frame number
69 newFrame.setName(_frameName);
70
71 // First read all the header lines
72 while (_reader.ready() && !(next = _reader.readLine()).equals("Z")) {
73 if (isValidLine(next)) {
74 processHeaderLine(newFrame, next);
75 }
76 }
77
78 // Now read all the items
79 Item currentItem = null;
80 while (_reader.ready() && !(next = _reader.readLine()).equals("Z")) {
81 // if this is the start of a new item add a new item
82 if (isValidLine(next)) {
83 if (getTag(next) == 'S') {
84 String value = getValue(next);
85 int id = Integer.parseInt(value.substring(2));
86
87 switch (value.charAt(0)) {
88 case 'P': // check if its a point
89 currentItem = new Dot(id);
90 break;
91 default:
92 currentItem = new Text(id);
93 break;
94 }
95 _linePoints.put(currentItem.getID(), currentItem);
96 newFrame.addItem(currentItem);
97 } else if (currentItem != null) {
98 processBodyLine(currentItem, next);
99 }
100 }
101 }
102
103 // Read the lines
104 while (_reader.ready() && !(next = _reader.readLine()).equals("Z")) {
105 if (isValidLine(next)) {
106 java.awt.Point idtype = separateValues(next.substring(2));
107 // The next line must be the endpoints
108 if (!_reader.ready())
109 throw new Exception("Unexpected end of file");
110 next = _reader.readLine();
111 java.awt.Point startend = separateValues(next.substring(2));
112 int start = startend.x;
113 int end = startend.y;
114
115 if (_linePoints.get(start) != null
116 && _linePoints.get(end) != null) {
117 newFrame.addItem(new Line(_linePoints.get(start),
118 _linePoints.get(end), idtype.x));
119 } else {
120 System.out
121 .println("Error reading line with unknown end points");
122 }
123 }
124 }
125
126 // Read the constraints
127 while (_reader.ready() && !(next = _reader.readLine()).equals("Z")) {
128 if (isValidLine(next)) {
129 java.awt.Point idtype = separateValues(next.substring(2));
130 // The next line must be the endpoints
131 if (!_reader.ready())
132 throw new Exception("Unexpected end of file");
133 next = _reader.readLine();
134 java.awt.Point startend = separateValues(next.substring(2));
135
136 Item a = _linePoints.get(startend.x);
137 Item b = _linePoints.get(startend.y);
138
139 new Constraint(a, b, idtype.x, idtype.y);
140 }
141 }
142
143 // Read the stats
144 while (_reader.ready() && ((next = _reader.readLine()) != null)) {
145 if (next.startsWith(SessionStats.ACTIVE_TIME_ATTRIBUTE)) {
146 try {
147 String value = next.substring(SessionStats.ACTIVE_TIME_ATTRIBUTE.length()).trim();
148 newFrame.setActiveTime(value);
149 } catch (Exception e) {
150 }
151
152 } else if (next.startsWith(SessionStats.DARK_TIME_ATTRIBUTE)) {
153 try {
154 String value = next.substring(SessionStats.DARK_TIME_ATTRIBUTE.length()).trim();
155 newFrame.setDarkTime(value);
156 } catch (Exception e) {
157 }
158
159 }
160 }
161
162 } catch (Exception e) {
163 e.printStackTrace();
164 System.out.println("Error reading frame file line: " + next + " "
165 + e.getMessage());
166 }
167
168 //newFrame.refreshItemPermissions();
169 _reader.close();
170 newFrame.setChanged(false);
171
172 return newFrame;
173 }
174
175 // Stores points used when constructing lines
176 private HashMap<Integer, Item> _linePoints = new HashMap<Integer, Item>();
177
178 /**
179 * Processes the body section of the Exp file, which contains all Items
180 *
181 * @param frame
182 * The Frame to add any created Items to.
183 * @param line
184 * The line of text read in from the file to process.
185 */
186 private void processBodyLine(Item item, String line) {
187 // separate the tag from the value
188 Character tag = getTag(line);
189 String value = getValue(line);
190
191 Method toRun = _ItemTags.get(tag);
192 if (toRun == null)
193 System.out.println("Error accessing tag method: " + tag);
194 Object[] vals = Conversion.Convert(toRun, value);
195
196 try {
197 if (vals != null)
198 toRun.invoke(item, vals);
199 } catch (Exception e) {
200 System.out.println("Error running tag method: " + tag);
201 // e.printStackTrace();
202 }
203 }
204
205 private static Character getTag(String line) {
206 assert (line.length() > 0);
207 return line.charAt(0);
208 }
209
210 private static String getValue(String line) {
211 if (line.length() > 2)
212 return line.substring(2);
213 else
214 return "";
215 }
216
217 /**
218 * Reads the header section of the file, which contains information about
219 * the Frame.
220 *
221 * @param frame
222 * The Frame to assign the read values to.
223 * @param line
224 * The line from the file to process.
225 * @return False if the end of the header has been reached, True otherwise.
226 */
227 private void processHeaderLine(Frame frame, String line) throws IOException {
228 // first separate the tag from the text
229 Character tag = getTag(line);
230 String value = getValue(line);
231 Method toRun = _FrameTags.get(tag);
232
233 if (toRun == null) {
234 if (tag != 'v') {
235 System.out.println("Tag '" + tag + "' in '" + line
236 + "' is not supported.");
237 }
238 return;
239 }
240
241 Object[] vals = Conversion.Convert(toRun, value);
242 try {
243 toRun.invoke(frame, vals);
244 } catch (Exception e) {
245 System.out.println("Error running method: "
246 + toRun.toGenericString());
247 e.printStackTrace();
248 }
249 }
250
251 // Returns a point from a String containing two ints separated by a space
252 private java.awt.Point separateValues(String line) {
253 int x = Integer.parseInt(line.substring(0, line.indexOf(" ")));
254 int y = Integer.parseInt(line.substring(line.indexOf(" ") + 1));
255
256 return new java.awt.Point(x, y);
257 }
258
259 public static int getVersion(String fullpath) {
260 try {
261 BufferedReader reader = new BufferedReader(new FileReader(fullpath));
262 String next = "";
263 // First read the header lines until we get the version number
264 while (reader.ready() && !(next = reader.readLine()).equals("Z")) {
265 if (isValidLine(next)) {
266 Character tag = getTag(next);
267 String value = getValue(next);
268 if (tag.equals('V'))
269 return Integer.parseInt(value);
270 }
271 }
272 } catch (Exception e) {
273 }
274 return -1;
275 }
276}
Note: See TracBrowser for help on using the repository browser.