source: trunk/src/org/expeditee/io/DefaultFrameWriter.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: 10.8 KB
Line 
1/**
2 * DefaultFrameWriter.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.Collection;
30import java.util.HashSet;
31import java.util.LinkedHashMap;
32
33import org.expeditee.agents.WriteTree;
34import org.expeditee.gio.EcosystemManager;
35import org.expeditee.gui.Frame;
36import org.expeditee.gui.FrameIO;
37import org.expeditee.items.Item;
38import org.expeditee.items.Line;
39import org.expeditee.items.Text;
40
41public abstract class DefaultFrameWriter extends ItemWriter implements
42 FrameWriter {
43
44 protected String _filename = null;
45
46 protected String _output = null;
47
48 protected ProxyWriter _writer = null;
49
50 protected String _format = "";
51
52 protected boolean _running = true;
53
54 protected boolean _stop = false;
55
56 // keep track of methods that are put on the same line
57 private static LinkedHashMap<Character, Method> _ItemTags = null;
58 // IMPORTANT: keys in _ItemTagsExt must start with underscore as their first character
59 private static LinkedHashMap<String, Method> _ItemTagsExt = null;
60
61 protected static LinkedHashMap<Character, Method> _FrameTags = null;
62
63 public DefaultFrameWriter() {
64
65 if (_ItemTags != null && _FrameTags != null)
66 return;
67
68 _ItemTags = new LinkedHashMap<Character, Method>();
69 _ItemTagsExt = new LinkedHashMap<String, Method>();
70 _FrameTags = new LinkedHashMap<Character, Method>();
71
72 try {
73 _FrameTags.put('V', Frame.class.getMethod("getVersion"));
74 _FrameTags.put('p', Frame.class.getMethod("getPermission"));
75 _FrameTags.put('U', Frame.class.getMethod("getOwner"));
76 _FrameTags.put('D', Frame.class.getMethod("getDateCreated"));
77 _FrameTags.put('M', Frame.class.getMethod("getLastModifyUser"));
78 _FrameTags.put('d', Frame.class.getMethod("getLastModifyDate"));
79 _FrameTags.put('F', Frame.class.getMethod("getFrozenDate"));
80 _FrameTags.put('O', Frame.class.getMethod("getForegroundColor"));
81 _FrameTags.put('B', Frame.class.getMethod("getBackgroundColor"));
82 _FrameTags.put('K', Frame.class.getMethod("getEncryptionLabel"));
83 _FrameTags.put('T', Frame.class.getMethod("getData"));
84 _FrameTags.put('E', Frame.class.getMethod("getEncryptionPermission"));
85 _FrameTags.put('G', Frame.class.getMethod("getGroup"));
86
87 // Note: As of 26/11/18 there are no unused letter item tags. Use other characters.
88 _ItemTags.put('S', Item.class.getMethod("getTypeAndID"));
89 _ItemTags.put('s', Item.class.getMethod("getDateCreated"));
90 _ItemTags.put('d', Item.class.getMethod("getColor"));
91 _ItemTags.put('G', Item.class.getMethod("getBackgroundColor"));
92 _ItemTags.put('K', Item.class.getMethod("getBorderColor"));
93
94 _ItemTags.put('P', Item.class.getMethod("getPosition"));
95 _ItemTags.put('T', Text.class.getMethod("getText"));
96 _ItemTags.put('F', Item.class.getMethod("getLink"));
97 _ItemTags.put('X', Item.class.getMethod("getAction"));
98 _ItemTags.put('x', Item.class.getMethod("getActionMark"));
99 _ItemTags.put('U', Item.class.getMethod("getActionCursorEnter"));
100 _ItemTags.put('V', Item.class.getMethod("getActionCursorLeave"));
101 _ItemTags.put('W', Item.class.getMethod("getActionEnterFrame"));
102 _ItemTags.put('Y', Item.class.getMethod("getActionLeaveFrame"));
103 _ItemTags.put('D', Item.class.getMethod("getData"));
104 _ItemTags.put('u', Item.class.getMethod("getHighlight"));
105 _ItemTags.put('e', Item.class.getMethod("getFillColor"));
106 _ItemTags.put('E', Item.class.getMethod("getGradientColor"));
107 _ItemTags.put('Q', Item.class.getMethod("getGradientAngle"));
108
109 _ItemTags.put('R', Item.class.getMethod("getAnchorLeft"));
110 _ItemTags.put('H', Item.class.getMethod("getAnchorRight"));
111 _ItemTags.put('N', Item.class.getMethod("getAnchorTop"));
112 _ItemTags.put('I', Item.class.getMethod("getAnchorBottom"));
113
114 _ItemTags.put('i', Item.class.getMethod("getFillPattern"));
115 _ItemTags.put('o', Item.class.getMethod("getOwner"));
116 _ItemTags.put('n', Item.class.getMethod("getLinkMark"));
117 _ItemTags.put('q', Item.class.getMethod("getLinkFrameset"));
118 _ItemTags.put('y', Item.class.getMethod("getLinkTemplate"));
119 _ItemTags.put('g', Item.class.getMethod("getLinePattern"));
120
121 _ItemTags.put('j', Item.class.getMethod("getArrow"));
122
123 _ItemTags.put('v', Item.class.getMethod("getDotType"));
124 _ItemTags.put('z', Item.class.getMethod("getFilled"));
125
126 _ItemTags.put('f', Text.class.getMethod("getFont"));
127 _ItemTags.put('t', Text.class.getMethod("getSpacing"));
128
129 _ItemTags.put('O', Text.class.getMethod("getMask"));
130
131 // TODO set a boolean flag to indicate that the text is a formula
132 // Store the formula in the text property NOT the answer
133 _ItemTags.put('J', Item.class.getMethod("getFormula"));
134
135 _ItemTags.put('a', Text.class.getMethod("getWordSpacing"));
136 _ItemTags.put('b', Text.class.getMethod("getLetterSpacing"));
137 _ItemTags.put('m', Text.class.getMethod("getInitialSpacing"));
138 _ItemTags.put('w', Text.class.getMethod("getWidthToSave"));
139 _ItemTags.put('M', Text.class.getMethod("getMinWidthToSave"));
140 _ItemTags.put('k', Text.class.getMethod("getJustification"));
141 _ItemTags.put('r', Text.class.getMethod("getAutoWrapToSave"));
142
143 _ItemTags.put('h', Item.class.getMethod("getThickness"));
144 _ItemTags.put('l', Item.class.getMethod("getLineIDs"));
145 _ItemTags.put('c', Item.class.getMethod("getConstraintIDs"));
146
147 _ItemTags.put('A', Item.class.getMethod("getTooltip"));
148 _ItemTags.put('B', Item.class.getMethod("getLinkHistory"));
149
150 _ItemTags.put('p', Item.class.getMethod("getPermission"));
151
152 _ItemTags.put('[', Item.class.getMethod("getMagnetizedItemLeft"));
153 _ItemTags.put(']', Item.class.getMethod("getMagnetizedItemRight"));
154 _ItemTags.put('^', Item.class.getMethod("getMagnetizedItemTop"));
155 _ItemTags.put('/', Item.class.getMethod("getMagnetizedItemBottom"));
156
157 _ItemTagsExt.put("_el", Item.class.getMethod("getEncryptionLabel"));
158
159 _ItemTagsExt.put("_ph", Text.class.getMethod("getPlaceholder"));
160 _ItemTagsExt.put("_sl", Text.class.getMethod("isSingleLineOnly"));
161 _ItemTagsExt.put("_ti", Text.class.getMethod("getTabIndex"));
162 } catch (Exception e) {
163 e.printStackTrace();
164 }
165 }
166
167 public void setOutputLocation(String filename) {
168 _filename = filename;
169 }
170
171 public String writeFrame(Frame toWrite) throws IOException {
172 return writeFrame(toWrite, null);
173 }
174
175 public String writeFrame(Frame toWrite, Writer writer) throws IOException {
176 try {
177 initialise(toWrite, writer);
178
179 outputFrame(toWrite);
180
181 _running = false;
182 return finaliseFrame();
183 } catch (IOException ioe) {
184 _running = false;
185 throw ioe;
186 }
187
188 }
189
190 /**
191 * Called before writing out the body items of each frame.
192 *
193 * @param starting
194 * the name of the frame currently being written out.
195 * @throws IOException
196 */
197 protected void writeStartFrame(Frame starting) throws IOException {
198 if (starting.getTitleItem() != null) {
199 if (starting.getTitleItem().isAnnotation())
200 this.writeAnnotationTitle(starting.getTitleItem());
201 else
202 this.writeTitle(starting.getTitleItem(), starting.getItems());
203 }
204 }
205
206 /**
207 * Called after writing out the body items of each frame.
208 *
209 * @param ending
210 * the name of the frame currently being written out.
211 * @throws IOException
212 */
213 protected void writeEndFrame(Frame ending) throws IOException {
214 }
215
216 protected final void initialise(Frame start) throws IOException {
217 initialise(start, null);
218 }
219
220 protected void initialise(Frame start, Writer writer) throws IOException {
221 if (_filename == null)
222 _filename = FrameIO.EXPORTS_PATH + getFileName(start) + _format;
223
224 if (writer != null) {
225 _writer = new ProxyWriter(writer);
226 _output = writer.toString();
227 } else if (_filename.equalsIgnoreCase(WriteTree.CLIPBOARD)) {
228 _writer = new ProxyWriter(true);
229 _output = WriteTree.CLIPBOARD;
230 } else {
231 if (_filename.contains(File.separator)) {
232 String extTest = _filename.substring(_filename
233 .lastIndexOf(File.separator) + 1);
234 if (!extTest.contains("."))
235 _filename += _format;
236 } else if (!_filename.contains("."))
237 _filename += _format;
238
239 if (!_filename.contains(File.separator))
240 _filename = FrameIO.EXPORTS_PATH + _filename;
241
242 File test = new File(_filename);
243
244 File parent = test.getParentFile();
245 if (parent != null && !parent.exists()) {
246 parent.mkdirs();
247 }
248
249 // Open an Output Stream Writer to set encoding
250 OutputStream fout = new FileOutputStream(_filename);
251 OutputStream bout = new BufferedOutputStream(fout);
252 Writer out = new OutputStreamWriter(bout, "UTF-8");
253
254 _writer = new ProxyWriter(out);
255 _output = _filename;
256 }
257 }
258
259 protected String getFileName(Frame start) {
260 return getValidFilename(start.getTitle());
261 }
262
263 public static String getValidFilename(String filename) {
264 return filename.replaceAll("[ \\.]", "_");
265 }
266
267 protected String finalise() throws IOException {
268 try {
269 _writer.flush();
270 _writer.close();
271 } catch (IOException ioe) {
272 } finally {
273 _writer.close();
274 }
275 String errorMessage = EcosystemManager.getMiscManager().open(_output);
276 if (errorMessage != null) System.err.println(errorMessage);
277 return " exported to " + _output;
278 }
279
280 protected String finaliseFrame() throws IOException {
281 return "Frame" + finalise();
282 }
283
284 protected void outputFrame(Frame toWrite) throws IOException {
285 writeStartFrame(toWrite);
286
287 Collection<Item> done = new HashSet<Item>();
288
289 for (Item i : getItemsToWrite(toWrite)) {
290 if (_stop) {
291 return;
292 }
293
294 if (i instanceof Line) {
295 if (done.contains(i)) {
296 continue;
297 }
298 done.addAll(i.getAllConnected());
299 }
300 writeItem(i);
301 }
302 writeEndFrame(toWrite);
303 }
304
305 protected Collection<Item> getItemsToWrite(Frame toWrite) {
306 return toWrite.getItemsToSave();
307 }
308
309 public boolean isRunning() {
310 return _running;
311 }
312
313 public void stop() {
314 _stop = true;
315 }
316
317 public String getFileContents() {
318 return "Not Supported";
319 }
320
321 /**
322 * @return the _ItemTags
323 */
324 public static LinkedHashMap<Character, Method> getItemTags() {
325 return _ItemTags;
326 }
327
328 /**
329 * @return the _ItemTagsExt
330 */
331 public static LinkedHashMap<String, Method> getItemTagsExt() {
332 return _ItemTagsExt;
333 }
334
335}
Note: See TracBrowser for help on using the repository browser.