/**
* DefaultFrameWriter.java
* Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.expeditee.io;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Set;
import org.expeditee.agents.WriteTree;
import org.expeditee.gio.EcosystemManager;
import org.expeditee.gui.Frame;
import org.expeditee.gui.FrameIO;
import org.expeditee.items.Item;
import org.expeditee.items.Line;
import org.expeditee.items.Text;
public abstract class DefaultFrameWriter extends ItemWriter implements
FrameWriter {
protected static final char MAGNETIZED_ITEM_BOTTOM = '/';
protected static final char MAGNETIZED_ITEM_TOP = '^';
protected static final char MAGNETIZED_ITEM_RIGHT = ']';
protected static final char MAGNETIZED_ITEM_LEFT = '[';
protected static final char PERMISSION = 'p';
protected static final char LINK_HISTORY = 'B';
protected static final char TOOLTIP = 'A';
protected static final char CONSTRAINT_IDS = 'c';
protected static final char LINE_IDS = 'l';
protected static final char THICKNESS = 'h';
protected static final char AUTO_WRAP_TO_SAVE = 'r';
protected static final char JUSTIFICATION = 'k';
protected static final char MIN_WIDTH_TO_SAVE = 'M';
protected static final char WIDTH_TO_SAVE = 'w';
protected static final char INITIAL_SPACING = 'm';
protected static final char LETTER_SPACING = 'b';
protected static final char WORD_SPACING = 'a';
protected static final char FORMULA = 'J';
protected static final char MASK = 'O';
protected static final char SPACING = 't';
protected static final char FONT = 'f';
protected static final char FILLED = 'z';
protected static final char DOT_TYPE = 'v';
protected static final char ARROW = 'j';
protected static final char LINE_PATTERN = 'g';
protected static final char LINK_TEMPLATE = 'y';
protected static final char LINK_FRAMESET = 'q';
protected static final char LINK_MARK = 'n';
protected static final char OWNER = 'o';
protected static final char FILL_PATTERN = 'i';
protected static final char ANCHOR_BOTTOM = 'I';
protected static final char ANCHOR_TOP = 'N';
protected static final char ANCHOR_RIGHT = 'H';
protected static final char ANCHOR_LEFT = 'R';
protected static final char GRADIENT_ANGLE = 'Q';
protected static final char GRADIENT_COLOR = 'E';
protected static final char FILL_COLOR = 'e';
protected static final char HIGHLIGHT = 'u';
protected static final char DATA = 'D';
protected static final char ACTION_LEAVE_FRAME = 'Y';
protected static final char ACTION_ENTER_FRAME = 'W';
protected static final char ACTION_CURSOR_LEAVE = 'V';
protected static final char ACTION_CURSOR_ENTER = 'U';
protected static final char ACTION_MARK = 'x';
protected static final char ACTION = 'X';
protected static final char LINK = 'F';
protected static final char TEXT = 'T';
protected static final char POSITION = 'P';
protected static final char BORDER_COLOR = 'K';
protected static final char BACKGROUND_COLOR = 'G';
protected static final char COLOR = 'd';
protected static final char DATE_CREATED = 's';
protected static final char TYPE_AND_ID = 'S';
public static final String TAB_INDEX_STR = "_ti";
public static final String SINGLE_LINE_ONLY_STR = "_sl";
public static final String PLACEHOLDER_STR = "_ph";
public static final String ENCRYPTION_LABEL_STR = "_el";
public static final String ACCEPTS_ENTER = "_ae";
public static final String ANCHOR_CENTERX_STR = "_ax";
public static final String ANCHOR_CENTERY_STR = "_ay";
public static final String MAGNETIZED_ITEM_BOTTOM_STR = MAGNETIZED_ITEM_BOTTOM + "";
public static final String MAGNETIZED_ITEM_TOP_STR = MAGNETIZED_ITEM_TOP + "";
public static final String MAGNETIZED_ITEM_RIGHT_STR = MAGNETIZED_ITEM_RIGHT + "";
public static final String MAGNETIZED_ITEM_LEFT_STR = MAGNETIZED_ITEM_LEFT + "";
public static final String PERMISSION_STR = PERMISSION + "";
public static final String LINK_HISTORY_STR = LINK_HISTORY + "";
public static final String TOOLTIP_STR = TOOLTIP + "";
public static final String CONSTRAINT_IDS_STR = CONSTRAINT_IDS + "";
public static final String LINE_IDS_STR = LINE_IDS + "";
public static final String THICKNESS_STR = THICKNESS + "";
public static final String AUTO_WRAP_TO_SAVE_STR = AUTO_WRAP_TO_SAVE + "";
public static final String JUSTIFICATION_STR = JUSTIFICATION + "";
public static final String MIN_WIDTH_TO_SAVE_STR = MIN_WIDTH_TO_SAVE + "";
public static final String WIDTH_TO_SAVE_STR = WIDTH_TO_SAVE + "";
public static final String INITIAL_SPACING_STR = INITIAL_SPACING + "";
public static final String LETTER_SPACING_STR = LETTER_SPACING + "";
public static final String WORD_SPACING_STR = WORD_SPACING + "";
public static final String FORMULA_STR = FORMULA + "";
public static final String MASK_STR = MASK + "";
public static final String SPACING_STR = SPACING + "";
public static final String FONT_STR = FONT + "";
public static final String FILLED_STR = FILLED + "";
public static final String DOT_TYPE_STR = DOT_TYPE + "";
public static final String ARROW_STR = ARROW + "";
public static final String LINE_PATTERN_STR = LINE_PATTERN + "";
public static final String LINK_TEMPLATE_STR = LINK_TEMPLATE + "";
public static final String LINK_FRAMESET_STR = LINK_FRAMESET + "";
public static final String LINK_MARK_STR = LINK_MARK + "";
public static final String OWNER_STR = OWNER + "";
public static final String FILL_PATTERN_STR = FILL_PATTERN + "";
public static final String ANCHOR_BOTTOM_STR = ANCHOR_BOTTOM + "";
public static final String ANCHOR_TOP_STR = ANCHOR_TOP + "";
public static final String ANCHOR_RIGHT_STR = ANCHOR_RIGHT + "";
public static final String ANCHOR_LEFT_STR = ANCHOR_LEFT + "";
public static final String GRADIENT_ANGLE_STR = GRADIENT_ANGLE + "";
public static final String GRADIENT_COLOR_STR = GRADIENT_COLOR + "";
public static final String FILL_COLOR_STR = FILL_COLOR + "";
public static final String HIGHLIGHT_STR = HIGHLIGHT + "";
public static final String DATA_STR = DATA + "";
public static final String ACTION_LEAVE_FRAME_STR = ACTION_LEAVE_FRAME + "";
public static final String ACTION_ENTER_FRAME_STR = ACTION_ENTER_FRAME + "";
public static final String ACTION_CURSOR_LEAVE_STR = ACTION_CURSOR_LEAVE + "";
public static final String ACTION_CURSOR_ENTER_STR = ACTION_CURSOR_ENTER + "";
public static final String ACTION_MARK_STR = ACTION_MARK + "";
public static final String ACTION_STR = ACTION + "";
public static final String LINK_STR = LINK + "";
public static final String TEXT_STR = TEXT + "";
public static final String POSITION_STR = POSITION + "";
public static final String BORDER_COLOR_STR = BORDER_COLOR + "";
public static final String BACKGROUND_COLOR_STR = BACKGROUND_COLOR + "";
public static final String COLOR_STR = COLOR + "";
public static final String DATE_CREATED_STR = DATE_CREATED + "";
public static final String TYPE_AND_ID_STR = TYPE_AND_ID + "";
protected String _filename = null;
protected String _output = null;
protected ProxyWriter _writer = null;
protected String _format = "";
protected boolean _running = true;
protected boolean _stop = false;
// keep track of methods that are put on the same line
private static LinkedHashMap _ItemCharTags = new LinkedHashMap();
// IMPORTANT: keys in _ItemTagsExt must start with underscore as their first character
private static LinkedHashMap _ItemStrTags = new LinkedHashMap();
private static LinkedHashMap _ItemTags = new LinkedHashMap();
protected static LinkedHashMap _FrameTags = new LinkedHashMap();
{
try {
/// C H I J L N P Q R S W X Y Z
/// a b c f g h i j l m n o q r s t u v w x y z
// Standard frame tags
_FrameTags.put('V', Frame.class.getMethod("getVersion"));
_FrameTags.put('p', Frame.class.getMethod("getPermission"));
_FrameTags.put('U', Frame.class.getMethod("getOwner"));
_FrameTags.put('D', Frame.class.getMethod("getDateCreated"));
_FrameTags.put('M', Frame.class.getMethod("getLastModifyUser"));
_FrameTags.put('d', Frame.class.getMethod("getLastModifyDate"));
_FrameTags.put('F', Frame.class.getMethod("getFrozenDate"));
_FrameTags.put('O', Frame.class.getMethod("getForegroundColor"));
_FrameTags.put('B', Frame.class.getMethod("getBackgroundColor"));
_FrameTags.put('T', Frame.class.getMethod("getData"));
_FrameTags.put('G', Frame.class.getMethod("getGroup"));
// Encryption frame tags
_FrameTags.put('K', Frame.class.getMethod("getFrameEncryptionLabel"));
_FrameTags.put('E', Frame.class.getMethod("getFrameEncryptionPermission"));
_FrameTags.put('k', Frame.class.getMethod("getHomogeneousEncryptionLabel"));
_FrameTags.put('e', Frame.class.getMethod("getEncryptionPermission"));
_FrameTags.put('a', Frame.class.getMethod("getHetrogeneousFrameOwnerLabels"));
// Note: As of 26/11/18 there are no unused letter item tags. Use other characters.
_ItemCharTags.put(DefaultFrameWriter.TYPE_AND_ID, Item.class.getMethod("getTypeAndID"));
_ItemCharTags.put(DefaultFrameWriter.DATE_CREATED, Item.class.getMethod("getDateCreated"));
_ItemCharTags.put(DefaultFrameWriter.COLOR, Item.class.getMethod("getColor"));
_ItemCharTags.put(DefaultFrameWriter.BACKGROUND_COLOR, Item.class.getMethod("getBackgroundColor"));
_ItemCharTags.put(DefaultFrameWriter.BORDER_COLOR, Item.class.getMethod("getBorderColor"));
_ItemCharTags.put(DefaultFrameWriter.POSITION, Item.class.getMethod("getPosition"));
_ItemCharTags.put(DefaultFrameWriter.TEXT, Text.class.getMethod("getText"));
_ItemCharTags.put(DefaultFrameWriter.LINK, Item.class.getMethod("getLink"));
_ItemCharTags.put(DefaultFrameWriter.ACTION, Item.class.getMethod("getAction"));
_ItemCharTags.put(DefaultFrameWriter.ACTION_MARK, Item.class.getMethod("getActionMark"));
_ItemCharTags.put(DefaultFrameWriter.ACTION_CURSOR_ENTER, Item.class.getMethod("getActionCursorEnter"));
_ItemCharTags.put(DefaultFrameWriter.ACTION_CURSOR_LEAVE, Item.class.getMethod("getActionCursorLeave"));
_ItemCharTags.put(DefaultFrameWriter.ACTION_ENTER_FRAME, Item.class.getMethod("getActionEnterFrame"));
_ItemCharTags.put(DefaultFrameWriter.ACTION_LEAVE_FRAME, Item.class.getMethod("getActionLeaveFrame"));
_ItemCharTags.put(DefaultFrameWriter.DATA, Item.class.getMethod("getData"));
_ItemCharTags.put(DefaultFrameWriter.HIGHLIGHT, Item.class.getMethod("getHighlight"));
_ItemCharTags.put(DefaultFrameWriter.FILL_COLOR, Item.class.getMethod("getFillColor"));
_ItemCharTags.put(DefaultFrameWriter.GRADIENT_COLOR, Item.class.getMethod("getGradientColor"));
_ItemCharTags.put(DefaultFrameWriter.GRADIENT_ANGLE, Item.class.getMethod("getGradientAngle"));
_ItemCharTags.put(DefaultFrameWriter.ANCHOR_LEFT, Item.class.getMethod("getAnchorLeft"));
_ItemCharTags.put(DefaultFrameWriter.ANCHOR_RIGHT, Item.class.getMethod("getAnchorRight"));
_ItemCharTags.put(DefaultFrameWriter.ANCHOR_TOP, Item.class.getMethod("getAnchorTop"));
_ItemCharTags.put(DefaultFrameWriter.ANCHOR_BOTTOM, Item.class.getMethod("getAnchorBottom"));
_ItemCharTags.put(DefaultFrameWriter.FILL_PATTERN, Item.class.getMethod("getFillPattern"));
_ItemCharTags.put(DefaultFrameWriter.OWNER, Item.class.getMethod("getOwner"));
_ItemCharTags.put(DefaultFrameWriter.LINK_MARK, Item.class.getMethod("getLinkMark"));
_ItemCharTags.put(DefaultFrameWriter.LINK_FRAMESET, Item.class.getMethod("getLinkFrameset"));
_ItemCharTags.put(DefaultFrameWriter.LINK_TEMPLATE, Item.class.getMethod("getLinkTemplate"));
_ItemCharTags.put(DefaultFrameWriter.LINE_PATTERN, Item.class.getMethod("getLinePattern"));
_ItemCharTags.put(DefaultFrameWriter.ARROW, Item.class.getMethod("getArrow"));
_ItemCharTags.put(DefaultFrameWriter.DOT_TYPE, Item.class.getMethod("getDotType"));
_ItemCharTags.put(DefaultFrameWriter.FILLED, Item.class.getMethod("getFilled"));
_ItemCharTags.put(DefaultFrameWriter.FONT, Text.class.getMethod("getFont"));
_ItemCharTags.put(DefaultFrameWriter.SPACING, Text.class.getMethod("getSpacing"));
_ItemCharTags.put(DefaultFrameWriter.MASK, Text.class.getMethod("getMask"));
// TODO set a boolean flag to indicate that the text is a formula
// Store the formula in the text property NOT the answer
_ItemCharTags.put(DefaultFrameWriter.FORMULA, Item.class.getMethod("getFormula"));
_ItemCharTags.put(DefaultFrameWriter.WORD_SPACING, Text.class.getMethod("getWordSpacing"));
_ItemCharTags.put(DefaultFrameWriter.LETTER_SPACING, Text.class.getMethod("getLetterSpacing"));
_ItemCharTags.put(DefaultFrameWriter.INITIAL_SPACING, Text.class.getMethod("getInitialSpacing"));
_ItemCharTags.put(DefaultFrameWriter.WIDTH_TO_SAVE, Text.class.getMethod("getWidthToSave"));
_ItemCharTags.put(DefaultFrameWriter.MIN_WIDTH_TO_SAVE, Text.class.getMethod("getMinWidthToSave"));
_ItemCharTags.put(DefaultFrameWriter.JUSTIFICATION, Text.class.getMethod("getJustification"));
_ItemCharTags.put(DefaultFrameWriter.AUTO_WRAP_TO_SAVE, Text.class.getMethod("getAutoWrapToSave"));
_ItemCharTags.put(DefaultFrameWriter.THICKNESS, Item.class.getMethod("getThickness"));
_ItemCharTags.put(DefaultFrameWriter.LINE_IDS, Item.class.getMethod("getLineIDs"));
_ItemCharTags.put(DefaultFrameWriter.CONSTRAINT_IDS, Item.class.getMethod("getConstraintIDs"));
_ItemCharTags.put(DefaultFrameWriter.TOOLTIP, Item.class.getMethod("getTooltip"));
_ItemCharTags.put(DefaultFrameWriter.LINK_HISTORY, Item.class.getMethod("getLinkHistory"));
_ItemCharTags.put(DefaultFrameWriter.PERMISSION, Item.class.getMethod("getPermission"));
_ItemCharTags.put(DefaultFrameWriter.MAGNETIZED_ITEM_LEFT, Item.class.getMethod("getMagnetizedItemLeft"));
_ItemCharTags.put(DefaultFrameWriter.MAGNETIZED_ITEM_RIGHT, Item.class.getMethod("getMagnetizedItemRight"));
_ItemCharTags.put(DefaultFrameWriter.MAGNETIZED_ITEM_TOP, Item.class.getMethod("getMagnetizedItemTop"));
_ItemCharTags.put(DefaultFrameWriter.MAGNETIZED_ITEM_BOTTOM, Item.class.getMethod("getMagnetizedItemBottom"));
_ItemStrTags.put(DefaultFrameWriter.ENCRYPTION_LABEL_STR, Item.class.getMethod("getEncryptionLabel"));
_ItemStrTags.put(DefaultFrameWriter.PLACEHOLDER_STR, Text.class.getMethod("getPlaceholder"));
_ItemStrTags.put(DefaultFrameWriter.SINGLE_LINE_ONLY_STR, Text.class.getMethod("isSingleLineOnly"));
_ItemStrTags.put(DefaultFrameWriter.TAB_INDEX_STR, Text.class.getMethod("getTabIndex"));
_ItemStrTags.put(DefaultFrameWriter.ACCEPTS_ENTER, Item.class.getMethod("acceptsKeyboardEnter"));
_ItemStrTags.put(DefaultFrameWriter.ANCHOR_CENTERX_STR, Item.class.getMethod("getAnchorCenterX"));
} catch (Exception e) {
e.printStackTrace();
}
Set keySet = _ItemCharTags.keySet();
for (Character key: keySet) {
_ItemTags.put(key + "", _ItemCharTags.get(key));
}
_ItemTags.putAll(_ItemStrTags);
}
public DefaultFrameWriter() {
}
public void setOutputLocation(String filename) {
_filename = filename;
}
public String writeFrame(Frame toWrite) throws IOException {
return writeFrame(toWrite, null);
}
public String writeFrame(Frame toWrite, Writer writer) throws IOException {
try {
initialise(toWrite, writer);
outputFrame(toWrite);
_running = false;
return finaliseFrame();
} catch (IOException ioe) {
_running = false;
throw ioe;
}
}
/**
* Called before writing out the body items of each frame.
*
* @param starting
* the name of the frame currently being written out.
* @throws IOException
*/
protected void writeStartFrame(Frame starting) throws IOException {
if (starting.getTitleItem() != null) {
if (starting.getTitleItem().isAnnotation())
this.writeAnnotationTitle(starting.getTitleItem());
else
this.writeTitle(starting.getTitleItem(), starting.getSortedItems());
}
}
/**
* Called after writing out the body items of each frame.
*
* @param ending
* the name of the frame currently being written out.
* @throws IOException
*/
protected void writeEndFrame(Frame ending) throws IOException {
}
protected final void initialise(Frame start) throws IOException {
initialise(start, null);
}
protected void initialise(Frame start, Writer writer) throws IOException {
if (_filename == null)
_filename = FrameIO.EXPORTS_PATH + getFileName(start) + _format;
if (writer != null) {
_writer = new ProxyWriter(writer);
_output = writer.toString();
} else if (_filename.equalsIgnoreCase(WriteTree.CLIPBOARD)) {
_writer = new ProxyWriter(true);
_output = WriteTree.CLIPBOARD;
} else {
if (_filename.contains(File.separator)) {
String extTest = _filename.substring(_filename
.lastIndexOf(File.separator) + 1);
if (!extTest.contains("."))
_filename += _format;
} else if (!_filename.contains("."))
_filename += _format;
if (!_filename.contains(File.separator))
_filename = FrameIO.EXPORTS_PATH + _filename;
File test = new File(_filename);
File parent = test.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
// Open an Output Stream Writer to set encoding
OutputStream fout = new FileOutputStream(_filename);
OutputStream bout = new BufferedOutputStream(fout);
Writer out = new OutputStreamWriter(bout, "UTF-8");
_writer = new ProxyWriter(out);
_output = _filename;
}
}
protected String getFileName(Frame start) {
return getValidFilename(start.getTitle());
}
public static String getValidFilename(String filename) {
return filename.replaceAll("[ \\.]", "_");
}
protected String finalise() throws IOException {
try {
_writer.flush();
_writer.close();
} catch (IOException ioe) {
} finally {
_writer.close();
}
String errorMessage = EcosystemManager.getMiscManager().open(_output);
if (errorMessage != null) System.err.println(errorMessage);
return " exported to " + _output;
}
protected String finaliseFrame() throws IOException {
return "Frame" + finalise();
}
protected void outputFrame(Frame toWrite) throws IOException {
writeStartFrame(toWrite);
Collection- done = new HashSet
- ();
for (Item i : getItemsToWrite(toWrite)) {
if (_stop) {
return;
}
if (i instanceof Line) {
if (done.contains(i)) {
continue;
}
done.addAll(i.getAllConnected());
}
writeItem(i);
}
writeEndFrame(toWrite);
}
protected Collection
- getItemsToWrite(Frame toWrite) {
return toWrite.getItemsToSave();
}
public boolean isRunning() {
return _running;
}
public void stop() {
_stop = true;
}
public String getFileContents() {
return "Not Supported";
}
public static LinkedHashMap getItemTags() {
return _ItemTags;
}
/**
* @return the _ItemTags
*/
public static LinkedHashMap getItemCharTags() {
return _ItemCharTags;
}
/**
* @return the _ItemTagsExt
*/
public static LinkedHashMap getItemStrTags() {
return _ItemStrTags;
}
}