1 | package org.expeditee.io;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Font;
|
---|
5 | import java.awt.Point;
|
---|
6 | import java.io.BufferedReader;
|
---|
7 | import java.io.FileReader;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.lang.reflect.Method;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.LinkedHashMap;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import org.expeditee.gui.Frame;
|
---|
15 | import org.expeditee.items.Constraint;
|
---|
16 | import org.expeditee.items.Dot;
|
---|
17 | import org.expeditee.items.Item;
|
---|
18 | import org.expeditee.items.Line;
|
---|
19 | import org.expeditee.items.Text;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Reads in Exp format files and constructs the Frame and Item objects they
|
---|
23 | * contain.
|
---|
24 | *
|
---|
25 | * @author mrww1
|
---|
26 | *
|
---|
27 | */
|
---|
28 | public class ExpReader implements FrameReader {
|
---|
29 |
|
---|
30 | public static final String EXTENTION = ".exp";
|
---|
31 |
|
---|
32 | private BufferedReader _reader = null;
|
---|
33 |
|
---|
34 | private static LinkedHashMap<Character, Method> _ItemTags = null;
|
---|
35 |
|
---|
36 | private static LinkedHashMap<Character, Method> _FrameTags = null;
|
---|
37 |
|
---|
38 | private String _frameName;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Does nothing, location must be set before use
|
---|
42 | */
|
---|
43 | public ExpReader(String frameName) {
|
---|
44 | _frameName = frameName;
|
---|
45 |
|
---|
46 | if (_ItemTags != null && _FrameTags != null)
|
---|
47 | return;
|
---|
48 |
|
---|
49 | _ItemTags = new LinkedHashMap<Character, Method>();
|
---|
50 | _FrameTags = new LinkedHashMap<Character, Method>();
|
---|
51 |
|
---|
52 | Class[] pString = { String.class };
|
---|
53 | Class[] pInt = { int.class };
|
---|
54 | Class[] pFloat = { float.class };
|
---|
55 | Class[] pColor = { Color.class };
|
---|
56 | Class[] pBool = { boolean.class };
|
---|
57 | Class[] pFont = { Font.class };
|
---|
58 | Class[] pPoint = { Point.class };
|
---|
59 | Class[] pArrow = { int.class, double.class };
|
---|
60 | Class[] pList = { List.class };
|
---|
61 | Class[] pIntArray = { int[].class };
|
---|
62 | Class[] pItem = { Item.class };
|
---|
63 |
|
---|
64 | try {
|
---|
65 | _FrameTags.put('V', Frame.class.getMethod("setVersion", pInt));
|
---|
66 | _FrameTags
|
---|
67 | .put('p', Frame.class.getMethod("setProtection", pString));
|
---|
68 | _FrameTags.put('U', Frame.class.getMethod("setOwner", pString));
|
---|
69 | _FrameTags.put('D', Frame.class
|
---|
70 | .getMethod("setDateCreated", pString));
|
---|
71 | _FrameTags.put('M', Frame.class.getMethod("setLastModifyUser",
|
---|
72 | pString));
|
---|
73 | _FrameTags.put('d', Frame.class.getMethod("setLastModifyDate",
|
---|
74 | pString));
|
---|
75 | _FrameTags
|
---|
76 | .put('F', Frame.class.getMethod("setFrozenDate", pString));
|
---|
77 |
|
---|
78 | _FrameTags.put('O', Frame.class.getMethod("setForegroundColor",
|
---|
79 | pColor));
|
---|
80 | _FrameTags.put('B', Frame.class.getMethod("setBackgroundColor",
|
---|
81 | pColor));
|
---|
82 |
|
---|
83 | _ItemTags.put('S', Item.class.getMethod("setID", pInt));
|
---|
84 | _ItemTags.put('s', Item.class.getMethod("setDateCreated", pString));
|
---|
85 | _ItemTags.put('d', Item.class.getMethod("setColor", pColor));
|
---|
86 | _ItemTags.put('G', Item.class.getMethod("setBackgroundColor",
|
---|
87 | pColor));
|
---|
88 | _ItemTags.put('P', Item.class.getMethod("setPosition", pPoint));
|
---|
89 | _ItemTags.put('F', Item.class.getMethod("setLink", pString));
|
---|
90 | _ItemTags.put('X', Item.class.getMethod("setAction", pList));
|
---|
91 | _ItemTags.put('x', Item.class.getMethod("setActionMark", pBool));
|
---|
92 | _ItemTags.put('U', Item.class.getMethod("setActionCursorEnter",
|
---|
93 | pList));
|
---|
94 | _ItemTags.put('V', Item.class.getMethod("setActionCursorLeave",
|
---|
95 | pList));
|
---|
96 | _ItemTags.put('W', Item.class.getMethod("setActionEnterFrame",
|
---|
97 | pList));
|
---|
98 | _ItemTags.put('Y', Item.class.getMethod("setActionLeaveFrame",
|
---|
99 | pList));
|
---|
100 | _ItemTags.put('D', Item.class.getMethod("setData", pString));
|
---|
101 | _ItemTags.put('u', Item.class.getMethod("setHighlight", pBool));
|
---|
102 | _ItemTags.put('e', Item.class.getMethod("setFillColor", pColor));
|
---|
103 | _ItemTags.put('i', Item.class.getMethod("setFillPattern", pString));
|
---|
104 | _ItemTags.put('o', Item.class.getMethod("setOwner", pString));
|
---|
105 | _ItemTags.put('n', Item.class.getMethod("setLinkMark", pBool));
|
---|
106 | _ItemTags
|
---|
107 | .put('q', Item.class.getMethod("setLinkFrameset", pString));
|
---|
108 | _ItemTags
|
---|
109 | .put('y', Item.class.getMethod("setLinkTemplate", pString));
|
---|
110 | _ItemTags.put('g', Item.class
|
---|
111 | .getMethod("setLinePattern", pIntArray));
|
---|
112 |
|
---|
113 | _ItemTags.put('j', Item.class.getMethod("setArrow", pArrow));
|
---|
114 |
|
---|
115 | _ItemTags.put('f', Text.class.getMethod("setFont", pFont));
|
---|
116 | _ItemTags.put('t', Text.class.getMethod("setSpacing", pInt));
|
---|
117 | _ItemTags.put('T', Text.class.getMethod("appendLine", pString));
|
---|
118 |
|
---|
119 | _ItemTags.put('a', Text.class.getMethod("setWordSpacing", pInt));
|
---|
120 | _ItemTags.put('b', Text.class.getMethod("setLetterSpacing", pInt));
|
---|
121 | _ItemTags.put('m', Text.class.getMethod("setInitialSpacing", pInt));
|
---|
122 | _ItemTags.put('w', Text.class.getMethod("setWidth", pInt));
|
---|
123 | _ItemTags.put('k', Text.class.getMethod("setJustification", pInt));
|
---|
124 |
|
---|
125 | _ItemTags.put('h', Dot.class.getMethod("setThickness", pFloat));
|
---|
126 | _ItemTags.put('l', Item.class.getMethod("setLineIDs", pString));
|
---|
127 | _ItemTags
|
---|
128 | .put('c', Dot.class.getMethod("setConstraintIDs", pString));
|
---|
129 |
|
---|
130 | // Lines and constraints are created differently
|
---|
131 | _ItemTags.put('L', Line.class.getMethod("setStartItem", pItem));
|
---|
132 | _ItemTags.put('C', Constraint.class.getMethod("getID",
|
---|
133 | (Class[]) null));
|
---|
134 | } catch (Exception e) {
|
---|
135 | e.printStackTrace();
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Determines whether a string begins with a KMS tag.
|
---|
141 | *
|
---|
142 | * @param s
|
---|
143 | * a line of text
|
---|
144 | * @return true if s begins with a KMS tag
|
---|
145 | */
|
---|
146 | private boolean isValidLine(String s) {
|
---|
147 | return s.length() >= 2 && s.charAt(1) == ' '
|
---|
148 | && Character.isLetter(s.charAt(0));
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Reads a KMS file with the given name from disk.
|
---|
153 | *
|
---|
154 | * @param frameName
|
---|
155 | * the name of the Frame to read in from a file.
|
---|
156 | * @return A new Frame object that contains any Items described in the file.
|
---|
157 | * @throws IOException
|
---|
158 | * Any exceptions occured by the BufferedReader.
|
---|
159 | */
|
---|
160 | public Frame readFrame(String fullpath) throws IOException {
|
---|
161 | _reader = new BufferedReader(new FileReader(fullpath));
|
---|
162 | String next = "";
|
---|
163 | Frame newFrame = new Frame();
|
---|
164 |
|
---|
165 | try {
|
---|
166 | //Framename must be set before setting the frame number
|
---|
167 | newFrame.setFrameName(_frameName);
|
---|
168 |
|
---|
169 | // First read all the header lines
|
---|
170 | while (_reader.ready() && !(next = _reader.readLine()).equals("Z")) {
|
---|
171 | if (isValidLine(next)) {
|
---|
172 | processHeaderLine(newFrame, next);
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | // Now read all the items
|
---|
177 | Item currentItem = null;
|
---|
178 | while (_reader.ready() && !(next = _reader.readLine()).equals("Z")) {
|
---|
179 | // if this is the start of a new item add a new item
|
---|
180 | if (isValidLine(next) ) {
|
---|
181 | if (getTag(next) == 'S') {
|
---|
182 | String value = getValue(next);
|
---|
183 | int id = Integer.parseInt(value.substring(2));
|
---|
184 |
|
---|
185 | switch (value.charAt(0)) {
|
---|
186 | case 'P': //check if its a point
|
---|
187 | currentItem = new Dot(id);
|
---|
188 | _linePoints.put(currentItem.getID(), currentItem);
|
---|
189 | break;
|
---|
190 | default:
|
---|
191 | currentItem = new Text(id);
|
---|
192 | break;
|
---|
193 | }
|
---|
194 | newFrame.addItem(currentItem);
|
---|
195 | } else if (currentItem != null) {
|
---|
196 | processBodyLine(currentItem, next);
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | // Read the lines
|
---|
202 | while (_reader.ready() && !(next = _reader.readLine()).equals("Z")) {
|
---|
203 | if (isValidLine(next)) {
|
---|
204 | java.awt.Point idtype = separateValues(next.substring(2));
|
---|
205 | // The next line must be the endpoints
|
---|
206 | if (!_reader.ready())
|
---|
207 | throw new Exception("Unexpected end of file");
|
---|
208 | next = _reader.readLine();
|
---|
209 | java.awt.Point startend = separateValues(next.substring(2));
|
---|
210 | int start = startend.x;
|
---|
211 | int end = startend.y;
|
---|
212 |
|
---|
213 | if (_linePoints.get(start) != null
|
---|
214 | && _linePoints.get(end) != null) {
|
---|
215 | newFrame.addItem(new Line(_linePoints.get(start),
|
---|
216 | _linePoints.get(end), idtype.x));
|
---|
217 | } else {
|
---|
218 | System.out
|
---|
219 | .println("Error reading line with unknown end points");
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | // Read the constraints
|
---|
225 | while (_reader.ready() && !(next = _reader.readLine()).equals("Z")) {
|
---|
226 | if (isValidLine(next)) {
|
---|
227 | java.awt.Point idtype = separateValues(next.substring(2));
|
---|
228 | // The next line must be the endpoints
|
---|
229 | if (!_reader.ready())
|
---|
230 | throw new Exception("Unexpected end of file");
|
---|
231 | next = _reader.readLine();
|
---|
232 | java.awt.Point startend = separateValues(next.substring(2));
|
---|
233 |
|
---|
234 | Dot a = (Dot) _linePoints.get(startend.x);
|
---|
235 | Dot b = (Dot) _linePoints.get(startend.y);
|
---|
236 |
|
---|
237 | new Constraint(a, b, idtype.x, idtype.y);
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | } catch (Exception e) {
|
---|
242 | System.out.println("Error reading frame file line: " + next + " "
|
---|
243 | + e.getMessage());
|
---|
244 | }
|
---|
245 |
|
---|
246 | _reader.close();
|
---|
247 | newFrame.setChanged(false);
|
---|
248 |
|
---|
249 | return newFrame;
|
---|
250 | }
|
---|
251 |
|
---|
252 | // Stores points used when constructing lines
|
---|
253 | private HashMap<Integer, Item> _linePoints = new HashMap<Integer, Item>();
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Processes the body section of the Exp file, which contains all Items
|
---|
257 | *
|
---|
258 | * @param frame
|
---|
259 | * The Frame to add any created Items to.
|
---|
260 | * @param line
|
---|
261 | * The line of text read in from the file to process.
|
---|
262 | */
|
---|
263 | private void processBodyLine(Item item, String line) {
|
---|
264 | // separate the tag from the value
|
---|
265 | Character tag = getTag(line);
|
---|
266 | String value = getValue(line);
|
---|
267 |
|
---|
268 | Method toRun = _ItemTags.get(tag);
|
---|
269 | if (toRun == null)
|
---|
270 | System.out.println("Error accessing tag method: " + tag);
|
---|
271 | Object[] vals = Conversion.Convert(toRun, value);
|
---|
272 |
|
---|
273 | try {
|
---|
274 | if (vals != null)
|
---|
275 | toRun.invoke(item, vals);
|
---|
276 | } catch (Exception e) {
|
---|
277 | System.out.println("Error running tag method: " + tag);
|
---|
278 | //e.printStackTrace();
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | private Character getTag(String line) {
|
---|
283 | assert (line.length() > 0);
|
---|
284 | return line.charAt(0);
|
---|
285 | }
|
---|
286 |
|
---|
287 | private String getValue(String line) {
|
---|
288 | if (line.length() > 2)
|
---|
289 | return line.substring(2);
|
---|
290 | else
|
---|
291 | return "";
|
---|
292 | }
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * Reads the header section of the file, which contains information about
|
---|
296 | * the Frame.
|
---|
297 | *
|
---|
298 | * @param frame
|
---|
299 | * The Frame to assign the read values to.
|
---|
300 | * @param line
|
---|
301 | * The line from the file to process.
|
---|
302 | * @return False if the end of the header has been reached, True otherwise.
|
---|
303 | */
|
---|
304 | private void processHeaderLine(Frame frame, String line) throws IOException {
|
---|
305 | // first separate the tag from the text
|
---|
306 | Character tag = getTag(line);
|
---|
307 | String value = getValue(line);
|
---|
308 | Method toRun = _FrameTags.get(tag);
|
---|
309 |
|
---|
310 | if (toRun == null) {
|
---|
311 | System.out.println("Tag '" + tag + "' in '" + line
|
---|
312 | + "' is not supported.");
|
---|
313 | return;
|
---|
314 | }
|
---|
315 |
|
---|
316 | Object[] vals = Conversion.Convert(toRun, value);
|
---|
317 | try {
|
---|
318 | toRun.invoke(frame, vals);
|
---|
319 | } catch (Exception e) {
|
---|
320 | System.out.println("Error running method: " + toRun.toGenericString());
|
---|
321 | e.printStackTrace();
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 | // Returns a point from a String containing two ints separated by a space
|
---|
326 | private java.awt.Point separateValues(String line) {
|
---|
327 | int x = Integer.parseInt(line.substring(0, line.indexOf(" ")));
|
---|
328 | int y = Integer.parseInt(line.substring(line.indexOf(" ") + 1));
|
---|
329 |
|
---|
330 | return new java.awt.Point(x, y);
|
---|
331 | }
|
---|
332 | }
|
---|