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

Last change on this file since 504 was 504, checked in by jts21, 11 years ago

New copy/paste handling, add pdfImporter, add dictionary and documentation to resources, other small changes

File size: 6.9 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 protected 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 protected void writeTerminator() throws IOException {
133 writeLine(TERMINATOR + "\n");
134 }
135
136 // writes the given line out to the file
137 protected 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 writeItemAlways(item);
155 }
156
157 protected void writeItemAlways(Item item) throws IOException {
158 if (item.isLineEnd())
159 writeLineEnd(item);
160 else if (!(item instanceof Line))
161 writeClass(item);
162 // lines are saved at the end of the file
163 // So dont worry about them in here
164 else
165 System.out.println("Unknown Item: " + item.getID());
166
167 writeLine("");
168 }
169
170 private List<Item> _lineEnds = new LinkedList<Item>();
171
172 // Writes out a LineEnd to the file
173 protected void writeLineEnd(Item point) throws IOException {
174 _lineEnds.add(point);
175 writeClass(point);
176 }
177
178 // writes out all lines to the file
179 protected void writeLineData() throws IOException {
180 List<Line> seen = new LinkedList<Line>();
181
182 // loop through all points stored
183 for (int i = 0; i < _lineEnds.size(); i++) {
184 List<Line> lines = _lineEnds.get(i).getLines();
185
186 // if this point is part of one or more lines
187 if (lines != null && lines.size() > 0) {
188 for (Line line : lines) {
189
190 // Brook: widget edges are not saved
191 if (line instanceof WidgetEdge) {
192 seen.add(line);
193 continue;
194 }
195
196 // only output new lines that have not yet been output
197 if (!seen.contains(line)) {
198 writeLine("L", line.getID() + " " + line.getLineType());
199 writeLine("s", line.getLineEnds());
200 writeLine("");
201
202 // add this line to the list of lines that have been
203 // seen
204 seen.add(line);
205 }
206 }
207 }
208 }
209 }
210
211 // writes out any constraints to the file
212 protected void writeConstraintData() throws IOException {
213 // outputs any constraints the points have
214
215 // loop through all the points
216 while (_lineEnds.size() > 0) {
217 Item i = _lineEnds.get(0);
218
219 // if there are any constraints to write
220 if (i.getConstraints() != null) {
221 List<Constraint> constraints = i.getConstraints();
222
223 // do not write constraints that have already been
224 // written
225 for (Constraint c : constraints) {
226 if (_lineEnds.contains(c.getStart())
227 && _lineEnds.contains(c.getEnd())) {
228 writeLine("C", c.getID() + " " + c.getType());
229 writeLine("s", c.getLineEnds());
230 writeLine("");
231 }
232
233 }
234 }
235 // remove the point from the list
236 _lineEnds.remove(0);
237 }
238 }
239
240 @Override
241 protected String finaliseFrame() throws IOException {
242 _writer.flush();
243 _writer.close();
244 _writer = null;
245
246 return "Frame successfully written to " + _filename;
247 }
248
249 private void writeClass(Object toWrite) throws IOException {
250 Iterator<String> it = _ItemTags.keySet().iterator();
251 Object[] param = {};
252
253 while (it.hasNext()) {
254 String tag = it.next();
255 Method toRun = _ItemTags.get(tag);
256 Class<?> declarer = toRun.getDeclaringClass();
257 if (declarer.isAssignableFrom(toWrite.getClass())) {
258 try {
259 Object o = toRun.invoke(toWrite, param);
260 o = Conversion.ConvertToExpeditee(toRun, o);
261 if (o != null) {
262 if (o instanceof List) {
263 for (Object line : (List) o)
264 writeLine(tag, line.toString());
265 } else
266 writeLine(tag, o.toString());
267 }
268 } catch (Exception e) {
269 e.printStackTrace();
270 }
271 }
272 }
273 }
274
275 /**
276 * Gets a string representation of the frame file contents.
277 */
278 public String getFileContents() {
279 return _stringWriter.toString();
280 }
281}
Note: See TracBrowser for help on using the repository browser.