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

Last change on this file since 457 was 362, checked in by ra33, 16 years ago

Added Spell Checker
Added word count stats
Fixed some mail stuff

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