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

Last change on this file since 457 was 427, checked in by ra33, 15 years ago
File size: 8.7 KB
Line 
1package org.expeditee.io;
2
3import java.awt.Desktop;
4import java.io.BufferedOutputStream;
5import java.io.File;
6import java.io.FileOutputStream;
7import java.io.IOException;
8import java.io.OutputStream;
9import java.io.OutputStreamWriter;
10import java.io.Writer;
11import java.lang.reflect.Method;
12import java.util.Collection;
13import java.util.HashSet;
14import java.util.LinkedHashMap;
15
16import org.expeditee.agents.WriteTree;
17import org.expeditee.gui.Browser;
18import org.expeditee.gui.Frame;
19import org.expeditee.gui.FrameIO;
20import org.expeditee.items.Item;
21import org.expeditee.items.Line;
22import org.expeditee.items.Text;
23
24public abstract class DefaultFrameWriter extends ItemWriter implements
25 FrameWriter {
26
27 protected String _filename = null;
28
29 protected String _output = null;
30
31 protected ProxyWriter _writer = null;
32
33 protected String _format = "";
34
35 protected boolean _running = true;
36
37 protected boolean _stop = false;
38
39 // keep track of methods that are put on the same line
40 protected static LinkedHashMap<String, Method> _ItemTags = null;
41
42 protected static LinkedHashMap<String, Method> _FrameTags = null;
43
44 public DefaultFrameWriter() {
45 if (_ItemTags != null && _FrameTags != null)
46 return;
47
48 _ItemTags = new LinkedHashMap<String, Method>();
49 _FrameTags = new LinkedHashMap<String, Method>();
50
51 Class[] param = {};
52
53 try {
54 _FrameTags.put("V", Frame.class.getMethod("getVersion", param));
55 _FrameTags.put("p", Frame.class.getMethod("getPermission", param));
56 _FrameTags.put("U", Frame.class.getMethod("getOwner", param));
57 _FrameTags.put("D", Frame.class.getMethod("getDateCreated", param));
58 _FrameTags.put("M", Frame.class.getMethod("getLastModifyUser",
59 param));
60 _FrameTags.put("d", Frame.class.getMethod("getLastModifyDate",
61 param));
62 _FrameTags.put("F", Frame.class.getMethod("getFrozenDate", param));
63
64 _FrameTags.put("O", Frame.class.getMethod("getForegroundColor",
65 param));
66 _FrameTags.put("B", Frame.class.getMethod("getBackgroundColor",
67 param));
68 _ItemTags.put("S", Item.class.getMethod("getTypeAndID", param));
69 _ItemTags.put("s", Item.class.getMethod("getDateCreated", param));
70 _ItemTags.put("d", Item.class.getMethod("getColor", param));
71 _ItemTags.put("G", Item.class
72 .getMethod("getBackgroundColor", param));
73 _ItemTags.put("K", Item.class.getMethod("getBorderColor", param));
74 _ItemTags.put("H", Item.class.getMethod("getAnchorRight", param));
75 _ItemTags.put("I", Item.class.getMethod("getAnchorBottom", param));
76 _ItemTags.put("P", Item.class.getMethod("getPosition", param));
77 _ItemTags.put("T", Text.class.getMethod("getText", param));
78 _ItemTags.put("F", Item.class.getMethod("getLink", param));
79 _ItemTags.put("X", Item.class.getMethod("getAction", param));
80 _ItemTags.put("x", Item.class.getMethod("getActionMark", param));
81 _ItemTags.put("U", Item.class.getMethod("getActionCursorEnter",
82 param));
83 _ItemTags.put("V", Item.class.getMethod("getActionCursorLeave",
84 param));
85 _ItemTags.put("W", Item.class.getMethod("getActionEnterFrame",
86 param));
87 _ItemTags.put("Y", Item.class.getMethod("getActionLeaveFrame",
88 param));
89 _ItemTags.put("D", Item.class.getMethod("getData", param));
90 _ItemTags.put("u", Item.class.getMethod("getHighlight", param));
91 _ItemTags.put("e", Item.class.getMethod("getFillColor", param));
92 _ItemTags.put("E", Item.class.getMethod("getGradientColor", param));
93 _ItemTags.put("Q", Item.class.getMethod("getGradientAngle", param));
94
95 _ItemTags.put("i", Item.class.getMethod("getFillPattern", param));
96 _ItemTags.put("o", Item.class.getMethod("getOwner", param));
97 _ItemTags.put("n", Item.class.getMethod("getLinkMark", param));
98 _ItemTags.put("q", Item.class.getMethod("getLinkFrameset", param));
99 _ItemTags.put("y", Item.class.getMethod("getLinkTemplate", param));
100 _ItemTags.put("g", Item.class.getMethod("getLinePattern", param));
101
102 _ItemTags.put("j", Item.class.getMethod("getArrow", param));
103
104 _ItemTags.put("v", Item.class.getMethod("getDotType", param));
105 _ItemTags.put("z", Item.class.getMethod("getFilled", param));
106
107 _ItemTags.put("f", Text.class.getMethod("getFont", param));
108 _ItemTags.put("t", Text.class.getMethod("getSpacing", param));
109
110 // TODO set a boolean flag to indicate that the text is a formula
111 // Store the formula in the text property NOT the answer
112 _ItemTags.put("J", Item.class.getMethod("getFormula", param));
113
114 _ItemTags.put("a", Text.class.getMethod("getWordSpacing", param));
115 _ItemTags.put("b", Text.class.getMethod("getLetterSpacing", param));
116 _ItemTags
117 .put("m", Text.class.getMethod("getInitialSpacing", param));
118 _ItemTags.put("w", Text.class.getMethod("getWidthToSave", param));
119 _ItemTags.put("k", Text.class.getMethod("getJustification", param));
120
121 _ItemTags.put("h", Item.class.getMethod("getThickness", param));
122 _ItemTags.put("l", Item.class.getMethod("getLineIDs", param));
123 _ItemTags.put("c", Item.class.getMethod("getConstraintIDs", param));
124
125 } catch (Exception e) {
126 e.printStackTrace();
127 }
128 }
129
130 public void setOutputLocation(String filename) {
131 _filename = filename;
132 }
133
134 public String writeFrame(Frame toWrite) throws IOException {
135 return writeFrame(toWrite, null);
136 }
137
138 public String writeFrame(Frame toWrite, Writer writer) throws IOException {
139 try {
140 initialise(toWrite, writer);
141
142 outputFrame(toWrite);
143
144 _running = false;
145 return finaliseFrame();
146 } catch (IOException ioe) {
147 _running = false;
148 throw ioe;
149 }
150
151 }
152
153 /**
154 * Called before writing out the body items of each frame.
155 *
156 * @param starting
157 * the name of the frame currently being written out.
158 * @throws IOException
159 */
160 protected void writeStartFrame(Frame starting) throws IOException {
161 if (starting.getTitleItem() != null) {
162 if (starting.getTitleItem().isAnnotation())
163 this.writeAnnotationTitle(starting.getTitleItem());
164 else
165 this.writeTitle(starting.getTitleItem(), starting.getItems());
166 }
167 }
168
169 /**
170 * Called after writing out the body items of each frame.
171 *
172 * @param ending
173 * the name of the frame currently being written out.
174 * @throws IOException
175 */
176 protected void writeEndFrame(Frame ending) throws IOException {
177 }
178
179 protected final void initialise(Frame start) throws IOException {
180 initialise(start, null);
181 }
182
183 protected void initialise(Frame start, Writer writer) throws IOException {
184 if (_filename == null)
185 _filename = FrameIO.EXPORTS_DIR + getFileName(start) + _format;
186
187 if (writer != null) {
188 _writer = new ProxyWriter(writer);
189 _output = writer.toString();
190 } else if (_filename.equalsIgnoreCase(WriteTree.CLIPBOARD)) {
191 _writer = new ProxyWriter(true);
192 _output = WriteTree.CLIPBOARD;
193 } else {
194 if (_filename.contains(File.separator)) {
195 String extTest = _filename.substring(_filename
196 .lastIndexOf(File.separator) + 1);
197 if (!extTest.contains("."))
198 _filename += _format;
199 } else if (!_filename.contains("."))
200 _filename += _format;
201
202 if (!_filename.contains(File.separator))
203 _filename = FrameIO.EXPORTS_DIR + _filename;
204
205 File test = new File(_filename);
206
207 File parent = test.getParentFile();
208 if (parent != null && !parent.exists()) {
209 parent.mkdirs();
210 }
211
212 // Open an Output Stream Writer to set encoding
213 OutputStream fout = new FileOutputStream(_filename);
214 OutputStream bout = new BufferedOutputStream(fout);
215 Writer out = new OutputStreamWriter(bout, "UTF-8");
216
217 _writer = new ProxyWriter(out);
218 _output = _filename;
219 }
220 }
221
222 protected String getFileName(Frame start) {
223 return getValidFilename(start.getTitle());
224 }
225
226 public static String getValidFilename(String filename) {
227 return filename.replaceAll("[ \\.]", "_");
228 }
229
230 protected String finalise() throws IOException {
231 try {
232 _writer.flush();
233 _writer.close();
234 } catch (IOException ioe) {
235 } finally {
236 _writer.close();
237 }
238 try {
239 if (Browser._theBrowser.isVersion6()) {
240 if (Desktop.isDesktopSupported()) {
241 Desktop.getDesktop().open(new File(_output));
242 }
243 }
244 } catch (Exception e) {
245 e.printStackTrace();
246 }
247 return " exported to " + _output;
248 }
249
250 protected String finaliseFrame() throws IOException {
251 return "Frame" + finalise();
252 }
253
254 protected void outputFrame(Frame toWrite) throws IOException {
255 writeStartFrame(toWrite);
256
257 Collection<Item> done = new HashSet<Item>();
258
259 for (Item i : getItemsToWrite(toWrite)) {
260 if (_stop) {
261 return;
262 }
263
264 if (i instanceof Line) {
265 if (done.contains(i)) {
266 continue;
267 }
268 done.addAll(i.getAllConnected());
269 }
270 writeItem(i);
271 }
272 writeEndFrame(toWrite);
273 }
274
275 protected Collection<Item> getItemsToWrite(Frame toWrite) {
276 return toWrite.getItemsToSave();
277 }
278
279 public boolean isRunning() {
280 return _running;
281 }
282
283 public void stop() {
284 _stop = true;
285 }
286
287 public String getFileContents() {
288 return "Not Supported";
289 }
290
291}
Note: See TracBrowser for help on using the repository browser.