source: trunk/org/expeditee/io/ExpWriter.java@ 4

Last change on this file since 4 was 4, checked in by davidb, 16 years ago

Starting source code to Expeditee

File size: 9.8 KB
Line 
1package org.expeditee.io;
2
3import java.io.File;
4import java.io.FileWriter;
5import java.io.IOException;
6import java.lang.reflect.Method;
7import java.util.Iterator;
8import java.util.LinkedHashMap;
9import java.util.LinkedList;
10import java.util.List;
11
12import org.expeditee.gui.Frame;
13import org.expeditee.items.Constraint;
14import org.expeditee.items.Dot;
15import org.expeditee.items.Item;
16import org.expeditee.items.Line;
17import org.expeditee.items.Picture;
18import org.expeditee.items.Text;
19import org.expeditee.stats.SessionStats;
20
21/**
22 * Writes a Frame out to a KMS format file.
23 *
24 * @author jdm18
25 *
26 */
27public class ExpWriter extends DefaultFrameWriter {
28
29 private ProxyWriter _writer = null;
30
31 private StringBuilder _stringWriter = null;
32
33 // keep track of methods that are put on the same line
34 private static LinkedHashMap<String, Method> _ItemTags = null;
35
36 private static LinkedHashMap<String, Method> _FrameTags = null;
37
38 private static final char TERMINATOR = 'Z';
39
40 public ExpWriter() {
41 if (_ItemTags != null && _FrameTags != null)
42 return;
43
44 _ItemTags = new LinkedHashMap<String, Method>();
45 _FrameTags = new LinkedHashMap<String, Method>();
46
47 Class[] param = {};
48
49 try {
50 _FrameTags.put("V", Frame.class.getMethod("getVersion", param));
51 _FrameTags.put("p", Frame.class.getMethod("getProtection", param));
52 _FrameTags.put("U", Frame.class.getMethod("getOwner", param));
53 _FrameTags.put("D", Frame.class.getMethod("getDateCreated", param));
54 _FrameTags.put("M", Frame.class.getMethod("getLastModifyUser",
55 param));
56 _FrameTags.put("d", Frame.class.getMethod("getLastModifyDate",
57 param));
58 _FrameTags.put("F", Frame.class.getMethod("getFrozenDate", param));
59
60 _FrameTags.put("O", Frame.class.getMethod("getForegroundColor",
61 param));
62 _FrameTags.put("B", Frame.class.getMethod("getBackgroundColor",
63 param));
64
65 _ItemTags.put("S", Item.class.getMethod("getTypeAndID", param));
66 _ItemTags.put("s", Item.class.getMethod("getDateCreated", param));
67 _ItemTags.put("d", Item.class.getMethod("getColor", param));
68 _ItemTags.put("G", Item.class
69 .getMethod("getBackgroundColor", param));
70 _ItemTags.put("P", Item.class.getMethod("getPosition", param));
71 _ItemTags.put("F", Item.class.getMethod("getLink", param));
72 _ItemTags.put("X", Item.class.getMethod("getAction", param));
73 _ItemTags.put("x", Item.class.getMethod("getActionMark", param));
74 _ItemTags.put("U", Item.class.getMethod("getActionCursorEnter",
75 param));
76 _ItemTags.put("V", Item.class.getMethod("getActionCursorLeave",
77 param));
78 _ItemTags.put("W", Item.class.getMethod("getActionEnterFrame",
79 param));
80 _ItemTags.put("Y", Item.class.getMethod("getActionLeaveFrame",
81 param));
82 _ItemTags.put("D", Item.class.getMethod("getData", param));
83 _ItemTags.put("u", Item.class.getMethod("getHighlight", param));
84 _ItemTags.put("e", Item.class.getMethod("getFillColor", param));
85 _ItemTags.put("i", Item.class.getMethod("getFillPattern", param));
86 _ItemTags.put("o", Item.class.getMethod("getOwner", param));
87 _ItemTags.put("n", Item.class.getMethod("getLinkMark", param));
88 _ItemTags.put("q", Item.class.getMethod("getLinkFrameset", param));
89 _ItemTags.put("y", Item.class.getMethod("getLinkTemplate", param));
90 _ItemTags.put("g", Item.class.getMethod("getLinePattern", param));
91
92 _ItemTags.put("j", Item.class.getMethod("getArrow", param));
93
94 _ItemTags.put("f", Text.class.getMethod("getFont", param));
95 _ItemTags.put("t", Text.class.getMethod("getSpacing", param));
96 _ItemTags.put("T", Text.class.getMethod("getText", param));
97
98 _ItemTags.put("a", Text.class.getMethod("getWordSpacing", param));
99 _ItemTags.put("b", Text.class.getMethod("getLetterSpacing", param));
100 _ItemTags
101 .put("m", Text.class.getMethod("getInitialSpacing", param));
102 _ItemTags.put("w", Text.class.getMethod("getWidth", param));
103 _ItemTags.put("k", Text.class.getMethod("getJustification", param));
104
105 _ItemTags.put("h", Dot.class.getMethod("getThickness", param));
106 _ItemTags.put("l", Dot.class.getMethod("getLineIDs", param));
107 _ItemTags.put("c", Dot.class.getMethod("getConstraintIDs", param));
108
109 } catch (Exception e) {
110 e.printStackTrace();
111 }
112 }
113
114 public void initialise(Frame start) throws IOException {
115 String name = start.getFramesetName().toLowerCase();
116
117 if (_filename == null)
118 _filename = start.path + name + File.separator
119 + start.getFrameNumber() + ExpReader.EXTENTION;
120
121 _stringWriter = new StringBuilder();
122 if (_filename.toLowerCase().equals("clipboard")) {
123 _writer = new ProxyWriter(true);
124 _filename = "Clipboard";
125 } else
126 _writer = new ProxyWriter(new FileWriter(_filename));
127 }
128
129 /**
130 * Writes the given Frame (and all items it contains) to a KMS file. Note:
131 * File path and location must be set before calling this or it will do
132 * nothing.
133 *
134 * @param frame
135 * The Frame to write out to the file.
136 * @throws IOException
137 * Any exceptions occured by the BufferedWriter.
138 */
139 public void outputFrame(Frame frame) throws IOException {
140 if (_writer == null)
141 return;
142
143 writeHeader(frame);
144
145 // write out each Item in the Frame.
146 List<Item> items = frame.getItems();
147
148 // do not write items with ID -1, also skip any lines
149 for (Item i : items) {
150 if (i.getID() >= 0 && !(i instanceof Line))
151 writeItem(i);
152 }
153
154 // write any lines or constraints
155 writeTerminator();
156 writeLineData();
157 writeTerminator();
158 writeConstraintData();
159
160 return;
161 }
162
163 private void writeHeader(Frame toWrite) throws IOException {
164 Iterator<String> it = _FrameTags.keySet().iterator();
165 Object[] param = {};
166
167 while (it.hasNext()) {
168 String tag = it.next();
169 try {
170 Object o = _FrameTags.get(tag).invoke(toWrite, param);
171 o = Conversion.ConvertToKMS(_FrameTags.get(tag), o);
172 if (o != null) {
173 writeLine(tag, (String) o);
174 }
175 } catch (Exception e) {
176 e.printStackTrace();
177 }
178 }
179
180 writeTerminator();
181 }
182
183 private void writeLine(String tag, String line) throws IOException {
184 writeLine(tag + " " + line);
185 }
186
187 private void writeTerminator() throws IOException {
188 writeLine(TERMINATOR + "\n");
189 }
190
191 // writes the given line out to the file
192 private void writeLine(String line) throws IOException {
193 // do not write empty lines
194 if (line == null)
195 return;
196
197 String toWrite = line + "\n";
198
199 _writer.write(toWrite);
200 _stringWriter.append(toWrite);
201 }
202
203 // writes the given Item out to the file
204 //This method is not used to write out LINE items
205 public void writeItem(Item item) throws IOException {
206 if (_writer == null)
207 return;
208
209 // Pictures are saved as the corresponding text items that created them
210 if (item instanceof Picture)
211 item = ((Picture) item).getText();
212
213 if (item.getLines().size() > 0)
214 writePoint(item);
215 else if (!(item instanceof Line))
216 writeClass(item);
217 // lines are saved at the end of the file
218 //So dont worry about them in here
219 else
220 System.out.println("Unknown Item: " + item.getID());
221
222 writeLine("");
223 }
224
225 private List<Item> _points = new LinkedList<Item>();
226
227 // Writes out a Dot to the file
228 protected void writePoint(Item point) throws IOException {
229 _points.add(point);
230 writeClass(point);
231 }
232
233 // writes out all lines to the file
234 private void writeLineData() throws IOException {
235 List<Line> seen = new LinkedList<Line>();
236
237 // loop through all points stored
238 for (int i = 0; i < _points.size(); i++) {
239 List<Line> lines = _points.get(i).getLines();
240
241 // if this point is part of one or more lines
242 if (lines != null && lines.size() > 0) {
243 for (Line line : lines) {
244
245 // only output new lines that have not yet been output
246 if (!seen.contains(line)) {
247 writeLine("L", line.getID() + " " + line.getLineType());
248 writeLine("s", line.getLineEnds());
249 writeLine("");
250
251 // add this line to the list of lines that have been
252 // seen
253 seen.add(line);
254 }
255 }
256 }
257 }
258 }
259
260 // writes out any constraints to the file
261 private void writeConstraintData() throws IOException {
262 // outputs any constraints the points have
263
264 // loop through all the points
265 while (_points.size() > 0) {
266 Item i = _points.get(0);
267
268 if (i instanceof Dot) {
269 Dot p = (Dot) i;
270
271 // if there are any constraints to write
272 if (p.getConstraints() != null) {
273 List<Constraint> constraints = p.getConstraints();
274
275 // do not write constraints that have already been
276 // written
277 for (Constraint c : constraints) {
278 if (_points.contains(c.getStart())
279 && _points.contains(c.getEnd())) {
280 writeLine("C", c.getID() + " "
281 + c.getConstraintType());
282 writeLine("s", c.getLineEnds());
283 writeLine("");
284 }
285
286 }
287 }
288 }
289 // remove the point from the list
290 _points.remove(0);
291 }
292 }
293
294 @Override
295 protected String finaliseFrame() throws IOException {
296 writeTerminator();
297
298 writeLine(SessionStats.getFrameEventList());
299
300 _writer.flush();
301 _writer.close();
302 _writer = null;
303
304 return "Frame successfully written to " + _filename;
305 }
306
307 private void writeClass(Object toWrite) throws IOException {
308 Iterator<String> it = _ItemTags.keySet().iterator();
309 Object[] param = {};
310
311 while (it.hasNext()) {
312 String tag = it.next();
313 Method toRun = _ItemTags.get(tag);
314 Class declarer = toRun.getDeclaringClass();
315 if (declarer == toWrite.getClass()
316 || declarer == toWrite.getClass().getSuperclass()
317 || declarer == toWrite.getClass().getSuperclass()
318 .getSuperclass()) {
319 try {
320 Object o = toRun.invoke(toWrite, param);
321 o = Conversion.ConvertToKMS(toRun, o);
322 if (o != null) {
323 if (o instanceof List) {
324 for (Object line : (List) o)
325 writeLine(tag, (String) line);
326 } else
327 writeLine(tag, (String) o);
328 }
329 } catch (Exception e) {
330 e.printStackTrace();
331 }
332 }
333 }
334 }
335
336 /**
337 * Gets a string representation of the frame file contents.
338 */
339 public String getFileContents() {
340 return _stringWriter.toString();
341 }
342}
Note: See TracBrowser for help on using the repository browser.