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

Last change on this file since 919 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

File size: 9.7 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.awt.Desktop;
22import java.io.BufferedOutputStream;
23import java.io.File;
24import java.io.FileOutputStream;
25import java.io.IOException;
26import java.io.OutputStream;
27import java.io.OutputStreamWriter;
28import java.io.Writer;
29import java.lang.reflect.Method;
30import java.util.Collection;
31import java.util.HashSet;
32import java.util.LinkedHashMap;
33
34import org.expeditee.agents.WriteTree;
35import org.expeditee.gui.Browser;
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
45 protected String _filename = null;
46
47 protected String _output = null;
48
49 protected ProxyWriter _writer = null;
50
51 protected String _format = "";
52
53 protected boolean _running = true;
54
55 protected boolean _stop = false;
56
57 // keep track of methods that are put on the same line
58 protected static LinkedHashMap<Character, Method> _ItemTags = null;
59
60 protected static LinkedHashMap<Character, Method> _FrameTags = null;
61
62 public DefaultFrameWriter() {
63
64 if (_ItemTags != null && _FrameTags != null)
65 return;
66
67 _ItemTags = new LinkedHashMap<Character, Method>();
68 _FrameTags = new LinkedHashMap<Character, Method>();
69
70 try {
71 _FrameTags.put('V', Frame.class.getMethod("getVersion"));
72 _FrameTags.put('p', Frame.class.getMethod("getPermission"));
73 _FrameTags.put('U', Frame.class.getMethod("getOwner"));
74 _FrameTags.put('D', Frame.class.getMethod("getDateCreated"));
75 _FrameTags.put('M', Frame.class.getMethod("getLastModifyUser"));
76 _FrameTags.put('d', Frame.class.getMethod("getLastModifyDate"));
77 _FrameTags.put('F', Frame.class.getMethod("getFrozenDate"));
78
79 _FrameTags.put('O', Frame.class.getMethod("getForegroundColor"));
80 _FrameTags.put('B', Frame.class.getMethod("getBackgroundColor"));
81
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 // TODO set a boolean flag to indicate that the text is a formula
124 // Store the formula in the text property NOT the answer
125 _ItemTags.put('J', Item.class.getMethod("getFormula"));
126
127 _ItemTags.put('a', Text.class.getMethod("getWordSpacing"));
128 _ItemTags.put('b', Text.class.getMethod("getLetterSpacing"));
129 _ItemTags.put('m', Text.class.getMethod("getInitialSpacing"));
130 _ItemTags.put('w', Text.class.getMethod("getWidthToSave"));
131 _ItemTags.put('k', Text.class.getMethod("getJustification"));
132 _ItemTags.put('r', Text.class.getMethod("getAutoWrapToSave"));
133
134 _ItemTags.put('h', Item.class.getMethod("getThickness"));
135 _ItemTags.put('l', Item.class.getMethod("getLineIDs"));
136 _ItemTags.put('c', Item.class.getMethod("getConstraintIDs"));
137
138 _ItemTags.put('A', Item.class.getMethod("getTooltip"));
139 _ItemTags.put('B', Item.class.getMethod("getLinkHistory"));
140
141 _ItemTags.put('p', Item.class.getMethod("getPermission"));
142
143 _ItemTags.put('[', Item.class.getMethod("getMagnetizedItemLeft"));
144 _ItemTags.put(']', Item.class.getMethod("getMagnetizedItemRight"));
145 _ItemTags.put('^', Item.class.getMethod("getMagnetizedItemTop"));
146 _ItemTags.put('_', Item.class.getMethod("getMagnetizedItemBottom"));
147 } catch (Exception e) {
148 e.printStackTrace();
149 }
150 }
151
152 public void setOutputLocation(String filename) {
153 _filename = filename;
154 }
155
156 public String writeFrame(Frame toWrite) throws IOException {
157 return writeFrame(toWrite, null);
158 }
159
160 public String writeFrame(Frame toWrite, Writer writer) throws IOException {
161 try {
162 initialise(toWrite, writer);
163
164 outputFrame(toWrite);
165
166 _running = false;
167 return finaliseFrame();
168 } catch (IOException ioe) {
169 _running = false;
170 throw ioe;
171 }
172
173 }
174
175 /**
176 * Called before writing out the body items of each frame.
177 *
178 * @param starting
179 * the name of the frame currently being written out.
180 * @throws IOException
181 */
182 protected void writeStartFrame(Frame starting) throws IOException {
183 if (starting.getTitleItem() != null) {
184 if (starting.getTitleItem().isAnnotation())
185 this.writeAnnotationTitle(starting.getTitleItem());
186 else
187 this.writeTitle(starting.getTitleItem(), starting.getItems());
188 }
189 }
190
191 /**
192 * Called after writing out the body items of each frame.
193 *
194 * @param ending
195 * the name of the frame currently being written out.
196 * @throws IOException
197 */
198 protected void writeEndFrame(Frame ending) throws IOException {
199 }
200
201 protected final void initialise(Frame start) throws IOException {
202 initialise(start, null);
203 }
204
205 protected void initialise(Frame start, Writer writer) throws IOException {
206 if (_filename == null)
207 _filename = FrameIO.EXPORTS_DIR + getFileName(start) + _format;
208
209 if (writer != null) {
210 _writer = new ProxyWriter(writer);
211 _output = writer.toString();
212 } else if (_filename.equalsIgnoreCase(WriteTree.CLIPBOARD)) {
213 _writer = new ProxyWriter(true);
214 _output = WriteTree.CLIPBOARD;
215 } else {
216 if (_filename.contains(File.separator)) {
217 String extTest = _filename.substring(_filename
218 .lastIndexOf(File.separator) + 1);
219 if (!extTest.contains("."))
220 _filename += _format;
221 } else if (!_filename.contains("."))
222 _filename += _format;
223
224 if (!_filename.contains(File.separator))
225 _filename = FrameIO.EXPORTS_DIR + _filename;
226
227 File test = new File(_filename);
228
229 File parent = test.getParentFile();
230 if (parent != null && !parent.exists()) {
231 parent.mkdirs();
232 }
233
234 // Open an Output Stream Writer to set encoding
235 OutputStream fout = new FileOutputStream(_filename);
236 OutputStream bout = new BufferedOutputStream(fout);
237 Writer out = new OutputStreamWriter(bout, "UTF-8");
238
239 _writer = new ProxyWriter(out);
240 _output = _filename;
241 }
242 }
243
244 protected String getFileName(Frame start) {
245 return getValidFilename(start.getTitle());
246 }
247
248 public static String getValidFilename(String filename) {
249 return filename.replaceAll("[ \\.]", "_");
250 }
251
252 protected String finalise() throws IOException {
253 try {
254 _writer.flush();
255 _writer.close();
256 } catch (IOException ioe) {
257 } finally {
258 _writer.close();
259 }
260 try {
261 if (Browser._theBrowser.isMinimumVersion6()) {
262 if (Desktop.isDesktopSupported()) {
263 Desktop.getDesktop().open(new File(_output));
264 }
265 }
266 } catch (Exception e) {
267 e.printStackTrace();
268 }
269 return " exported to " + _output;
270 }
271
272 protected String finaliseFrame() throws IOException {
273 return "Frame" + finalise();
274 }
275
276 protected void outputFrame(Frame toWrite) throws IOException {
277 writeStartFrame(toWrite);
278
279 Collection<Item> done = new HashSet<Item>();
280
281 for (Item i : getItemsToWrite(toWrite)) {
282 if (_stop) {
283 return;
284 }
285
286 if (i instanceof Line) {
287 if (done.contains(i)) {
288 continue;
289 }
290 done.addAll(i.getAllConnected());
291 }
292 writeItem(i);
293 }
294 writeEndFrame(toWrite);
295 }
296
297 protected Collection<Item> getItemsToWrite(Frame toWrite) {
298 return toWrite.getItemsToSave();
299 }
300
301 public boolean isRunning() {
302 return _running;
303 }
304
305 public void stop() {
306 _stop = true;
307 }
308
309 public String getFileContents() {
310 return "Not Supported";
311 }
312
313}
Note: See TracBrowser for help on using the repository browser.