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

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

Rollback implementation of _maxWidth. After talks with David, this functionality is to be implemented slightly differently.

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