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