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

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