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

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