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