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