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.InvocationTargetException;
|
---|
7 | import java.lang.reflect.Method;
|
---|
8 | import java.util.Iterator;
|
---|
9 | import java.util.LinkedList;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import org.expeditee.gui.Frame;
|
---|
13 | import org.expeditee.items.Constraint;
|
---|
14 | import org.expeditee.items.Dot;
|
---|
15 | import org.expeditee.items.Item;
|
---|
16 | import org.expeditee.items.Line;
|
---|
17 | import org.expeditee.items.Picture;
|
---|
18 | import org.expeditee.stats.SessionStats;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Writes a Frame out to a KMS format file.
|
---|
22 | *
|
---|
23 | * @author jdm18
|
---|
24 | *
|
---|
25 | */
|
---|
26 | public class KMSWriter extends DefaultFrameWriter {
|
---|
27 |
|
---|
28 | private ProxyWriter _writer = null;
|
---|
29 |
|
---|
30 | private StringBuilder _stringWriter = null;
|
---|
31 |
|
---|
32 | private static final char TERMINATOR = 'Z';
|
---|
33 |
|
---|
34 | private static final char DELIMITER = '+';
|
---|
35 |
|
---|
36 | public KMSWriter() {
|
---|
37 | super();
|
---|
38 | }
|
---|
39 |
|
---|
40 | public void initialise(Frame start) throws IOException {
|
---|
41 | String name = start.getFramesetName().toLowerCase();
|
---|
42 |
|
---|
43 | if (_filename == null)
|
---|
44 | _filename = start.path + name + File.separator + name + "."
|
---|
45 | + start.getFrameNumber();
|
---|
46 |
|
---|
47 | _stringWriter = new StringBuilder();
|
---|
48 | if (_filename.toLowerCase().equals("clipboard")) {
|
---|
49 | _writer = new ProxyWriter(true);
|
---|
50 | _filename = "Clipboard";
|
---|
51 | } else
|
---|
52 | _writer = new ProxyWriter(new FileWriter(_filename));
|
---|
53 |
|
---|
54 | try {
|
---|
55 | _ItemTags.put("S", Item.class.getMethod("getID", new Class[] {}));
|
---|
56 | } catch (Exception e) {
|
---|
57 |
|
---|
58 | }
|
---|
59 | // _writer = new BufferedWriter(new FileWriter(filepath.toLowerCase() +
|
---|
60 | // filename.toLowerCase()));
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Writes the given Frame (and all items it contains) to a KMS file. Note:
|
---|
65 | * File path and location must be set before calling this or it will do
|
---|
66 | * nothing.
|
---|
67 | *
|
---|
68 | * @param frame
|
---|
69 | * The Frame to write out to the file.
|
---|
70 | * @throws IOException
|
---|
71 | * Any exceptions occured by the BufferedWriter.
|
---|
72 | */
|
---|
73 | public void outputFrame(Frame frame) throws IOException {
|
---|
74 | if (_writer == null)
|
---|
75 | return;
|
---|
76 |
|
---|
77 | writeHeader(frame);
|
---|
78 |
|
---|
79 | // write out each Item in the Frame.
|
---|
80 | List<Item> items = frame.getItems();
|
---|
81 |
|
---|
82 | // do not write items with ID -1, also skip any lines
|
---|
83 | for (Item i : items) {
|
---|
84 | if (i.getID() >= 0 && !(i instanceof Line))
|
---|
85 | writeItem(i);
|
---|
86 | }
|
---|
87 |
|
---|
88 | // write any lines or constraints
|
---|
89 | writeLineData();
|
---|
90 | writeConstraintData();
|
---|
91 |
|
---|
92 | return;
|
---|
93 | }
|
---|
94 |
|
---|
95 | private void writeHeader(Frame toWrite) throws IOException {
|
---|
96 | Iterator<String> it = _FrameTags.keySet().iterator();
|
---|
97 | Object[] param = {};
|
---|
98 |
|
---|
99 | // Write the version tag
|
---|
100 | writeLine("v", "22");
|
---|
101 |
|
---|
102 | while (it.hasNext()) {
|
---|
103 | String tag = it.next();
|
---|
104 | try {
|
---|
105 | Object o = _FrameTags.get(tag).invoke(toWrite, param);
|
---|
106 | o = Conversion.ConvertToKMS(_FrameTags.get(tag), o);
|
---|
107 | if (o != null) {
|
---|
108 | writeLine(tag, (String) o);
|
---|
109 | }
|
---|
110 | } catch (IllegalArgumentException e) {
|
---|
111 | // TODO Auto-generated catch block
|
---|
112 | e.printStackTrace();
|
---|
113 | } catch (IllegalAccessException e) {
|
---|
114 | // TODO Auto-generated catch block
|
---|
115 | e.printStackTrace();
|
---|
116 | } catch (InvocationTargetException e) {
|
---|
117 | // TODO Auto-generated catch block
|
---|
118 | e.printStackTrace();
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | private void writeLine(String tag, String line) throws IOException {
|
---|
124 | writeLine(DELIMITER + tag + DELIMITER + " " + line);
|
---|
125 | }
|
---|
126 |
|
---|
127 | private void writeTerminator() throws IOException {
|
---|
128 | writeLine("" + DELIMITER + TERMINATOR + DELIMITER);
|
---|
129 | }
|
---|
130 |
|
---|
131 | // writes the given line out to the file
|
---|
132 | private void writeLine(String line) throws IOException {
|
---|
133 | // do not write empty lines
|
---|
134 | if (line == null)
|
---|
135 | return;
|
---|
136 |
|
---|
137 | String toWrite = line + "\n";
|
---|
138 |
|
---|
139 | _writer.write(toWrite);
|
---|
140 | _stringWriter.append(toWrite);
|
---|
141 | }
|
---|
142 |
|
---|
143 | // writes the given Item out to the file
|
---|
144 | public void writeItem(Item item) throws IOException {
|
---|
145 | if (_writer == null)
|
---|
146 | return;
|
---|
147 |
|
---|
148 | writeLine("");
|
---|
149 | // Pictures are saved as the corresponding text items that created them
|
---|
150 | if (item instanceof Picture)
|
---|
151 | item = ((Picture) item).getText();
|
---|
152 |
|
---|
153 | if (item.getLines().size() > 0)
|
---|
154 | writePoint(item);
|
---|
155 | else if (!(item instanceof Line))
|
---|
156 | writeClass(item);
|
---|
157 | else
|
---|
158 | System.out.println("Unknown Item: " + item.getID());
|
---|
159 | }
|
---|
160 |
|
---|
161 | private List<Item> _points = new LinkedList<Item>();
|
---|
162 |
|
---|
163 | // Writes out a Dot to the file
|
---|
164 | protected void writePoint(Item point) throws IOException {
|
---|
165 | _points.add(point);
|
---|
166 | writeClass(point);
|
---|
167 | }
|
---|
168 |
|
---|
169 | // writes out all lines to the file
|
---|
170 | private void writeLineData() throws IOException {
|
---|
171 | List<Line> seen = new LinkedList<Line>();
|
---|
172 |
|
---|
173 | // loop through all points stored
|
---|
174 | for (int i = 0; i < _points.size(); i++) {
|
---|
175 | List<Line> lines = _points.get(i).getLines();
|
---|
176 |
|
---|
177 | // if this point is part of one or more lines
|
---|
178 | if (lines != null && lines.size() > 0) {
|
---|
179 | for (Line line : lines) {
|
---|
180 |
|
---|
181 | // only output new lines that have not yet been output
|
---|
182 | if (!seen.contains(line)) {
|
---|
183 | writeLine("L", line.getID() + " " + line.getLineType());
|
---|
184 | writeLine("s", line.getLineEnds());
|
---|
185 |
|
---|
186 | // add this line to the list of lines that have been
|
---|
187 | // seen
|
---|
188 | seen.add(line);
|
---|
189 | }
|
---|
190 |
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | // writes out any constraints to the file
|
---|
197 | private void writeConstraintData() throws IOException {
|
---|
198 | // outputs any constraints the points have
|
---|
199 |
|
---|
200 | // loop through all the points
|
---|
201 | while (_points.size() > 0) {
|
---|
202 | Item i = _points.get(0);
|
---|
203 |
|
---|
204 | if (i instanceof Dot) {
|
---|
205 | Dot p = (Dot) i;
|
---|
206 |
|
---|
207 | // if there are any constraints to write
|
---|
208 | if (p.getConstraints() != null) {
|
---|
209 | List<Constraint> constraints = p.getConstraints();
|
---|
210 |
|
---|
211 | // do not write constraints that have already been
|
---|
212 | // written
|
---|
213 | for (Constraint c : constraints) {
|
---|
214 | if (_points.contains(c.getStart())
|
---|
215 | && _points.contains(c.getEnd())) {
|
---|
216 | writeLine("C", c.getID() + " " + c.getType());
|
---|
217 | writeLine("s", c.getLineEnds());
|
---|
218 | }
|
---|
219 |
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 | // remove the point from the list
|
---|
224 | _points.remove(0);
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | @Override
|
---|
229 | protected String finaliseFrame() throws IOException {
|
---|
230 | writeTerminator();
|
---|
231 |
|
---|
232 | writeLine(SessionStats.getFrameEventList());
|
---|
233 |
|
---|
234 | _writer.flush();
|
---|
235 | _writer.close();
|
---|
236 | _writer = null;
|
---|
237 |
|
---|
238 | return "Frame successfully written to " + _filename;
|
---|
239 | }
|
---|
240 |
|
---|
241 | private void writeClass(Object toWrite) throws IOException {
|
---|
242 | Iterator<String> it = _ItemTags.keySet().iterator();
|
---|
243 | Object[] param = {};
|
---|
244 |
|
---|
245 | while (it.hasNext()) {
|
---|
246 | String tag = it.next();
|
---|
247 | Method toRun = _ItemTags.get(tag);
|
---|
248 | Class declarer = toRun.getDeclaringClass();
|
---|
249 | if (declarer == toWrite.getClass()
|
---|
250 | || declarer == toWrite.getClass().getSuperclass()
|
---|
251 | || declarer == toWrite.getClass().getSuperclass()
|
---|
252 | .getSuperclass()) {
|
---|
253 | try {
|
---|
254 | Object o = toRun.invoke(toWrite, param);
|
---|
255 | o = Conversion.ConvertToKMS(toRun, o);
|
---|
256 | if (o != null) {
|
---|
257 | if (o instanceof List) {
|
---|
258 | for (Object line : (List) o)
|
---|
259 | writeLine(tag, (String) line);
|
---|
260 | } else
|
---|
261 | writeLine(tag, (String) o);
|
---|
262 | }
|
---|
263 | } catch (IllegalArgumentException e) {
|
---|
264 | // TODO Auto-generated catch block
|
---|
265 | e.printStackTrace();
|
---|
266 | } catch (IllegalAccessException e) {
|
---|
267 | // TODO Auto-generated catch block
|
---|
268 | e.printStackTrace();
|
---|
269 | } catch (InvocationTargetException e) {
|
---|
270 | // TODO Auto-generated catch block
|
---|
271 | e.printStackTrace();
|
---|
272 | }
|
---|
273 | }
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * Gets a string representation of the frame file contents.
|
---|
279 | */
|
---|
280 | public String getFileContents() {
|
---|
281 | return _stringWriter.toString();
|
---|
282 | }
|
---|
283 | }
|
---|