1 | /**
|
---|
2 | * ExpWriter.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.io;
|
---|
20 |
|
---|
21 | import java.io.BufferedOutputStream;
|
---|
22 | import java.io.File;
|
---|
23 | import java.io.FileOutputStream;
|
---|
24 | import java.io.IOException;
|
---|
25 | import java.io.OutputStream;
|
---|
26 | import java.io.OutputStreamWriter;
|
---|
27 | import java.io.Writer;
|
---|
28 | import java.lang.reflect.Method;
|
---|
29 | import java.util.Iterator;
|
---|
30 | import java.util.LinkedHashMap;
|
---|
31 | import java.util.LinkedList;
|
---|
32 | import java.util.List;
|
---|
33 |
|
---|
34 | import org.expeditee.agents.DefaultAgent;
|
---|
35 | import org.expeditee.gui.Frame;
|
---|
36 | import org.expeditee.items.Constraint;
|
---|
37 | import org.expeditee.items.Item;
|
---|
38 | import org.expeditee.items.Line;
|
---|
39 | import org.expeditee.items.widgets.WidgetEdge;
|
---|
40 | import org.expeditee.stats.SessionStats;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Writes a Frame out to a Expeditee format file.
|
---|
44 | *
|
---|
45 | * @author jdm18
|
---|
46 | *
|
---|
47 | */
|
---|
48 | public class ExpWriter extends DefaultFrameWriter {
|
---|
49 |
|
---|
50 | protected ProxyWriter _writer = null;
|
---|
51 |
|
---|
52 | protected StringBuilder _stringWriter = null;
|
---|
53 |
|
---|
54 | protected String _framename;
|
---|
55 |
|
---|
56 | protected static final char TERMINATOR = 'Z';
|
---|
57 |
|
---|
58 | public ExpWriter() {
|
---|
59 | super();
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public void initialise(Frame start, Writer writer) throws IOException {
|
---|
64 | _framename = start.getName();
|
---|
65 | String name = start.getFramesetName().toLowerCase();
|
---|
66 |
|
---|
67 | if (_filename == null)
|
---|
68 | _filename = start.getPath() + name + File.separator
|
---|
69 | + start.getNumber() + ExpReader.EXTENTION;
|
---|
70 |
|
---|
71 | _stringWriter = new StringBuilder();
|
---|
72 |
|
---|
73 | if (writer != null) {
|
---|
74 | _writer = new ProxyWriter(writer);
|
---|
75 | _output = writer.toString();
|
---|
76 | } else if (_filename.equalsIgnoreCase(DefaultAgent.CLIPBOARD)) {
|
---|
77 | _writer = new ProxyWriter(true);
|
---|
78 | _filename = DefaultAgent.CLIPBOARD;
|
---|
79 | } else {
|
---|
80 | // Open an Output Stream Writer to set encoding
|
---|
81 | OutputStream fout = new FileOutputStream(_filename);
|
---|
82 | OutputStream bout = new BufferedOutputStream(fout);
|
---|
83 | Writer out = new OutputStreamWriter(bout, "UTF-8");
|
---|
84 |
|
---|
85 | _writer = new ProxyWriter(out);
|
---|
86 | }
|
---|
87 |
|
---|
88 | try {
|
---|
89 | _FrameTags.remove('A');
|
---|
90 | getItemCharTags().put('S', Item.class.getMethod("getTypeAndID",
|
---|
91 | new Class[] {}));
|
---|
92 | } catch (Exception e) {
|
---|
93 |
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Writes the given Frame (and all items it contains) to a Expeditee file.
|
---|
99 | * Note: File path and location must be set before calling this or it will
|
---|
100 | * do nothing.
|
---|
101 | *
|
---|
102 | * @param frame
|
---|
103 | * The Frame to write out to the file.
|
---|
104 | * @throws IOException
|
---|
105 | * Any exceptions occured by the BufferedWriter.
|
---|
106 | */
|
---|
107 | public void outputFrame(Frame frame) throws IOException {
|
---|
108 | if (_writer == null)
|
---|
109 | return;
|
---|
110 |
|
---|
111 | preOutputFrame();
|
---|
112 |
|
---|
113 | writeHeader(frame);
|
---|
114 |
|
---|
115 | // write each item in the frame
|
---|
116 | for (Item i : frame.getItemsToSave()) {
|
---|
117 | assert (!(i instanceof Line));
|
---|
118 | writeItem(i);
|
---|
119 | }
|
---|
120 |
|
---|
121 | for (final Item i: frame.getBodyItemsWithInsufficientPermissions()) {
|
---|
122 | assert (!(i instanceof Line));
|
---|
123 | writeItem(i);
|
---|
124 | }
|
---|
125 |
|
---|
126 | // write any lines or constraints
|
---|
127 | writeTerminator();
|
---|
128 | writeLineData();
|
---|
129 | writeTerminator();
|
---|
130 | writeConstraintData();
|
---|
131 | writeTerminator();
|
---|
132 | writeLine(SessionStats.getFrameEventList(frame));
|
---|
133 | }
|
---|
134 |
|
---|
135 | protected void preOutputFrame() {
|
---|
136 | }
|
---|
137 |
|
---|
138 | protected void writeHeader(Frame toWrite) throws IOException {
|
---|
139 | Iterator<Character> it = _FrameTags.keySet().iterator();
|
---|
140 | Object[] param = {};
|
---|
141 |
|
---|
142 | while (it.hasNext()) {
|
---|
143 | Character tag = it.next();
|
---|
144 | try {
|
---|
145 | Object o = _FrameTags.get(tag).invoke(toWrite, param);
|
---|
146 | o = Conversion.ConvertToExpeditee(_FrameTags.get(tag), o);
|
---|
147 | if (o != null) {
|
---|
148 | if (o instanceof List) {
|
---|
149 | for (Object line: (List) o) {
|
---|
150 | writeLine(tag.toString(), line.toString());
|
---|
151 | }
|
---|
152 | } else {
|
---|
153 | writeLine(tag.toString(), o.toString());
|
---|
154 | }
|
---|
155 | }
|
---|
156 | } catch (Exception e) {
|
---|
157 | e.printStackTrace();
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | writeTerminator();
|
---|
162 | }
|
---|
163 |
|
---|
164 | // while (it.hasNext()) {
|
---|
165 | // Character tag = it.next();
|
---|
166 | // Method toRun = _ItemTags.get(tag);
|
---|
167 | // Class<?> declarer = toRun.getDeclaringClass();
|
---|
168 | // if (declarer.isAssignableFrom(toWrite.getClass())) {
|
---|
169 | // try {
|
---|
170 | // Object o = toRun.invoke(toWrite, param);
|
---|
171 | // o = Conversion.ConvertToExpeditee(toRun, o);
|
---|
172 | // if (o != null) {
|
---|
173 | // if (o instanceof List) {
|
---|
174 | // for (Object line : (List) o) {
|
---|
175 | // writeLine(tag.toString(), line.toString());
|
---|
176 | // }
|
---|
177 | // } else
|
---|
178 | // writeLine(tag.toString(), o.toString());
|
---|
179 | // }
|
---|
180 | // } catch (Exception e) {
|
---|
181 | // e.printStackTrace();
|
---|
182 | // }
|
---|
183 | // }
|
---|
184 | // }
|
---|
185 |
|
---|
186 | protected void writeLine(String tag, String line) throws IOException {
|
---|
187 | writeLine(tag + " " + line);
|
---|
188 | }
|
---|
189 |
|
---|
190 | protected void writeTerminator() throws IOException {
|
---|
191 | writeLine(TERMINATOR + "\n");
|
---|
192 | }
|
---|
193 |
|
---|
194 | // writes the given line out to the file
|
---|
195 | protected void writeLine(String line) throws IOException {
|
---|
196 | // do not write empty lines
|
---|
197 | if (line == null)
|
---|
198 | return;
|
---|
199 |
|
---|
200 | String toWrite = line + "\n";
|
---|
201 |
|
---|
202 | _writer.write(toWrite);
|
---|
203 | _stringWriter.append(toWrite);
|
---|
204 | }
|
---|
205 |
|
---|
206 | // writes the given Item out to the file
|
---|
207 | // This method is not used to write out LINE items
|
---|
208 | public void writeItem(Item item) throws IOException {
|
---|
209 | if (_writer == null)
|
---|
210 | return;
|
---|
211 |
|
---|
212 | writeItemAlways(item);
|
---|
213 | }
|
---|
214 |
|
---|
215 | protected void writeItemAlways(Item item) throws IOException {
|
---|
216 | if (item.isLineEnd())
|
---|
217 | writeLineEnd(item);
|
---|
218 | else if (!(item instanceof Line))
|
---|
219 | writeClass(item);
|
---|
220 | // lines are saved at the end of the file
|
---|
221 | // So dont worry about them in here
|
---|
222 | // else
|
---|
223 | // System.out.println("Unknown Item: " + item.getID() + " (" + item.getClass().getName() + ")");
|
---|
224 |
|
---|
225 | writeLine("");
|
---|
226 | }
|
---|
227 |
|
---|
228 | protected List<Item> _lineEnds = new LinkedList<Item>();
|
---|
229 |
|
---|
230 | // Writes out a LineEnd to the file
|
---|
231 | protected void writeLineEnd(Item point) throws IOException {
|
---|
232 | _lineEnds.add(point);
|
---|
233 | writeClass(point);
|
---|
234 | }
|
---|
235 |
|
---|
236 | // writes out all lines to the file
|
---|
237 | protected void writeLineData() throws IOException {
|
---|
238 | List<Line> seen = new LinkedList<Line>();
|
---|
239 |
|
---|
240 | // loop through all points stored
|
---|
241 | for (int i = 0; i < _lineEnds.size(); i++) {
|
---|
242 | List<Line> lines = _lineEnds.get(i).getLines();
|
---|
243 |
|
---|
244 | // if this point is part of one or more lines
|
---|
245 | if (lines != null && lines.size() > 0) {
|
---|
246 | for (Line line : lines) {
|
---|
247 |
|
---|
248 | // Brook: widget edges are not saved
|
---|
249 | if (line instanceof WidgetEdge) {
|
---|
250 | seen.add(line);
|
---|
251 | continue;
|
---|
252 | }
|
---|
253 |
|
---|
254 | // only output new lines that have not yet been output
|
---|
255 | if (!seen.contains(line)) {
|
---|
256 | writeLine("L", line.getID() + " " + line.getLineType());
|
---|
257 | writeLine("s", line.getLineEnds());
|
---|
258 | writeLine("");
|
---|
259 |
|
---|
260 | // add this line to the list of lines that have been
|
---|
261 | // seen
|
---|
262 | seen.add(line);
|
---|
263 | }
|
---|
264 | }
|
---|
265 | }
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | // writes out any constraints to the file
|
---|
270 | protected void writeConstraintData() throws IOException {
|
---|
271 | // outputs any constraints the points have
|
---|
272 |
|
---|
273 | // loop through all the points
|
---|
274 | while (_lineEnds.size() > 0) {
|
---|
275 | Item i = _lineEnds.get(0);
|
---|
276 |
|
---|
277 | // if there are any constraints to write
|
---|
278 | if (i.getConstraints() != null) {
|
---|
279 | List<Constraint> constraints = i.getConstraints();
|
---|
280 |
|
---|
281 | // do not write constraints that have already been
|
---|
282 | // written
|
---|
283 | for (Constraint c : constraints) {
|
---|
284 | if (_lineEnds.contains(c.getStart())
|
---|
285 | && _lineEnds.contains(c.getEnd())) {
|
---|
286 | writeLine("C", c.getID() + " " + c.getType());
|
---|
287 | writeLine("s", c.getLineEnds());
|
---|
288 | writeLine("");
|
---|
289 | }
|
---|
290 |
|
---|
291 | }
|
---|
292 | }
|
---|
293 | // remove the point from the list
|
---|
294 | _lineEnds.remove(0);
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | @Override
|
---|
299 | protected String finaliseFrame() throws IOException {
|
---|
300 | _writer.flush();
|
---|
301 | _writer.close();
|
---|
302 | _writer = null;
|
---|
303 |
|
---|
304 | return "Frame successfully written to " + _filename;
|
---|
305 | }
|
---|
306 |
|
---|
307 | protected void writeClass(Item toWrite) throws IOException {
|
---|
308 | writeTags(toWrite, new Object[] {}, getItemCharTags());
|
---|
309 | writeTags(toWrite, new Object[] {}, getItemStrTags());
|
---|
310 | }
|
---|
311 |
|
---|
312 | protected <T> void writeTags(Item toWrite, Object[] param, LinkedHashMap<T, Method> tags) {
|
---|
313 | Iterator<T> it = tags.keySet().iterator();
|
---|
314 | while (it.hasNext()) {
|
---|
315 | T tag = it.next();
|
---|
316 | writeTag(toWrite, param, tags, tag);
|
---|
317 | }
|
---|
318 | }
|
---|
319 |
|
---|
320 | protected <T> void writeTag(Item toWrite, Object[] param, LinkedHashMap<T, Method> tags, T tag) {
|
---|
321 | Method toRun = tags.get(tag);
|
---|
322 | Class<?> declarer = toRun.getDeclaringClass();
|
---|
323 | if (declarer.isAssignableFrom(toWrite.getClass())) {
|
---|
324 | try {
|
---|
325 | Object o = toRun.invoke(toWrite, param);
|
---|
326 | o = Conversion.ConvertToExpeditee(toRun, o);
|
---|
327 | if (o != null) {
|
---|
328 | if (o instanceof List) {
|
---|
329 | for (Object line : (List) o) {
|
---|
330 | writeLine(tag.toString(), line.toString());
|
---|
331 | }
|
---|
332 | } else
|
---|
333 | writeLine(tag.toString(), o.toString());
|
---|
334 | }
|
---|
335 | } catch (Exception e) {
|
---|
336 | e.printStackTrace();
|
---|
337 | }
|
---|
338 | }
|
---|
339 | }
|
---|
340 |
|
---|
341 | /**
|
---|
342 | * Gets a string representation of the frame file contents.
|
---|
343 | */
|
---|
344 | public String getFileContents() {
|
---|
345 | return _stringWriter.toString();
|
---|
346 | }
|
---|
347 | }
|
---|