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

Last change on this file since 919 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

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