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

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

Created a unioned list for item properties in DefaultFrameWriter. Used it in Item.

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