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

Last change on this file since 309 was 298, checked in by ra33, 16 years ago

Adding networking stuff for peer to peer sharing of frames

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 System.out.println("Error reading frame file line: " + next + " "
164 + e.getMessage());
165 }
166
167 //newFrame.refreshItemPermissions();
168 _reader.close();
169 newFrame.setChanged(false);
170
171 return newFrame;
172 }
173
174 // Stores points used when constructing lines
175 private HashMap<Integer, Item> _linePoints = new HashMap<Integer, Item>();
176
177 /**
178 * Processes the body section of the Exp file, which contains all Items
179 *
180 * @param frame
181 * The Frame to add any created Items to.
182 * @param line
183 * The line of text read in from the file to process.
184 */
185 private void processBodyLine(Item item, String line) {
186 // separate the tag from the value
187 Character tag = getTag(line);
188 String value = getValue(line);
189
190 Method toRun = _ItemTags.get(tag);
191 if (toRun == null)
192 System.out.println("Error accessing tag method: " + tag);
193 Object[] vals = Conversion.Convert(toRun, value);
194
195 try {
196 if (vals != null)
197 toRun.invoke(item, vals);
198 } catch (Exception e) {
199 System.out.println("Error running tag method: " + tag);
200 // e.printStackTrace();
201 }
202 }
203
204 private static Character getTag(String line) {
205 assert (line.length() > 0);
206 return line.charAt(0);
207 }
208
209 private static String getValue(String line) {
210 if (line.length() > 2)
211 return line.substring(2);
212 else
213 return "";
214 }
215
216 /**
217 * Reads the header section of the file, which contains information about
218 * the Frame.
219 *
220 * @param frame
221 * The Frame to assign the read values to.
222 * @param line
223 * The line from the file to process.
224 * @return False if the end of the header has been reached, True otherwise.
225 */
226 private void processHeaderLine(Frame frame, String line) throws IOException {
227 // first separate the tag from the text
228 Character tag = getTag(line);
229 String value = getValue(line);
230 Method toRun = _FrameTags.get(tag);
231
232 if (toRun == null) {
233 if (tag != 'v') {
234 System.out.println("Tag '" + tag + "' in '" + line
235 + "' is not supported.");
236 }
237 return;
238 }
239
240 Object[] vals = Conversion.Convert(toRun, value);
241 try {
242 toRun.invoke(frame, vals);
243 } catch (Exception e) {
244 System.out.println("Error running method: "
245 + toRun.toGenericString());
246 e.printStackTrace();
247 }
248 }
249
250 // Returns a point from a String containing two ints separated by a space
251 private java.awt.Point separateValues(String line) {
252 int x = Integer.parseInt(line.substring(0, line.indexOf(" ")));
253 int y = Integer.parseInt(line.substring(line.indexOf(" ") + 1));
254
255 return new java.awt.Point(x, y);
256 }
257
258 public static int getVersion(String fullpath) {
259 try {
260 BufferedReader reader = new BufferedReader(new FileReader(fullpath));
261 String next = "";
262 // First read the header lines until we get the version number
263 while (reader.ready() && !(next = reader.readLine()).equals("Z")) {
264 if (isValidLine(next)) {
265 Character tag = getTag(next);
266 String value = getValue(next);
267 if (tag.equals('V'))
268 return Integer.parseInt(value);
269 }
270 }
271 } catch (Exception e) {
272 }
273 return 0;
274 }
275}
Note: See TracBrowser for help on using the repository browser.