source: trunk/src/org/expeditee/io/DefaultFrameWriter.java@ 1413

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

Changed surrogates to work the way discussed with David. EncryptedExpReader/Writer updated to work with this.

File size: 18.3 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 protected static final char MAGNETIZED_ITEM_BOTTOM = '/';
44 protected static final char MAGNETIZED_ITEM_TOP = '^';
45 protected static final char MAGNETIZED_ITEM_RIGHT = ']';
46 protected static final char MAGNETIZED_ITEM_LEFT = '[';
47 protected static final char PERMISSION = 'p';
48 protected static final char LINK_HISTORY = 'B';
49 protected static final char TOOLTIP = 'A';
50 protected static final char CONSTRAINT_IDS = 'c';
51 protected static final char LINE_IDS = 'l';
52 protected static final char THICKNESS = 'h';
53 protected static final char AUTO_WRAP_TO_SAVE = 'r';
54 protected static final char JUSTIFICATION = 'k';
55 protected static final char MIN_WIDTH_TO_SAVE = 'M';
56 protected static final char WIDTH_TO_SAVE = 'w';
57 protected static final char INITIAL_SPACING = 'm';
58 protected static final char LETTER_SPACING = 'b';
59 protected static final char WORD_SPACING = 'a';
60 protected static final char FORMULA = 'J';
61 protected static final char MASK = 'O';
62 protected static final char SPACING = 't';
63 protected static final char FONT = 'f';
64 protected static final char FILLED = 'z';
65 protected static final char DOT_TYPE = 'v';
66 protected static final char ARROW = 'j';
67 protected static final char LINE_PATTERN = 'g';
68 protected static final char LINK_TEMPLATE = 'y';
69 protected static final char LINK_FRAMESET = 'q';
70 protected static final char LINK_MARK = 'n';
71 protected static final char OWNER = 'o';
72 protected static final char FILL_PATTERN = 'i';
73 protected static final char ANCHOR_BOTTOM = 'I';
74 protected static final char ANCHOR_TOP = 'N';
75 protected static final char ANCHOR_RIGHT = 'H';
76 protected static final char ANCHOR_LEFT = 'R';
77 protected static final char GRADIENT_ANGLE = 'Q';
78 protected static final char GRADIENT_COLOR = 'E';
79 protected static final char FILL_COLOR = 'e';
80 protected static final char HIGHLIGHT = 'u';
81 protected static final char DATA = 'D';
82 protected static final char ACTION_LEAVE_FRAME = 'Y';
83 protected static final char ACTION_ENTER_FRAME = 'W';
84 protected static final char ACTION_CURSOR_LEAVE = 'V';
85 protected static final char ACTION_CURSOR_ENTER = 'U';
86 protected static final char ACTION_MARK = 'x';
87 protected static final char ACTION = 'X';
88 protected static final char LINK = 'F';
89 protected static final char TEXT = 'T';
90 protected static final char POSITION = 'P';
91 protected static final char BORDER_COLOR = 'K';
92 protected static final char BACKGROUND_COLOR = 'G';
93 protected static final char COLOR = 'd';
94 protected static final char DATE_CREATED = 's';
95 protected static final char TYPE_AND_ID = 'S';
96
97 public static final String TAB_INDEX_STR = "_ti";
98 public static final String SINGLE_LINE_ONLY_STR = "_sl";
99 public static final String PLACEHOLDER_STR = "_ph";
100 public static final String ENCRYPTION_LABEL_STR = "_el";
101 public static final String MAGNETIZED_ITEM_BOTTOM_STR = MAGNETIZED_ITEM_BOTTOM + "";
102 public static final String MAGNETIZED_ITEM_TOP_STR = MAGNETIZED_ITEM_TOP + "";
103 public static final String MAGNETIZED_ITEM_RIGHT_STR = MAGNETIZED_ITEM_RIGHT + "";
104 public static final String MAGNETIZED_ITEM_LEFT_STR = MAGNETIZED_ITEM_LEFT + "";
105 public static final String PERMISSION_STR = PERMISSION + "";
106 public static final String LINK_HISTORY_STR = LINK_HISTORY + "";
107 public static final String TOOLTIP_STR = TOOLTIP + "";
108 public static final String CONSTRAINT_IDS_STR = CONSTRAINT_IDS + "";
109 public static final String LINE_IDS_STR = LINE_IDS + "";
110 public static final String THICKNESS_STR = THICKNESS + "";
111 public static final String AUTO_WRAP_TO_SAVE_STR = AUTO_WRAP_TO_SAVE + "";
112 public static final String JUSTIFICATION_STR = JUSTIFICATION + "";
113 public static final String MIN_WIDTH_TO_SAVE_STR = MIN_WIDTH_TO_SAVE + "";
114 public static final String WIDTH_TO_SAVE_STR = WIDTH_TO_SAVE + "";
115 public static final String INITIAL_SPACING_STR = INITIAL_SPACING + "";
116 public static final String LETTER_SPACING_STR = LETTER_SPACING + "";
117 public static final String WORD_SPACING_STR = WORD_SPACING + "";
118 public static final String FORMULA_STR = FORMULA + "";
119 public static final String MASK_STR = MASK + "";
120 public static final String SPACING_STR = SPACING + "";
121 public static final String FONT_STR = FONT + "";
122 public static final String FILLED_STR = FILLED + "";
123 public static final String DOT_TYPE_STR = DOT_TYPE + "";
124 public static final String ARROW_STR = ARROW + "";
125 public static final String LINE_PATTERN_STR = LINE_PATTERN + "";
126 public static final String LINK_TEMPLATE_STR = LINK_TEMPLATE + "";
127 public static final String LINK_FRAMESET_STR = LINK_FRAMESET + "";
128 public static final String LINK_MARK_STR = LINK_MARK + "";
129 public static final String OWNER_STR = OWNER + "";
130 public static final String FILL_PATTERN_STR = FILL_PATTERN + "";
131 public static final String ANCHOR_BOTTOM_STR = ANCHOR_BOTTOM + "";
132 public static final String ANCHOR_TOP_STR = ANCHOR_TOP + "";
133 public static final String ANCHOR_RIGHT_STR = ANCHOR_RIGHT + "";
134 public static final String ANCHOR_LEFT_STR = ANCHOR_LEFT + "";
135 public static final String GRADIENT_ANGLE_STR = GRADIENT_ANGLE + "";
136 public static final String GRADIENT_COLOR_STR = GRADIENT_COLOR + "";
137 public static final String FILL_COLOR_STR = FILL_COLOR + "";
138 public static final String HIGHLIGHT_STR = HIGHLIGHT + "";
139 public static final String DATA_STR = DATA + "";
140 public static final String ACTION_LEAVE_FRAME_STR = ACTION_LEAVE_FRAME + "";
141 public static final String ACTION_ENTER_FRAME_STR = ACTION_ENTER_FRAME + "";
142 public static final String ACTION_CURSOR_LEAVE_STR = ACTION_CURSOR_LEAVE + "";
143 public static final String ACTION_CURSOR_ENTER_STR = ACTION_CURSOR_ENTER + "";
144 public static final String ACTION_MARK_STR = ACTION_MARK + "";
145 public static final String ACTION_STR = ACTION + "";
146 public static final String LINK_STR = LINK + "";
147 public static final String TEXT_STR = TEXT + "";
148 public static final String POSITION_STR = POSITION + "";
149 public static final String BORDER_COLOR_STR = BORDER_COLOR + "";
150 public static final String BACKGROUND_COLOR_STR = BACKGROUND_COLOR + "";
151 public static final String COLOR_STR = COLOR + "";
152 public static final String DATE_CREATED_STR = DATE_CREATED + "";
153 public static final String TYPE_AND_ID_STR = TYPE_AND_ID + "";
154
155 protected String _filename = null;
156
157 protected String _output = null;
158
159 protected ProxyWriter _writer = null;
160
161 protected String _format = "";
162
163 protected boolean _running = true;
164
165 protected boolean _stop = false;
166
167 // keep track of methods that are put on the same line
168 private static LinkedHashMap<Character, Method> _ItemTags = new LinkedHashMap<Character, Method>();
169 // IMPORTANT: keys in _ItemTagsExt must start with underscore as their first character
170 private static LinkedHashMap<String, Method> _ItemTagsExt = new LinkedHashMap<String, Method>();
171
172 protected static LinkedHashMap<Character, Method> _FrameTags = new LinkedHashMap<Character, Method>();
173
174 {
175 try {
176 _FrameTags.put('V', Frame.class.getMethod("getVersion"));
177 _FrameTags.put('p', Frame.class.getMethod("getPermission"));
178 _FrameTags.put('U', Frame.class.getMethod("getOwner"));
179 _FrameTags.put('D', Frame.class.getMethod("getDateCreated"));
180 _FrameTags.put('M', Frame.class.getMethod("getLastModifyUser"));
181 _FrameTags.put('d', Frame.class.getMethod("getLastModifyDate"));
182 _FrameTags.put('F', Frame.class.getMethod("getFrozenDate"));
183 _FrameTags.put('O', Frame.class.getMethod("getForegroundColor"));
184 _FrameTags.put('B', Frame.class.getMethod("getBackgroundColor"));
185 _FrameTags.put('K', Frame.class.getMethod("getEncryptionLabel"));
186 _FrameTags.put('T', Frame.class.getMethod("getData"));
187 _FrameTags.put('E', Frame.class.getMethod("getEncryptionPermission"));
188 _FrameTags.put('G', Frame.class.getMethod("getGroup"));
189
190 // Note: As of 26/11/18 there are no unused letter item tags. Use other characters.
191 _ItemTags.put(DefaultFrameWriter.TYPE_AND_ID, Item.class.getMethod("getTypeAndID"));
192 _ItemTags.put(DefaultFrameWriter.DATE_CREATED, Item.class.getMethod("getDateCreated"));
193 _ItemTags.put(DefaultFrameWriter.COLOR, Item.class.getMethod("getColor"));
194 _ItemTags.put(DefaultFrameWriter.BACKGROUND_COLOR, Item.class.getMethod("getBackgroundColor"));
195 _ItemTags.put(DefaultFrameWriter.BORDER_COLOR, Item.class.getMethod("getBorderColor"));
196
197 _ItemTags.put(DefaultFrameWriter.POSITION, Item.class.getMethod("getPosition"));
198 _ItemTags.put(DefaultFrameWriter.TEXT, Text.class.getMethod("getText"));
199 _ItemTags.put(DefaultFrameWriter.LINK, Item.class.getMethod("getLink"));
200 _ItemTags.put(DefaultFrameWriter.ACTION, Item.class.getMethod("getAction"));
201 _ItemTags.put(DefaultFrameWriter.ACTION_MARK, Item.class.getMethod("getActionMark"));
202 _ItemTags.put(DefaultFrameWriter.ACTION_CURSOR_ENTER, Item.class.getMethod("getActionCursorEnter"));
203 _ItemTags.put(DefaultFrameWriter.ACTION_CURSOR_LEAVE, Item.class.getMethod("getActionCursorLeave"));
204 _ItemTags.put(DefaultFrameWriter.ACTION_ENTER_FRAME, Item.class.getMethod("getActionEnterFrame"));
205 _ItemTags.put(DefaultFrameWriter.ACTION_LEAVE_FRAME, Item.class.getMethod("getActionLeaveFrame"));
206 _ItemTags.put(DefaultFrameWriter.DATA, Item.class.getMethod("getData"));
207 _ItemTags.put(DefaultFrameWriter.HIGHLIGHT, Item.class.getMethod("getHighlight"));
208 _ItemTags.put(DefaultFrameWriter.FILL_COLOR, Item.class.getMethod("getFillColor"));
209 _ItemTags.put(DefaultFrameWriter.GRADIENT_COLOR, Item.class.getMethod("getGradientColor"));
210 _ItemTags.put(DefaultFrameWriter.GRADIENT_ANGLE, Item.class.getMethod("getGradientAngle"));
211
212 _ItemTags.put(DefaultFrameWriter.ANCHOR_LEFT, Item.class.getMethod("getAnchorLeft"));
213 _ItemTags.put(DefaultFrameWriter.ANCHOR_RIGHT, Item.class.getMethod("getAnchorRight"));
214 _ItemTags.put(DefaultFrameWriter.ANCHOR_TOP, Item.class.getMethod("getAnchorTop"));
215 _ItemTags.put(DefaultFrameWriter.ANCHOR_BOTTOM, Item.class.getMethod("getAnchorBottom"));
216
217 _ItemTags.put(DefaultFrameWriter.FILL_PATTERN, Item.class.getMethod("getFillPattern"));
218 _ItemTags.put(DefaultFrameWriter.OWNER, Item.class.getMethod("getOwner"));
219 _ItemTags.put(DefaultFrameWriter.LINK_MARK, Item.class.getMethod("getLinkMark"));
220 _ItemTags.put(DefaultFrameWriter.LINK_FRAMESET, Item.class.getMethod("getLinkFrameset"));
221 _ItemTags.put(DefaultFrameWriter.LINK_TEMPLATE, Item.class.getMethod("getLinkTemplate"));
222 _ItemTags.put(DefaultFrameWriter.LINE_PATTERN, Item.class.getMethod("getLinePattern"));
223
224 _ItemTags.put(DefaultFrameWriter.ARROW, Item.class.getMethod("getArrow"));
225
226 _ItemTags.put(DefaultFrameWriter.DOT_TYPE, Item.class.getMethod("getDotType"));
227 _ItemTags.put(DefaultFrameWriter.FILLED, Item.class.getMethod("getFilled"));
228
229 _ItemTags.put(DefaultFrameWriter.FONT, Text.class.getMethod("getFont"));
230 _ItemTags.put(DefaultFrameWriter.SPACING, Text.class.getMethod("getSpacing"));
231
232 _ItemTags.put(DefaultFrameWriter.MASK, Text.class.getMethod("getMask"));
233
234 // TODO set a boolean flag to indicate that the text is a formula
235 // Store the formula in the text property NOT the answer
236 _ItemTags.put(DefaultFrameWriter.FORMULA, Item.class.getMethod("getFormula"));
237
238 _ItemTags.put(DefaultFrameWriter.WORD_SPACING, Text.class.getMethod("getWordSpacing"));
239 _ItemTags.put(DefaultFrameWriter.LETTER_SPACING, Text.class.getMethod("getLetterSpacing"));
240 _ItemTags.put(DefaultFrameWriter.INITIAL_SPACING, Text.class.getMethod("getInitialSpacing"));
241 _ItemTags.put(DefaultFrameWriter.WIDTH_TO_SAVE, Text.class.getMethod("getWidthToSave"));
242 _ItemTags.put(DefaultFrameWriter.MIN_WIDTH_TO_SAVE, Text.class.getMethod("getMinWidthToSave"));
243 _ItemTags.put(DefaultFrameWriter.JUSTIFICATION, Text.class.getMethod("getJustification"));
244 _ItemTags.put(DefaultFrameWriter.AUTO_WRAP_TO_SAVE, Text.class.getMethod("getAutoWrapToSave"));
245
246 _ItemTags.put(DefaultFrameWriter.THICKNESS, Item.class.getMethod("getThickness"));
247 _ItemTags.put(DefaultFrameWriter.LINE_IDS, Item.class.getMethod("getLineIDs"));
248 _ItemTags.put(DefaultFrameWriter.CONSTRAINT_IDS, Item.class.getMethod("getConstraintIDs"));
249
250 _ItemTags.put(DefaultFrameWriter.TOOLTIP, Item.class.getMethod("getTooltip"));
251 _ItemTags.put(DefaultFrameWriter.LINK_HISTORY, Item.class.getMethod("getLinkHistory"));
252
253 _ItemTags.put(DefaultFrameWriter.PERMISSION, Item.class.getMethod("getPermission"));
254
255 _ItemTags.put(DefaultFrameWriter.MAGNETIZED_ITEM_LEFT, Item.class.getMethod("getMagnetizedItemLeft"));
256 _ItemTags.put(DefaultFrameWriter.MAGNETIZED_ITEM_RIGHT, Item.class.getMethod("getMagnetizedItemRight"));
257 _ItemTags.put(DefaultFrameWriter.MAGNETIZED_ITEM_TOP, Item.class.getMethod("getMagnetizedItemTop"));
258 _ItemTags.put(DefaultFrameWriter.MAGNETIZED_ITEM_BOTTOM, Item.class.getMethod("getMagnetizedItemBottom"));
259
260 _ItemTagsExt.put(DefaultFrameWriter.ENCRYPTION_LABEL_STR, Item.class.getMethod("getEncryptionLabel"));
261
262 _ItemTagsExt.put(DefaultFrameWriter.PLACEHOLDER_STR, Text.class.getMethod("getPlaceholder"));
263 _ItemTagsExt.put(DefaultFrameWriter.SINGLE_LINE_ONLY_STR, Text.class.getMethod("isSingleLineOnly"));
264 _ItemTagsExt.put(DefaultFrameWriter.TAB_INDEX_STR, Text.class.getMethod("getTabIndex"));
265 } catch (Exception e) {
266 e.printStackTrace();
267 }
268 }
269
270 public DefaultFrameWriter() {
271
272
273 }
274
275 public void setOutputLocation(String filename) {
276 _filename = filename;
277 }
278
279 public String writeFrame(Frame toWrite) throws IOException {
280 return writeFrame(toWrite, null);
281 }
282
283 public String writeFrame(Frame toWrite, Writer writer) throws IOException {
284 try {
285 initialise(toWrite, writer);
286
287 outputFrame(toWrite);
288
289 _running = false;
290 return finaliseFrame();
291 } catch (IOException ioe) {
292 _running = false;
293 throw ioe;
294 }
295
296 }
297
298 /**
299 * Called before writing out the body items of each frame.
300 *
301 * @param starting
302 * the name of the frame currently being written out.
303 * @throws IOException
304 */
305 protected void writeStartFrame(Frame starting) throws IOException {
306 if (starting.getTitleItem() != null) {
307 if (starting.getTitleItem().isAnnotation())
308 this.writeAnnotationTitle(starting.getTitleItem());
309 else
310 this.writeTitle(starting.getTitleItem(), starting.getItems());
311 }
312 }
313
314 /**
315 * Called after writing out the body items of each frame.
316 *
317 * @param ending
318 * the name of the frame currently being written out.
319 * @throws IOException
320 */
321 protected void writeEndFrame(Frame ending) throws IOException {
322 }
323
324 protected final void initialise(Frame start) throws IOException {
325 initialise(start, null);
326 }
327
328 protected void initialise(Frame start, Writer writer) throws IOException {
329 if (_filename == null)
330 _filename = FrameIO.EXPORTS_PATH + getFileName(start) + _format;
331
332 if (writer != null) {
333 _writer = new ProxyWriter(writer);
334 _output = writer.toString();
335 } else if (_filename.equalsIgnoreCase(WriteTree.CLIPBOARD)) {
336 _writer = new ProxyWriter(true);
337 _output = WriteTree.CLIPBOARD;
338 } else {
339 if (_filename.contains(File.separator)) {
340 String extTest = _filename.substring(_filename
341 .lastIndexOf(File.separator) + 1);
342 if (!extTest.contains("."))
343 _filename += _format;
344 } else if (!_filename.contains("."))
345 _filename += _format;
346
347 if (!_filename.contains(File.separator))
348 _filename = FrameIO.EXPORTS_PATH + _filename;
349
350 File test = new File(_filename);
351
352 File parent = test.getParentFile();
353 if (parent != null && !parent.exists()) {
354 parent.mkdirs();
355 }
356
357 // Open an Output Stream Writer to set encoding
358 OutputStream fout = new FileOutputStream(_filename);
359 OutputStream bout = new BufferedOutputStream(fout);
360 Writer out = new OutputStreamWriter(bout, "UTF-8");
361
362 _writer = new ProxyWriter(out);
363 _output = _filename;
364 }
365 }
366
367 protected String getFileName(Frame start) {
368 return getValidFilename(start.getTitle());
369 }
370
371 public static String getValidFilename(String filename) {
372 return filename.replaceAll("[ \\.]", "_");
373 }
374
375 protected String finalise() throws IOException {
376 try {
377 _writer.flush();
378 _writer.close();
379 } catch (IOException ioe) {
380 } finally {
381 _writer.close();
382 }
383 String errorMessage = EcosystemManager.getMiscManager().open(_output);
384 if (errorMessage != null) System.err.println(errorMessage);
385 return " exported to " + _output;
386 }
387
388 protected String finaliseFrame() throws IOException {
389 return "Frame" + finalise();
390 }
391
392 protected void outputFrame(Frame toWrite) throws IOException {
393 writeStartFrame(toWrite);
394
395 Collection<Item> done = new HashSet<Item>();
396
397 for (Item i : getItemsToWrite(toWrite)) {
398 if (_stop) {
399 return;
400 }
401
402 if (i instanceof Line) {
403 if (done.contains(i)) {
404 continue;
405 }
406 done.addAll(i.getAllConnected());
407 }
408 writeItem(i);
409 }
410 writeEndFrame(toWrite);
411 }
412
413 protected Collection<Item> getItemsToWrite(Frame toWrite) {
414 return toWrite.getItemsToSave();
415 }
416
417 public boolean isRunning() {
418 return _running;
419 }
420
421 public void stop() {
422 _stop = true;
423 }
424
425 public String getFileContents() {
426 return "Not Supported";
427 }
428
429 /**
430 * @return the _ItemTags
431 */
432 public static LinkedHashMap<Character, Method> getItemTags() {
433 return _ItemTags;
434 }
435
436 /**
437 * @return the _ItemTagsExt
438 */
439 public static LinkedHashMap<String, Method> getItemTagsExt() {
440 return _ItemTagsExt;
441 }
442
443}
Note: See TracBrowser for help on using the repository browser.