source: trunk/src/org/expeditee/io/ExpWriter.java@ 289

Last change on this file since 289 was 198, checked in by ra33, 16 years ago

added new package to separate widget classes from item classes

File size: 6.3 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.LinkedList;
9import java.util.List;
10
11import org.expeditee.agents.DefaultAgent;
12import org.expeditee.gui.Frame;
13import org.expeditee.items.Constraint;
14import org.expeditee.items.Item;
15import org.expeditee.items.Line;
16import org.expeditee.items.widgets.WidgetEdge;
17import org.expeditee.stats.SessionStats;
18
19/**
20 * Writes a Frame out to a Expeditee format file.
21 *
22 * @author jdm18
23 *
24 */
25public class ExpWriter extends DefaultFrameWriter {
26
27 private ProxyWriter _writer = null;
28
29 private StringBuilder _stringWriter = null;
30
31 private static final char TERMINATOR = 'Z';
32
33 public ExpWriter() {
34 super();
35 }
36
37 public void initialise(Frame start) throws IOException {
38 String name = start.getFramesetName().toLowerCase();
39
40 if (_filename == null)
41 _filename = start.path + name + File.separator
42 + start.getNumber() + ExpReader.EXTENTION;
43
44 _stringWriter = new StringBuilder();
45 if (_filename.equalsIgnoreCase(DefaultAgent.CLIPBOARD)) {
46 _writer = new ProxyWriter(true);
47 _filename = DefaultAgent.CLIPBOARD;
48 } else
49 _writer = new ProxyWriter(new FileWriter(_filename));
50
51 try {
52 _FrameTags.remove("A");
53 _ItemTags.put("S", Item.class.getMethod("getTypeAndID", new Class[] {}));
54 } catch (Exception e) {
55
56 }
57 }
58
59 /**
60 * Writes the given Frame (and all items it contains) to a Expeditee file. Note:
61 * File path and location must be set before calling this or it will do
62 * nothing.
63 *
64 * @param frame
65 * The Frame to write out to the file.
66 * @throws IOException
67 * Any exceptions occured by the BufferedWriter.
68 */
69 public void outputFrame(Frame frame) throws IOException {
70 if (_writer == null)
71 return;
72
73 writeHeader(frame);
74
75
76 // write each item in the frame
77 for (Item i : frame.getItemsToSave()) {
78 assert(!(i instanceof Line));
79 writeItem(i);
80 }
81
82 // write any lines or constraints
83 writeTerminator();
84 writeLineData();
85 writeTerminator();
86 writeConstraintData();
87 writeTerminator();
88 writeLine(SessionStats.getFrameEventList(frame));
89
90 return;
91 }
92
93 private void writeHeader(Frame toWrite) throws IOException {
94 Iterator<String> it = _FrameTags.keySet().iterator();
95 Object[] param = {};
96
97 while (it.hasNext()) {
98 String tag = it.next();
99 try {
100 Object o = _FrameTags.get(tag).invoke(toWrite, param);
101 o = Conversion.ConvertToExpeditee(_FrameTags.get(tag), o);
102 if (o != null) {
103 writeLine(tag, o.toString());
104 }
105 } catch (Exception e) {
106 e.printStackTrace();
107 }
108 }
109
110 writeTerminator();
111 }
112
113 private void writeLine(String tag, String line) throws IOException {
114 writeLine(tag + " " + line);
115 }
116
117 private void writeTerminator() throws IOException {
118 writeLine(TERMINATOR + "\n");
119 }
120
121 // writes the given line out to the file
122 private void writeLine(String line) throws IOException {
123 // do not write empty lines
124 if (line == null)
125 return;
126
127 String toWrite = line + "\n";
128
129 _writer.write(toWrite);
130 _stringWriter.append(toWrite);
131 }
132
133 // writes the given Item out to the file
134 // This method is not used to write out LINE items
135 public void writeItem(Item item) throws IOException {
136 if (_writer == null)
137 return;
138
139 if (item.isLineEnd())
140 writeLineEnd(item);
141 else if (!(item instanceof Line))
142 writeClass(item);
143 // lines are saved at the end of the file
144 // So dont worry about them in here
145 else
146 System.out.println("Unknown Item: " + item.getID());
147
148 writeLine("");
149 }
150
151 private List<Item> _lineEnds = new LinkedList<Item>();
152
153 // Writes out a LineEnd to the file
154 protected void writeLineEnd(Item point) throws IOException {
155 _lineEnds.add(point);
156 writeClass(point);
157 }
158
159 // writes out all lines to the file
160 private void writeLineData() throws IOException {
161 List<Line> seen = new LinkedList<Line>();
162
163 // loop through all points stored
164 for (int i = 0; i < _lineEnds.size(); i++) {
165 List<Line> lines = _lineEnds.get(i).getLines();
166
167 // if this point is part of one or more lines
168 if (lines != null && lines.size() > 0) {
169 for (Line line : lines) {
170
171 // Brook: widget edges are not saved
172 if (line instanceof WidgetEdge) {
173 seen.add(line);
174 continue;
175 }
176
177 // only output new lines that have not yet been output
178 if (!seen.contains(line)) {
179 writeLine("L", line.getID() + " " + line.getLineType());
180 writeLine("s", line.getLineEnds());
181 writeLine("");
182
183 // add this line to the list of lines that have been
184 // seen
185 seen.add(line);
186 }
187 }
188 }
189 }
190 }
191
192 // writes out any constraints to the file
193 private void writeConstraintData() throws IOException {
194 // outputs any constraints the points have
195
196 // loop through all the points
197 while (_lineEnds.size() > 0) {
198 Item i = _lineEnds.get(0);
199
200 // if there are any constraints to write
201 if (i.getConstraints() != null) {
202 List<Constraint> constraints = i.getConstraints();
203
204 // do not write constraints that have already been
205 // written
206 for (Constraint c : constraints) {
207 if (_lineEnds.contains(c.getStart())
208 && _lineEnds.contains(c.getEnd())) {
209 writeLine("C", c.getID() + " " + c.getType());
210 writeLine("s", c.getLineEnds());
211 writeLine("");
212 }
213
214 }
215 }
216 // remove the point from the list
217 _lineEnds.remove(0);
218 }
219 }
220
221 @Override
222 protected String finaliseFrame() throws IOException {
223 _writer.flush();
224 _writer.close();
225 _writer = null;
226
227 return "Frame successfully written to " + _filename;
228 }
229
230 private void writeClass(Object toWrite) throws IOException {
231 Iterator<String> it = _ItemTags.keySet().iterator();
232 Object[] param = {};
233
234 while (it.hasNext()) {
235 String tag = it.next();
236 Method toRun = _ItemTags.get(tag);
237 Class<?> declarer = toRun.getDeclaringClass();
238 if (declarer.isAssignableFrom(toWrite.getClass())) {
239 try {
240 Object o = toRun.invoke(toWrite, param);
241 o = Conversion.ConvertToExpeditee(toRun, o);
242 if (o != null) {
243 if (o instanceof List) {
244 for (Object line : (List) o)
245 writeLine(tag, line.toString());
246 } else
247 writeLine(tag, o.toString());
248 }
249 } catch (Exception e) {
250 e.printStackTrace();
251 }
252 }
253 }
254 }
255
256 /**
257 * Gets a string representation of the frame file contents.
258 */
259 public String getFileContents() {
260 return _stringWriter.toString();
261 }
262}
Note: See TracBrowser for help on using the repository browser.