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

Last change on this file since 1408 was 1408, checked in by bln4, 5 years ago

Implementation of the surrogate system.
When you set an item to have a encryption label, a surrogate for that item is generated.
The newly updated EncryptedExpReader/Writer has been updated to maintain the connection between the item and its surrogate.

Coming up next:
Surrogate mode. The ability to simulate viewing and editing an encrypted frame with a limited set of labels.

File size: 9.0 KB
RevLine 
[919]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
[4]19package org.expeditee.io;
20
[348]21import java.io.BufferedOutputStream;
[4]22import java.io.File;
[348]23import java.io.FileOutputStream;
[4]24import java.io.IOException;
[348]25import java.io.OutputStream;
26import java.io.OutputStreamWriter;
[306]27import java.io.Writer;
[4]28import java.lang.reflect.Method;
29import java.util.Iterator;
[1408]30import java.util.LinkedHashMap;
[4]31import java.util.LinkedList;
32import java.util.List;
33
[80]34import org.expeditee.agents.DefaultAgent;
[4]35import org.expeditee.gui.Frame;
36import org.expeditee.items.Constraint;
37import org.expeditee.items.Item;
38import org.expeditee.items.Line;
[198]39import org.expeditee.items.widgets.WidgetEdge;
[4]40import org.expeditee.stats.SessionStats;
41
42/**
[80]43 * Writes a Frame out to a Expeditee format file.
[4]44 *
45 * @author jdm18
46 *
47 */
48public class ExpWriter extends DefaultFrameWriter {
49
[1200]50 protected ProxyWriter _writer = null;
[4]51
[504]52 protected StringBuilder _stringWriter = null;
[4]53
[1323]54 protected String _framename;
55
[1408]56 protected static final char TERMINATOR = 'Z';
[4]57
58 public ExpWriter() {
[67]59 super();
[4]60 }
61
[306]62 @Override
63 public void initialise(Frame start, Writer writer) throws IOException {
[1323]64 _framename = start.getName();
[4]65 String name = start.getFramesetName().toLowerCase();
66
67 if (_filename == null)
[298]68 _filename = start.getPath() + name + File.separator
[80]69 + start.getNumber() + ExpReader.EXTENTION;
[4]70
71 _stringWriter = new StringBuilder();
[348]72
73 if (writer != null) {
[306]74 _writer = new ProxyWriter(writer);
75 _output = writer.toString();
[348]76 } else if (_filename.equalsIgnoreCase(DefaultAgent.CLIPBOARD)) {
[4]77 _writer = new ProxyWriter(true);
[80]78 _filename = DefaultAgent.CLIPBOARD;
[348]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
[67]88 try {
[737]89 _FrameTags.remove('A');
[1408]90 getItemTags().put('S', Item.class.getMethod("getTypeAndID",
[348]91 new Class[] {}));
[67]92 } catch (Exception e) {
93
94 }
[4]95 }
96
97 /**
[348]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.
[4]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;
[1200]110
111 preOutputFrame();
[4]112
113 writeHeader(frame);
114
[108]115 // write each item in the frame
116 for (Item i : frame.getItemsToSave()) {
[348]117 assert (!(i instanceof Line));
[108]118 writeItem(i);
[4]119 }
[1200]120
121 for (final Item i: frame.getBodyItemsWithInsufficientPermissions()) {
122 assert (!(i instanceof Line));
123 writeItem(i);
124 }
[50]125
[4]126 // write any lines or constraints
127 writeTerminator();
128 writeLineData();
129 writeTerminator();
130 writeConstraintData();
[105]131 writeTerminator();
132 writeLine(SessionStats.getFrameEventList(frame));
[4]133 }
134
[1200]135 protected void preOutputFrame() {
136 }
137
[1408]138 protected void writeHeader(Frame toWrite) throws IOException {
[737]139 Iterator<Character> it = _FrameTags.keySet().iterator();
[4]140 Object[] param = {};
[50]141
[4]142 while (it.hasNext()) {
[737]143 Character tag = it.next();
[4]144 try {
145 Object o = _FrameTags.get(tag).invoke(toWrite, param);
[80]146 o = Conversion.ConvertToExpeditee(_FrameTags.get(tag), o);
[4]147 if (o != null) {
[1326]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 }
[4]155 }
156 } catch (Exception e) {
157 e.printStackTrace();
158 }
159 }
160
161 writeTerminator();
162 }
[1326]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// }
[4]185
[1408]186 protected void writeLine(String tag, String line) throws IOException {
[4]187 writeLine(tag + " " + line);
188 }
189
[504]190 protected void writeTerminator() throws IOException {
[4]191 writeLine(TERMINATOR + "\n");
192 }
193
194 // writes the given line out to the file
[504]195 protected void writeLine(String line) throws IOException {
[4]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 }
[348]205
[4]206 // writes the given Item out to the file
[50]207 // This method is not used to write out LINE items
[4]208 public void writeItem(Item item) throws IOException {
209 if (_writer == null)
210 return;
211
[504]212 writeItemAlways(item);
213 }
214
215 protected void writeItemAlways(Item item) throws IOException {
[58]216 if (item.isLineEnd())
[108]217 writeLineEnd(item);
[4]218 else if (!(item instanceof Line))
219 writeClass(item);
220 // lines are saved at the end of the file
[50]221 // So dont worry about them in here
[737]222// else
223// System.out.println("Unknown Item: " + item.getID() + " (" + item.getClass().getName() + ")");
[50]224
[4]225 writeLine("");
226 }
227
[1408]228 protected List<Item> _lineEnds = new LinkedList<Item>();
[4]229
[108]230 // Writes out a LineEnd to the file
231 protected void writeLineEnd(Item point) throws IOException {
232 _lineEnds.add(point);
[4]233 writeClass(point);
234 }
235
236 // writes out all lines to the file
[504]237 protected void writeLineData() throws IOException {
[4]238 List<Line> seen = new LinkedList<Line>();
239
240 // loop through all points stored
[108]241 for (int i = 0; i < _lineEnds.size(); i++) {
242 List<Line> lines = _lineEnds.get(i).getLines();
[4]243
244 // if this point is part of one or more lines
245 if (lines != null && lines.size() > 0) {
246 for (Line line : lines) {
[50]247
[10]248 // Brook: widget edges are not saved
249 if (line instanceof WidgetEdge) {
250 seen.add(line);
251 continue;
252 }
[4]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
[504]270 protected void writeConstraintData() throws IOException {
[4]271 // outputs any constraints the points have
272
273 // loop through all the points
[108]274 while (_lineEnds.size() > 0) {
275 Item i = _lineEnds.get(0);
[4]276
[50]277 // if there are any constraints to write
278 if (i.getConstraints() != null) {
279 List<Constraint> constraints = i.getConstraints();
[4]280
[50]281 // do not write constraints that have already been
282 // written
283 for (Constraint c : constraints) {
[108]284 if (_lineEnds.contains(c.getStart())
285 && _lineEnds.contains(c.getEnd())) {
[50]286 writeLine("C", c.getID() + " " + c.getType());
287 writeLine("s", c.getLineEnds());
288 writeLine("");
289 }
[4]290
291 }
292 }
293 // remove the point from the list
[108]294 _lineEnds.remove(0);
[4]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
[1408]307 protected void writeClass(Item toWrite) throws IOException {
308 writeTags(toWrite, new Object[] {}, getItemTags());
309 writeTags(toWrite, new Object[] {}, getItemTagsExt());
310 }
[4]311
[1408]312 protected <T> void writeTags(Item toWrite, Object[] param, LinkedHashMap<T, Method> tags) {
313 Iterator<T> it = tags.keySet().iterator();
[4]314 while (it.hasNext()) {
[1408]315 T tag = it.next();
316 writeTag(toWrite, param, tags, tag);
[4]317 }
[1408]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());
[1321]334 }
[1408]335 } catch (Exception e) {
336 e.printStackTrace();
[1321]337 }
338 }
[4]339 }
340
341 /**
342 * Gets a string representation of the frame file contents.
343 */
344 public String getFileContents() {
345 return _stringWriter.toString();
346 }
347}
Note: See TracBrowser for help on using the repository browser.